Revert "HHH-11845 - Warn user when multiple persistence-units use the same name"

This reverts commit 1242fd9580.
This commit is contained in:
Vlad Mihalcea 2018-01-18 15:56:47 +02:00
parent 1242fd9580
commit 2f92109c48
3 changed files with 24 additions and 144 deletions

View File

@ -68,8 +68,8 @@ public class PersistenceXmlParser {
ClassLoaderServiceImpl.fromConfigSettings( integration ),
PersistenceUnitTransactionType.RESOURCE_LOCAL
);
parser.doResolve( integration );
return new ArrayList<>( parser.persistenceUnits.values() );
return new ArrayList<>( parser.doResolve( integration ).values() );
}
/**
@ -118,11 +118,10 @@ public class PersistenceXmlParser {
transactionType
);
parser.parsePersistenceXml( persistenceXmlUrl, integration );
final Map<String,ParsedPersistenceXmlDescriptor> persistenceUnits = parser.parsePersistenceXml( persistenceXmlUrl, integration );
assert persistenceUnits.size() == 1;
assert parser.persistenceUnits.size() == 1;
return parser.persistenceUnits.values().iterator().next();
return persistenceUnits.values().iterator().next();
}
/**
@ -174,10 +173,10 @@ public class PersistenceXmlParser {
transactionType
);
parser.parsePersistenceXml( persistenceXmlUrl, integration );
assert parser.persistenceUnits.containsKey( name );
final Map<String,ParsedPersistenceXmlDescriptor> persistenceUnits = parser.parsePersistenceXml( persistenceXmlUrl, integration );
assert persistenceUnits.containsKey( name );
return parser.persistenceUnits.get( name );
return persistenceUnits.get( name );
}
/**
@ -201,52 +200,51 @@ public class PersistenceXmlParser {
*
* @return Map of persistence-unit descriptors keyed by the PU name
*/
public static Map<String, ParsedPersistenceXmlDescriptor> parse(
public static Map<String,ParsedPersistenceXmlDescriptor> parse(
URL persistenceXmlUrl,
PersistenceUnitTransactionType transactionType,
Map integration
) {
Map integration) {
PersistenceXmlParser parser = new PersistenceXmlParser(
ClassLoaderServiceImpl.fromConfigSettings( integration ),
transactionType
);
parser.doResolve( integration );
return parser.persistenceUnits;
return parser.doResolve( integration );
}
private final ClassLoaderService classLoaderService;
private final PersistenceUnitTransactionType defaultTransactionType;
private final Map<String, ParsedPersistenceXmlDescriptor> persistenceUnits;
private PersistenceXmlParser(ClassLoaderService classLoaderService, PersistenceUnitTransactionType defaultTransactionType) {
this.classLoaderService = classLoaderService;
this.defaultTransactionType = defaultTransactionType;
this.persistenceUnits = new ConcurrentHashMap<>();
}
private void doResolve(Map integration) {
private Map<String,ParsedPersistenceXmlDescriptor> doResolve(Map integration) {
final Map<String,ParsedPersistenceXmlDescriptor> persistenceUnits = new ConcurrentHashMap<>();
final List<URL> xmlUrls = classLoaderService.locateResources( "META-INF/persistence.xml" );
if ( xmlUrls.isEmpty() ) {
LOG.unableToFindPersistenceXmlInClasspath();
}
else {
parsePersistenceXml( xmlUrls, integration );
}
}
private void parsePersistenceXml(List<URL> xmlUrls, Map integration) {
for ( URL xmlUrl : xmlUrls ) {
parsePersistenceXml( xmlUrl, integration );
persistenceUnits.putAll( parsePersistenceXml( xmlUrl, integration ) );
}
}
private void parsePersistenceXml(URL xmlUrl, Map integration) {
return persistenceUnits;
}
private Map<String,ParsedPersistenceXmlDescriptor> parsePersistenceXml(URL xmlUrl, Map integration) {
LOG.tracef( "Attempting to parse persistence.xml file : %s", xmlUrl.toExternalForm() );
final Document doc = loadUrl( xmlUrl );
final Element top = doc.getDocumentElement();
final Map<String,ParsedPersistenceXmlDescriptor> persistenceUnits = new ConcurrentHashMap<>();
final NodeList children = top.getChildNodes();
for ( int i = 0; i < children.getLength() ; i++ ) {
if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) {
@ -287,6 +285,7 @@ public class PersistenceXmlParser {
}
}
}
return persistenceUnits;
}
private void decodeTransactionType(ParsedPersistenceXmlDescriptor persistenceUnit) {

View File

@ -1,95 +0,0 @@
/*
* 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.jpa.test.persistenceunit;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.testing.logger.LoggerInspectionRule;
import org.hibernate.testing.logger.Triggerable;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
import org.jboss.logging.Logger;
import static org.hibernate.internal.util.ConfigHelper.findAsResource;
import static org.junit.Assert.assertTrue;
/**
* @author Andrea Boriero
*/
@TestForIssue(jiraKey = "HHH-11845")
@RunWith(BMUnitRunner.class)
public class DuplicatePersistenceUnitNameTest extends BaseUnitTestCase {
private Triggerable triggerable;
@Rule
public LoggerInspectionRule logInspection = new LoggerInspectionRule(
Logger.getMessageLogger( CoreMessageLogger.class, PersistenceXmlParser.class.getName() )
);
@Before
public void setUp() {
final Set messagesPrefixes = new HashSet<>();
messagesPrefixes.add( "HHH015018" );
triggerable = logInspection.watchForLogMessages( messagesPrefixes );
}
@After
public void tearDown() {
triggerable.reset();
}
@Test
public void testDuplicatePersistenceUnitNameLogAWarnMessage() {
final Map<String, Object> properties = new HashMap<String, Object>();
properties.put( AvailableSettings.CLASSLOADERS, Arrays.asList( new TestClassLoader() ) );
PersistenceXmlParser.locatePersistenceUnits( properties );
assertTrue( "The warn HHH015018 has not been logged ", triggerable.wasTriggered() );
}
private static class TestClassLoader extends ClassLoader {
final List<URL> urls;
public TestClassLoader() {
urls = Arrays.asList(
findAsResource(
"org/hibernate/jpa/test/persistenceunit/META-INF/persistence.xml"
)
,
findAsResource(
"org/hibernate/jpa/test/persistenceunit/META-INF/persistenceUnitForNameDuplicationTest.xml"
)
);
}
@Override
protected Enumeration<URL> findResources(String name) throws IOException {
return name.equals( "META-INF/persistence.xml" ) ?
Collections.enumeration( urls ) :
Collections.emptyEnumeration();
}
}
}

View File

@ -1,24 +0,0 @@
<?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">
<!--
The PU name is also present in the persistence.xml located in the same folder,
this should cause a warn message to be logged.
See org.hibernate.jpa.test.persistenceunit.DuplicatePersistenceUnitNameTest
-->
<persistence-unit name="ExcludeUnlistedClassesTest1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.jpa.test.persistenceunit.DataPoint</class>
</persistence-unit>
</persistence>