import com.springbook.*;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.easymock.MockControl;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
public class ControllerTest extends TestCase {
private ApplicationContext ctx;
private RentABike store;
public void setUp() throws Exception {
ctx = new FileSystemXmlApplicationContext("war/WEB-INF/rentaBikeApp-servlet.xml");
store = (RentABike)ctx.getBean("rentaBike");
}
public void testBikesController() throws Exception {
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 void testBikeValidator() throws Exception {
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 void testGetName() {
assertEquals("Bruce's Bikes", store.getStoreName());
}
public void testGetBike() {
Bike bike = store.getBike("11111");
assertNotNull(bike);
assertEquals("Shimano", bike.getManufacturer());
}
public void testGetBikes() {
List bikes = store.getBikes();
assertNotNull(bikes);
assertEquals(3, bikes.size());
}
public void testDeleteBike() {
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 void testSaveBike() {
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);
}
public void testJDBC() throws Exception {
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 void testGetBikesWithMocks() throws Exception {
DataSource mockDataSource;
Connection mockConnection;
Statement mockStatement;
ResultSet mockRS;
MockControl controlDataSource =
MockControl.createControl(DataSource.class);
MockControl controlConnection =
MockControl.createNiceControl(Connection.class);
MockControl controlStatement =
MockControl.createControl(Statement.class);
MockControl controlRS =
MockControl.createControl(ResultSet.class);
mockDataSource = (DataSource)controlDataSource.getMock();
mockConnection = (Connection)controlConnection.getMock();
mockStatement = (Statement)controlStatement.getMock();
mockRS = (ResultSet)controlRS.getMock();
mockDataSource.getConnection();
controlDataSource.setReturnValue(mockConnection);
mockConnection.createStatement();
controlConnection.setReturnValue(mockStatement);
mockStatement.executeQuery("SELECT * FROM bikes");
controlStatement.setReturnValue(mockRS);
controlRS.expectAndReturn(mockRS.next(), false);
controlStatement.expectAndReturn(mockStatement.getWarnings(), null);
mockRS.close();
mockStatement.close();
mockConnection.close();
mockDataSource.getConnection();
controlDataSource.setReturnValue(mockConnection);
mockConnection.close();
controlConnection.replay();
controlDataSource.replay();
controlStatement.replay();
controlRS.replay();
((JDBCRentABike)store).setDataSource(mockDataSource);
store.getBikes();
controlConnection.verify();
controlDataSource.verify();
controlStatement.verify();
controlRS.verify();
}
}
|