|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * Copyright (c) 2010, Red Hat Inc. or third-party contributors as |
| 5 | + * indicated by the @author tags or express copyright attribution |
| 6 | + * statements applied by the authors. All third-party contributions are |
| 7 | + * distributed under license by Red Hat Inc. |
| 8 | + * |
| 9 | + * This copyrighted material is made available to anyone wishing to use, modify, |
| 10 | + * copy, or redistribute it subject to the terms and conditions of the GNU |
| 11 | + * Lesser General Public License, as published by the Free Software Foundation. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 16 | + * for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with this distribution; if not, write to: |
| 20 | + * Free Software Foundation, Inc. |
| 21 | + * 51 Franklin Street, Fifth Floor |
| 22 | + * Boston, MA 02110-1301 USA |
| 23 | + */ |
| 24 | +package org.hibernate.test.proxyclass; |
| 25 | + |
| 26 | +import javax.persistence.Entity; |
| 27 | +import javax.persistence.Id; |
| 28 | + |
| 29 | +import org.hibernate.Session; |
| 30 | +import org.hibernate.Transaction; |
| 31 | +import org.hibernate.annotations.Proxy; |
| 32 | +import org.hibernate.proxy.HibernateProxy; |
| 33 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 34 | +import org.junit.Assert; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests if javassist instrumentation is done with the proper classloader for entities with proxy class. The classloader |
| 39 | + * of {@link HibernateProxy} will not see {@link IPerson}, since it is only accessible from this package. But: the |
| 40 | + * classloader of {@link IPerson} will see {@link HibernateProxy}, so instrumentation will only work if this classloader |
| 41 | + * is chosen for creating the instrumented proxy class. We need to check the class of a loaded object though, since |
| 42 | + * building the configuration will not fail, only log the error and fall back to using the entity class itself as a |
| 43 | + * proxy. |
| 44 | + * |
| 45 | + * @author lgathy |
| 46 | + */ |
| 47 | +public class ProxyInterfaceClassLoaderTest extends BaseCoreFunctionalTestCase { |
| 48 | + |
| 49 | + @Override |
| 50 | + protected Class<?>[] getAnnotatedClasses() { |
| 51 | + return new Class[] { Person.class }; |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testProxyClassLoader() { |
| 56 | + |
| 57 | + Session s = openSession(); |
| 58 | + Transaction t = s.beginTransaction(); |
| 59 | + IPerson p = new Person(); |
| 60 | + p.setId( 1 ); |
| 61 | + s.persist( p ); |
| 62 | + s.flush(); |
| 63 | + s.clear(); |
| 64 | + |
| 65 | + Object lp = s.load( Person.class, p.getId() ); |
| 66 | + |
| 67 | + Assert.assertTrue( "Loaded entity is not an instance of the proxy interface", IPerson.class.isInstance( lp ) ); |
| 68 | + Assert.assertFalse( "Proxy class was not created", Person.class.isInstance( lp ) ); |
| 69 | + |
| 70 | + s.delete( lp ); |
| 71 | + t.commit(); |
| 72 | + s.close(); |
| 73 | + } |
| 74 | + |
| 75 | + interface IPerson { |
| 76 | + |
| 77 | + int getId(); |
| 78 | + |
| 79 | + void setId(int id); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + @Entity |
| 84 | + @Proxy(proxyClass = IPerson.class) |
| 85 | + static class Person implements IPerson { |
| 86 | + |
| 87 | + @Id |
| 88 | + private int id; |
| 89 | + |
| 90 | + public int getId() { |
| 91 | + return id; |
| 92 | + } |
| 93 | + |
| 94 | + public void setId(int id) { |
| 95 | + this.id = id; |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments