added unit tests
This commit is contained in:
@@ -6,5 +6,16 @@
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/resources"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="con" path="com.springsource.platform.ide.jdt.core.MANIFEST_CLASSPATH_CONTAINER"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.junit/com.springsource.org.junit/4.4.0/com.springsource.org.junit-4.4.0.jar" sourcepath="/IVY_CACHE/org.junit/com.springsource.org.junit/4.4.0/com.springsource.org.junit-sources-4.4.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.test/2.5.4.A/org.springframework.test-2.5.4.A.jar" sourcepath="IVY_CACHE/org.springframework/org.springframework.test/2.5.4.A/org.springframework.test-soruces-2.5.4.A.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-1.1.1.jar" sourcepath="/IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-sources-1.1.1.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.jboss.util/com.springsource.org.jboss.util/2.0.4.GA/com.springsource.org.jboss.util-2.0.4.GA.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.dom4j/com.springsource.org.dom4j/1.6.1/com.springsource.org.dom4j-1.6.1.jar" sourcepath="IVY_CACHE/org.dom4j/com.springsource.org.dom4j/1.6.1/com.springsource.org.dom4j-sources-1.6.1.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/edu.oswego.cs.concurrent/com.springsource.edu.oswego.cs.dl.util.concurrent/1.3.4/com.springsource.edu.oswego.cs.dl.util.concurrent-1.3.4.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.jboss.javassist/com.springsource.javassist/3.3.0.ga/com.springsource.javassist-3.3.0.ga.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.collections/3.2.0/com.springsource.org.apache.commons.collections-3.2.0.jar" sourcepath="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.collections/3.2.0/com.springsource.org.apache.commons.collections-sources-3.2.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/net.sourceforge.cglib/com.springsource.net.sf.cglib/2.1.3/com.springsource.net.sf.cglib-2.1.3.jar" sourcepath="IVY_CACHE/net.sourceforge.cglib/com.springsource.net.sf.cglib/2.1.3/com.springsource.net.sf.cglib-sources-2.1.3.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/javax.transaction/com.springsource.javax.transaction/1.1.0/com.springsource.javax.transaction-1.1.0.jar" sourcepath="/IVY_CACHE/javax.transaction/com.springsource.javax.transaction/1.1.0/com.springsource.javax.transaction-sources-1.1.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.antlr/com.springsource.antlr/2.7.6/com.springsource.antlr-2.7.6.jar"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
|
||||
@@ -21,7 +21,18 @@
|
||||
<dependency org="org.hibernate" name="org.hibernate.ejb-library" rev="3.3.1.ga" conf="provided->runtime" />
|
||||
<dependency org="org.springframework" name="org.springframework.spring-library" rev="2.5.4.A" conf="provided-platform->compile" />
|
||||
<dependency org="org.hsqldb" name="com.springsource.org.hsqldb" rev="1.8.0.9" conf="provided->runtime" />
|
||||
|
||||
|
||||
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.4.0" conf="test->runtime" />
|
||||
<dependency org="org.springframework" name="org.springframework.test" rev="2.5.4.A" conf="test->runtime" />
|
||||
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1" conf="test->runtime" />
|
||||
<dependency org="org.jboss.util" name="com.springsource.org.jboss.util" rev="2.0.4.GA" conf="test->runtime" />
|
||||
<dependency org="org.dom4j" name="com.springsource.org.dom4j" rev="1.6.1" conf="test->runtime" />
|
||||
<dependency org="edu.oswego.cs.concurrent" name="com.springsource.edu.oswego.cs.dl.util.concurrent" rev="1.3.4" conf="test->runtime" />
|
||||
<dependency org="org.jboss.javassist" name="com.springsource.javassist" rev="3.3.0.ga" conf="test->runtime" />
|
||||
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.collections" rev="3.2.0" conf="test->runtime" />
|
||||
<dependency org="net.sourceforge.cglib" name="com.springsource.net.sf.cglib" rev="2.1.3" conf="test->runtime" />
|
||||
<dependency org="javax.transaction" name="com.springsource.javax.transaction" rev="1.1.0" conf="test->runtime" />
|
||||
<dependency org="org.antlr" name="com.springsource.antlr" rev="2.7.6" conf="test->runtime" />
|
||||
</dependencies>
|
||||
|
||||
</ivy-module>
|
||||
|
||||
@@ -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<Hotel> 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<Hotel> 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<Hotel> hotels = hotelSearchAgent.findHotels(criteria);
|
||||
assertEquals(5, hotels.size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<EFBFBD> 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')
|
||||
@@ -0,0 +1,16 @@
|
||||
<?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.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>
|
||||
@@ -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.search"/>
|
||||
|
||||
<!-- 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-hotel-persistence.xml"/>
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="jpaVendorAdapter">
|
||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -2,10 +2,23 @@
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="src" path="src/main/resources"/>
|
||||
<classpathentry kind="src" path="src/main/webapp"/>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/resources"/>
|
||||
<classpathentry kind="src" path="src/main/webapp"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="con" path="com.springsource.platform.ide.jdt.core.MANIFEST_CLASSPATH_CONTAINER"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.junit/com.springsource.org.junit/4.4.0/com.springsource.org.junit-4.4.0.jar" sourcepath="/IVY_CACHE/org.junit/com.springsource.org.junit/4.4.0/com.springsource.org.junit-sources-4.4.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.test/2.5.4.A/org.springframework.test-2.5.4.A.jar" sourcepath="IVY_CACHE/org.springframework/org.springframework.test/2.5.4.A/org.springframework.test-soruces-2.5.4.A.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-1.1.1.jar" sourcepath="/IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-sources-1.1.1.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.jboss.util/com.springsource.org.jboss.util/2.0.4.GA/com.springsource.org.jboss.util-2.0.4.GA.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.dom4j/com.springsource.org.dom4j/1.6.1/com.springsource.org.dom4j-1.6.1.jar" sourcepath="IVY_CACHE/org.dom4j/com.springsource.org.dom4j/1.6.1/com.springsource.org.dom4j-sources-1.6.1.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/edu.oswego.cs.concurrent/com.springsource.edu.oswego.cs.dl.util.concurrent/1.3.4/com.springsource.edu.oswego.cs.dl.util.concurrent-1.3.4.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.jboss.javassist/com.springsource.javassist/3.3.0.ga/com.springsource.javassist-3.3.0.ga.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.collections/3.2.0/com.springsource.org.apache.commons.collections-3.2.0.jar" sourcepath="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.collections/3.2.0/com.springsource.org.apache.commons.collections-sources-3.2.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/net.sourceforge.cglib/com.springsource.net.sf.cglib/2.1.3/com.springsource.net.sf.cglib-2.1.3.jar" sourcepath="IVY_CACHE/net.sourceforge.cglib/com.springsource.net.sf.cglib/2.1.3/com.springsource.net.sf.cglib-sources-2.1.3.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/javax.transaction/com.springsource.javax.transaction/1.1.0/com.springsource.javax.transaction-1.1.0.jar" sourcepath="/IVY_CACHE/javax.transaction/com.springsource.javax.transaction/1.1.0/com.springsource.javax.transaction-sources-1.1.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.antlr/com.springsource.antlr/2.7.6/com.springsource.antlr-2.7.6.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.easymock/com.springsource.org.easymock/2.3.0/com.springsource.org.easymock-2.3.0.jar" sourcepath="/IVY_CACHE/org.easymock/com.springsource.org.easymock/2.3.0/com.springsource.org.easymock-sources-2.3.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/javax.el/com.springsource.javax.el/2.1.0/com.springsource.javax.el-2.1.0.jar" sourcepath="/IVY_CACHE/javax.el/com.springsource.javax.el/2.1.0/com.springsource.javax.el-sources-2.1.0.jar"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
|
||||
@@ -32,6 +32,20 @@
|
||||
<dependency org="org.springframework.security" name="org.springframework.security" rev="2.0.2.A" conf="provided->provided" />
|
||||
<dependency org="com.sun.facelets" name="com.springsource.com.sun.facelets" rev="1.1.14" conf="provided->runtime" />
|
||||
<dependency org="org.jboss.el" name="com.springsource.org.jboss.el" rev="2.0.0.GA" conf="provided->runtime" />
|
||||
|
||||
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.4.0" conf="test->runtime" />
|
||||
<dependency org="org.springframework" name="org.springframework.test" rev="2.5.4.A" conf="test->runtime" />
|
||||
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1" conf="test->runtime" />
|
||||
<dependency org="org.jboss.util" name="com.springsource.org.jboss.util" rev="2.0.4.GA" conf="test->runtime" />
|
||||
<dependency org="org.dom4j" name="com.springsource.org.dom4j" rev="1.6.1" conf="test->runtime" />
|
||||
<dependency org="edu.oswego.cs.concurrent" name="com.springsource.edu.oswego.cs.dl.util.concurrent" rev="1.3.4" conf="test->runtime" />
|
||||
<dependency org="org.jboss.javassist" name="com.springsource.javassist" rev="3.3.0.ga" conf="test->runtime" />
|
||||
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.collections" rev="3.2.0" conf="test->runtime" />
|
||||
<dependency org="net.sourceforge.cglib" name="com.springsource.net.sf.cglib" rev="2.1.3" conf="test->runtime" />
|
||||
<dependency org="javax.transaction" name="com.springsource.javax.transaction" rev="1.1.0" conf="test->runtime" />
|
||||
<dependency org="org.antlr" name="com.springsource.antlr" rev="2.7.6" conf="test->runtime" />
|
||||
<dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" conf="test->runtime" />
|
||||
<dependency org="javax.el" name="com.springsource.javax.el" rev="2.1.0" conf="test->runtime" />
|
||||
</dependencies>
|
||||
|
||||
</ivy-module>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user