SGF-82, SGF-83 - Initial draft of repository integration.
Added support for annotation based entity mapping (@Region, @Id, @PersistenceConstructor). Added support for Spring Data repositories (query execution, query derivation).
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GemfirePersistentEntity}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class GemfirePersistentEntityUnitTests {
|
||||
|
||||
@Test
|
||||
public void defaultsRegionNameToClassName() {
|
||||
GemfirePersistentEntity<UnannotatedRegion> entity = new GemfirePersistentEntity<UnannotatedRegion>(
|
||||
ClassTypeInformation.from(UnannotatedRegion.class));
|
||||
assertThat(entity.getRegionName(), is(UnannotatedRegion.class.getSimpleName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultsAnnotatedRegionToCLassName() {
|
||||
GemfirePersistentEntity<UnnamedRegion> entity = new GemfirePersistentEntity<UnnamedRegion>(
|
||||
ClassTypeInformation.from(UnnamedRegion.class));
|
||||
assertThat(entity.getRegionName(), is(UnnamedRegion.class.getSimpleName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readsRegionNameFromAnnotation() {
|
||||
|
||||
GemfirePersistentEntity<AnnotatedRegion> entity = new GemfirePersistentEntity<AnnotatedRegion>(
|
||||
ClassTypeInformation.from(AnnotatedRegion.class));
|
||||
assertThat(entity.getRegionName(), is("Foo"));
|
||||
}
|
||||
|
||||
static class UnannotatedRegion {
|
||||
|
||||
}
|
||||
|
||||
@Region("Foo")
|
||||
static class AnnotatedRegion {
|
||||
|
||||
}
|
||||
|
||||
@Region
|
||||
static class UnnamedRegion {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.MappingPdxSerializer;
|
||||
import org.springframework.data.gemfire.repository.sample.Address;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
|
||||
import com.gemstone.gemfire.cache.Cache;
|
||||
import com.gemstone.gemfire.cache.CacheFactory;
|
||||
import com.gemstone.gemfire.cache.DataPolicy;
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
import com.gemstone.gemfire.cache.RegionFactory;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link MappingPdxSerializer}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class MappingPdxSerializerIntegrationTest {
|
||||
|
||||
Region<Object, Object> region;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
MappingPdxSerializer serializer = new MappingPdxSerializer(new GemfireMappingContext(),
|
||||
new DefaultConversionService());
|
||||
|
||||
CacheFactory factory = new CacheFactory();
|
||||
factory.setPdxSerializer(serializer);
|
||||
factory.setPdxPersistent(true);
|
||||
|
||||
Cache cache = factory.create();
|
||||
|
||||
RegionFactory<Object, Object> regionFactory = cache.createRegionFactory();
|
||||
regionFactory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
|
||||
region = regionFactory.create("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeAndDeserializeCorrectly() {
|
||||
|
||||
Address address = new Address();
|
||||
address.zipCode = "01234";
|
||||
address.city = "London";
|
||||
|
||||
Person person = new Person(1L, "Oliver", "Gierke");
|
||||
person.address = address;
|
||||
|
||||
region.put(1L, person);
|
||||
Object result = region.get(1L);
|
||||
|
||||
assertThat(result instanceof Person, is(true));
|
||||
|
||||
Person reference = person;
|
||||
assertThat(reference.getFirstname(), is(person.getFirstname()));
|
||||
assertThat(reference.getLastname(), is(person.getLastname()));
|
||||
assertThat(reference.address, is(person.address));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping;
|
||||
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.data.convert.EntityInstantiator;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
import org.springframework.data.mapping.model.ParameterValueProvider;
|
||||
|
||||
import com.gemstone.gemfire.pdx.PdxReader;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MappingPdxSerializer}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MappingPdxSerializerUnitTests {
|
||||
|
||||
GemfireMappingContext context;
|
||||
ConversionService conversionService;
|
||||
MappingPdxSerializer serializer;
|
||||
|
||||
@Mock
|
||||
EntityInstantiator instantiator;
|
||||
@Mock
|
||||
PdxReader reader;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
context = new GemfireMappingContext();
|
||||
conversionService = new GenericConversionService();
|
||||
serializer = new MappingPdxSerializer(context, conversionService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void usesRegisteredInstantiator() {
|
||||
|
||||
Person person = new Person(1L, "Oliver", "Gierke");
|
||||
|
||||
ParameterValueProvider<GemfirePersistentProperty> provider = any(ParameterValueProvider.class);
|
||||
GemfirePersistentEntity<?> entity = any(GemfirePersistentEntity.class);
|
||||
when(instantiator.createInstance(entity, provider)).thenReturn(person);
|
||||
|
||||
Map<Class<?>, EntityInstantiator> instantiators = new HashMap<Class<?>, EntityInstantiator>();
|
||||
instantiators.put(Person.class, instantiator);
|
||||
|
||||
serializer.setGemfireInstantiators(instantiators);
|
||||
serializer.fromData(Person.class, reader);
|
||||
|
||||
verify(instantiator, times(1)).createInstance(eq(context.getPersistentEntity(Person.class)),
|
||||
any(ParameterValueProvider.class));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2012 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.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.expression.TypedValue;
|
||||
|
||||
import com.gemstone.bp.edu.emory.mathcs.backport.java.util.Arrays;
|
||||
import com.gemstone.gemfire.pdx.PdxReader;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PdxReaderPropertyAccessorUnitTests {
|
||||
|
||||
@Mock
|
||||
PdxReader reader;
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void appliesToPdxReadersOnly() {
|
||||
List<Class<?>> classes = Arrays.asList(PdxReaderPropertyAccessor.INSTANCE.getSpecificTargetClasses());
|
||||
assertThat(classes, hasItem(PdxReader.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canReadPropertyIfReaderHasField() {
|
||||
|
||||
when(reader.hasField("key")).thenReturn(true);
|
||||
assertThat(PdxReaderPropertyAccessor.INSTANCE.canRead(null, reader, "key"), is(true));
|
||||
|
||||
when(reader.hasField("key")).thenReturn(false);
|
||||
assertThat(PdxReaderPropertyAccessor.INSTANCE.canRead(null, reader, "key"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsTypedNullIfNullIsReadFromReader() {
|
||||
|
||||
when(reader.readObject("key")).thenReturn(null);
|
||||
assertThat(PdxReaderPropertyAccessor.INSTANCE.read(null, reader, "key"), is(TypedValue.NULL));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsTypeValueWithValueReadFromReader() {
|
||||
|
||||
when(reader.readObject("key")).thenReturn("String");
|
||||
|
||||
TypedValue result = PdxReaderPropertyAccessor.INSTANCE.read(null, reader, "key");
|
||||
|
||||
assertThat(result.getTypeDescriptor(), is(TypeDescriptor.valueOf(String.class)));
|
||||
assertThat(result.getValue(), is((Object) "String"));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void doesNotSupportWrites() {
|
||||
|
||||
assertThat(PdxReaderPropertyAccessor.INSTANCE.canWrite(null, null, null), is(false));
|
||||
PdxReaderPropertyAccessor.INSTANCE.write(null, null, null, reader);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2012 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.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.gemfire.mapping.Regions;
|
||||
import org.springframework.data.gemfire.repository.sample.PersonRepository;
|
||||
import org.springframework.data.gemfire.repository.support.AbstractGemfireRepositoryFactoryIntegrationTests;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Integration tests for namespace usage.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@ContextConfiguration("repo-context.xml")
|
||||
public class NamespaceRepositoryIntegrationTests extends AbstractGemfireRepositoryFactoryIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
PersonRepository repository;
|
||||
|
||||
@Override
|
||||
protected PersonRepository getRepository(Regions regions) {
|
||||
return repository;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2012 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.query;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GemfireQueryCreator}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class GemfireQueryCreatorUnitTests {
|
||||
|
||||
GemfirePersistentEntity<Person> entity;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setUp() {
|
||||
|
||||
GemfireMappingContext context = new GemfireMappingContext();
|
||||
entity = (GemfirePersistentEntity<Person>) context.getPersistentEntity(Person.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createsQueryForSimplePropertyReferenceCorrectly() {
|
||||
|
||||
PartTree partTree = new PartTree("findByFirstname", Person.class);
|
||||
GemfireQueryCreator creator = new GemfireQueryCreator(partTree, entity);
|
||||
|
||||
QueryString query = creator.createQuery();
|
||||
assertThat(query.toString(), is("SELECT * FROM /simple x WHERE x.firstname = $1"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2012 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.query;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.repository.Query;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GemfireQueryMethodUnitTests {
|
||||
|
||||
@Mock
|
||||
RepositoryMetadata metadata;
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void detectsAnnotatedQueryCorrectly() throws Exception {
|
||||
|
||||
GemfireMappingContext context = new GemfireMappingContext();
|
||||
when(metadata.getDomainClass()).thenReturn((Class) Person.class);
|
||||
when(metadata.getReturnedDomainClass(Mockito.any(Method.class))).thenReturn((Class) Person.class);
|
||||
|
||||
GemfireQueryMethod method = new GemfireQueryMethod(Sample.class.getMethod("annotated"), metadata, context);
|
||||
assertThat(method.hasAnnotatedQuery(), is(true));
|
||||
assertThat(method.getAnnotatedQuery(), is("foo"));
|
||||
|
||||
method = new GemfireQueryMethod(Sample.class.getMethod("annotatedButEmpty"), metadata, context);
|
||||
assertThat(method.hasAnnotatedQuery(), is(false));
|
||||
assertThat(method.getAnnotatedQuery(), is(nullValue()));
|
||||
|
||||
method = new GemfireQueryMethod(Sample.class.getMethod("notAnnotated"), metadata, context);
|
||||
assertThat(method.hasAnnotatedQuery(), is(false));
|
||||
assertThat(method.getAnnotatedQuery(), is(nullValue()));
|
||||
}
|
||||
|
||||
interface Sample {
|
||||
|
||||
@Query("foo")
|
||||
void annotated();
|
||||
|
||||
@Query("")
|
||||
void annotatedButEmpty();
|
||||
|
||||
void notAnnotated();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2012 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.query;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.gemfire.repository.query.Predicates.AtomicPredicate;
|
||||
import org.springframework.data.repository.query.parser.Part;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class PredicatesUnitTests {
|
||||
|
||||
@Test
|
||||
public void atomicPredicateDefaultsAlias() {
|
||||
|
||||
Part part = new Part("firstname", Person.class);
|
||||
|
||||
Iterable<Integer> indexes = Arrays.asList(1);
|
||||
|
||||
Predicate predicate = new AtomicPredicate(part, indexes.iterator());
|
||||
assertThat(predicate.toString(null), is("x.firstname = $1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void concatenatesAndPredicateCorrectly() {
|
||||
|
||||
Part left = new Part("firstname", Person.class);
|
||||
Part right = new Part("lastname", Person.class);
|
||||
Iterator<Integer> indexes = Arrays.asList(1, 2).iterator();
|
||||
|
||||
Predicates predicate = Predicates.create(left, indexes);
|
||||
predicate = predicate.and(new AtomicPredicate(right, indexes));
|
||||
|
||||
assertThat(predicate.toString(null), is("x.firstname = $1 AND x.lastname = $2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void concatenatesOrPredicateCorrectly() {
|
||||
|
||||
Part left = new Part("firstname", Person.class);
|
||||
Part right = new Part("lastname", Person.class);
|
||||
Iterator<Integer> indexes = Arrays.asList(1, 2).iterator();
|
||||
|
||||
Predicates predicate = Predicates.create(left, indexes);
|
||||
predicate = predicate.or(new AtomicPredicate(right, indexes));
|
||||
|
||||
assertThat(predicate.toString(null), is("x.firstname = $1 OR x.lastname = $2"));
|
||||
}
|
||||
|
||||
static class Person {
|
||||
|
||||
String firstname;
|
||||
String lastname;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2012 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.query;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class QueryStringUnitTests {
|
||||
|
||||
@Mock
|
||||
@SuppressWarnings("rawtypes")
|
||||
Region region;
|
||||
|
||||
@Test
|
||||
public void replacesDomainObjectWithRegionNameCorrectly() {
|
||||
|
||||
QueryString query = new QueryString("SELECT * FROM /Person p WHERE p.firstname = $1");
|
||||
|
||||
when(region.getName()).thenReturn("foo");
|
||||
assertThat(query.forRegion(Person.class, region).toString(), is("SELECT * FROM /foo p WHERE p.firstname = $1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindsInValuesCorrectly() {
|
||||
|
||||
QueryString query = new QueryString("SELECT * FROM /Person p WHERE p.firstname IN SET $1");
|
||||
List<Integer> values = Arrays.asList(1, 2, 3);
|
||||
assertThat(query.bindIn(values).toString(), is("SELECT * FROM /Person p WHERE p.firstname IN SET ('1', '2', '3')"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsInParameterIndexesCorrectly() {
|
||||
|
||||
QueryString query = new QueryString("IN SET $1 OR IN SET $2");
|
||||
Iterable<Integer> indexes = query.getInParameterIndexes();
|
||||
assertThat(indexes, is((Iterable<Integer>) Arrays.asList(1, 2)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2012 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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class Address {
|
||||
|
||||
public String zipCode;
|
||||
public String city;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2012 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 java.io.Serializable;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.gemfire.mapping.Region;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Region("simple")
|
||||
public class Person implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 508843183613325255L;
|
||||
|
||||
@Id
|
||||
public Long id;
|
||||
public String firstname;
|
||||
public String lastname;
|
||||
public Address address;
|
||||
|
||||
public Person(Long id, String firstname, String lastname) {
|
||||
this.id = id;
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the firstname
|
||||
*/
|
||||
public String getFirstname() {
|
||||
return firstname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the lastname
|
||||
*/
|
||||
public String getLastname() {
|
||||
return lastname;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2012 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 java.util.Collection;
|
||||
|
||||
import org.springframework.data.gemfire.repository.Query;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface PersonRepository extends Repository<Person, Long> {
|
||||
|
||||
@Query("SELECT * FROM /Person p WHERE p.firstname = $1")
|
||||
Collection<Person> findByFirstnameAnnotated(String firstname);
|
||||
|
||||
@Query("SELECT * FROM /Person p WHERE p.firstname IN SET $1")
|
||||
Collection<Person> findByFirstnamesAnnotated(Collection<String> firstnames);
|
||||
|
||||
Collection<Person> findByFirstname(String firstname);
|
||||
|
||||
Collection<Person> findByFirstnameIn(Collection<String> firstnames);
|
||||
|
||||
Collection<Person> findByFirstnameIn(String... firstnames);
|
||||
|
||||
Collection<Person> findByFirstnameAndLastname(String firstname, String lastname);
|
||||
|
||||
Collection<Person> findByFirstnameOrLastname(String firstname, String lastname);
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright 2012 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.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
|
||||
import org.springframework.data.gemfire.mapping.Regions;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
import org.springframework.data.gemfire.repository.sample.PersonRepository;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
|
||||
/**
|
||||
* Integration test for {@link GemfireRepositoryFactory}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public abstract class AbstractGemfireRepositoryFactoryIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
List<Region<?, ?>> regions;
|
||||
|
||||
Person dave, carter, boyd, stefan, leroi, jeff;
|
||||
PersonRepository repository;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
dave = new Person(1L, "Dave", "Matthews");
|
||||
carter = new Person(2L, "Carter", "Beauford");
|
||||
boyd = new Person(3L, "Boyd", "Tinsley");
|
||||
stefan = new Person(4L, "Stefan", "Lessard");
|
||||
leroi = new Person(5L, "Leroi", "Moore");
|
||||
jeff = new Person(6L, "Jeff", "Coffin");
|
||||
|
||||
GemfireMappingContext context = new GemfireMappingContext();
|
||||
|
||||
Regions regions = new Regions(this.regions, context);
|
||||
GemfireTemplate template = new GemfireTemplate(regions.getRegion(Person.class));
|
||||
|
||||
template.put(dave.id, dave);
|
||||
template.put(carter.id, carter);
|
||||
template.put(boyd.id, boyd);
|
||||
template.put(stefan.id, stefan);
|
||||
template.put(leroi.id, leroi);
|
||||
template.put(jeff.id, jeff);
|
||||
|
||||
repository = getRepository(regions);
|
||||
}
|
||||
|
||||
protected abstract PersonRepository getRepository(Regions regions);
|
||||
|
||||
@Test
|
||||
public void foo() {
|
||||
assertResultsFound(repository.findByFirstnameAnnotated("Dave"), dave);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executesAnnotatedInQueryMethodCorrectly() {
|
||||
assertResultsFound(repository.findByFirstnamesAnnotated(Arrays.asList("Carter", "Dave")), carter, dave);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executesInQueryMethodCorrectly() {
|
||||
assertResultsFound(repository.findByFirstnameIn(Arrays.asList("Carter", "Dave")), carter, dave);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executesDerivedQueryCorrectly() {
|
||||
assertResultsFound(repository.findByFirstname("Carter"), carter);
|
||||
assertResultsFound(repository.findByFirstnameIn(Arrays.asList("Stefan", "Boyd")), stefan, boyd);
|
||||
assertResultsFound(repository.findByFirstnameIn("Leroi"), leroi);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executesDerivedQueryWithAndCorrectly() {
|
||||
assertResultsFound(repository.findByFirstnameAndLastname("Carter", "Beauford"), carter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executesDerivedQueryWithOrCorrectly() {
|
||||
assertResultsFound(repository.findByFirstnameOrLastname("Carter", "Matthews"), carter, dave);
|
||||
}
|
||||
|
||||
private <T> void assertResultsFound(Collection<T> result, T... expected) {
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertThat(result.size(), is(expected.length));
|
||||
|
||||
for (T element : expected) {
|
||||
assertThat(result.contains(element), is(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2012 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.support;
|
||||
|
||||
import org.springframework.data.gemfire.mapping.Regions;
|
||||
import org.springframework.data.gemfire.repository.sample.PersonRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Integration test for {@link GemfireRepositoryFactory}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@ContextConfiguration("../config/repo-context.xml")
|
||||
public class GemfireRepositoryFactoryIntegrationTests extends AbstractGemfireRepositoryFactoryIntegrationTests {
|
||||
|
||||
@Override
|
||||
protected PersonRepository getRepository(Regions regions) {
|
||||
|
||||
GemfireRepositoryFactory factory = new GemfireRepositoryFactory(regions, null);
|
||||
return factory.getRepository(PersonRepository.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2012 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.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.data.gemfire.repository.sample.Person;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.data.repository.core.support.ReflectionEntityInformation;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.gemstone.gemfire.cache.query.SelectResults;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link SimpleGemfireRepository}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("../../basic-template.xml")
|
||||
public class SimpleGemfireRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
GemfireTemplate template;
|
||||
|
||||
SimpleGemfireRepository<Person, Long> repository;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
EntityInformation<Person, Long> information = new ReflectionEntityInformation<Person, Long>(Person.class);
|
||||
repository = new SimpleGemfireRepository<Person, Long>(template, information);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void storeAndDeleteEntity() {
|
||||
|
||||
Person person = new Person(1L, "Oliver", "Gierke");
|
||||
|
||||
repository.save(person);
|
||||
|
||||
assertThat(repository.count(), is(1L));
|
||||
assertThat(repository.findOne(person.id), is(person));
|
||||
assertThat(repository.findAll().size(), is(1));
|
||||
|
||||
repository.delete(person);
|
||||
|
||||
assertThat(repository.count(), is(0L));
|
||||
assertThat(repository.findOne(person.id), is(nullValue()));
|
||||
assertThat(repository.findAll().size(), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryRegion() throws Exception {
|
||||
|
||||
Person person = new Person(1L, "Oliver", "Gierke");
|
||||
|
||||
template.put(1L, person);
|
||||
|
||||
SelectResults<Person> persons = template.find("SELECT * FROM /simple s WHERE s.firstname = $1", person.firstname);
|
||||
|
||||
assertThat(persons.size(), is(1));
|
||||
assertThat(persons.iterator().next(), is(person));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllWithGivenIds() {
|
||||
|
||||
Person dave = new Person(1L, "Dave", "Matthews");
|
||||
Person carter = new Person(2L, "Carter", "Beauford");
|
||||
Person leroi = new Person(3L, "Leroi", "Moore");
|
||||
|
||||
template.put(dave.id, dave);
|
||||
template.put(carter.id, carter);
|
||||
template.put(leroi.id, leroi);
|
||||
|
||||
Collection<Person> result = repository.findAll(Arrays.asList(carter.id, leroi.id));
|
||||
assertThat(result, hasItems(carter, leroi));
|
||||
assertThat(result, not(hasItems(dave)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user