mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 20:35:49 +00:00
moved Spring config files and changed AtomView example so it is based on Vets instead of Visits
This commit is contained in:
parent
d92d53110b
commit
cec8110236
13 changed files with 114 additions and 199 deletions
|
@ -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>
|
||||
|
|
22
pom.xml
22
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>
|
109
readme.txt
109
readme.txt
|
@ -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>
|
|
@ -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");
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -11,6 +11,10 @@
|
|||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework.test.web">
|
||||
<level value="trace" />
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="info" /><!--
|
||||
|
|
|
@ -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>
|
||||
|
|
@ -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>
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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>
|
|
@ -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"));
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue