[hibernate-commits] Hibernate SVN: r18657 - core/trunk/core/src/test/java/org/hibernate/util.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Jan 27 14:16:39 EST 2010


Author: steve.ebersole at jboss.com
Date: 2010-01-27 14:16:39 -0500 (Wed, 27 Jan 2010)
New Revision: 18657

Added:
   core/trunk/core/src/test/java/org/hibernate/util/SerializableThing.java
   core/trunk/core/src/test/java/org/hibernate/util/SerializationHelperTest.java
Log:
HHH-4799 - Create a *unit* test that asserts SerializationHelper's ability to deser a class using an "isolated classloader" (aka a TCCL)


Added: core/trunk/core/src/test/java/org/hibernate/util/SerializableThing.java
===================================================================
--- core/trunk/core/src/test/java/org/hibernate/util/SerializableThing.java	                        (rev 0)
+++ core/trunk/core/src/test/java/org/hibernate/util/SerializableThing.java	2010-01-27 19:16:39 UTC (rev 18657)
@@ -0,0 +1,34 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * 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, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY 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
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.util;
+
+import java.io.Serializable;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class SerializableThing implements Serializable {
+}

Added: core/trunk/core/src/test/java/org/hibernate/util/SerializationHelperTest.java
===================================================================
--- core/trunk/core/src/test/java/org/hibernate/util/SerializationHelperTest.java	                        (rev 0)
+++ core/trunk/core/src/test/java/org/hibernate/util/SerializationHelperTest.java	2010-01-27 19:16:39 UTC (rev 18657)
@@ -0,0 +1,100 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * 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, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY 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
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.util;
+
+import java.io.InputStream;
+import java.io.Serializable;
+
+import junit.framework.TestCase;
+
+import org.hibernate.bytecode.util.ByteCodeHelper;
+
+/**
+ * This is basically a test to assert the expectations of {@link org.hibernate.type.SerializableType}
+ * in regards to deserializing bytes from second level caches.
+ *
+ * @author Steve Ebersole
+ */
+public class SerializationHelperTest extends TestCase {
+	private ClassLoader original;
+	private CustomClassLoader custom;
+
+	protected void setUp() throws Exception {
+		original = Thread.currentThread().getContextClassLoader();
+		custom = new CustomClassLoader( original );
+		Thread.currentThread().setContextClassLoader( custom );
+
+	}
+
+	protected void tearDown() throws Exception {
+		Thread.currentThread().setContextClassLoader( original );
+	}
+
+	public void testSerializeDeserialize() throws Exception {
+		Class clazz = Thread.currentThread().getContextClassLoader().loadClass( "org.hibernate.util.SerializableThing" );
+		Object instance = clazz.newInstance();
+
+		// SerializableType.toBytes() logic, as called from SerializableType.disassemble()
+		byte[] bytes = SerializationHelper.serialize( (Serializable) instance );
+
+		// SerializableType.fromBytes() logic, as called from SerializableType.assemble
+		//		NOTE : specifically we use Serializable.class.getClassLoader for the CL in many cases
+		//			which are the problematic cases
+		Object instance2 = SerializationHelper.deserialize( bytes, Serializable.class.getClassLoader() );
+
+		assertEquals( instance.getClass(), instance2.getClass() );
+		assertEquals( instance.getClass().getClassLoader(), instance2.getClass().getClassLoader() );
+		assertEquals( custom, instance2.getClass().getClassLoader() );
+	}
+
+	public static class CustomClassLoader extends ClassLoader {
+		public CustomClassLoader(ClassLoader parent) {
+			super( parent );
+		}
+
+		public Class loadClass(String name) throws ClassNotFoundException {
+			if ( ! name.equals( "org.hibernate.util.SerializableThing" ) ) {
+				return getParent().loadClass( name );
+			}
+
+			Class c = findLoadedClass( name );
+			if ( c != null ) {
+				return c;
+			}
+
+			InputStream is = this.getResourceAsStream( name.replace( '.', '/' ) + ".class" );
+			if ( is == null ) {
+				throw new ClassNotFoundException( name + " not found" );
+			}
+
+			try {
+				byte[] bytecode = ByteCodeHelper.readByteCode( is );
+				return defineClass( name, bytecode, 0, bytecode.length );
+			}
+			catch( Throwable t ) {
+				throw new ClassNotFoundException( name + " not found", t );
+			}
+		}
+	}
+}



More information about the hibernate-commits mailing list