added a Spring Data implementation

- there are now 3 data access implementations (jdbc, jpa and
spring-data-jpa)
- added corresponding bean profiles
- JUnit tests are now successful
This commit is contained in:
Mic 2013-01-18 16:56:01 +08:00
parent c9c8c4e085
commit 97aba3f4e6
50 changed files with 368 additions and 426 deletions

View file

@ -1,15 +1,13 @@
package org.springframework.samples.petclinic;
import java.util.Collection;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.Collection;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.transaction.annotation.Transactional;
@ -117,7 +115,7 @@ public abstract class AbstractOwnerRepositoryTests {
o1.getPets();
}
@Test
@Test @Transactional
public void insertOwner() {
Collection<Owner> owners = this.ownerRepository.findByLastName("Schultz");
int found = owners.size();
@ -132,7 +130,7 @@ public abstract class AbstractOwnerRepositoryTests {
assertEquals("Verifying number of owners after inserting a new one.", found + 1, owners.size());
}
@Test
@Test @Transactional
public void updateOwner() throws Exception {
Owner o1 = this.ownerRepository.findById(1);
String old = o1.getLastName();

View file

@ -1,12 +1,12 @@
package org.springframework.samples.petclinic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.samples.petclinic.repository.PetRepository;
@ -95,7 +95,7 @@ public abstract class AbstractPetRepositoryTests {
@Test
public void getPetTypes() {
Collection<PetType> petTypes = this.petRepository.getPetTypes();
Collection<PetType> petTypes = this.petRepository.findPetTypes();
PetType t1 = EntityUtils.getById(petTypes, PetType.class, 1);
assertEquals("cat", t1.getName());
@ -105,7 +105,7 @@ public abstract class AbstractPetRepositoryTests {
@Test
public void findPet() {
Collection<PetType> types = this.petRepository.getPetTypes();
Collection<PetType> types = this.petRepository.findPetTypes();
Pet p7 = this.petRepository.findById(7);
assertTrue(p7.getName().startsWith("Samantha"));
assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), p7.getType().getId());
@ -122,24 +122,24 @@ public abstract class AbstractPetRepositoryTests {
int found = o6.getPets().size();
Pet pet = new Pet();
pet.setName("bowser");
Collection<PetType> types = this.petRepository.getPetTypes();
Collection<PetType> types = this.petRepository.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(new Date());
o6.addPet(pet);
assertEquals(found + 1, o6.getPets().size());
// both storePet and storeOwner are necessary to cover all ORM tools
this.petRepository.storePet(pet);
this.petRepository.save(pet);
this.ownerRepository.save(o6);
o6 = this.ownerRepository.findById(6);
assertEquals(found + 1, o6.getPets().size());
}
@Test
@Test @Transactional
public void updatePet() throws Exception {
Pet p7 = this.petRepository.findById(7);
String old = p7.getName();
p7.setName(old + "X");
this.petRepository.storePet(p7);
this.petRepository.save(p7);
p7 = this.petRepository.findById(7);
assertEquals(old + "X", p7.getName());
}

View file

@ -1,12 +1,10 @@
package org.springframework.samples.petclinic;
import java.util.Collection;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.Collection;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.repository.VetRepository;
import org.springframework.samples.petclinic.service.ClinicService;
@ -90,8 +88,8 @@ public abstract class AbstractVetRepositoryTests {
@Test @Transactional
public void getVets() {
Collection<Vet> vets = this.vetRepository.getVets();
public void findVets() {
Collection<Vet> vets = this.vetRepository.findAll();
Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
assertEquals("Leary", v1.getLastName());

View file

@ -1,17 +1,12 @@
package org.springframework.samples.petclinic;
import java.util.Collection;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.transaction.annotation.Transactional;
@ -101,8 +96,8 @@ public abstract class AbstractVisitRepositoryTests {
p7.addVisit(visit);
visit.setDescription("test");
// both storeVisit and storePet are necessary to cover all ORM tools
this.visitRepository.storeVisit(visit);
this.petRepository.storePet(p7);
this.visitRepository.save(visit);
this.petRepository.save(p7);
// assertTrue(!visit.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
p7 = this.petRepository.findById(7);
assertEquals(found + 1, p7.getVisits().size());

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jdbc;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractOwnerRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jdbc;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractPetRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jdbc;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVetRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jdbc;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVisitRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractPetRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVetRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.jpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVisitRepositoryTests;
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

View file

@ -0,0 +1,34 @@
package org.springframework.samples.petclinic.springdatajpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractOwnerRepositoryTests;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>
* Provides the following services:
* <ul>
* <li>Injects test dependencies, meaning that we don't need to perform
* application context lookups. See the setClinic() method. Injection uses
* autowiring by type.</li>
* <li>Executes each test method in its own transaction, which is automatically
* rolled back by default. This means that even if tests insert or otherwise
* change database state, there is no need for a teardown or cleanup script.</li>
* </ul>
* <p>
* </p>
*
* @author Rod Johnson
* @author Sam Brannen
* @author Michael Isvy
*/
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({"jpa","spring-data-jpa"})
public class JpaOwnerRepositoryImplTests extends AbstractOwnerRepositoryTests {
}

View file

@ -0,0 +1,28 @@
package org.springframework.samples.petclinic.springdatajpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractPetRepositoryTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>
* Integration tests for the {@link JdbcClinicImpl} implementation.
* </p>
* <p>
* </p>
*
* @author Thomas Risberg
* @author Michael Isvy
*/
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@ActiveProfiles({"jpa","spring-data-jpa"})
public class JpaPetRepositoryImplTests extends AbstractPetRepositoryTests {
}

View file

@ -0,0 +1,28 @@
package org.springframework.samples.petclinic.springdatajpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVetRepositoryTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>
* Integration tests for the {@link JdbcClinicImpl} implementation.
* </p>
* <p>
* </p>
*
* @author Thomas Risberg
* @author Michael Isvy
*/
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@ActiveProfiles({"jpa","spring-data-jpa"})
public class JpaVetRepositoryImplTests extends AbstractVetRepositoryTests {
}

View file

@ -0,0 +1,28 @@
package org.springframework.samples.petclinic.springdatajpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractVisitRepositoryTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>
* Integration tests for the {@link JdbcClinicImpl} implementation.
* </p>
* <p>
* </p>
*
* @author Thomas Risberg
* @author Michael Isvy
*/
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@ActiveProfiles({"jpa","spring-data-jpa"})
public class JpaVisitRepositoryImplTests extends AbstractVisitRepositoryTests {
}

View file

@ -1,5 +1,5 @@
package org.springframework.samples.petclinic.jpa;
package org.springframework.samples.petclinic.springdatajpa;
import org.junit.runner.RunWith;
import org.springframework.samples.petclinic.AbstractOwnerRepositoryTests;

View file

@ -16,23 +16,24 @@
package org.springframework.samples.petclinic.web;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Test;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.Visit;
import com.sun.syndication.feed.atom.Entry;
import com.sun.syndication.feed.atom.Feed;
/**
* @author Arjen Poutsma
*/