Results 1 to 3 of 3

Thread: testing spring DAO

  1. #1
    Join Date
    Dec 2005
    Posts
    4

    Default testing spring DAO

    This is spring code for executing stored procedure. how to do unit testing using JUnit for this code. Is there any other better way of executing stored procedure in spring.

    class AbstractDao extends StoredProcedure {
    private static final String P_RESULTS = "p_results";


    protected static SqlOutParameter createResultsParameter(final RowMapper mapper) {
    return new SqlOutParameter(P_RESULTS, OracleTypes.CURSOR, mapper);
    }


    public AbstractDao(final DataSource ds,
    final String spName,
    final boolean isFunction,
    final SqlParameter[] parameters) {
    super(ds, spName);
    setFunction(isFunction);
    for (int i = 0; i < parameters.length; i++) {
    declareParameter(parameters[i]);
    }
    compile();
    }

    protected List executeCursorResults(final Map params) {
    return (List) super.execute(params).get(P_RESULTS);
    }
    }




    import com.Booking;
    import com.BookingIds;
    import com.Vehicle;


    class BookingDaoImpl extends AbstractDao implements BookingDao {
    private static final String SQL_DEFAULT = "abcdefghi_prc";
    private static final String P_VEHICLE_ID = "p_vehicle_id";
    private static final String P_JOURNEY_ID = "p_journey_id";
    private static final String P_LANGUAGE = "p_language";
    private static final String P_BOOKING_ID = "p_booking_id";
    private static final String P_ACCOUNT = "p_account";
    private static final String P_USER = "p_user";
    private static final String P_CUSTOMER_REF = "p_customer_ref";
    private static final String P_HAZARDOUS = "p_hazardous";
    private static final String P_ROUTED_DESCRIPTION = "p_routed_description";
    private static final String P_DEPARTURE_DATE = "p_departure_date";
    private static final String P_UNIT_TYPE_DESCRIPTION = "p_unit_type_description";
    private static final String P_UNIT_ID = "p_unit_id";


    private static SqlParameter[] createDefaultParameters() {
    return new SqlParameter[] {
    new SqlParameter(P_VEHICLE_ID, Types.INTEGER),
    new SqlParameter(P_JOURNEY_ID, Types.INTEGER),
    new SqlParameter(P_LANGUAGE, Types.VARCHAR),
    new SqlOutParameter(P_BOOKING_ID, Types.INTEGER),
    new SqlOutParameter(P_ACCOUNT, Types.INTEGER),
    new SqlOutParameter(P_USER, Types.VARCHAR),
    new SqlOutParameter(P_CUSTOMER_REF, Types.VARCHAR),
    new SqlOutParameter(P_HAZARDOUS, Types.VARCHAR),
    new SqlOutParameter(P_ROUTED_DESCRIPTION, Types.VARCHAR),
    new SqlOutParameter(P_DEPARTURE_DATE, Types.DATE),
    new SqlOutParameter(P_UNIT_TYPE_DESCRIPTION, Types.VARCHAR),
    new SqlOutParameter(P_UNIT_ID, Types.VARCHAR),

    };
    }


    public BookingDaoImpl(final DataSource ds) {
    this(ds, SQL_DEFAULT, false, createDefaultParameters());
    }


    public BookingDaoImpl(final DataSource ds,
    final String spName,
    final boolean isFunction,
    final SqlParameter[] parameters) {
    super(ds, spName, isFunction, parameters);
    }


    public Booking getBooking(final BookingIds ids) {
    final Map params = new HashMap(5);
    params.put(P_VEHICLE_ID, ids.getVehicleId());
    params.put(P_JOURNEY_ID, ids.getJourneyId());
    params.put(P_LANGUAGE, new BigDecimal(1));
    return createBooking(execute(params));
    }


    private Booking createBooking(final Map results) {
    final BigDecimal accountId = new BigDecimal(((Integer) results.get(P_ACCOUNT)).intValue());
    final BigDecimal bookingId = new BigDecimal(((Integer) results.get(P_BOOKING_ID)).intValue());
    final String customerRef = (String) results.get(P_CUSTOMER_REF);
    final String user = (String) results.get(P_USER);
    final short numberOfPassengers = ((Integer) results.get(P_NUMBER_PASSANGERS)).shortValue();
    final Calendar bookingDate = DaoUtils.getCalendar((Date) results.get(P_BOOKING_DATE));
    final String unitId = (String) results.get(P_UNIT_ID);
    final boolean hazardous = DaoUtils.getBoolean((String) results.get(P_HAZARDOUS)).booleanValue();
    final String unitType = (String) results.get(P_UNIT_TYPE_DESCRIPTION);

    final Booking booking = new Booking();
    booking.setAccountId(accountId);
    booking.setBookingId(bookingId);
    booking.setCustomerRef(customerRef);

    final Vehicle vehicle = new Vehicle();
    booking.setVehicle(vehicle);
    vehicle.setUnitId(unitId);
    vehicle.setHazardousLoad(hazardous);
    vehicle.setType(unitType);

    return booking;
    }
    }

    Thanks

  2. #2
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    I would not have a DAO class extend StoredProcedure - I would instead have a DAO class that extends JdbcDaoSupport and then let that class reference/create a MyStoredProcedure class that extends StoredProcedure.

    For unit testing and a DAO example see my JDBC slides at http://www.springdeveloper.com/oscon/jdbc.pdfhttp://www.springdeveloper.com/oscon/jdbc.pdf
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    Dec 2005
    Posts
    4

    Default

    Quote Originally Posted by trisberg
    I would not have a DAO class extend StoredProcedure - I would instead have a DAO class that extends JdbcDaoSupport and then let that class reference/create a MyStoredProcedure class that extends StoredProcedure.

    For unit testing and a DAO example see my JDBC slides at http://www.springdeveloper.com/oscon/jdbc.pdfhttp://www.springdeveloper.com/oscon/jdbc.pdf
    Thanks for your reply.
    What advantages do I get if
    my AbstractDao extends JdbcDaoSupport and BookingDaoImpl extends StoredProcedure and then create AbstractDao reference creating a BookingDaoImpl.

    Iam using this AbstractDao for many other Impl classes like BookingDaoImpl for storedprocedures. Is there any problem in doing this way. Please suggest.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •