diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/Animal.java b/src/test/java/org/springframework/data/gemfire/repository/sample/Animal.java index 53e160f4..12c3c085 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/sample/Animal.java +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/Animal.java @@ -1,7 +1,12 @@ package org.springframework.data.gemfire.repository.sample; import org.springframework.data.annotation.Id; +import org.springframework.util.ObjectUtils; +/** + * @author Stuart Williams + * @author John Blum + */ public class Animal { @Id @@ -25,9 +30,33 @@ public class Animal { this.name = name; } + @Override + public boolean equals(final Object obj) { + if (obj == this) { + return true; + } + + if (!(obj instanceof Animal)) { + return false; + } + + Animal that = (Animal) obj; + + return ObjectUtils.nullSafeEquals(this.getId(), that.getId()) + && ObjectUtils.nullSafeEquals(this.getName(), that.getName()); + } + + @Override + public int hashCode() { + int hashValue = 17; + hashValue = 37 * hashValue + ObjectUtils.nullSafeHashCode(getId()); + hashValue = 37 * hashValue + ObjectUtils.nullSafeHashCode(getName()); + return hashValue; + } + @Override public String toString() { - return "Foo [id=" + id + ", name=" + name + "]"; + return String.format("{ type = %1$s, id = %2$d, name = %3$s }", getClass().getSimpleName(), getId(), getName()); } } diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/AnimalRepositoryTest.java b/src/test/java/org/springframework/data/gemfire/repository/sample/AnimalRepositoryTest.java index cc319464..8ec7bc1c 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/sample/AnimalRepositoryTest.java +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/AnimalRepositoryTest.java @@ -9,51 +9,66 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +/** + * @author Stuart Williams + * @author John Blum + */ @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) +@SuppressWarnings("unused") public class AnimalRepositoryTest { @Autowired - CatRepository catRepo; + private CatRepository catRepo; @Autowired - DogRepository dogRepo; + private DogRepository dogRepo; + + protected static Animal createAnimal(final long id, final String name) { + Animal animal = new Animal(); + animal.setId(id); + animal.setName(name); + return animal; + } @Test - public void foo() { + public void testAnimals() { + Animal felix = createAnimal(1, "Felix"); + Animal leo = createAnimal(2, "Leo"); + Animal credo = createAnimal(3, "Credo"); + Animal fido = createAnimal(1, "Fido"); - Animal felix = new Animal(); - felix.setId(1); - felix.setName("Felix"); - - Animal leo = new Animal(); - leo.setId(2); - leo.setName("Leo"); - - Animal fido = new Animal(); - fido.setId(1); - fido.setName("Fido"); - - Animal leo_ = catRepo.save(leo); - assertNotNull(leo_); - - Animal felix_ = catRepo.save(felix); - assertNotNull(felix_); - - Animal fido_ = dogRepo.save(fido); - assertNotNull(fido_); - - assertEquals(2L, catRepo.count()); - assertEquals(1L, dogRepo.count()); + assertNotNull(catRepo.save(felix)); + assertNotNull(catRepo.save(leo)); + assertNotNull(catRepo.save(credo)); + assertNotNull(dogRepo.save(fido)); + assertNotNull(dogRepo.save(credo)); + assertEquals(3L, catRepo.count()); + assertEquals(2L, dogRepo.count()); Animal foundFelix = catRepo.findOne(1L); + assertEquals(felix, foundFelix); - Animal findOther = catRepo.findByName("Leo"); - assertEquals(leo, findOther); + Animal foundLeo = catRepo.findBy("Leo"); + + assertEquals(leo, foundLeo); + + Animal foundCredoTheCat = catRepo.findByName("Credo"); + + assertEquals(credo, foundCredoTheCat); + assertEquals(foundCredoTheCat, catRepo.findBy("Credo")); + assertEquals(foundCredoTheCat, catRepo.findOne(3L)); Animal foundFido = dogRepo.findBy("Fido"); + assertEquals(fido, foundFido); + + Animal foundCredoTheDog = dogRepo.findByName("Credo"); + + assertEquals(credo, foundCredoTheDog); + assertEquals(foundCredoTheDog, dogRepo.findBy("Credo")); + assertEquals(foundCredoTheDog, dogRepo.findOne(3L)); } } diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/CatRepository.java b/src/test/java/org/springframework/data/gemfire/repository/sample/CatRepository.java index 86b07e76..d2eac8ec 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/sample/CatRepository.java +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/CatRepository.java @@ -1,15 +1,35 @@ +/* + * Copyright 2010-2013 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.data.gemfire.repository.sample; import org.springframework.data.gemfire.mapping.Region; import org.springframework.data.gemfire.repository.GemfireRepository; import org.springframework.data.gemfire.repository.Query; +/** + * @author Stuart Williams + * @author John Blum + */ @Region("Cats") public interface CatRepository extends GemfireRepository { Animal findByName(String name); - @Query("SELECT * FROM /Foo x WHERE x.name = $1") + @Query("SELECT * FROM /Animals x WHERE x.name = $1") Animal findBy(String name); } diff --git a/src/test/java/org/springframework/data/gemfire/repository/sample/DogRepository.java b/src/test/java/org/springframework/data/gemfire/repository/sample/DogRepository.java index daa32239..0eee1271 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/sample/DogRepository.java +++ b/src/test/java/org/springframework/data/gemfire/repository/sample/DogRepository.java @@ -1,15 +1,35 @@ +/* + * Copyright 2010-2013 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.data.gemfire.repository.sample; import org.springframework.data.gemfire.mapping.Region; import org.springframework.data.gemfire.repository.GemfireRepository; import org.springframework.data.gemfire.repository.Query; +/** + * @author Stuart Williams + * @author John Blum + */ @Region("Dogs") public interface DogRepository extends GemfireRepository { Animal findByName(String name); - @Query("SELECT * FROM /FooTwo x WHERE x.name = $1") + @Query("SELECT * FROM /Animals x WHERE x.name = $1") Animal findBy(String name); } diff --git a/src/test/resources/org/springframework/data/gemfire/repository/sample/AnimalRepositoryTest-context.xml b/src/test/resources/org/springframework/data/gemfire/repository/sample/AnimalRepositoryTest-context.xml index 4832e3eb..ac42ea6f 100644 --- a/src/test/resources/org/springframework/data/gemfire/repository/sample/AnimalRepositoryTest-context.xml +++ b/src/test/resources/org/springframework/data/gemfire/repository/sample/AnimalRepositoryTest-context.xml @@ -2,38 +2,35 @@ - - + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd +"> - fooRepositoryTest - localhost[11236] + springGemFireAnimalRepositoryTest config 0 - localhost[11236] - + + value-constraint="org.springframework.data.gemfire.repository.sample.Animal"/> + value-constraint="org.springframework.data.gemfire.repository.sample.Animal"/> - +