Implements JIRA ticket SGF-261 allowing an application domain object, or rather entity to be persisted to multiple Regions in the GemFire Cache.

This commit is contained in:
John Blum
2014-03-06 13:18:58 -08:00
parent 8fb61ef348
commit 36de58ccd7
8 changed files with 181 additions and 76 deletions

View File

@@ -1,3 +1,19 @@
/*
* 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.annotation.Id;
@@ -56,7 +72,7 @@ public class Animal {
@Override
public String toString() {
return String.format("{ type = %1$s, id = %2$d, name = %3$s }", getClass().getSimpleName(), getId(), getName());
return String.format("{ @type = %1$s, id = %2$d, name = %3$s }", getClass().getSimpleName(), getId(), getName());
}
}

View File

@@ -1,8 +1,23 @@
/*
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -11,11 +26,18 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* The AnimalRepositoryTest class is a test suite of test cases testing the functionality behind PR #55 involving
* persisting application domain object/entities to multiple Regions in GemFire's Cache.
* <p/>
* @author Stuart Williams
* @author John Blum
* @see org.junit.Test
* @see org.junit.runner.RunWith
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @link https://github.com/spring-projects/spring-data-gemfire/pull/55
*/
@ContextConfiguration
@ContextConfiguration("AnimalRepositoryTest-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@SuppressWarnings("unused")
public class AnimalRepositoryTest {
@@ -26,9 +48,6 @@ public class AnimalRepositoryTest {
@Autowired
private DogRepository dogRepo;
@Autowired
private RabbitRepository rabbitRepo;
protected static Animal createAnimal(final long id, final String name) {
Animal animal = new Animal();
animal.setId(id);
@@ -76,20 +95,4 @@ public class AnimalRepositoryTest {
assertEquals(foundCerberusTheDog, dogRepo.findOne(3L));
}
@Test(expected = ClassCastException.class)
public void testStoreAnimalHavingLongIdInRabbitsRegionWithStringKey() {
try {
rabbitRepo.save(createAnimal(123L, "Harry"));
}
// NOTE the ClassCastException thrown from GemFire is expected; this is not correct and the identifying type
// mismatch should have been caught by GemfireRepositoryFactory.getTemplate(..) method on line 129
// (appropriately throwing an IllegalArgumentException) after satisfying the condition on line 128,
// if only the @Region annotation were set on the domain class/entity!
catch (ClassCastException unexpected) {
//unexpected.printStackTrace(System.err);
assertTrue(unexpected.getMessage().contains("key ( java.lang.Long ) does not satisfy keyConstraint ( java.lang.String )"));
throw unexpected;
}
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* The IncompatibleRegionKeyEntityIdAnimalRepositoryTest class is a test suite of test cases testing the functionality
* behind PR #55 involving persisting application domain object/entities to multiple Regions in GemFire's Cache.
* <p/>
* @author John Blum
* @see org.junit.Test
* @see org.springframework.context.ConfigurableApplicationContext
* @since 1.4.0
* @link https://github.com/spring-projects/spring-data-gemfire/pull/55
*/
public class IncompatibleRegionKeyEntityIdAnimalRepositoryTest {
protected static final String APPLICATION_CONTEXT_CONFIG_LOCATION = String.format("%1$s%2$s%1$s%3$s",
File.separator, AnimalRepositoryTest.class.getPackage().getName().replace('.', File.separatorChar),
"IncompatibleRegionKeyEntityIdAnimalRepositoryTest-context.xml");
@Test(expected = IllegalArgumentException.class)
public void testStoreAnimalHavingLongIdInRabbitsRegionWithStringKey() {
try {
ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(
APPLICATION_CONTEXT_CONFIG_LOCATION);
applicationContext.getBean(RabbitRepository.class);
}
// NOTE the ClassCastException thrown from GemFire is unexpected; this is not correct and the identifying type
// mismatch should be caught and handled by GemfireRepositoryFactory.getTemplate(..) method on line 129
// (appropriately throwing an IllegalArgumentException) after satisfying the condition on line 128,
// which always occurs with the @Region annotation set on the domain class/entity!
catch (ClassCastException unexpected) {
//unexpected.printStackTrace(System.err);
//assertTrue(unexpected.getMessage().contains("key ( java.lang.Long ) does not satisfy keyConstraint ( java.lang.String )"));
fail(unexpected.getMessage());
}
catch (BeanCreationException expected) {
//expected.printStackTrace(System.err);
assertTrue(expected.getCause() instanceof IllegalArgumentException);
assertEquals(String.format("The region referenced only supports keys of type %1$s but the entity to be stored has an id of type %2$s!",
String.class, Long.class), expected.getCause().getMessage());
throw (IllegalArgumentException) expected.getCause();
}
}
}

View File

@@ -79,7 +79,7 @@ public class Plant {
@Override
public String toString() {
return String.format("{ type = %1$s, id = %2$s, name = %3$s }", getClass().getSimpleName(), getId(), getName());
return String.format("{ @type = %1$s, id = %2$s, name = %3$s }", getClass().getSimpleName(), getId(), getName());
}
}

View File

@@ -19,6 +19,8 @@ package org.springframework.data.gemfire.repository.sample;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ConfigurableApplicationContext;
@@ -29,19 +31,22 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
* persisting application domain object/entities to multiple Regions in GemFire's Cache.
* <p/>
* @author John Blum
* @see org.springframework.context.ApplicationContext
* @see org.junit.Test
* @see org.springframework.context.ConfigurableApplicationContext
* @since 1.4.0
* @link https://github.com/spring-projects/spring-data-gemfire/pull/55
*/
public class PlantRepositoryTest {
protected static final String APPLICATION_CONTEXT_CONFIG_LOCATION =
"/org/springframework/data/gemfire/repository/sample/PlantRepositoryTest-context.xml";
protected static final String APPLICATION_CONTEXT_CONFIG_LOCATION = String.format("%1$s%2$s%1$s%3$s",
File.separator, PlantRepositoryTest.class.getPackage().getName().replace('.', File.separatorChar),
"PlantRepositoryTest-context.xml");
@Test(expected = IllegalArgumentException.class)
public void testStorePlantHavingStringIdInPlantsRegionWithLongKey() {
try {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(APPLICATION_CONTEXT_CONFIG_LOCATION);
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
APPLICATION_CONTEXT_CONFIG_LOCATION);
context.getBean(PlantRepository.class);
}
// NOTE technically, the IllegalArgumentException for incompatible Region 'Key' and Entity ID is thrown