Skip to content

Commit

Permalink
SGF-504 - Add additional tests and polish.
Browse files Browse the repository at this point in the history
  • Loading branch information
jxblum committed Jul 13, 2016
1 parent 8423cdb commit 9c37a5a
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 44 deletions.
Expand Up @@ -27,14 +27,15 @@
import org.springframework.context.annotation.Import;
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
import org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean;
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
import org.springframework.data.gemfire.repository.support.SimpleGemfireRepository;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;

/**
* Annotation to enable Gemfire repositories.
*
*
* @author Oliver Gierke
* @author John Blum
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
Expand Down Expand Up @@ -90,7 +91,7 @@
* Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So
* for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning
* for {@code PersonRepositoryImpl}.
*
*
* @return a String indicating the postfix to append to the Repository interface name when looking up the custom
* Repository implementing class.
*/
Expand All @@ -99,23 +100,23 @@
/**
* Configures the location of where to find the Spring Data named queries properties file. Will default to
* {@code META-INFO/jpa-named-queries.properties}.
*
*
* @return a String indicating the location of the name queries properties file.
*/
String namedQueriesLocation() default "";

/**
* Returns the key of the {@link QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
* {@link Key#CREATE_IF_NOT_FOUND}.
*
*
* @return the Key used to determine the Query lookup and creation strategy.
*/
Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND;

/**
* Returns the {@link FactoryBean} class to be used for each repository instance. Defaults to
* {@link GemfireRepositoryFactoryBean}.
*
*
* @return the {@link FactoryBean} class type used for each Repository interface.
*/
Class<?> repositoryFactoryBeanClass() default GemfireRepositoryFactoryBean.class;
Expand All @@ -125,12 +126,12 @@
*
* @since 1.7
*/
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
Class<?> repositoryBaseClass() default SimpleGemfireRepository.class;

/**
* Configures the name of the {@link GemfireMappingContext} bean definition to be used to create repositories
* discovered through this annotation. If not configured a default one will be created.
*
*
* @return the bean name of the {@link org.springframework.data.mapping.context.MappingContext} used by the
* Repository to map entities to the underlying data store.
*/
Expand Down
Expand Up @@ -46,8 +46,7 @@ public class GemfireRepositoryConfigurationExtension extends RepositoryConfigura

private static final String GEMFIRE_MODULE_PREFIX = "gemfire";
private static final String MAPPING_CONTEXT_PROPERTY_NAME = "gemfireMappingContext";
private static final String MAPPING_CONTEXT_REF_ANNOTATION_ATTRIBUTE = "mappingContextRef";
private static final String MAPPING_CONTEXT_REF_XML_ATTRIBUTE = "mapping-context-ref";
private static final String MAPPING_CONTEXT_REF_ATTRIBUTE_NAME = "mappingContextRef";

static final String DEFAULT_MAPPING_CONTEXT_BEAN_NAME = String.format("%1$s.%2$s",
GemfireMappingContext.class.getName(), "DEFAULT");
Expand Down Expand Up @@ -95,7 +94,7 @@ public String getRepositoryFactoryClassName() {
@Override
public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource configurationSource) {
builder.addPropertyReference(MAPPING_CONTEXT_PROPERTY_NAME, resolveMappingContextBeanName(
configurationSource.getAttribute(MAPPING_CONTEXT_REF_ANNOTATION_ATTRIBUTE)));
configurationSource.getAttribute(MAPPING_CONTEXT_REF_ATTRIBUTE_NAME)));
}

/*
Expand All @@ -105,7 +104,7 @@ public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfi
@Override
public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource configurationSource) {
builder.addPropertyReference(MAPPING_CONTEXT_PROPERTY_NAME, resolveMappingContextBeanName(
configurationSource.getElement().getAttribute(MAPPING_CONTEXT_REF_XML_ATTRIBUTE)));
configurationSource.getAttribute(MAPPING_CONTEXT_REF_ATTRIBUTE_NAME)));
}

/* (non-Javadoc) */
Expand All @@ -122,7 +121,7 @@ public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConf

super.registerBeansForRoot(registry, configurationSource);

if (!StringUtils.hasText(configurationSource.getAttribute(MAPPING_CONTEXT_REF_ANNOTATION_ATTRIBUTE))) {
if (!StringUtils.hasText(configurationSource.getAttribute(MAPPING_CONTEXT_REF_ATTRIBUTE_NAME))) {
registry.registerBeanDefinition(DEFAULT_MAPPING_CONTEXT_BEAN_NAME,
new RootBeanDefinition(GemfireMappingContext.class));
}
Expand Down
@@ -0,0 +1,137 @@
/*
* 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;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.util.Arrays;
import java.util.Map;
import java.util.Properties;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.LocalRegionFactoryBean;
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
import org.springframework.data.gemfire.repository.sample.Person;
import org.springframework.data.gemfire.repository.sample.PersonRepository;
import org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean;
import org.springframework.data.repository.core.support.RepositoryFactoryInformation;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.RegionAttributes;

/**
* Test suite of test cases testing that the GemFire-based {@link org.springframework.data.repository.Repository}
* factories, implementing the {@link RepositoryFactoryInformation} interface, can in fact be looked up in the
* Spring {@link ApplicationContext}.
*
* @author John Blum
* @since 1.9.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@SuppressWarnings("unused")
public class GemFireRepositoryFactoryInformationIntegrationTests {

@Autowired
private ApplicationContext applicationContext;

@Test
public void canAccessRepositoryFactoryInformationFactoryBeans() {
Map<String, RepositoryFactoryInformation> repositoryFactories =
applicationContext.getBeansOfType(RepositoryFactoryInformation.class);

assertThat(repositoryFactories, is(notNullValue(Map.class)));
assertThat(repositoryFactories.size(), is(greaterThan(0)));
assertThat(repositoryFactories.keySet(), hasItem("&personRepository"));
assertThat(repositoryFactories.get("&personRepository"), is(instanceOf(GemfireRepositoryFactoryBean.class)));
assertThat(Arrays.asList(applicationContext.getBeanNamesForType(PersonRepository.class)),
hasItem("personRepository"));
}

@Configuration
@EnableGemfireRepositories(basePackageClasses = { Person.class },
includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
value = org.springframework.data.gemfire.repository.sample.PersonRepository.class))
static class GemFireConfiguration {

String applicationName() {
return GemFireRepositoryFactoryInformationIntegrationTests.class.getSimpleName();
}

Properties gemfireProperties() {
Properties gemfireProperties = new Properties();

gemfireProperties.setProperty("name", applicationName());
gemfireProperties.setProperty("mcast-port", "0");
gemfireProperties.setProperty("log-level", "warning");

return gemfireProperties;
}

@Bean
CacheFactoryBean gemfireCache() {
CacheFactoryBean gemfireCache = new CacheFactoryBean();

gemfireCache.setClose(true);
gemfireCache.setProperties(gemfireProperties());

return gemfireCache;
}

@Bean(name = "simple")
LocalRegionFactoryBean<Long, Person> simpleRegion(Cache gemfireCache,
RegionAttributes<Long, Person> simpleRegionAttributes) {

LocalRegionFactoryBean<Long, Person> simpleRegion = new LocalRegionFactoryBean<Long, Person>();

simpleRegion.setAttributes(simpleRegionAttributes);
simpleRegion.setCache(gemfireCache);
simpleRegion.setClose(false);
simpleRegion.setPersistent(false);

return simpleRegion;
}

@Bean
@SuppressWarnings("unchecked")
RegionAttributesFactoryBean simpleRegionAttributes() {
RegionAttributesFactoryBean simpleRegionAttributes = new RegionAttributesFactoryBean();

simpleRegionAttributes.setKeyConstraint(Long.class);
simpleRegionAttributes.setValueConstraint(Person.class);

return simpleRegionAttributes;
}
}
}

0 comments on commit 9c37a5a

Please sign in to comment.