DATACMNS-1498 - Improved attribute lookup on RepositoryConfigurationSource.

This commit is contained in:
Oliver Drotbohm
2019-03-14 12:48:35 +01:00
parent 0bf160eb97
commit 4e6b12fc8a
5 changed files with 102 additions and 8 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.util.Streamable;
/**
@@ -138,6 +139,34 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
assertThat(configurationSource.getRepositoryBaseClassName()).isNotPresent();
}
@Test // DATACMNS-1498
public void allowsLookupOfNonStringAttribute() {
RepositoryConfigurationSource source = getConfigSource(DefaultConfiguration.class);
assertThat(source.getAttribute("repositoryBaseClass", Class.class)).hasValue(PagingAndSortingRepository.class);
assertThat(source.getRequiredAttribute("repositoryBaseClass", Class.class))
.isEqualTo(PagingAndSortingRepository.class);
}
@Test // DATACMNS-1498
public void rejectsInvalidAttributeName() {
RepositoryConfigurationSource source = getConfigSource(DefaultConfiguration.class);
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> source.getAttribute("fooBar"));
}
@Test // DATACMNS-1498
public void lookupOfEmptyStringExposesAbsentValue() {
RepositoryConfigurationSource source = getConfigSource(DefaultConfiguration.class);
assertThat(source.getAttribute("namedQueriesLocation", String.class)).isEmpty();
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> source.getRequiredAttribute("namedQueriesLocation", String.class));
}
private AnnotationRepositoryConfigurationSource getConfigSource(Class<?> type) {
AnnotationMetadata metadata = new StandardAnnotationMetadata(type, true);

View File

@@ -16,6 +16,7 @@
package org.springframework.data.repository.config;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import org.junit.Test;
@@ -40,8 +41,8 @@ public class XmlRepositoryConfigurationSourceUnitTests {
RepositoryConfigurationSource source = mock(XmlRepositoryConfigurationSource.class);
ReflectionTestUtils.setField(source, "element", element);
when(source.getAttribute(anyString())).thenCallRealMethod();
when(source.getAttribute(anyString())).thenCallRealMethod();
when(element.getAttribute("some-xml-attribute")).thenReturn("value");
assertThat(source.getAttribute("someXmlAttribute")).hasValue("value");