From 28f7630acc5811f44336cabe3fd2440f432098c6 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 8 May 2014 12:11:21 +0200 Subject: [PATCH] DATACMNS-502 - Prevent IndexOutOfBoundException in AnnotationRepositoryConfigurationSource. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- ...notationRepositoryConfigurationSource.java | 2 +- src/test/java/TypeInDefaultPackage.java | 26 +++++++++++++++++++ ...epositoryConfigurationSourceUnitTests.java | 14 ++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/test/java/TypeInDefaultPackage.java diff --git a/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java b/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java index d1d8b2e83..411a9dbbf 100644 --- a/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java +++ b/src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java @@ -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 packages = new HashSet(); diff --git a/src/test/java/TypeInDefaultPackage.java b/src/test/java/TypeInDefaultPackage.java new file mode 100644 index 000000000..46e7d8c4a --- /dev/null +++ b/src/test/java/TypeInDefaultPackage.java @@ -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 { + +} diff --git a/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java b/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java index e10fead16..c4d2a3508 100644 --- a/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java +++ b/src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java @@ -120,6 +120,20 @@ public class AnnotationRepositoryConfigurationSourceUnitTests { assertThat(source.getAttribute("namedQueriesLocation"), is("bar")); } + /** + * @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(configurationSource.getBasePackages(), hasItem("")); + } + private AnnotationRepositoryConfigurationSource getConfigSource(Class type) { AnnotationMetadata metadata = new StandardAnnotationMetadata(type);