Review and test changes for PR #55, which involves storing an entity to multiple Regions in the GemFire Cache.

This commit is contained in:
John Blum
2014-03-05 10:00:30 -08:00
parent 5f98eb66b5
commit 19a1fe4bc0
5 changed files with 126 additions and 45 deletions

View File

@@ -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());
}
}

View File

@@ -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));
}
}

View File

@@ -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, Long> {
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);
}

View File

@@ -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, Long> {
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);
}