From 045dc129e81677a67edeb9ee7fac2df8221d6431 Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Thu, 3 Jul 2008 22:55:47 +0000 Subject: [PATCH] added unit tests --- .../.classpath | 11 ++ .../ivy.xml | 13 +- .../search/impl/JpaHotelSearchAgentTests.java | 51 +++++++ .../src/test/resources/import.sql | 23 +++ .../test/resources/test-hotel-persistence.xml | 16 +++ .../src/test/resources/test-jpa-context.xml | 42 ++++++ .../.classpath | 15 +- .../ivy.xml | 14 ++ .../booking/impl/JpaHotelBookingAgent.java | 3 - .../HotelBookingFlowExecutionTests.java | 86 +++++++++++ .../impl/JpaHotelBookingAgentTests.java | 49 +++++++ .../search/HotelSearchFlowExecutionTests.java | 134 ++++++++++++++++++ .../resources/test-booking-persistence.xml | 18 +++ .../src/test/resources/test-jpa-context.xml | 42 ++++++ 14 files changed, 512 insertions(+), 5 deletions(-) create mode 100644 spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/java/org/springframework/samples/springtravel/hotel/search/impl/JpaHotelSearchAgentTests.java create mode 100644 spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/import.sql create mode 100644 spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/test-hotel-persistence.xml create mode 100644 spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/test-jpa-context.xml create mode 100644 spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/booking/HotelBookingFlowExecutionTests.java create mode 100644 spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/booking/impl/JpaHotelBookingAgentTests.java create mode 100644 spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/search/HotelSearchFlowExecutionTests.java create mode 100644 spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/resources/test-booking-persistence.xml create mode 100644 spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/resources/test-jpa-context.xml diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/.classpath b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/.classpath index c32daf65..a97461d7 100644 --- a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/.classpath +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/.classpath @@ -6,5 +6,16 @@ + + + + + + + + + + + diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/ivy.xml b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/ivy.xml index 1c16099f..fe84fed0 100644 --- a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/ivy.xml +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/ivy.xml @@ -21,7 +21,18 @@ - + + + + + + + + + + + + diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/java/org/springframework/samples/springtravel/hotel/search/impl/JpaHotelSearchAgentTests.java b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/java/org/springframework/samples/springtravel/hotel/search/impl/JpaHotelSearchAgentTests.java new file mode 100644 index 00000000..82761516 --- /dev/null +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/java/org/springframework/samples/springtravel/hotel/search/impl/JpaHotelSearchAgentTests.java @@ -0,0 +1,51 @@ +package org.springframework.samples.springtravel.hotel.search.impl; + +import java.util.List; + +import org.springframework.samples.springtravel.hotel.search.Hotel; +import org.springframework.samples.springtravel.hotel.search.HotelSearchAgent; +import org.springframework.samples.springtravel.hotel.search.SearchCriteria; +import org.springframework.test.jpa.AbstractJpaTests; + +public class JpaHotelSearchAgentTests extends AbstractJpaTests { + + private HotelSearchAgent hotelSearchAgent; + + @Override + protected String[] getConfigLocations() { + return new String[] { "classpath:/test-jpa-context.xml" }; + } + + public void setHotelSearchAgent(HotelSearchAgent hotelSearchAgent) { + this.hotelSearchAgent = hotelSearchAgent; + } + + public void testFindHotelsWithEmptyCriteria() { + SearchCriteria criteria = new SearchCriteria(); + criteria.setPage(0); + criteria.setPageSize(Integer.MAX_VALUE); + criteria.setSearchString(""); + List hotels = hotelSearchAgent.findHotels(criteria); + assertEquals(23, hotels.size()); + } + + public void testFindHotelsWithSearchTerm() { + SearchCriteria criteria = new SearchCriteria(); + criteria.setPage(0); + criteria.setPageSize(Integer.MAX_VALUE); + criteria.setSearchString("westin"); + List hotels = hotelSearchAgent.findHotels(criteria); + assertEquals(1, hotels.size()); + assertTrue(((Hotel)hotels.get(0)).getName().contains("Westin")); + } + + public void testFindHotelsWithPageSize() { + SearchCriteria criteria = new SearchCriteria(); + criteria.setPage(0); + criteria.setPageSize(5); + criteria.setSearchString(""); + List hotels = hotelSearchAgent.findHotels(criteria); + assertEquals(5, hotels.size()); + } + +} diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/import.sql b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/import.sql new file mode 100644 index 00000000..14284da7 --- /dev/null +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/import.sql @@ -0,0 +1,23 @@ +insert into Hotel (id, price, name, address, city, state, zip, country) values (1, 199, 'Westin Diplomat', '3555 S. Ocean Drive', 'Hollywood', 'FL', '33019', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (2, 60, 'Jameson Inn', '890 Palm Bay Rd NE', 'Palm Bay', 'FL', '32905', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (3, 199, 'Chilworth Manor', 'The Cottage, Southampton Business Park', 'Southampton', 'Hants', 'SO16 7JF', 'UK') +insert into Hotel (id, price, name, address, city, state, zip, country) values (4, 120, 'Marriott Courtyard', 'Tower Place, Buckhead', 'Atlanta', 'GA', '30305', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (5, 180, 'Doubletree', 'Tower Place, Buckhead', 'Atlanta', 'GA', '30305', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (6, 450, 'W Hotel', 'Union Square, Manhattan', 'NY', 'NY', '10011', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (7, 450, 'W Hotel', 'Lexington Ave, Manhattan', 'NY', 'NY', '10011', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (8, 250, 'Hotel Rouge', '1315 16th Street NW', 'Washington', 'DC', '20036', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (9, 300, '70 Park Avenue Hotel', '70 Park Avenue', 'NY', 'NY', '10011', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (10, 300, 'Conrad Miami', '1395 Brickell Ave', 'Miami', 'FL', '33131', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (11, 80, 'Sea Horse Inn', '2106 N Clairemont Ave', 'Eau Claire', 'WI', '54703', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (12, 90, 'Super 8 Eau Claire Campus Area', '1151 W Macarthur Ave', 'Eau Claire', 'WI', '54701', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (13, 160, 'Marriot Downtown', '55 Fourth Street', 'San Francisco', 'CA', '94103', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (14, 200, 'Hilton Diagonal Mar', 'Passeig del Taulat 262-264', 'Barcelona', 'Catalunya', '08019', 'Spain') +insert into Hotel (id, price, name, address, city, state, zip, country) values (15, 210, 'Hilton Tel Aviv', 'Independence Park', 'Tel Aviv', '', '63405', 'Israel') +insert into Hotel (id, price, name, address, city, state, zip, country) values (16, 240, 'InterContinental Tokyo Bay', 'Takeshiba Pier', 'Tokyo', '', '105', 'Japan') +insert into Hotel (id, price, name, address, city, state, zip, country) values (17, 130, 'Hotel Beaulac', ' Esplanade Léopold-Robert 2', 'Neuchatel', '', '2000', 'Switzerland') +insert into Hotel (id, price, name, address, city, state, zip, country) values (18, 140, 'Conrad Treasury Place', 'William & George Streets', 'Brisbane', 'QLD', '4001', 'Australia') +insert into Hotel (id, price, name, address, city, state, zip, country) values (19, 230, 'Ritz Carlton', '1228 Sherbrooke St', 'West Montreal', 'Quebec', 'H3G1H6', 'Canada') +insert into Hotel (id, price, name, address, city, state, zip, country) values (20, 460, 'Ritz Carlton', 'Peachtree Rd, Buckhead', 'Atlanta', 'GA', '30326', 'USA') +insert into Hotel (id, price, name, address, city, state, zip, country) values (21, 220, 'Swissotel', '68 Market Street', 'Sydney', 'NSW', '2000', 'Australia') +insert into Hotel (id, price, name, address, city, state, zip, country) values (22, 250, 'Meliá White House', 'Albany Street', 'Regents Park London', '', 'NW13UP', 'Great Britain') +insert into Hotel (id, price, name, address, city, state, zip, country) values (23, 210, 'Hotel Allegro', '171 West Randolph Street', 'Chicago', 'IL', '60601', 'USA') \ No newline at end of file diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/test-hotel-persistence.xml b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/test-hotel-persistence.xml new file mode 100644 index 00000000..e691ef90 --- /dev/null +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/test-hotel-persistence.xml @@ -0,0 +1,16 @@ + + + + org.hibernate.ejb.HibernatePersistence + org.springframework.samples.springtravel.hotel.search.Hotel + + + + + + + + \ No newline at end of file diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/test-jpa-context.xml b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/test-jpa-context.xml new file mode 100644 index 00000000..b0bafa91 --- /dev/null +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.hotel.search/src/test/resources/test-jpa-context.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/.classpath b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/.classpath index a6c9b671..c50613be 100644 --- a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/.classpath +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/.classpath @@ -2,10 +2,23 @@ + - + + + + + + + + + + + + + diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/ivy.xml b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/ivy.xml index 63fa6a09..f4dc2989 100644 --- a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/ivy.xml +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/ivy.xml @@ -32,6 +32,20 @@ + + + + + + + + + + + + + + diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/main/java/org/springframework/samples/springtravel/hotel/booking/impl/JpaHotelBookingAgent.java b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/main/java/org/springframework/samples/springtravel/hotel/booking/impl/JpaHotelBookingAgent.java index 136658f6..46bf1890 100644 --- a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/main/java/org/springframework/samples/springtravel/hotel/booking/impl/JpaHotelBookingAgent.java +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/main/java/org/springframework/samples/springtravel/hotel/booking/impl/JpaHotelBookingAgent.java @@ -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 diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/booking/HotelBookingFlowExecutionTests.java b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/booking/HotelBookingFlowExecutionTests.java new file mode 100644 index 00000000..97840721 --- /dev/null +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/booking/HotelBookingFlowExecutionTests.java @@ -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; + } + +} diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/booking/impl/JpaHotelBookingAgentTests.java b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/booking/impl/JpaHotelBookingAgentTests.java new file mode 100644 index 00000000..4b4531ed --- /dev/null +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/booking/impl/JpaHotelBookingAgentTests.java @@ -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 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 bookings = hotelBookingAgent.findBookings("scott"); + assertEquals(1, bookings.size()); + assertEquals("Westin Diplomat", bookings.get(0).getHotel().getName()); + } + +} diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/search/HotelSearchFlowExecutionTests.java b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/search/HotelSearchFlowExecutionTests.java new file mode 100644 index 00000000..655f3265 --- /dev/null +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/java/org/springframework/samples/springtravel/hotel/search/HotelSearchFlowExecutionTests.java @@ -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 bookings = new ArrayList(); + 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 hotels = new ArrayList(); + 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 hotels = new ArrayList(); + 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"); + } + +} diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/resources/test-booking-persistence.xml b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/resources/test-booking-persistence.xml new file mode 100644 index 00000000..5049fd1d --- /dev/null +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/resources/test-booking-persistence.xml @@ -0,0 +1,18 @@ + + + + org.hibernate.ejb.HibernatePersistence + org.springframework.samples.springtravel.hotel.booking.User + org.springframework.samples.springtravel.hotel.booking.HotelBooking + org.springframework.samples.springtravel.hotel.search.Hotel + + + + + + + + \ No newline at end of file diff --git a/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/resources/test-jpa-context.xml b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/resources/test-jpa-context.xml new file mode 100644 index 00000000..10cf528d --- /dev/null +++ b/spring-webflow-samples/spring-travel-platform/org.springframework.samples.springtravel.webapp/src/test/resources/test-jpa-context.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file