From 3869845ee0737677270f72740a30cdd31cfc8182 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Wed, 11 Sep 2013 10:52:35 -0400 Subject: [PATCH] HHH-8364 test case --- .../EntityManagerFactoryBuilderImpl.java | 1 - .../jpa/test/persistenceunit/DataPoint.java | 44 ++++++++ .../ExcludeUnlistedClassesTest.java | 101 ++++++++++++++++++ .../persistenceunit/UnlistedDataPoint.java | 44 ++++++++ .../persistenceunit/META-INF/persistence.xml | 31 ++++++ 5 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/DataPoint.java create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/ExcludeUnlistedClassesTest.java create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/UnlistedDataPoint.java create mode 100644 hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/persistenceunit/META-INF/persistence.xml diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java index 094f2c0851..89f647784d 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java @@ -509,7 +509,6 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil } else if ( appClassLoader != null ) { classLoader = appClassLoader; - integrationSettings.remove( org.hibernate.cfg.AvailableSettings.APP_CLASSLOADER ); } else { classLoader = persistenceUnit.getClassLoader(); diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/DataPoint.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/DataPoint.java new file mode 100644 index 0000000000..9805b41533 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/DataPoint.java @@ -0,0 +1,44 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @authors tag. All rights reserved. + * See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License, v. 2.1. + * This program is distributed in the hope that it will be useful, but WITHOUT A + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public License, + * v.2.1 along with this distribution; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +package org.hibernate.jpa.test.persistenceunit; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Brett Meyer + */ +@Entity +public class DataPoint { + + @Id + @GeneratedValue + private long id; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } +} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/ExcludeUnlistedClassesTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/ExcludeUnlistedClassesTest.java new file mode 100644 index 0000000000..3e36af2f2d --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/ExcludeUnlistedClassesTest.java @@ -0,0 +1,101 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @authors tag. All rights reserved. + * See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License, v. 2.1. + * This program is distributed in the hope that it will be useful, but WITHOUT A + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public License, + * v.2.1 along with this distribution; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +package org.hibernate.jpa.test.persistenceunit; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.net.URL; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.EntityManagerFactory; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.internal.util.ConfigHelper; +import org.hibernate.jpa.HibernatePersistenceProvider; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +/** + * @author Brett Meyer + */ +@TestForIssue(jiraKey = "HHH-8364") +public class ExcludeUnlistedClassesTest extends BaseUnitTestCase { + + @Test + public void testExcludeUnlistedClasses() { + // see src/test/resources/org/hibernate/jpa/test/persistenceunit/persistence.xml + doTest( "ExcludeUnlistedClassesTest1", true ); + doTest( "ExcludeUnlistedClassesTest2", false ); + doTest( "ExcludeUnlistedClassesTest3", true ); + doTest( "ExcludeUnlistedClassesTest4", false ); + } + + private void doTest(String persistenceUnitName, boolean shouldScan) { + final Map properties = new HashMap(); + properties.put( AvailableSettings.APP_CLASSLOADER, new TestClassLoader() ); + final HibernatePersistenceProvider provider = new HibernatePersistenceProvider(); + final EntityManagerFactory emf = provider.createEntityManagerFactory( persistenceUnitName, properties ); + assertNotNull( emf.getMetamodel().entity( DataPoint.class ) ); + if (shouldScan) { + assertNull( emf.getMetamodel().entity( UnlistedDataPoint.class ) ); + } + else { + assertNotNull( emf.getMetamodel().entity( UnlistedDataPoint.class ) ); + } + } + + private static class TestClassLoader extends ClassLoader { + + /** + * testStoppableClassLoaderService() needs a custom JDK service implementation. Rather than using a real one + * on the test classpath, force it in here. + */ + @Override + protected Enumeration findResources(String name) throws IOException { + if (name.equals( "META-INF/persistence.xml" )) { + final URL puUrl = ConfigHelper.findAsResource( + "org/hibernate/jpa/test/persistenceunit/META-INF/persistence.xml" ); + return new Enumeration() { + boolean hasMore = true; + + @Override + public boolean hasMoreElements() { + return hasMore; + } + + @Override + public URL nextElement() { + hasMore = false; + return puUrl; + } + }; + } + else { + return java.util.Collections.emptyEnumeration(); + } + } + } +} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/UnlistedDataPoint.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/UnlistedDataPoint.java new file mode 100644 index 0000000000..22668f0370 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/persistenceunit/UnlistedDataPoint.java @@ -0,0 +1,44 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @authors tag. All rights reserved. + * See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License, v. 2.1. + * This program is distributed in the hope that it will be useful, but WITHOUT A + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public License, + * v.2.1 along with this distribution; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +package org.hibernate.jpa.test.persistenceunit; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Brett Meyer + */ +@Entity +public class UnlistedDataPoint { + + @Id + @GeneratedValue + private long id; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } +} diff --git a/hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/persistenceunit/META-INF/persistence.xml b/hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/persistenceunit/META-INF/persistence.xml new file mode 100644 index 0000000000..f5645ff87d --- /dev/null +++ b/hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/persistenceunit/META-INF/persistence.xml @@ -0,0 +1,31 @@ + + + + + org.hibernate.jpa.HibernatePersistenceProvider + org.hibernate.jpa.test.persistenceunit.DataPoint + + + + org.hibernate.jpa.HibernatePersistenceProvider + org.hibernate.jpa.test.persistenceunit.DataPoint + + + + + org.hibernate.jpa.HibernatePersistenceProvider + org.hibernate.jpa.test.persistenceunit.DataPoint + false + + + + org.hibernate.jpa.HibernatePersistenceProvider + org.hibernate.jpa.test.persistenceunit.DataPoint + true + + + \ No newline at end of file