DATAKV-87 - Polished implementation of configurability of Map type on @EnableMapRepositories.

Removed the MapKeyValueAdapterFactory in favor of using instantiating MapKeyValueAdapters directly. Reverted additional hook in KeyValueRepositoryConfigurationExtension as we now rather use inner bean definitions.

Original pull request: #2.
This commit is contained in:
Oliver Gierke
2014-12-05 19:14:06 +01:00
parent b16a454c6c
commit bc88cb48ac
7 changed files with 77 additions and 332 deletions

View File

@@ -1,96 +0,0 @@
/*
* Copyright 2014 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.map;
import static org.hamcrest.core.IsEqual.*;
import static org.hamcrest.core.IsInstanceOf.*;
import static org.junit.Assert.*;
import java.io.Serializable;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import org.junit.Test;
import org.springframework.core.CollectionFactory;
/**
* @author Christoph Strobl
*/
public class MapKeyValueAdapterFactoryUnitTests {
/**
* @see DATACMNS-525
*/
@Test
public void shouldDefaultToConcurrentHashMapWhenTypeIsNull() {
assertThat(new MapKeyValueAdapterFactory(null).getAdapter().getKeySpaceMap("foo"),
instanceOf(ConcurrentHashMap.class));
}
/**
* @see DATACMNS-525
*/
@Test
public void shouldDefaultToCollecitonUtilsDefaultForInterfaceTypes() {
assertThat(new MapKeyValueAdapterFactory(Map.class).getAdapter().getKeySpaceMap("foo"),
instanceOf(CollectionFactory.createMap(Map.class, 0).getClass()));
}
/**
* @see DATACMNS-525
*/
@Test
public void shouldUseConcreteMapTypeWhenInstantiable() {
assertThat(new MapKeyValueAdapterFactory(ConcurrentSkipListMap.class).getAdapter().getKeySpaceMap("foo"),
instanceOf(CollectionFactory.createMap(ConcurrentSkipListMap.class, 0).getClass()));
}
/**
* @see DATACMNS-525
*/
@Test
public void shouldPopulateAdapterWithValues() {
MapKeyValueAdapterFactory factory = new MapKeyValueAdapterFactory();
factory.setInitialValuesForKeyspace("foo", Collections.singletonMap("1", "STANIS"));
factory.setInitialValuesForKeyspace("bar", Collections.singletonMap("1", "ROBERT"));
assertThat((String) factory.getAdapter().get("1", "foo"), equalTo("STANIS"));
assertThat((String) factory.getAdapter().get("1", "bar"), equalTo("ROBERT"));
}
/**
* @see DATACMNS-525
*/
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenSettingValuesForNullKeySpace() {
new MapKeyValueAdapterFactory().setInitialValuesForKeyspace(null, Collections.<Serializable, Object> emptyMap());
}
/**
* @see DATACMNS-525
*/
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenSettingNullValuesForKeySpace() {
new MapKeyValueAdapterFactory().setInitialValuesForKeyspace("foo", null);
}
}

View File

@@ -23,13 +23,14 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.keyvalue.core.KeyValueAdapter;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.data.map.MapKeyValueAdapter;
import org.springframework.test.util.ReflectionTestUtils;
/**
* Integration tests for {@link MapRepositoryConfigurationExtension}.
@@ -70,56 +71,27 @@ public class MapRepositoriesConfigurationExtensionIntegrationTests {
* @see DATAKV-87
*/
@Test
public void registeresMapKeyValueAdapterFactoryWithDefaultMapTypeWhenIsNotCostomized() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
assertThat(context.getBeanFactory().getBeanDefinition("mapKeyValueAdapterFactory").getConstructorArgumentValues()
.getGenericArgumentValue(Class.class).getValue().equals(ConcurrentHashMap.class), is(true));
context.close();
public void considersMapTypeConfiguredOnAnnotation() {
assertKeyValueTemplateWithAdapterFor(ConcurrentSkipListMap.class, new AnnotationConfigApplicationContext(
ConfigWithCustomizedMapType.class));
}
/**
* @see DATAKV-87
*/
@Test
public void registeresMapKeyValueAdapterFactoryWithGivenMapTypeWhenIsCostomized() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithCustomizedMapType.class);
assertThat(context.getBeanFactory().getBeanDefinition("mapKeyValueAdapterFactory").getConstructorArgumentValues()
.getGenericArgumentValue(Class.class).getValue().equals(ConcurrentSkipListMap.class), is(true));
context.close();
public void doesNotConsiderMapConfiguredIfTemplateIsPresent() {
assertKeyValueTemplateWithAdapterFor(ConcurrentHashMap.class, new AnnotationConfigApplicationContext(
ConfigWithCustomizedMapTypeAndExplicitDefinitionOfKeyValueTemplate.class));
}
/**
* @see DATAKV-87
*/
@Test
public void doesNotRegisterMapKeyValueAdapterFactoryWhenKeyValueTemplateIsCustomized() {
private static void assertKeyValueTemplateWithAdapterFor(Class<?> mapType, ApplicationContext context) {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
ConfigWithCustomizedMapTypeAndExplicitDefinitionOfKeyValueTemplate.class);
KeyValueTemplate template = context.getBean(KeyValueTemplate.class);
Object adapter = ReflectionTestUtils.getField(template, "adapter");
assertThat(Arrays.asList(context.getBeanDefinitionNames()), not(hasItem("mapKeyValueAdapterFactory")));
context.close();
}
/**
* @see DATAKV-87
*/
@Test
public void doesNotRegisterMapKeyValueAdapterFactoryWhenTemplateReferenceIsCustomized() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
ConfigWithCustomTemplateReference.class);
assertThat(Arrays.asList(context.getBeanDefinitionNames()), not(hasItem("mapKeyValueAdapterFactory")));
context.close();
assertThat(adapter, is(instanceOf(MapKeyValueAdapter.class)));
assertThat(ReflectionTestUtils.getField(adapter, "store"), is(instanceOf(mapType)));
}
@Configuration
@@ -140,8 +112,7 @@ public class MapRepositoriesConfigurationExtensionIntegrationTests {
@Bean
public KeyValueTemplate mapKeyValueTemplate() {
return new KeyValueTemplate(Mockito.mock(KeyValueAdapter.class));
return new KeyValueTemplate(new MapKeyValueAdapter());
}
}
}