HHH-18231 Move PersistenceXmlParser to SPI

This commit is contained in:
Yoann Rodière 2024-06-05 15:39:02 +02:00 committed by Steve Ebersole
parent 93ea757382
commit 6883aa2ff2
12 changed files with 279 additions and 90 deletions

View File

@ -18,9 +18,8 @@ import jakarta.persistence.spi.PersistenceUnitInfo;
import jakarta.persistence.spi.ProviderUtil; import jakarta.persistence.spi.ProviderUtil;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor;
import org.hibernate.jpa.boot.spi.PersistenceConfigurationDescriptor; import org.hibernate.jpa.boot.spi.PersistenceConfigurationDescriptor;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser; import org.hibernate.jpa.boot.spi.PersistenceXmlParser;
import org.hibernate.jpa.boot.spi.Bootstrap; import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor; import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
@ -76,7 +75,7 @@ public class HibernatePersistenceProvider implements PersistenceProvider {
log.tracef( "Attempting to obtain correct EntityManagerFactoryBuilder for persistenceUnitName : %s", persistenceUnitName ); log.tracef( "Attempting to obtain correct EntityManagerFactoryBuilder for persistenceUnitName : %s", persistenceUnitName );
final Map<?,?> integration = wrap( properties ); final Map<?,?> integration = wrap( properties );
final List<ParsedPersistenceXmlDescriptor> units; final List<PersistenceUnitDescriptor> units;
try { try {
units = PersistenceXmlParser.create( integration, providedClassLoader, providedClassLoaderService ) units = PersistenceXmlParser.create( integration, providedClassLoader, providedClassLoaderService )
.locatePersistenceUnits(); .locatePersistenceUnits();
@ -93,7 +92,7 @@ public class HibernatePersistenceProvider implements PersistenceProvider {
throw new PersistenceException( "No name provided and multiple persistence units found" ); throw new PersistenceException( "No name provided and multiple persistence units found" );
} }
for ( ParsedPersistenceXmlDescriptor persistenceUnit : units ) { for ( PersistenceUnitDescriptor persistenceUnit : units ) {
if ( log.isDebugEnabled() ) { if ( log.isDebugEnabled() ) {
log.debugf( log.debugf(
"Checking persistence-unit [name=%s, explicit-provider=%s] against incoming persistence unit name [%s]", "Checking persistence-unit [name=%s, explicit-provider=%s] against incoming persistence unit name [%s]",

View File

@ -14,7 +14,6 @@ import org.hibernate.Internal;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor; import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser;
import jakarta.persistence.spi.PersistenceUnitInfo; import jakarta.persistence.spi.PersistenceUnitInfo;
import jakarta.persistence.spi.PersistenceUnitTransactionType; import jakarta.persistence.spi.PersistenceUnitTransactionType;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.jpa.boot.internal; package org.hibernate.jpa.boot.spi;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -33,6 +33,7 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.EntityManagerMessageLogger; import org.hibernate.internal.EntityManagerMessageLogger;
import org.hibernate.internal.log.DeprecationLogger; import org.hibernate.internal.log.DeprecationLogger;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor;
import org.hibernate.jpa.internal.util.ConfigurationHelper; import org.hibernate.jpa.internal.util.ConfigurationHelper;
import jakarta.persistence.PersistenceException; import jakarta.persistence.PersistenceException;
@ -45,7 +46,7 @@ import static org.hibernate.internal.HEMLogging.messageLogger;
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class PersistenceXmlParser { public final class PersistenceXmlParser {
private static final EntityManagerMessageLogger LOG = messageLogger( PersistenceXmlParser.class ); private static final EntityManagerMessageLogger LOG = messageLogger( PersistenceXmlParser.class );
@ -82,7 +83,7 @@ public class PersistenceXmlParser {
private final Map<?, ?> integration; private final Map<?, ?> integration;
private final ClassLoaderService classLoaderService; private final ClassLoaderService classLoaderService;
protected PersistenceXmlParser(Map<?, ?> integration, ClassLoader providedClassLoader, private PersistenceXmlParser(Map<?, ?> integration, ClassLoader providedClassLoader,
ClassLoaderService providedClassLoaderService) { ClassLoaderService providedClassLoaderService) {
this.integration = integration; this.integration = integration;
if ( providedClassLoaderService != null ) { if ( providedClassLoaderService != null ) {
@ -108,14 +109,22 @@ public class PersistenceXmlParser {
} }
} }
/**
* @return The {@link ClassLoaderService} used by this parser.
* Useful to retrieve URLs of persistence.xml files.
*/
public ClassLoaderService getClassLoaderService() {
return classLoaderService;
}
/** /**
* Find all persistence-units from all accessible {@code META-INF/persistence.xml} resources * Find all persistence-units from all accessible {@code META-INF/persistence.xml} resources
* *
* @return List of descriptors for all discovered persistence-units. * @return List of descriptors for all discovered persistence-units.
*/ */
@SuppressWarnings("removal") @SuppressWarnings("removal")
public List<ParsedPersistenceXmlDescriptor> locatePersistenceUnits() { public List<PersistenceUnitDescriptor> locatePersistenceUnits() {
Map<String, ParsedPersistenceXmlDescriptor> persistenceUnits = new HashMap<>(); Map<String, PersistenceUnitDescriptor> persistenceUnits = new HashMap<>();
final List<URL> xmlUrls = classLoaderService.locateResources( "META-INF/persistence.xml" ); final List<URL> xmlUrls = classLoaderService.locateResources( "META-INF/persistence.xml" );
if ( xmlUrls.isEmpty() ) { if ( xmlUrls.isEmpty() ) {
LOG.unableToFindPersistenceXmlInClasspath(); LOG.unableToFindPersistenceXmlInClasspath();
@ -127,70 +136,18 @@ public class PersistenceXmlParser {
} }
/** /**
* Parse a specific {@code persistence.xml} with the assumption that it defines a single * Generic method to parse a specified {@code persistence.xml} and return a Map of descriptors
* persistence-unit. * for all discovered persistence-units keyed by the PU name.
* *
* @param persistenceXmlUrl The {@code persistence.xml} URL * @param persistenceXmlUrl The URL of the {@code persistence.xml} to parse
* *
* @return The single persistence-unit descriptor * @return Map of persistence-unit descriptors keyed by the PU name
*/ */
@SuppressWarnings("removal") @SuppressWarnings("removal")
public ParsedPersistenceXmlDescriptor locateIndividualPersistenceUnit(URL persistenceXmlUrl) { public Map<String, PersistenceUnitDescriptor> parse(URL persistenceXmlUrl) {
return locateIndividualPersistenceUnit( persistenceXmlUrl, PersistenceUnitTransactionType.RESOURCE_LOCAL ); Map<String, PersistenceUnitDescriptor> persistenceUnits = new HashMap<>();
} parsePersistenceXml( persistenceUnits, persistenceXmlUrl, PersistenceUnitTransactionType.RESOURCE_LOCAL );
return persistenceUnits;
/**
* Parse a specific {@code persistence.xml} with the assumption that it defines a single
* persistence-unit.
*
* @param persistenceXmlUrl The {@code persistence.xml} URL
* @param transactionType The specific PersistenceUnitTransactionType to incorporate into the persistence-unit descriptor
*
* @return The single persistence-unit descriptor
*/
public ParsedPersistenceXmlDescriptor locateIndividualPersistenceUnit(
URL persistenceXmlUrl,
@SuppressWarnings("removal")
PersistenceUnitTransactionType transactionType) {
Map<String, ParsedPersistenceXmlDescriptor> persistenceUnits = new HashMap<>();
parsePersistenceXml( persistenceUnits, persistenceXmlUrl, transactionType );
assert persistenceUnits.size() == 1;
return persistenceUnits.values().iterator().next();
}
/**
* Parse a specific {@code persistence.xml} and return the descriptor for the persistence-unit with matching name
*
* @param persistenceXmlUrl The {@code persistence.xml} URL
* @param name The PU name to match
*
* @return The matching persistence-unit descriptor
*/
@SuppressWarnings("removal")
public ParsedPersistenceXmlDescriptor locateNamedPersistenceUnit(URL persistenceXmlUrl, String name) {
return locateNamedPersistenceUnit( persistenceXmlUrl, name, PersistenceUnitTransactionType.RESOURCE_LOCAL );
}
/**
* Parse a specific {@code persistence.xml} and return the descriptor for the persistence-unit with matching name
*
* @param persistenceXmlUrl The {@code persistence.xml} URL
* @param name The PU name to match
* @param transactionType The specific PersistenceUnitTransactionType to incorporate into the persistence-unit descriptor
*
* @return The matching persistence-unit descriptor
*/
public ParsedPersistenceXmlDescriptor locateNamedPersistenceUnit(
URL persistenceXmlUrl,
String name,
@SuppressWarnings("removal")
PersistenceUnitTransactionType transactionType) {
assert StringHelper.isNotEmpty( name );
Map<String, ParsedPersistenceXmlDescriptor> persistenceUnits = new HashMap<>();
parsePersistenceXml( persistenceUnits, persistenceXmlUrl, transactionType );
assert persistenceUnits.containsKey( name );
return persistenceUnits.get( name );
} }
/** /**
@ -202,16 +159,16 @@ public class PersistenceXmlParser {
* *
* @return Map of persistence-unit descriptors keyed by the PU name * @return Map of persistence-unit descriptors keyed by the PU name
*/ */
public Map<String, ParsedPersistenceXmlDescriptor> parse( public Map<String, PersistenceUnitDescriptor> parse(
URL persistenceXmlUrl, URL persistenceXmlUrl,
@SuppressWarnings("removal") @SuppressWarnings("removal")
PersistenceUnitTransactionType transactionType) { PersistenceUnitTransactionType transactionType) {
Map<String, ParsedPersistenceXmlDescriptor> persistenceUnits = new HashMap<>(); Map<String, PersistenceUnitDescriptor> persistenceUnits = new HashMap<>();
parsePersistenceXml( persistenceUnits, persistenceXmlUrl, transactionType ); parsePersistenceXml( persistenceUnits, persistenceXmlUrl, transactionType );
return persistenceUnits; return persistenceUnits;
} }
private void parsePersistenceXml(Map<String, ParsedPersistenceXmlDescriptor> persistenceUnits, private void parsePersistenceXml(Map<String, PersistenceUnitDescriptor> persistenceUnits,
List<URL> xmlUrls, List<URL> xmlUrls,
@SuppressWarnings("removal") @SuppressWarnings("removal")
PersistenceUnitTransactionType defaultTransactionType) { PersistenceUnitTransactionType defaultTransactionType) {
@ -221,7 +178,7 @@ public class PersistenceXmlParser {
} }
@SuppressWarnings("removal") @SuppressWarnings("removal")
protected void parsePersistenceXml(Map<String, ParsedPersistenceXmlDescriptor> persistenceUnits, protected void parsePersistenceXml(Map<String, PersistenceUnitDescriptor> persistenceUnits,
URL xmlUrl, PersistenceUnitTransactionType defaultTransactionType) { URL xmlUrl, PersistenceUnitTransactionType defaultTransactionType) {
if ( LOG.isTraceEnabled() ) { if ( LOG.isTraceEnabled() ) {
LOG.tracef( "Attempting to parse persistence.xml file : %s", xmlUrl.toExternalForm() ); LOG.tracef( "Attempting to parse persistence.xml file : %s", xmlUrl.toExternalForm() );

View File

@ -13,10 +13,9 @@ import java.util.Map;
import org.hibernate.boot.archive.scan.internal.DisabledScanner; import org.hibernate.boot.archive.scan.internal.DisabledScanner;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.JdbcSettings; import org.hibernate.cfg.JdbcSettings;
import org.hibernate.cfg.MappingSettings;
import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor; import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser; import org.hibernate.jpa.boot.spi.PersistenceXmlParser;
import org.hibernate.jpa.boot.spi.Bootstrap; import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
@ -53,7 +52,9 @@ public class PersistenceUnitNameTests {
private static EntityManagerFactory loadFactory(String name, ServiceRegistryScope scope) { private static EntityManagerFactory loadFactory(String name, ServiceRegistryScope scope) {
final URL puFile = PersistenceUnitNameTests.class.getClassLoader().getResource( "xml/jakarta/simple/2units.xml" ); final URL puFile = PersistenceUnitNameTests.class.getClassLoader().getResource( "xml/jakarta/simple/2units.xml" );
final ParsedPersistenceXmlDescriptor descriptor = PersistenceXmlParser.locateNamedPersistenceUnit( puFile, name ); var descriptors = PersistenceXmlParser.create().parse( puFile );
assertThat( descriptors ).containsKey( name );
final PersistenceUnitDescriptor descriptor = descriptors.get( name );
final EntityManagerFactoryBuilder emfBuilder = Bootstrap.getEntityManagerFactoryBuilder( final EntityManagerFactoryBuilder emfBuilder = Bootstrap.getEntityManagerFactoryBuilder(
descriptor, descriptor,
buildSettings( scope ) buildSettings( scope )

View File

@ -9,7 +9,7 @@ package org.hibernate.orm.test.jpa.jakarta;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.Collections; import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
@ -23,8 +23,8 @@ import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitDefaultsImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitMetadataImpl; import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitMetadataImpl;
import org.hibernate.boot.jaxb.spi.Binding; import org.hibernate.boot.jaxb.spi.Binding;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor; import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser; import org.hibernate.jpa.boot.spi.PersistenceXmlParser;
import org.hibernate.testing.orm.junit.ServiceRegistry; import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.ServiceRegistryScope; import org.hibernate.testing.orm.junit.ServiceRegistryScope;
@ -71,9 +71,12 @@ public class JakartaXmlSmokeTests {
public void testLoadingPersistenceXml(ServiceRegistryScope scope) { public void testLoadingPersistenceXml(ServiceRegistryScope scope) {
final ClassLoaderService cls = scope.getRegistry().getService( ClassLoaderService.class ); final ClassLoaderService cls = scope.getRegistry().getService( ClassLoaderService.class );
final URL url = cls.locateResource( "xml/jakarta/simple/persistence.xml" ); final URL url = cls.locateResource( "xml/jakarta/simple/persistence.xml" );
final ParsedPersistenceXmlDescriptor descriptor = PersistenceXmlParser.create() final Map<String, PersistenceUnitDescriptor> descriptors = PersistenceXmlParser.create()
.locateIndividualPersistenceUnit( url ); .parse( url );
assertThat( descriptor.getName() ).isEqualTo( "defaultpar" ); String expectedPuName = "defaultpar";
assertThat( descriptors ).containsOnlyKeys( expectedPuName );
var descriptor = descriptors.get( expectedPuName );
assertThat( descriptor.getName() ).isEqualTo( expectedPuName );
assertThat( descriptor.getManagedClassNames() ).contains( "org.hibernate.jpa.test.pack.defaultpar.Lighter" ); assertThat( descriptor.getManagedClassNames() ).contains( "org.hibernate.jpa.test.pack.defaultpar.Lighter" );
} }
} }

View File

@ -19,7 +19,7 @@ import java.util.Set;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser; import org.hibernate.jpa.boot.spi.PersistenceXmlParser;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;

View File

@ -19,8 +19,8 @@ import java.util.Map;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.util.ConfigHelper; import org.hibernate.internal.util.ConfigHelper;
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor; import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser; import org.hibernate.jpa.boot.spi.PersistenceXmlParser;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test; import org.junit.Test;
@ -51,7 +51,7 @@ public class ExcludeUnlistedClassesTest extends BaseUnitTestCase {
final Map<String, Object> properties = new HashMap<String, Object>(); final Map<String, Object> properties = new HashMap<String, Object>();
properties.put( AvailableSettings.CLASSLOADERS, Arrays.asList( new TestClassLoader() ) ); properties.put( AvailableSettings.CLASSLOADERS, Arrays.asList( new TestClassLoader() ) );
final List<ParsedPersistenceXmlDescriptor> parsedDescriptors = PersistenceXmlParser.create( properties ) final List<PersistenceUnitDescriptor> parsedDescriptors = PersistenceXmlParser.create( properties )
.locatePersistenceUnits(); .locatePersistenceUnits();
doTest( parsedDescriptors, "ExcludeUnlistedClassesTest1", false ); doTest( parsedDescriptors, "ExcludeUnlistedClassesTest1", false );
@ -60,9 +60,9 @@ public class ExcludeUnlistedClassesTest extends BaseUnitTestCase {
doTest( parsedDescriptors, "ExcludeUnlistedClassesTest4", true ); doTest( parsedDescriptors, "ExcludeUnlistedClassesTest4", true );
} }
private void doTest(List<ParsedPersistenceXmlDescriptor> parsedDescriptors, private void doTest(List<PersistenceUnitDescriptor> parsedDescriptors,
final String persistenceUnitName, final boolean shouldExclude) { final String persistenceUnitName, final boolean shouldExclude) {
for (final ParsedPersistenceXmlDescriptor descriptor : parsedDescriptors) { for (final PersistenceUnitDescriptor descriptor : parsedDescriptors) {
if (descriptor.getName().equals( persistenceUnitName )) { if (descriptor.getName().equals( persistenceUnitName )) {
assertEquals(descriptor.isExcludeUnlistedClasses(), shouldExclude); assertEquals(descriptor.isExcludeUnlistedClasses(), shouldExclude);
return; return;

View File

@ -0,0 +1,146 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.jpa.persistenceunit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.internal.util.ConfigHelper.findAsResource;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
import org.hibernate.jpa.boot.spi.PersistenceXmlParser;
import org.hibernate.testing.logger.LoggerInspectionRule;
import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.jboss.logging.Logger;
import jakarta.persistence.spi.PersistenceUnitTransactionType;
@SuppressWarnings("removal")
@JiraKey("HHH-18231")
public class PersistenceXmlParserTest {
@Rule
public LoggerInspectionRule logInspection = new LoggerInspectionRule(
Logger.getMessageLogger( CoreMessageLogger.class, PersistenceXmlParser.class.getName() )
);
@Test
public void create_classLoaders() {
var parser = PersistenceXmlParser.create(
Map.of( AvailableSettings.CLASSLOADERS, Arrays.asList( new TestClassLoader( "pu1" ) ) ),
new TestClassLoader( "pu2" ),
null
);
assertThat( parser.getClassLoaderService() ).isNotNull();
assertThat( parser.locatePersistenceUnits() )
.extracting( PersistenceUnitDescriptor::getName )
.containsExactlyInAnyOrder( "pu1", "pu2" );
}
@Test
public void create_classLoaderService() {
var myClassLoaderService = new ClassLoaderServiceImpl( new TestClassLoader( "pu3" ) );
var parser = PersistenceXmlParser.create(
// Should be ignored
Map.of( AvailableSettings.CLASSLOADERS, Arrays.asList( new TestClassLoader( "pu1" ) ) ),
// Should be ignored
new TestClassLoader( "pu2" ),
myClassLoaderService
);
assertThat( parser.getClassLoaderService() ).isSameAs( myClassLoaderService );
assertThat( parser.locatePersistenceUnits() )
.extracting( PersistenceUnitDescriptor::getName )
.containsExactlyInAnyOrder( "pu3" );
}
@Test
public void locatePersistenceUnits() {
var parser = PersistenceXmlParser.create(
Map.of(),
new TestClassLoader( "pu1" ),
null
);
assertThat( parser.locatePersistenceUnits() )
.singleElement()
.returns( "pu1", PersistenceUnitDescriptor::getName );
}
@Test
public void locatePersistenceUnits_empty() {
var noFileLog = logInspection.watchForLogMessages( "HHH000318" );
var parser = PersistenceXmlParser.create();
assertThat( parser.locatePersistenceUnits() ).isEmpty();
assertThat( noFileLog.wasTriggered() ).isTrue();
}
@Test
public void parse() {
var parser = PersistenceXmlParser.create();
var result = parser.parse( findPuResource( "multipu" ) );
assertThat( result )
.containsOnlyKeys( "multipu1", "multipu2", "multipu3" );
assertThat( result.get( "multipu1" ) )
.returns( "multipu1", PersistenceUnitDescriptor::getName )
.returns(
PersistenceUnitTransactionType.RESOURCE_LOCAL, PersistenceUnitDescriptor::getTransactionType );
assertThat( result.get( "multipu2" ) )
.returns( "multipu2", PersistenceUnitDescriptor::getName )
.returns(
PersistenceUnitTransactionType.RESOURCE_LOCAL, PersistenceUnitDescriptor::getTransactionType );
assertThat( result.get( "multipu3" ) )
.returns( "multipu3", PersistenceUnitDescriptor::getName )
.returns( PersistenceUnitTransactionType.JTA, PersistenceUnitDescriptor::getTransactionType );
}
@Test
public void parse_defaultTransactionType() {
var parser = PersistenceXmlParser.create();
var result = parser.parse( findPuResource( "multipu" ), PersistenceUnitTransactionType.JTA );
assertThat( result.get( "multipu1" ) )
.returns( "multipu1", PersistenceUnitDescriptor::getName )
.returns(
PersistenceUnitTransactionType.JTA, PersistenceUnitDescriptor::getTransactionType );
assertThat( result.get( "multipu2" ) )
.returns( "multipu2", PersistenceUnitDescriptor::getName )
.returns( PersistenceUnitTransactionType.RESOURCE_LOCAL, PersistenceUnitDescriptor::getTransactionType );
assertThat( result.get( "multipu3" ) )
.returns( "multipu3", PersistenceUnitDescriptor::getName )
.returns( PersistenceUnitTransactionType.JTA, PersistenceUnitDescriptor::getTransactionType );
}
private static URL findPuResource(String resourceName) {
return findAsResource( "org/hibernate/jpa/test/persistenceunit/META-INF/" + resourceName + ".xml" );
}
private static class TestClassLoader extends ClassLoader {
private final URL url;
public TestClassLoader(String resourceName) {
url = findPuResource( resourceName );
}
@Override
protected Enumeration<URL> findResources(String name) throws IOException {
return name.equals( "META-INF/persistence.xml" ) ?
Collections.enumeration( List.of( url ) ) :
Collections.emptyEnumeration();
}
}
}

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="multipu1">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.orm.test.jpa.persistenceunit.DataPoint</class>
</persistence-unit>
<persistence-unit name="multipu2" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.orm.test.jpa.persistenceunit.DataPoint</class>
</persistence-unit>
<persistence-unit name="multipu3" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.orm.test.jpa.persistenceunit.DataPoint</class>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.orm.test.jpa.persistenceunit.DataPoint</class>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="pu2" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.orm.test.jpa.persistenceunit.DataPoint</class>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="pu3" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.orm.test.jpa.persistenceunit.DataPoint</class>
</persistence-unit>
</persistence>