Browse Source

DATACMNS-1498 - Improved attribute lookup on RepositoryConfigurationSource.

pull/366/head
Oliver Drotbohm 7 years ago
parent
commit
4e6b12fc8a
No known key found for this signature in database
GPG Key ID: 6E42B5787543F690
  1. 32
      src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java
  2. 30
      src/main/java/org/springframework/data/repository/config/RepositoryConfigurationSource.java
  3. 16
      src/main/java/org/springframework/data/repository/config/XmlRepositoryConfigurationSource.java
  4. 29
      src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java
  5. 3
      src/test/java/org/springframework/data/repository/config/XmlRepositoryConfigurationSourceUnitTests.java

32
src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java

@ -328,9 +328,35 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura @@ -328,9 +328,35 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
*/
@Override
public Optional<String> getAttribute(String name) {
return getAttribute(name, String.class);
}
String attribute = attributes.getString(name);
return StringUtils.hasText(attribute) ? Optional.of(attribute) : Optional.empty();
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#getAttribute(java.lang.String, java.lang.Class)
*/
@Override
public <T> Optional<T> getAttribute(String name, Class<T> type) {
if (!attributes.containsKey(name)) {
throw new IllegalArgumentException(String.format("No attribute named %s found!", name));
}
Object value = attributes.get(name);
if (value == null) {
return Optional.empty();
}
Assert.isInstanceOf(type, value,
() -> String.format("Attribute value for %s is of type %s but was expected to be of type %s!", name,
value.getClass(), type));
Object result = String.class.isInstance(value) //
? StringUtils.hasText((String) value) ? value : null //
: value;
return Optional.ofNullable(type.cast(result));
}
/*
@ -342,7 +368,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura @@ -342,7 +368,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
return hasExplicitFilters;
}
/*
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#getBootstrapMode()
*/

30
src/main/java/org/springframework/data/repository/config/RepositoryConfigurationSource.java

@ -24,6 +24,7 @@ import org.springframework.core.type.filter.TypeFilter; @@ -24,6 +24,7 @@ import org.springframework.core.type.filter.TypeFilter;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.util.Streamable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Interface containing the configurable options for the Spring Data repository subsystem.
@ -103,6 +104,31 @@ public interface RepositoryConfigurationSource { @@ -103,6 +104,31 @@ public interface RepositoryConfigurationSource {
*/
Optional<String> getAttribute(String name);
/**
* Returns the value for the attribute with the given name and type. The name is expected to be handed in camel-case.
*
* @param name must not be {@literal null} or empty.
* @param type the type of the attribute to look up.
* @return the attribute with the given name or {@link Optional#empty()} if not configured or empty.
* @since 2.2
*/
<T> Optional<T> getAttribute(String name, Class<T> type);
/**
* Returns the attribute value for the attribute of the given name.
*
* @param name must not be {@literal null} or empty.
* @return the attribute with the given name and type.
* @since 2.2
*/
default <T> T getRequiredAttribute(String name, Class<T> type) {
Assert.hasText(name, "Attribute name must not be null or empty!");
return getAttribute(name, type)
.orElseThrow(() -> new IllegalArgumentException(String.format("No attribute named %s found!", name)));
}
/**
* Returns whether the configuration uses explicit filtering to scan for repository types.
*
@ -131,7 +157,7 @@ public interface RepositoryConfigurationSource { @@ -131,7 +157,7 @@ public interface RepositoryConfigurationSource {
/**
* Returns the {@link ImplementationDetectionConfiguration} to be used to scan for custom implementations of the
* repository instances to be created from this {@link RepositoryConfigurationSource}.
*
*
* @param factory
* @return will never be {@literal null}.
* @since 2.1
@ -140,7 +166,7 @@ public interface RepositoryConfigurationSource { @@ -140,7 +166,7 @@ public interface RepositoryConfigurationSource {
/**
* Defines the repository {@link BootstrapMode} to be used.
*
*
* @return
* @since 2.1
*/

16
src/main/java/org/springframework/data/repository/config/XmlRepositoryConfigurationSource.java

@ -188,7 +188,6 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou @@ -188,7 +188,6 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
*/
@Override
public boolean shouldConsiderNestedRepositories() {
return getNullDefaultedAttribute(element, CONSIDER_NESTED_REPOSITORIES).map(Boolean::parseBoolean).orElse(false);
}
@ -205,6 +204,19 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou @@ -205,6 +204,19 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
return StringUtils.hasText(attribute) ? Optional.of(attribute) : Optional.empty();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#getAttribute(java.lang.String, java.lang.Class)
*/
@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> getAttribute(String name, Class<T> type) {
Assert.isAssignable(String.class, type, "Only String attribute lookups are allowed for XML namespaces!");
return (Optional<T>) getAttribute(name);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#usesExplicitFilters()
@ -214,7 +226,7 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou @@ -214,7 +226,7 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
return !(this.includeFilters.isEmpty() && this.excludeFilters.isEmpty());
}
/*
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#getBootstrapMode()
*/

29
src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java

@ -33,6 +33,7 @@ import org.springframework.core.io.DefaultResourceLoader; @@ -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 { @@ -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);

3
src/test/java/org/springframework/data/repository/config/XmlRepositoryConfigurationSourceUnitTests.java

@ -16,6 +16,7 @@ @@ -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 { @@ -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");

Loading…
Cancel
Save