DATAJPA-353 - Fixed mapping file locations detected by CPSPUPP to conform to the JPA spec.
Adjusted scanForMappingFileLocations(…) in ClasspathScanningPersistenceUnitPostProcessor to resolve the paths to class-path loadable paths. Updated test cases accordingly. Added test case and required resources to verify that mapping files with recursive wildcard pattern can be found from multiple locations in class path. Original pull request: #24.
This commit is contained in:
committed by
Oliver Gierke
parent
4ff0671105
commit
f25032abfa
@@ -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<String> 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<String> mappingFileUris = new HashSet<String>();
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0">
|
||||
</entity-mappings>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0">
|
||||
</entity-mappings>
|
||||
Reference in New Issue
Block a user