diff --git a/.springBeans b/.springBeans index 91f4b0eb167f0dc961b4ca0ce7ff7b13188a9818..0c2dbe44c2c9ff5df22a6e1f98f581b1db7b8497 100644 --- a/.springBeans +++ b/.springBeans @@ -8,7 +8,8 @@ <enableImports><![CDATA[false]]></enableImports> <configs> <config>src/main/resources/spring/datasource-config.xml</config> - <config>src/main/webapp/WEB-INF/mvc-view-config.xml</config> + <config>src/main/resources/spring/mvc-core-config.xml</config> + <config>src/main/resources/spring/mvc-view-config.xml</config> </configs> <configSets> </configSets> diff --git a/pom.xml b/pom.xml index 8e0c71261df176c1073c135c9a2a274ff5946371..803ccc6e29cbb8d6db40dbe538051748314e47f8 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,8 @@ <aspectj.version>1.7.1</aspectj.version> <hibernate.validator.version>4.2.0.Final</hibernate.validator.version> <joda.version>2.0</joda.version> + <spring.data.jpa>1.1.0.RELEASE</spring.data.jpa> + <junit.version>4.10</junit.version> </properties> <dependencies> @@ -152,7 +154,7 @@ <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> - <version>1.1.0.RELEASE</version> + <version>${spring.data.jpa}</version> </dependency> <!-- Servlet --> @@ -207,7 +209,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>4.10</version> + <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> @@ -312,21 +314,5 @@ </executions> </plugin> </plugins> - <testResources> - <testResource> - <directory>src/test/resources</directory> - <filtering>true</filtering> - </testResource> - - <testResource> - <directory>src/main/webapp/WEB-INF</directory> - - <filtering>true</filtering> - - <includes> - <include>**/mvc-*-config.xml</include> - </includes> - </testResource> - </testResources> </build> </project> \ No newline at end of file diff --git a/readme.txt b/readme.txt deleted file mode 100644 index 6d26c339a8939b8d183f54f06f8e214fdbc388d2..0000000000000000000000000000000000000000 --- a/readme.txt +++ /dev/null @@ -1,109 +0,0 @@ -========================================================================== -=== Spring PetClinic Sample Application -========================================================================== - -@author Ken Krebs -@author Juergen Hoeller -@author Rob Harrop -@author Costin Leau -@author Sam Brannen -@author Scott Andrews - -========================================================================== -=== Data Access Strategies -========================================================================== - -PetClinic features alternative DAO implementations and application -configurations for JDBC, Hibernate, and JPA, with HSQLDB and MySQL as -target databases. The default PetClinic configuration is JDBC on HSQLDB. -See "src/main/resources/jdbc.properties" as well as web.xml and -applicationContext-*.xml in the "src/main/webapp/WEB-INF" folder for -details. A simple comment change in web.xml switches between the data -access strategies. - -The JDBC and Hibernate versions of PetClinic also demonstrate JMX support -via the use of <context:mbean-export/> for exporting MBeans. -SimpleJdbcClinic exposes the SimpleJdbcClinicMBean management interface -via JMX through the use of the @ManagedResource and @ManagedOperation -annotations; whereas, the HibernateStatistics service is exposed via JMX -through auto-detection of the service MBean. You can start up the JDK's -JConsole to manage the exported bean. - -All data access strategies can work with JTA for transaction management by -activating the JtaTransactionManager and a JndiObjectFactoryBean that -refers to a transactional container DataSource. The default for JDBC is -DataSourceTransactionManager; for Hibernate, HibernateTransactionManager; -for JPA, JpaTransactionManager. Those local strategies allow for working -with any locally defined DataSource. - -Note that the sample configurations for JDBC, Hibernate, and JPA configure -a BasicDataSource from the Apache Commons DBCP project for connection -pooling. - -========================================================================== -=== Build and Deployment -========================================================================== - -The Spring PetClinic sample application is built using Maven. -When the project is first built, Maven will automatically download all required -dependencies (if these haven't been downloaded before). Thus the initial build -may take a few minutes depending on the speed of your Internet connection, -but subsequent builds will be much faster. - -Available build commands: - -- mvn clean --> cleans the project -- mvn clean test --> cleans the project and runs all tests -- mvn clean package --> cleans the project and builds the WAR - -After building the project with "mvn clean package", you will find the -resulting WAR file in the "target/" directory. By default, an -embedded HSQLDB instance in configured. No other steps are necessary to -get the data source up and running: you can simply deploy the built WAR -file directly to your Servlet container. - -For MySQL, you'll need to use the corresponding schema and SQL scripts in -the "db/mysql" subdirectory. Follow the steps outlined in -"db/mysql/petclinic_db_setup_mysql.txt" for explicit details. - -In you intend to use a local DataSource, the JDBC settings can be adapted -in "src/main/resources/jdbc.properties". To use a JTA DataSource, you need -to set up corresponding DataSources in your Java EE container. - -Notes on enabling Log4J: - - Log4J is disabled by default due to issues with JBoss. - - Uncomment the Log4J listener in "WEB-INF/web.xml" to enable logging. - -Notes on service static resources: - - Most web containers provide a 'default' servlet for serving static - resources; Petclinic relies on it for its images. - - On containers without such a mapping (ex: GlassFish), uncomment the - 'default' declaration in "WEB-INF/web.xml". - -========================================================================== -=== JPA on Tomcat -========================================================================== - -This section provides tips on using the Java Persistence API (JPA) on -Apache Tomcat 4.x or higher with a persistence provider that requires -class instrumentation (such as TopLink Essentials). - -To use JPA class instrumentation, Tomcat has to be instructed to use a -custom class loader which supports instrumentation. See the JPA section of -the Spring reference manual for complete details. - -The basic steps are: - - Copy "org.springframework.instrument.tomcat-3.0.0.RELEASE.jar" from the - Spring distribution to "TOMCAT_HOME/server/lib". - - If you're running on Tomcat 5.x, modify "TOMCAT_HOME/conf/server.xml" - and add a new "<Context>" element for 'petclinic' (see below). You can - alternatively deploy the WAR including "META-INF/context.xml" from this - sample application's "src/main/webapp" directory, in which case you - will need to uncomment the Loader element in that file to enable the - use of the TomcatInstrumentableClassLoader. - -<Context path="/petclinic" docBase="/petclinic/location" ...> - <!-- please note that useSystemClassLoaderAsParent is available since Tomcat 5.5.20; remove it if previous versions are being used --> - <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader" useSystemClassLoaderAsParent="false"/> - ... -</Context> diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java index ff43486a53c71cb627a0d914fae58dd8995bb40b..3500a437fbe8f97781a84e697a9e96431bfd82b5 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java @@ -48,7 +48,6 @@ public class JpaVisitRepositoryImpl implements VisitRepository { } - @Override @SuppressWarnings("unchecked") public List<Visit> findByPetId(Integer petId) { Query query = this.em.createQuery("SELECT visit FROM Visit v where v.pets.id= :id"); diff --git a/src/main/java/org/springframework/samples/petclinic/web/VisitsAtomView.java b/src/main/java/org/springframework/samples/petclinic/web/VetsAtomView.java similarity index 62% rename from src/main/java/org/springframework/samples/petclinic/web/VisitsAtomView.java rename to src/main/java/org/springframework/samples/petclinic/web/VetsAtomView.java index 089d1044cd51bb714ed10c63293548e5f8ef9531..6df94372ef8a700e81ab26e7541dbd69f9f1e930 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VisitsAtomView.java +++ b/src/main/java/org/springframework/samples/petclinic/web/VetsAtomView.java @@ -16,14 +16,14 @@ package org.springframework.samples.petclinic.web; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.springframework.samples.petclinic.model.Visit; +import org.springframework.samples.petclinic.model.Vet; +import org.springframework.samples.petclinic.model.Vets; import org.springframework.web.servlet.view.feed.AbstractAtomFeedView; import com.sun.syndication.feed.atom.Content; @@ -36,45 +36,37 @@ import com.sun.syndication.feed.atom.Feed; * @author Alef Arendsen * @author Arjen Poutsma */ -public class VisitsAtomView extends AbstractAtomFeedView { +public class VetsAtomView extends AbstractAtomFeedView { @Override protected void buildFeedMetadata(Map<String, Object> model, Feed feed, HttpServletRequest request) { - feed.setId("tag:springsource.com"); - feed.setTitle("Pet ClinicService Visits"); - @SuppressWarnings("unchecked") - List<Visit> visits = (List<Visit>) model.get("visits"); - for (Visit visit : visits) { - Date date = visit.getDate().toDate(); - if (feed.getUpdated() == null || date.compareTo(feed.getUpdated()) > 0) { - feed.setUpdated(date); - } - } + feed.setId("tag:springsource.org"); + feed.setTitle("Veterinarians"); + //feed.setUpdated(date); } @Override protected List<Entry> buildFeedEntries(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { - @SuppressWarnings("unchecked") - List<Visit> visits = (List<Visit>) model.get("visits"); - List<Entry> entries = new ArrayList<Entry>(visits.size()); + Vets vets = (Vets) model.get("vets"); + List<Vet> vetList = vets.getVetList(); + List<Entry> entries = new ArrayList<Entry>(vetList.size()); - for (Visit visit : visits) { + for (Vet vet : vetList) { Entry entry = new Entry(); - String date = String.format("%1$tY-%1$tm-%1$td", visit.getDate().toDate()); // see http://diveintomark.org/archives/2004/05/28/howto-atom-id#other - entry.setId(String.format("tag:springsource.com,%s:%d", date, visit.getId())); - entry.setTitle(String.format("%s visit on %s", visit.getPet().getName(), date)); - entry.setUpdated(visit.getDate().toDate()); + entry.setId(String.format("tag:springsource.org,%s", vet.getId())); + entry.setTitle(String.format("Vet: %s %s", vet.getFirstName(), vet.getLastName())); + //entry.setUpdated(visit.getDate().toDate()); Content summary = new Content(); - summary.setValue(visit.getDescription()); + summary.setValue(vet.getSpecialties().toString()); entry.setSummary(summary); entries.add(entry); } - + response.setContentType("blabla"); return entries; } diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index f865a8473768bf93f24215566b65bf4739530bb4..f52e0fcec6706890088becad7b9998e51e08c706 100755 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -11,6 +11,10 @@ </layout> </appender> + <logger name="org.springframework.test.web"> + <level value="trace" /> + </logger> + <!-- Root Logger --> <root> <priority value="info" /><!-- diff --git a/src/main/webapp/WEB-INF/mvc-core-config.xml b/src/main/resources/spring/mvc-core-config.xml similarity index 100% rename from src/main/webapp/WEB-INF/mvc-core-config.xml rename to src/main/resources/spring/mvc-core-config.xml diff --git a/src/main/webapp/WEB-INF/mvc-view-config.xml b/src/main/resources/spring/mvc-view-config.xml similarity index 93% rename from src/main/webapp/WEB-INF/mvc-view-config.xml rename to src/main/resources/spring/mvc-view-config.xml index 93be9b6e72eb7ff2c0361988ea2631f9471e3f2a..053fa3fdb5b19b60796e086abd525e2c1194b23f 100644 --- a/src/main/webapp/WEB-INF/mvc-view-config.xml +++ b/src/main/resources/spring/mvc-view-config.xml @@ -38,10 +38,10 @@ <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <!-- Renders an Atom feed of the visits. Used by the BeanNameViewResolver --> - <bean id="visitList" class="org.springframework.samples.petclinic.web.VisitsAtomView"/> + <bean id="vets/vetList.atom" class="org.springframework.samples.petclinic.web.VetsAtomView"/> <!-- Renders an XML view. Used by the BeanNameViewResolver --> - <bean id="vets/vetList" class="org.springframework.web.servlet.view.xml.MarshallingView"> + <bean id="vets/vetList.xml" class="org.springframework.web.servlet.view.xml.MarshallingView"> <property name="marshaller" ref="marshaller"/> </bean> diff --git a/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp b/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp index fc68b56d9d31395a8fb9844e4b24baa6002c339f..54bb103564e2e8979a5e5035d111c2346d3ac769 100644 --- a/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp +++ b/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp @@ -40,6 +40,9 @@ <td> <a href="<spring:url value="/vets.xml" htmlEscape="true" />">View as XML</a> </td> + <td> + <a href="<spring:url value="/vets.atom" htmlEscape="true" />">Subscribe to Atom feed</a> + </td> </tr> </table> diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 2817b8996a3f9a289789595cf813bd029149a527..79b9bf6bab2a71448de1ab9c435eefabd18c9e14 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -38,7 +38,7 @@ id="WebApp_ID" version="2.5"> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> - <param-value>/WEB-INF/mvc-core-config.xml</param-value> + <param-value>classpath:spring/mvc-core-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> diff --git a/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewTest.java b/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewTest.java index c785c5dbf2f184f3cc27d504a202fcabfad7c1d3..90b52e0b33fabf08ecbeb3ddeff0018a158c846b 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewTest.java +++ b/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewTest.java @@ -27,24 +27,9 @@ import java.util.Map; import org.joda.time.DateTime; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.model.Visit; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.ResultActions; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import org.springframework.web.context.WebApplicationContext; -import static org.hamcrest.Matchers.containsString; import com.sun.syndication.feed.atom.Entry; import com.sun.syndication.feed.atom.Feed; @@ -53,44 +38,17 @@ import com.sun.syndication.feed.atom.Feed; * @author Arjen Poutsma * @author Michael Isvy */ -@RunWith(SpringJUnit4ClassRunner.class) -@WebAppConfiguration -// Spring configuration files that are inside WEB-INF folder can be referenced here because they've been -// added to the classpath inside the Maven pom.xml file (inside <build> <testResources> ... </testResources> </build>) -@ContextConfiguration({"classpath*:mvc-*-config.xml", "classpath*:spring/*-config.xml"}) -@ActiveProfiles("jdbc") public class VisitsAtomViewTest { - @Autowired - private WebApplicationContext webApplicationContext; - - private MockMvc mockMvc; - - @Before - public void setup() { - this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build(); - } - - private VisitsAtomView visitView; + private VetsAtomView visitView; private Map<String, Object> model; private Feed feed; - //@Test - public void getVisits() throws Exception { - MediaType mediaType = MediaType.APPLICATION_ATOM_XML; - ResultActions actions = this.mockMvc.perform(get("/owners/7/pets/9/visits.atom").accept(mediaType)); - actions.andExpect(status().isOk()); - actions.andExpect(content().contentType("application/atom+xml")); - //actions.andExpect(content().xml("Pet ClinicService Visits")); - actions.andExpect(xpath("//*").string(containsString("Pet ClinicService Visits"))); - - } - @Before public void setUp() { - visitView = new VisitsAtomView(); + visitView = new VetsAtomView(); PetType dog = new PetType(); dog.setName("dog"); Pet bello = new Pet(); diff --git a/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewTestWithContainer-config.xml b/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewTestWithContainer-config.xml new file mode 100644 index 0000000000000000000000000000000000000000..e0fd0acd02f2b7d0eb87db84adb9672323f8add1 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewTestWithContainer-config.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + - DispatcherServlet application context for PetClinic's web tier. +--> +<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> + + <import resource="classpath:spring/dao-config.xml"/> + <import resource="classpath:spring/mvc-core-config.xml"/> + +</beans> diff --git a/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewWithContainerTest.java b/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewWithContainerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1cfcdcf88877fee27f9e75e2fb63ad55e55fff48 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewWithContainerTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.samples.petclinic.web; + +import static org.hamcrest.Matchers.containsString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +/** + * @author Arjen Poutsma + * @author Michael Isvy + */ +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +// Spring configuration files that are inside WEB-INF folder can be referenced here because they've been +// added to the classpath inside the Maven pom.xml file (inside <build> <testResources> ... </testResources> </build>) +@ContextConfiguration("VisitsAtomViewTestWithContainer-config.xml") +@ActiveProfiles("jdbc") +public class VisitsAtomViewWithContainerTest { + + @Autowired + private WebApplicationContext webApplicationContext; + + private MockMvc mockMvc; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build(); + } + + @Test + public void getVisits() throws Exception { + MediaType mediaType = MediaType.APPLICATION_ATOM_XML; + ResultActions actions = this.mockMvc.perform(get("/vets.atom")); + actions.andExpect(status().isOk()); + actions.andExpect(xpath("//*").string(containsString("Pet ClinicService Visits"))); + actions.andExpect(content().contentType("application/atom+xml")); + + } +}