From cec81102361803190249864308b8e25b816421dc Mon Sep 17 00:00:00 2001 From: Mic Date: Tue, 19 Feb 2013 16:36:23 +0800 Subject: [PATCH] moved Spring config files and changed AtomView example so it is based on Vets instead of Visits --- .springBeans | 3 +- pom.xml | 22 +--- readme.txt | 109 ------------------ .../jpa/JpaVisitRepositoryImpl.java | 1 - ...{VisitsAtomView.java => VetsAtomView.java} | 38 +++--- src/main/resources/log4j.xml | 4 + .../spring}/mvc-core-config.xml | 0 .../spring}/mvc-view-config.xml | 4 +- src/main/webapp/WEB-INF/jsp/vets/vetList.jsp | 3 + src/main/webapp/WEB-INF/web.xml | 2 +- .../petclinic/web/VisitsAtomViewTest.java | 46 +------- ...VisitsAtomViewTestWithContainer-config.xml | 11 ++ .../web/VisitsAtomViewWithContainerTest.java | 70 +++++++++++ 13 files changed, 114 insertions(+), 199 deletions(-) delete mode 100644 readme.txt rename src/main/java/org/springframework/samples/petclinic/web/{VisitsAtomView.java => VetsAtomView.java} (62%) rename src/main/{webapp/WEB-INF => resources/spring}/mvc-core-config.xml (100%) rename src/main/{webapp/WEB-INF => resources/spring}/mvc-view-config.xml (93%) create mode 100644 src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewTestWithContainer-config.xml create mode 100644 src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewWithContainerTest.java diff --git a/.springBeans b/.springBeans index 91f4b0eb1..0c2dbe44c 100644 --- a/.springBeans +++ b/.springBeans @@ -8,7 +8,8 @@ src/main/resources/spring/datasource-config.xml - src/main/webapp/WEB-INF/mvc-view-config.xml + src/main/resources/spring/mvc-core-config.xml + src/main/resources/spring/mvc-view-config.xml diff --git a/pom.xml b/pom.xml index 8e0c71261..803ccc6e2 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,8 @@ 1.7.1 4.2.0.Final 2.0 + 1.1.0.RELEASE + 4.10 @@ -152,7 +154,7 @@ org.springframework.data spring-data-jpa - 1.1.0.RELEASE + ${spring.data.jpa} @@ -207,7 +209,7 @@ junit junit - 4.10 + ${junit.version} test @@ -312,21 +314,5 @@ - - - src/test/resources - true - - - - src/main/webapp/WEB-INF - - true - - - **/mvc-*-config.xml - - - \ No newline at end of file diff --git a/readme.txt b/readme.txt deleted file mode 100644 index 6d26c339a..000000000 --- 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 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 "" 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. - - - - - ... - 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 ff43486a5..3500a437f 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 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 089d1044c..6df94372e 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 model, Feed feed, HttpServletRequest request) { - feed.setId("tag:springsource.com"); - feed.setTitle("Pet ClinicService Visits"); - @SuppressWarnings("unchecked") - List visits = (List) 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 buildFeedEntries(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { - @SuppressWarnings("unchecked") - List visits = (List) model.get("visits"); - List entries = new ArrayList(visits.size()); + Vets vets = (Vets) model.get("vets"); + List vetList = vets.getVetList(); + List entries = new ArrayList(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 f865a8473..f52e0fcec 100755 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -11,6 +11,10 @@ + + + + - + - + diff --git a/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp b/src/main/webapp/WEB-INF/jsp/vets/vetList.jsp index fc68b56d9..54bb10356 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 @@ ">View as XML + + ">Subscribe to Atom feed + diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 2817b8996..79b9bf6ba 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"> org.springframework.web.servlet.DispatcherServlet contextConfigLocation - /WEB-INF/mvc-core-config.xml + classpath:spring/mvc-core-config.xml 1 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 c785c5dbf..90b52e0b3 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 ... ) -@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 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 000000000..e0fd0acd0 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/web/VisitsAtomViewTestWithContainer-config.xml @@ -0,0 +1,11 @@ + + + + + + + + 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 000000000..1cfcdcf88 --- /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 ... ) +@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")); + + } +}