Browse Source

DATACMNS-502 - Prevent IndexOutOfBoundException in AnnotationRepositoryConfigurationSource.

If a configuration class resides in the default package and no base package is defined in the @Enable…Repositories annotation we previously failed in extracting the package from the class name (as there is none).

We now use Spring's ClassUtils that correctly handles this case and returns an empty String for the default package.
1.7.x
Oliver Gierke 12 years ago
parent
commit
e3137a1507
  1. 2
      src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java
  2. 26
      src/test/java/TypeInDefaultPackage.java
  3. 47
      src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java

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

@ -95,7 +95,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura @@ -95,7 +95,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
// Default configuration - return package of annotated class
if (value.length == 0 && basePackages.length == 0 && basePackageClasses.length == 0) {
String className = metadata.getClassName();
return Collections.singleton(className.substring(0, className.lastIndexOf('.')));
return Collections.singleton(ClassUtils.getPackageName(className));
}
Set<String> packages = new HashSet<String>();

26
src/test/java/TypeInDefaultPackage.java

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
/*
* 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.
*/
import org.springframework.data.repository.config.EnableRepositories;
/**
* Dummy type to be able to reference the default package.
*
* @author Oliver Gierke
*/
@EnableRepositories
public class TypeInDefaultPackage {
}

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

@ -52,6 +52,9 @@ public class AnnotationRepositoryConfigurationSourceUnitTests { @@ -52,6 +52,9 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
environment);
}
/**
* @see DATACMNS-47
*/
@Test
public void findsBasePackagesForClasses() {
@ -59,6 +62,9 @@ public class AnnotationRepositoryConfigurationSourceUnitTests { @@ -59,6 +62,9 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
assertThat(basePackages, hasItem(AnnotationRepositoryConfigurationSourceUnitTests.class.getPackage().getName()));
}
/**
* @see DATACMNS-47
*/
@Test
public void evaluatesExcludeFiltersCorrectly() {
@ -69,26 +75,28 @@ public class AnnotationRepositoryConfigurationSourceUnitTests { @@ -69,26 +75,28 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
assertThat(candidate.getBeanClassName(), is(MyRepository.class.getName()));
}
/**
* @see DATACMNS-47
*/
@Test
public void defaultsToPackageOfAnnotatedClass() {
AnnotationMetadata metadata = new StandardAnnotationMetadata(DefaultConfiguration.class);
AnnotationRepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(metadata,
EnableRepositories.class, resourceLoader, environment);
AnnotationRepositoryConfigurationSource source = getConfigSource(DefaultConfiguration.class);
Iterable<String> packages = source.getBasePackages();
assertThat(packages, hasItem(DefaultConfiguration.class.getPackage().getName()));
assertThat(source.shouldConsiderNestedRepositories(), is(false));
}
/**
* @see DATACMNS-47
*/
@Test
public void returnsConfiguredBasePackage() {
AnnotationMetadata metadata = new StandardAnnotationMetadata(DefaultConfigurationWithBasePackage.class);
AnnotationRepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(metadata,
EnableRepositories.class, resourceLoader, environment);
AnnotationRepositoryConfigurationSource source = getConfigSource(DefaultConfigurationWithBasePackage.class);
Iterable<String> packages = source.getBasePackages();
assertThat(packages, hasItem("foo"));
}
@ -98,11 +106,28 @@ public class AnnotationRepositoryConfigurationSourceUnitTests { @@ -98,11 +106,28 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
@Test
public void returnsConsiderNestedRepositories() {
AnnotationMetadata metadata = new StandardAnnotationMetadata(DefaultConfigurationWithNestedRepositories.class);
AnnotationRepositoryConfigurationSource source = new AnnotationRepositoryConfigurationSource(metadata,
AnnotationRepositoryConfigurationSource source = getConfigSource(DefaultConfigurationWithNestedRepositories.class);
assertThat(source.shouldConsiderNestedRepositories(), is(true));
}
/**
* @see DATACMNS-502
*/
@Test
public void returnsEmptyStringForBasePackage() throws Exception {
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(getClass().getClassLoader().loadClass(
"TypeInDefaultPackage"));
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
EnableRepositories.class, resourceLoader, environment);
assertThat(source.shouldConsiderNestedRepositories(), is(true));
assertThat(configurationSource.getBasePackages(), hasItem(""));
}
private AnnotationRepositoryConfigurationSource getConfigSource(Class<?> type) {
AnnotationMetadata metadata = new StandardAnnotationMetadata(type);
return new AnnotationRepositoryConfigurationSource(metadata, EnableRepositories.class, resourceLoader, environment);
}
public static class Person {}

Loading…
Cancel
Save