Skip to content

Commit eef090d

Browse files
committed
HHH-8364 corrected test
1 parent 34965da commit eef090d

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/ExcludeUnlistedClassesTest.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,39 @@
2020
*/
2121
package org.hibernate.jpa.test.persistenceunit;
2222

23-
import static org.junit.Assert.assertNull;
24-
import static org.junit.Assert.assertNotNull;
23+
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.fail;
2525

2626
import java.io.IOException;
2727
import java.net.URL;
2828
import java.util.Enumeration;
2929
import java.util.HashMap;
30+
import java.util.List;
3031
import java.util.Map;
3132

32-
import javax.persistence.EntityManagerFactory;
33-
3433
import org.hibernate.cfg.AvailableSettings;
3534
import org.hibernate.internal.util.ConfigHelper;
36-
import org.hibernate.jpa.HibernatePersistenceProvider;
35+
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor;
36+
import org.hibernate.jpa.boot.internal.PersistenceXmlParser;
3737
import org.hibernate.testing.TestForIssue;
3838
import org.hibernate.testing.junit4.BaseUnitTestCase;
3939
import org.junit.Test;
4040

4141
/**
42+
* HHH-8364 discusses the use of <exclude-unlisted-classes> within Java SE environments. It was intended for Java EE
43+
* only, but was probably supported in Java SE/Hibernate for user friendliness. If we are going to supports its use
44+
* like that, the following should happen:
45+
*
46+
* Omitted == do scan
47+
* <exclude-unlisted-classes /> == don't scan
48+
* <exclude-unlisted-classes>false</exclude-unlisted-classes> == do scan
49+
* <exclude-unlisted-classes>true</exclude-unlisted-classes> == don't scan
50+
*
51+
* This is true for both JPA 1 & 2. The "false" default in the JPA 1.0 XSD was a bug.
52+
*
53+
* Note that we're ignoring the XSD "true" default if the element is omitted. Due to the negation semantics, I thought
54+
* it made more sense from a user standpoint.
55+
*
4256
* @author Brett Meyer
4357
*/
4458
@TestForIssue(jiraKey = "HHH-8364")
@@ -47,32 +61,31 @@ public class ExcludeUnlistedClassesTest extends BaseUnitTestCase {
4761
@Test
4862
public void testExcludeUnlistedClasses() {
4963
// see src/test/resources/org/hibernate/jpa/test/persistenceunit/persistence.xml
50-
doTest( "ExcludeUnlistedClassesTest1", true );
51-
doTest( "ExcludeUnlistedClassesTest2", false );
52-
doTest( "ExcludeUnlistedClassesTest3", true );
53-
doTest( "ExcludeUnlistedClassesTest4", false );
64+
65+
final Map<String, Object> properties = new HashMap<String, Object>();
66+
properties.put( AvailableSettings.RESOURCES_CLASSLOADER, new TestClassLoader() );
67+
final List<ParsedPersistenceXmlDescriptor> parsedDescriptors = PersistenceXmlParser.locatePersistenceUnits(
68+
properties );
69+
70+
doTest( parsedDescriptors, "ExcludeUnlistedClassesTest1", false );
71+
doTest( parsedDescriptors, "ExcludeUnlistedClassesTest2", true );
72+
doTest( parsedDescriptors, "ExcludeUnlistedClassesTest3", false );
73+
doTest( parsedDescriptors, "ExcludeUnlistedClassesTest4", true );
5474
}
5575

56-
private void doTest(String persistenceUnitName, boolean shouldScan) {
57-
final Map<String, Object> properties = new HashMap<String, Object>();
58-
properties.put( AvailableSettings.APP_CLASSLOADER, new TestClassLoader() );
59-
final HibernatePersistenceProvider provider = new HibernatePersistenceProvider();
60-
final EntityManagerFactory emf = provider.createEntityManagerFactory( persistenceUnitName, properties );
61-
assertNotNull( emf.getMetamodel().entity( DataPoint.class ) );
62-
if (shouldScan) {
63-
assertNull( emf.getMetamodel().entity( UnlistedDataPoint.class ) );
64-
}
65-
else {
66-
assertNotNull( emf.getMetamodel().entity( UnlistedDataPoint.class ) );
76+
private void doTest(List<ParsedPersistenceXmlDescriptor> parsedDescriptors,
77+
final String persistenceUnitName, final boolean shouldExclude) {
78+
for (final ParsedPersistenceXmlDescriptor descriptor : parsedDescriptors) {
79+
if (descriptor.getName().equals( persistenceUnitName )) {
80+
assertEquals(descriptor.isExcludeUnlistedClasses(), shouldExclude);
81+
return;
82+
}
6783
}
84+
fail("Could not find the persistence unit: " + persistenceUnitName);
6885
}
6986

7087
private static class TestClassLoader extends ClassLoader {
7188

72-
/**
73-
* testStoppableClassLoaderService() needs a custom JDK service implementation. Rather than using a real one
74-
* on the test classpath, force it in here.
75-
*/
7689
@Override
7790
protected Enumeration<URL> findResources(String name) throws IOException {
7891
if (name.equals( "META-INF/persistence.xml" )) {

0 commit comments

Comments
 (0)