[jboss-cvs] JBoss Messaging SVN: r4408 - in trunk/tests: src/org/jboss/messaging/tests/unit/core/util and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Jun 6 10:59:17 EDT 2008


Author: jmesnil
Date: 2008-06-06 10:59:16 -0400 (Fri, 06 Jun 2008)
New Revision: 4408

Added:
   trunk/tests/src/org/jboss/messaging/tests/unit/core/util/ObjectInputStreamWithClassLoaderTest.java
Modified:
   trunk/tests/jms-tests/src/org/jboss/test/messaging/jms/message/ObjectMessageTest.java
Log:
added unit tests for ObjectInputStreamWithClassLoader

Modified: trunk/tests/jms-tests/src/org/jboss/test/messaging/jms/message/ObjectMessageTest.java
===================================================================
--- trunk/tests/jms-tests/src/org/jboss/test/messaging/jms/message/ObjectMessageTest.java	2008-06-06 14:30:15 UTC (rev 4407)
+++ trunk/tests/jms-tests/src/org/jboss/test/messaging/jms/message/ObjectMessageTest.java	2008-06-06 14:59:16 UTC (rev 4408)
@@ -21,17 +21,13 @@
   */
 package org.jboss.test.messaging.jms.message;
 
-import java.io.File;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.ArrayList;
-import java.util.StringTokenizer;
-
 import javax.jms.DeliveryMode;
 import javax.jms.JMSException;
 import javax.jms.Message;
 import javax.jms.ObjectMessage;
 
+import org.jboss.messaging.tests.unit.core.util.ObjectInputStreamWithClassLoaderTest;
+
 /**
  * A test that sends/receives object messages to the JMS provider and verifies their integrity.
  *
@@ -83,7 +79,7 @@
 
          SomeObject testObject = new SomeObject(3, 7);
 
-         ClassLoader testClassLoader = newClassLoader(testObject.getClass());
+         ClassLoader testClassLoader = ObjectInputStreamWithClassLoaderTest.newClassLoader(testObject.getClass());
 
          om.setObject(testObject);
 
@@ -146,39 +142,4 @@
       ObjectMessage om = (ObjectMessage)m;
       assertEquals("this is the serializable object", om.getObject());
    }
-
-   protected static ClassLoader newClassLoader(Class anyUserClass) throws Exception
-   {
-      URL classLocation = anyUserClass.getProtectionDomain().getCodeSource().getLocation();
-      StringTokenizer tokenString = new StringTokenizer(System.getProperty("java.class.path"),
-         File.pathSeparator);
-      String pathIgnore = System.getProperty("java.home");
-      if (pathIgnore == null)
-      {
-         pathIgnore = classLocation.toString();
-      }
-
-      ArrayList urls = new ArrayList();
-      while (tokenString.hasMoreElements())
-      {
-         String value = tokenString.nextToken();
-         URL itemLocation = new File(value).toURL();
-         if (!itemLocation.equals(classLocation) &&
-                      itemLocation.toString().indexOf(pathIgnore) >= 0)
-         {
-            urls.add(itemLocation);
-         }
-      }
-
-      URL[] urlArray = (URL[]) urls.toArray(new URL[urls.size()]);
-
-      ClassLoader masterClassLoader = URLClassLoader.newInstance(urlArray, null);
-
-
-      ClassLoader appClassLoader = URLClassLoader.newInstance(new URL[]{classLocation},
-                                      masterClassLoader);
-
-      return appClassLoader;
-   }
-
 }

Added: trunk/tests/src/org/jboss/messaging/tests/unit/core/util/ObjectInputStreamWithClassLoaderTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/util/ObjectInputStreamWithClassLoaderTest.java	                        (rev 0)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/util/ObjectInputStreamWithClassLoaderTest.java	2008-06-06 14:59:16 UTC (rev 4408)
@@ -0,0 +1,127 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.messaging.tests.unit.core.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import junit.framework.TestCase;
+
+import org.jboss.messaging.util.ObjectInputStreamWithClassLoader;
+
+/**
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ *
+ * @version <tt>$Revision$</tt>
+ *
+ */
+public class ObjectInputStreamWithClassLoaderTest extends TestCase
+{
+   // Constants -----------------------------------------------------
+
+   // Attributes ----------------------------------------------------
+
+   // Static --------------------------------------------------------
+
+   public static ClassLoader newClassLoader(Class anyUserClass) throws Exception
+   {
+      URL classLocation = anyUserClass.getProtectionDomain().getCodeSource().getLocation();
+      StringTokenizer tokenString = new StringTokenizer(System.getProperty("java.class.path"),
+         File.pathSeparator);
+      String pathIgnore = System.getProperty("java.home");
+      if (pathIgnore == null)
+      {
+         pathIgnore = classLocation.toString();
+      }
+
+      List<URL> urls = new ArrayList<URL>();
+      while (tokenString.hasMoreElements())
+      {
+         String value = tokenString.nextToken();
+         URL itemLocation = new File(value).toURL();
+         if (!itemLocation.equals(classLocation) &&
+                      itemLocation.toString().indexOf(pathIgnore) >= 0)
+         {
+            urls.add(itemLocation);
+         }
+      }
+
+      URL[] urlArray = (URL[]) urls.toArray(new URL[urls.size()]);
+
+      ClassLoader masterClassLoader = URLClassLoader.newInstance(urlArray, null);
+      ClassLoader appClassLoader = URLClassLoader.newInstance(new URL[]{classLocation},
+                                      masterClassLoader);
+      return appClassLoader;
+   }
+   
+   // Constructors --------------------------------------------------
+
+   // Public --------------------------------------------------------
+
+   public void testClassLoaderIsolation() throws Exception
+   {
+
+      ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
+      try
+      {
+         AnObject obj = new AnObject();
+         byte[] bytes = toBytes(obj);
+         
+         ClassLoader testClassLoader = newClassLoader(obj.getClass());
+         Thread.currentThread().setContextClassLoader(testClassLoader);
+
+         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+         ObjectInputStreamWithClassLoader ois = new ObjectInputStreamWithClassLoader(bais);
+
+         Object deserializedObj= ois.readObject();
+         
+         assertNotSame(obj, deserializedObj);
+         assertNotSame(obj.getClass(), deserializedObj.getClass());
+         assertNotSame(obj.getClass().getClassLoader(),
+               deserializedObj.getClass().getClassLoader());
+         assertSame(testClassLoader,
+               deserializedObj.getClass().getClassLoader());
+      }
+      finally
+      {
+         Thread.currentThread().setContextClassLoader(originalClassLoader);
+      }
+
+   }
+
+   // Package protected ---------------------------------------------
+
+   // Protected -----------------------------------------------------
+
+   // Private -------------------------------------------------------
+
+   private static byte[] toBytes(Object obj) throws IOException
+   {
+      assertTrue(obj instanceof Serializable);
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      ObjectOutputStream oos = new ObjectOutputStream(baos);
+      oos.writeObject(obj);
+      oos.flush();
+      return baos.toByteArray();    
+   }
+   
+   // Inner classes -------------------------------------------------
+   
+   private static class AnObject implements Serializable
+   {
+      private static final long serialVersionUID = -5172742084489525256L;      
+   }
+}




More information about the jboss-cvs-commits mailing list