diff --git a/src/main/java/org/springframework/data/jpa/support/ClasspathScanningPersistenceUnitPostProcessor.java b/src/main/java/org/springframework/data/jpa/support/ClasspathScanningPersistenceUnitPostProcessor.java index 8239cc833..ca9f99cae 100644 --- a/src/main/java/org/springframework/data/jpa/support/ClasspathScanningPersistenceUnitPostProcessor.java +++ b/src/main/java/org/springframework/data/jpa/support/ClasspathScanningPersistenceUnitPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2013 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. @@ -45,6 +45,7 @@ import org.springframework.util.StringUtils; * XML mapping files can be scanned as well by configuring a file name pattern. * * @author Oliver Gierke + * @author Thomas Darimont */ public class ClasspathScanningPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor, ResourceLoaderAware { @@ -108,9 +109,11 @@ public class ClasspathScanningPersistenceUnitPostProcessor implements Persistenc } /** - * Scanes the configured base package for files matching the configured mapping file name pattern. Will simply return - * an empty {@link Set} in case no {@link ResourceLoader} or mapping file name pattern was configured. + * Scans the configured base package for files matching the configured mapping file name pattern. Will simply return + * an empty {@link Set} in case no {@link ResourceLoader} or mapping file name pattern was configured. Resulting paths + * are resource-loadable from the application classpath according to the JPA spec. * + * @see javax.persistence.spi.PersistenceUnitInfo.PersistenceUnitInfo#getMappingFileNames() * @return */ private Set scanForMappingFileLocations() { @@ -119,8 +122,9 @@ public class ClasspathScanningPersistenceUnitPostProcessor implements Persistenc return Collections.emptySet(); } - String path = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + basePackage.replace('.', File.separatorChar) - + File.separator + mappingFileNamePattern; + String basePackagePathComponent = basePackage.replace('.', File.separatorChar); + String path = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + basePackagePathComponent + File.separator + + mappingFileNamePattern; Set mappingFileUris = new HashSet(); Resource[] scannedResources = new Resource[0]; @@ -132,7 +136,9 @@ public class ClasspathScanningPersistenceUnitPostProcessor implements Persistenc for (Resource resource : scannedResources) { try { - mappingFileUris.add(resource.getURI().toString()); + String resourcePath = resource.getURI().getPath(); + String resourcePathInClasspath = resourcePath.substring(resourcePath.indexOf(basePackagePathComponent)); + mappingFileUris.add(resourcePathInClasspath); } catch (IOException e) { throw new IllegalStateException(String.format("Couldn't get URI for %s!", resource.toString()), e); } diff --git a/src/test/java/org/springframework/data/jpa/support/ClasspathScanningPersistenceUnitPostProcessorUnitTests.java b/src/test/java/org/springframework/data/jpa/support/ClasspathScanningPersistenceUnitPostProcessorUnitTests.java index 963e0a0f2..b1f1341a2 100644 --- a/src/test/java/org/springframework/data/jpa/support/ClasspathScanningPersistenceUnitPostProcessorUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/support/ClasspathScanningPersistenceUnitPostProcessorUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2013 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. @@ -31,12 +31,12 @@ import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor; * Unit tests for {@link ClasspathScanningPersistenceUnitPostProcessor}. * * @author Oliver Gierke + * @author Thomas Darimont */ @RunWith(MockitoJUnitRunner.class) public class ClasspathScanningPersistenceUnitPostProcessorUnitTests { - @Mock - MutablePersistenceUnitInfo pui; + @Mock MutablePersistenceUnitInfo pui; String basePackage = getClass().getPackage().getName(); @Test(expected = IllegalArgumentException.class) @@ -75,19 +75,35 @@ public class ClasspathScanningPersistenceUnitPostProcessorUnitTests { @Test public void findsMappingFile() { - DefaultResourceLoader loader = new DefaultResourceLoader(); - String expected = getClass().getResource("mapping.xml").toString(); - ClasspathScanningPersistenceUnitPostProcessor processor = new ClasspathScanningPersistenceUnitPostProcessor( basePackage); processor.setMappingFileNamePattern("*.xml"); - processor.setResourceLoader(loader); + processor.setResourceLoader(new DefaultResourceLoader()); processor.postProcessPersistenceUnitInfo(pui); + String expected = getClass().getPackage().getName().replace('.', '/') + "/mapping.xml"; + verify(pui).addManagedClassName(SampleEntity.class.getName()); verify(pui).addMappingFileName(expected); } + /** + * @see DATAJPA-353 + */ + @Test + public void shouldFindJpaMappingFilesFromMultipleLocationsOnClasspath() { + + ClasspathScanningPersistenceUnitPostProcessor processor = new ClasspathScanningPersistenceUnitPostProcessor( + basePackage); + + processor.setResourceLoader(new DefaultResourceLoader()); + processor.setMappingFileNamePattern("**/*orm.xml"); + processor.postProcessPersistenceUnitInfo(pui); + + verify(pui).addMappingFileName("org/springframework/data/jpa/support/module1/module1-orm.xml"); + verify(pui).addMappingFileName("org/springframework/data/jpa/support/module2/module2-orm.xml"); + } + @Entity public static class SampleEntity { diff --git a/src/test/resources/org/springframework/data/jpa/support/module1/module1-orm.xml b/src/test/resources/org/springframework/data/jpa/support/module1/module1-orm.xml new file mode 100644 index 000000000..3118a8873 --- /dev/null +++ b/src/test/resources/org/springframework/data/jpa/support/module1/module1-orm.xml @@ -0,0 +1,3 @@ + + + diff --git a/src/test/resources/org/springframework/data/jpa/support/module2/module2-orm.xml b/src/test/resources/org/springframework/data/jpa/support/module2/module2-orm.xml new file mode 100644 index 000000000..3118a8873 --- /dev/null +++ b/src/test/resources/org/springframework/data/jpa/support/module2/module2-orm.xml @@ -0,0 +1,3 @@ + + +