20
20
*/
21
21
package org .hibernate .jpa .test .persistenceunit ;
22
22
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 ;
25
25
26
26
import java .io .IOException ;
27
27
import java .net .URL ;
28
28
import java .util .Enumeration ;
29
29
import java .util .HashMap ;
30
+ import java .util .List ;
30
31
import java .util .Map ;
31
32
32
- import javax .persistence .EntityManagerFactory ;
33
-
34
33
import org .hibernate .cfg .AvailableSettings ;
35
34
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 ;
37
37
import org .hibernate .testing .TestForIssue ;
38
38
import org .hibernate .testing .junit4 .BaseUnitTestCase ;
39
39
import org .junit .Test ;
40
40
41
41
/**
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
+ *
42
56
* @author Brett Meyer
43
57
*/
44
58
@ TestForIssue (jiraKey = "HHH-8364" )
@@ -47,32 +61,31 @@ public class ExcludeUnlistedClassesTest extends BaseUnitTestCase {
47
61
@ Test
48
62
public void testExcludeUnlistedClasses () {
49
63
// 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 );
54
74
}
55
75
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
+ }
67
83
}
84
+ fail ("Could not find the persistence unit: " + persistenceUnitName );
68
85
}
69
86
70
87
private static class TestClassLoader extends ClassLoader {
71
88
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
- */
76
89
@ Override
77
90
protected Enumeration <URL > findResources (String name ) throws IOException {
78
91
if (name .equals ( "META-INF/persistence.xml" )) {
0 commit comments