FileDocCategorySizeDatePackage
ControllerTest.javaAPI DocExample3144Mon Apr 25 00:38:46 BST 2005None

ControllerTest

public class ControllerTest extends TestCase

Fields Summary
private org.springframework.context.ApplicationContext
ctx
private com.springbook.RentABike
store
Constructors Summary
Methods Summary
public voidsetUp()

      ctx = new FileSystemXmlApplicationContext("war/WEB-INF/rentaBikeApp-servlet.xml");
      store = (RentABike)ctx.getBean("rentaBike");
   
public voidtestBikeValidator()

      BikeValidator v = (BikeValidator) ctx.getBean("bikeValidator");
      Bike bike = new Bike("test", "test", 1, "test", 2.00, "test");
      Errors errs = new BindException(bike, "bike");
      v.validate(bike, errs);
      assertFalse(errs.hasErrors());
      bike = new Bike();
      errs = new BindException(bike, "bike");
      v.validate(bike, errs);
      assertTrue(errs.hasErrors());
   
public voidtestBikesController()

      BikesController controller = (BikesController) ctx.getBean("bikesController");
      ModelAndView mav = controller.handleRequest(null, null);
      RentABike store = (RentABike)mav.getModel().get("rentaBike");
      assertNotNull(store);
      assertEquals(3, store.getBikes().size());
   
public voidtestDeleteBike()

      List bikes = store.getBikes();
      assertEquals(3, bikes.size());
      Bike bike = (Bike)bikes.get(0);
      store.deleteBike(bike);
      bikes = store.getBikes();
      assertEquals(2, bikes.size());
   
public voidtestGetBike()

      Bike bike = store.getBike("11111");
      assertNotNull(bike);
      assertEquals("Shimano", bike.getManufacturer());
   
public voidtestGetBikes()

      List bikes = store.getBikes();
      assertNotNull(bikes);
      assertEquals(3, bikes.size());
   
public voidtestGetName()

      assertEquals("Bruce's Bikes", store.getStoreName());
   
public voidtestJDBC()

      try {
         System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/bikestore");
      } catch (Exception ex) {
         assertTrue(ex.getMessage().indexOf("authorization") > 0);
      }
      assertTrue(true);
   
public voidtestSaveBike()

      Bike bike = (Bike)store.getBikes().get(0);
      assertFalse ("Royal".equals(bike.getManufacturer()));
      bike.setManufacturer("Royal");
      store.saveBike(bike);
      Iterator iter = store.getBikes().iterator();
      boolean found = false;
      while(iter.hasNext()) {
         Bike b = (Bike)iter.next();
         if(b.getManufacturer().equals("Royal")) found = true;
      }
      assertTrue(found);