added unit tests

This commit is contained in:
Scott Andrews
2008-07-03 22:55:47 +00:00
parent 04449bcb0b
commit 045dc129e8
14 changed files with 512 additions and 5 deletions

View File

@@ -9,11 +9,8 @@ import org.springframework.samples.springtravel.hotel.booking.HotelBooking;
import org.springframework.samples.springtravel.hotel.booking.HotelBookingAgent;
import org.springframework.samples.springtravel.hotel.booking.User;
import org.springframework.samples.springtravel.hotel.search.Hotel;
import org.springframework.samples.springtravel.hotel.search.SearchCriteria;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
/**
* A JPA-based implementation of the Hotel Booking Agent. Delegates to a JPA entity

View File

@@ -0,0 +1,86 @@
package org.springframework.samples.springtravel.hotel.booking;
import org.easymock.EasyMock;
import org.springframework.faces.model.converter.FacesConversionService;
import org.springframework.samples.springtravel.hotel.search.Hotel;
import org.springframework.webflow.config.FlowDefinitionResource;
import org.springframework.webflow.config.FlowDefinitionResourceFactory;
import org.springframework.webflow.core.collection.LocalAttributeMap;
import org.springframework.webflow.core.collection.MutableAttributeMap;
import org.springframework.webflow.test.MockExternalContext;
import org.springframework.webflow.test.MockFlowBuilderContext;
import org.springframework.webflow.test.execution.AbstractXmlFlowExecutionTests;
public class HotelBookingFlowExecutionTests extends AbstractXmlFlowExecutionTests {
private HotelBookingAgent hotelBookingAgent;
protected void setUp() {
hotelBookingAgent = EasyMock.createMock(HotelBookingAgent.class);
}
@Override
protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory) {
return resourceFactory.createFileResource("src/main/webapp/WEB-INF/hotel/booking/booking.xml");
}
@Override
protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext) {
builderContext.registerBean("hotelBookingAgent", hotelBookingAgent);
builderContext.getFlowBuilderServices().setConversionService(new FacesConversionService());
}
public void testStartBookingFlow() {
HotelBooking booking = createTestBooking();
EasyMock.expect(hotelBookingAgent.createBooking(1L, "keith")).andReturn(booking);
EasyMock.replay(hotelBookingAgent);
MutableAttributeMap input = new LocalAttributeMap();
input.put("hotelId", "1");
MockExternalContext context = new MockExternalContext();
context.setCurrentUser("keith");
startFlow(input, context);
assertCurrentStateEquals("enterBookingDetails");
assertResponseWrittenEquals("enterBookingDetails", context);
assertTrue(getRequiredFlowAttribute("booking") instanceof HotelBooking);
EasyMock.verify(hotelBookingAgent);
}
public void testEnterBookingDetails_Proceed() {
setCurrentState("enterBookingDetails");
getFlowScope().put("booking", createTestBooking());
MockExternalContext context = new MockExternalContext();
context.setEventId("proceed");
resumeFlow(context);
assertCurrentStateEquals("reviewBooking");
assertResponseWrittenEquals("reviewBooking", context);
}
public void testReviewBooking_Confirm() {
setCurrentState("reviewBooking");
getFlowScope().put("booking", createTestBooking());
MockExternalContext context = new MockExternalContext();
context.setEventId("confirm");
resumeFlow(context);
assertFlowExecutionEnded();
assertFlowExecutionOutcomeEquals("bookingConfirmed");
}
private HotelBooking createTestBooking() {
Hotel hotel = new Hotel();
hotel.setId(1L);
hotel.setName("Jameson Inn");
User user = new User("keith", "pass", "Keith Donald");
HotelBooking booking = new HotelBooking(hotel, user);
return booking;
}
}

View File

@@ -0,0 +1,49 @@
package org.springframework.samples.springtravel.hotel.booking.impl;
import java.util.List;
import org.springframework.samples.springtravel.hotel.booking.HotelBooking;
import org.springframework.samples.springtravel.hotel.booking.HotelBookingAgent;
import org.springframework.test.jpa.AbstractJpaTests;
public class JpaHotelBookingAgentTests extends AbstractJpaTests {
private HotelBookingAgent hotelBookingAgent;
@Override
protected String[] getConfigLocations() {
return new String[] { "classpath:/test-jpa-context.xml" };
}
public void setHotelBookingAgent(HotelBookingAgent hotelBookingAgent) {
this.hotelBookingAgent = hotelBookingAgent;
}
@Override
protected void onSetUpInTransaction() throws Exception {
super.onSetUpInTransaction();
hotelBookingAgent.createBooking(1L, "scott");
}
public void testCancelBooking() {
List<HotelBooking> bookings = hotelBookingAgent.findBookings("scott");
assertEquals(1, bookings.size());
hotelBookingAgent.cancelBooking(bookings.get(0).getId());
bookings = hotelBookingAgent.findBookings("scott");
assertEquals(0, bookings.size());
}
public void testCreateBooking() {
HotelBooking booking = hotelBookingAgent.createBooking(1L, "scott");
assertEquals("Scott", booking.getUser().getName());
assertEquals("Westin Diplomat", booking.getHotel().getName());
}
public void testFindBookings() {
List<HotelBooking> bookings = hotelBookingAgent.findBookings("scott");
assertEquals(1, bookings.size());
assertEquals("Westin Diplomat", bookings.get(0).getHotel().getName());
}
}

View File

@@ -0,0 +1,134 @@
package org.springframework.samples.springtravel.hotel.search;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.DataModel;
import org.easymock.EasyMock;
import org.springframework.binding.mapping.Mapper;
import org.springframework.binding.mapping.MappingResults;
import org.springframework.faces.model.OneSelectionTrackingListDataModel;
import org.springframework.faces.model.converter.FacesConversionService;
import org.springframework.samples.springtravel.hotel.booking.HotelBooking;
import org.springframework.samples.springtravel.hotel.booking.HotelBookingAgent;
import org.springframework.samples.springtravel.hotel.booking.User;
import org.springframework.webflow.config.FlowDefinitionResource;
import org.springframework.webflow.config.FlowDefinitionResourceFactory;
import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.engine.EndState;
import org.springframework.webflow.engine.Flow;
import org.springframework.webflow.test.MockExternalContext;
import org.springframework.webflow.test.MockFlowBuilderContext;
import org.springframework.webflow.test.execution.AbstractXmlFlowExecutionTests;
public class HotelSearchFlowExecutionTests extends AbstractXmlFlowExecutionTests {
private HotelSearchAgent hotelSearchAgent;
private HotelBookingAgent hotelBookingAgent;
protected void setUp() {
hotelSearchAgent = EasyMock.createMock(HotelSearchAgent.class);
hotelBookingAgent = EasyMock.createMock(HotelBookingAgent.class);
}
@Override
protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory) {
return resourceFactory.createFileResource("src/main/webapp/WEB-INF/hotel/search/search.xml");
}
@Override
protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext) {
builderContext.registerBean("hotelSearchAgent", hotelSearchAgent);
builderContext.registerBean("hotelBookingAgent", hotelBookingAgent);
builderContext.getFlowBuilderServices().setConversionService(new FacesConversionService());
}
public void testStartMainFlow() {
List<HotelBooking> bookings = new ArrayList<HotelBooking>();
bookings.add(new HotelBooking(new Hotel(), new User("keith", "password", "Keith Donald")));
EasyMock.expect(hotelBookingAgent.findBookings("keith")).andReturn(bookings);
EasyMock.replay(hotelBookingAgent);
MockExternalContext context = new MockExternalContext();
context.setCurrentUser("keith");
startFlow(context);
assertCurrentStateEquals("enterSearchCriteria");
assertResponseWrittenEquals("enterSearchCriteria", context);
assertTrue(getRequiredFlowAttribute("searchCriteria") instanceof SearchCriteria);
assertTrue(getRequiredViewAttribute("bookings") instanceof DataModel);
EasyMock.verify(hotelBookingAgent);
}
public void testSearchHotels() {
setCurrentState("enterSearchCriteria");
SearchCriteria criteria = new SearchCriteria();
criteria.setSearchString("Jameson");
getFlowScope().put("searchCriteria", criteria);
List<Hotel> hotels = new ArrayList<Hotel>();
hotels.add(new Hotel());
EasyMock.expect(hotelSearchAgent.findHotels(criteria)).andReturn(hotels);
EasyMock.replay(hotelSearchAgent);
MockExternalContext context = new MockExternalContext();
context.setEventId("search");
resumeFlow(context);
EasyMock.verify(hotelSearchAgent);
assertCurrentStateEquals("reviewHotels");
assertResponseWrittenEquals("reviewHotels", context);
assertTrue(getRequiredViewAttribute("hotels") instanceof DataModel);
}
public void testSelectHotel() {
setCurrentState("reviewHotels");
List<Hotel> hotels = new ArrayList<Hotel>();
Hotel hotel = new Hotel();
hotel.setId(1L);
hotel.setName("Jameson Inn");
hotels.add(hotel);
OneSelectionTrackingListDataModel dataModel = new OneSelectionTrackingListDataModel(hotels);
dataModel.select(hotel);
getViewScope().put("hotels", dataModel);
MockExternalContext context = new MockExternalContext();
context.setEventId("select");
resumeFlow(context);
assertCurrentStateEquals("reviewHotel");
assertNull(getFlowAttribute("hotels"));
assertSame(hotel, getFlowAttribute("hotel"));
}
public void testBookHotel() {
setCurrentState("reviewHotel");
Hotel hotel = new Hotel();
hotel.setId(1L);
hotel.setName("Jameson Inn");
getFlowScope().put("hotel", hotel);
Flow mockBookingFlow = new Flow("booking");
mockBookingFlow.setInputMapper(new Mapper() {
public MappingResults map(Object source, Object target) {
assertEquals(new Long(1), ((AttributeMap) source).get("hotelId"));
return null;
}
});
new EndState(mockBookingFlow, "bookingConfirmed");
getFlowDefinitionRegistry().registerFlowDefinition(mockBookingFlow);
MockExternalContext context = new MockExternalContext();
context.setEventId("book");
resumeFlow(context);
assertFlowExecutionEnded();
assertFlowExecutionOutcomeEquals("finish");
}
}

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="bookingDatabase">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.springframework.samples.springtravel.hotel.booking.User</class>
<class>org.springframework.samples.springtravel.hotel.booking.HotelBooking</class>
<class>org.springframework.samples.springtravel.hotel.search.Hotel</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
</properties>
</persistence-unit>
</persistence>

View File

@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Enables annotation based configuration for @Transactional, @Repository, and @PersistenceContext -->
<context:component-scan base-package="org.springframework.samples.springtravel.hotel.booking"/>
<!-- Instructs Spring to perform declarative transaction management on annotated classes -->
<tx:annotation-driven />
<!-- Deploys a in-memory "booking" datasource populated; can easily swap out -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:booking" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<!-- Drives local transactions using the JPA API -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:/test-booking-persistence.xml"/>
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
</beans>