[jboss-svn-commits] JBoss Common SVN: r3643 - invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Nov 7 17:33:43 EST 2009


Author: ALRubinger
Date: 2009-11-07 17:33:43 -0500 (Sat, 07 Nov 2009)
New Revision: 3643

Added:
   invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/InvocationImplTestCase.java
Removed:
   invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/InvocationImplTest.java
Modified:
   invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/OriginalVersionInvocationImpl.java
Log:
[EJBTHREE-1948] Rename test case so picked up by Surefire

Deleted: invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/InvocationImplTest.java
===================================================================
--- invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/InvocationImplTest.java	2009-11-07 22:26:13 UTC (rev 3642)
+++ invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/InvocationImplTest.java	2009-11-07 22:33:43 UTC (rev 3643)
@@ -1,444 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
-  *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software 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 software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.ejb3.container.core;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.Externalizable;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.ObjectStreamClass;
-import java.io.ObjectStreamConstants;
-import java.io.ObjectStreamField;
-import java.io.OutputStream;
-import java.io.Serializable;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.Arrays;
-import java.util.logging.Logger;
-
-import junit.framework.TestCase;
-
-import org.jboss.ejb3.container.api.Invocation;
-import org.jboss.ejb3.container.api.InvocationContext;
-import org.jboss.ejb3.container.spi.InvocationContextProvider;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-/**
- * InvocationImplTest
- * 
- * Tests ensuring the contract of {@link Invocation} holds
- * when using the {@link InvocationImpl}.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-public class InvocationImplTest
-{
-
-   //-------------------------------------------------------------------------------------||
-   // Class Members ----------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Logger
-    */
-   private static final Logger log = Logger.getLogger(InvocationImplTest.class.getName());
-
-   /**
-    * Original invocation constructed
-    */
-   private static Invocation original;
-
-   /**
-    * Duplicate invocation created via serialization copy
-    */
-   private static Invocation roundtrip;
-
-   /**
-    * Key for a property within the invocation context
-    */
-   private static final String KEY = "KEY";
-
-   /**
-    * Value to be associated with "KEY" in the invocation context
-    */
-   private static final String VALUE = "VALUE";
-
-   //-------------------------------------------------------------------------------------||
-   // Lifecycle --------------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Creates a new Invocation instance and creates a clone
-    * by roundtripping via the JDK serialization mechanism
-    * 
-    * @throws Exception
-    */
-   @BeforeClass
-   public static void initialize() throws Exception
-   {
-      // Create an Invocation
-      original = getInvocation();
-
-      // Set an InvocationContext property
-      original.getContext().setProperty(KEY, VALUE);
-
-      // Create a roundtrip instance via serialization
-      roundtrip = serializeAndDeserialize(original);
-      log.info("Created a serialized copy of the invocation: " + roundtrip);
-   }
-
-   //-------------------------------------------------------------------------------------||
-   // Tests ------------------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Ensures that {@link Invocation} instances after a serialization
-    * roundtrip are not equal by reference 
-    */
-   @Test
-   public void testEqualityByReferenceToRoundtrip()
-   {
-      TestCase.assertFalse("Roundtrip instance should not be equal to original by reference", original == roundtrip);
-   }
-
-   /**
-    * Ensures that {@link Invocation} instances after a serialization
-    * roundtrip are equal by value
-    */
-   @Test
-   public void testEqualityByValueToRoundtrip()
-   {
-      TestCase.assertTrue("Roundtrip instance should be equal to original by value", original.equals(roundtrip));
-   }
-
-   /**
-    * Ensures that {@link Invocation} instances are equal to themselves
-    */
-   @Test
-   public void testEqualityByValueToSame()
-   {
-      TestCase.assertTrue("Invocation objects must be equal by value to equal references", original.equals(original));
-   }
-
-   /**
-    * Ensures that the hash code of {@link Invocation} instances equal by 
-    * value are equal
-    */
-   @Test
-   public void testHashCode()
-   {
-      TestCase.assertTrue("Hash codes of objects equal by value must be equal", original.hashCode() == roundtrip
-            .hashCode());
-   }
-
-   /**
-    * Ensures that the target method, even after invocation serialization roundtrip, 
-    * remains intact (correct) and may be invoked upon
-    * @throws Exception
-    */
-   @Test
-   public void testRoundtripValidTargetMethod() throws Exception
-   {
-      final Addable target = new Calculator();
-      final Method targetMethod = roundtrip.getTargetMethod();
-      final int[] args =
-      {1, 2, 3};
-      final int expected = 6;
-      final int actual = ((Integer) targetMethod.invoke(target, args)).intValue();
-      log.info("Adding " + Arrays.asList(args) + ": expecting " + expected + ", got " + actual);
-      TestCase.assertEquals("Method invocation to roundtrip target method did not succeed as expected", expected,
-            actual);
-   }
-
-   /**
-    * Tests that the {@link InvocationImpl} originally released
-    * is able to be represented into the current form
-    * 
-    * @throws IOException
-    */
-   @Test
-   public void testWireProtocolOriginalToCurrent() throws IOException
-   {
-      // Make an Invocation of the form originally released
-      final Invocation originalFormat = new OriginalVersionInvocationImpl(original.getTargetMethod(), original
-            .getArgs());
-
-      // Test that the form originally released may be represented as the current type
-      this.testWireProtocol(originalFormat, original.getClass());
-   }
-
-   /**
-    * Tests that the {@link InvocationImpl} current form
-    * is able to be represented as the type originally released
-    * 
-    * @throws IOException
-    */
-   @Test
-   public void testWireProtocolCurrentToOriginal() throws IOException
-   {
-      this.testWireProtocol(original, OriginalVersionInvocationImpl.class);
-   }
-
-   /**
-    * Tests that the {@link InvocationContext} may be obtained
-    * from an {@link Invocation}
-    * 
-    * @throws IOException
-    */
-   @Test
-   public void testInvocationContext() throws IOException
-   {
-      // Obtain
-      final Invocation invocation = roundtrip;
-      final InvocationContext context = invocation.getContext();
-      log.info("Got context " + context + " from " + invocation);
-
-      // Test
-      TestCase.assertNotNull("Invocation context should not be null", context);
-
-      // Get out the property
-      final String actual = (String) context.getProperty(KEY);
-      TestCase.assertEquals("Property value was not as expected after serialization", VALUE, actual);
-   }
-
-   /**
-    * Tests that an empty {@link InvocationContext} is reinstantiated
-    * during deserialization
-    * 
-    * @throws IOException
-    */
-   @Test
-   public void testEmptyInvocationContextReinstantiated() throws Exception
-   {
-      // Make an invocation (with empty InvocationContext)
-      final Invocation invocation = getInvocation();
-
-      // Roundtrip serialization
-      final Invocation roundtrip = serializeAndDeserialize(invocation);
-
-      // Test
-      final InvocationContextProvider context = (InvocationContextProvider) roundtrip.getContext();
-      TestCase.assertNotNull("Empty invocation context after serialization should not be null", context);
-      TestCase.assertTrue("Empty invocation context after serialization should still be empty", context.isEmpty());
-   }
-
-   //-------------------------------------------------------------------------------------||
-   // Internal Helper Methods ------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Obtains a test {@link Invocation}
-    * @return
-    */
-   private static Invocation getInvocation()
-   {
-      // Obtain information needed to construct an Invocation instance
-      final Method targetMethod = Addable.class.getMethods()[0];
-      final Integer[] args =
-      {1, 2, 3};
-
-      // Create an Invocation
-      final Invocation invocation = new InvocationImpl(targetMethod, args);
-      log.info("Created invocation: " + invocation);
-
-      // Return
-      return invocation;
-   }
-
-   /**
-    * Ensures that the specified client object may be serialized to the specified type
-    * 
-    * @param clientObject The object to be serialzed
-    * @param targetType The type we should be represented as
-    * @throws IOException
-    */
-   private void testWireProtocol(final Invocation clientObject, final Class<? extends Invocation> targetType)
-         throws IOException
-   {
-      // Roundtrip the object, now representing as the target type
-      final Invocation roundtrip = serializeAndDeserialize(clientObject, targetType);
-
-      // The type of the object put through roundtrip serialization must be of the type specified
-      TestCase.assertEquals(targetType, roundtrip.getClass());
-   }
-
-   /**
-    * Roundtrip serializes/deserializes the specified {@link Invocation}
-    * 
-    * @param invocation
-    * @return
-    * @throws IOException
-    * @throws ClassNotFoundException
-    */
-   private static Invocation serializeAndDeserialize(final Invocation invocation) throws IOException,
-         ClassNotFoundException
-   {
-      assert invocation != null : "Invocation must be specified";
-      final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
-      final ObjectOutputStream out = new ObjectOutputStream(byteOut);
-      out.writeObject(invocation);
-      out.flush();
-      out.close();
-      final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray()));
-      final Invocation roundtrip = (Invocation) in.readObject();
-      in.close();
-      return roundtrip;
-   }
-
-   /**
-    * Roundtrip serializes/deserializes the specified {@link Invocation}
-    * and reconsitutes/redefines as the specified target type
-    * 
-    * @param invocation The original {@link Invocation} instance
-    * @param The new type we should cast to after deserialization 
-    * @see http://crazybob.org/2006/01/unit-testing-serialization-evolution.html
-    * @see http://crazybob.org/2006/01/unit-testing-serialization-evolution_13.html
-    * @see http://www.theserverside.com/news/thread.tss?thread_id=38398
-    * @author Bob Lee
-    */
-   private static <S extends Invocation> S serializeAndDeserialize(final Invocation invocation,
-         final Class<S> targetType) throws IOException
-   {
-      final ByteArrayOutputStream bout = new ByteArrayOutputStream();
-      final ObjectOutputStream oout = new SpoofingObjectOutputStream(bout, invocation.getClass(), targetType);
-      oout.writeObject(invocation);
-      oout.flush();
-      oout.close();
-      final ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
-      final ObjectInputStream oin = new ObjectInputStream(bin);
-      try
-      {
-         final Object obj = oin.readObject();
-         oin.close();
-         log.info("Original invocation type " + invocation.getClass().getName() + " now represented as "
-               + obj.getClass().getName());
-         return targetType.cast(obj);
-      }
-      catch (ClassNotFoundException e)
-      {
-         throw new RuntimeException(e);
-      }
-   }
-
-   //-------------------------------------------------------------------------------------||
-   // Internal Helper Classes ------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * SpoofingObjectOutputStream
-    * 
-    * ObjectOutputStream which will replace a class name with one explicitly given
-    *
-    * @see http://crazybob.org/2006/01/unit-testing-serialization-evolution_13.html
-    * @author Bob Lee
-    * @version $Revision: $
-    */
-   static class SpoofingObjectOutputStream extends ObjectOutputStream
-   {
-
-      String oldName;
-
-      String newName;
-
-      public SpoofingObjectOutputStream(OutputStream out, Class<?> oldClass, Class<?> newClass) throws IOException
-      {
-         super(out);
-         this.oldName = oldClass.getName();
-         this.newName = newClass.getName();
-      }
-
-      @Override
-      protected void writeClassDescriptor(ObjectStreamClass descriptor) throws IOException
-      {
-         Class<?> clazz = descriptor.forClass();
-
-         boolean externalizable = Externalizable.class.isAssignableFrom(clazz);
-         boolean serializable = Serializable.class.isAssignableFrom(clazz);
-         boolean hasWriteObjectData = hasWriteObjectMethod(clazz);
-         boolean isEnum = Enum.class.isAssignableFrom(clazz);
-
-         writeUTF(replace(descriptor.getName()));
-         writeLong(descriptor.getSerialVersionUID());
-         byte flags = 0;
-         if (externalizable)
-         {
-            flags |= ObjectStreamConstants.SC_EXTERNALIZABLE;
-            flags |= ObjectStreamConstants.SC_BLOCK_DATA;
-         }
-         else if (serializable)
-         {
-            flags |= ObjectStreamConstants.SC_SERIALIZABLE;
-         }
-         if (hasWriteObjectData)
-         {
-            flags |= ObjectStreamConstants.SC_WRITE_METHOD;
-         }
-         if (isEnum)
-         {
-            flags |= ObjectStreamConstants.SC_ENUM;
-         }
-         writeByte(flags);
-
-         ObjectStreamField[] fields = descriptor.getFields();
-         writeShort(fields.length);
-         for (ObjectStreamField field : fields)
-         {
-            writeByte(field.getTypeCode());
-            writeUTF(field.getName());
-            if (!field.isPrimitive())
-            {
-               writeObject(replace(field.getTypeString()));
-            }
-         }
-      }
-
-      String replace(String className)
-      {
-         if (className.equals(newName))
-         {
-            throw new RuntimeException("Found instance of " + className + "." + " Expected instance of " + oldName
-                  + ".");
-         }
-         return className == oldName ? newName : className;
-      }
-
-      boolean hasWriteObjectMethod(Class<?> clazz)
-      {
-         try
-         {
-            Method method = clazz.getDeclaredMethod("writeObject", ObjectOutputStream.class);
-            int modifiers = method.getModifiers();
-            return method.getReturnType() == Void.TYPE && !Modifier.isStatic(modifiers)
-                  && Modifier.isPrivate(modifiers);
-         }
-         catch (NoSuchMethodException e)
-         {
-            return false;
-         }
-      }
-   }
-
-}

Copied: invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/InvocationImplTestCase.java (from rev 3642, invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/InvocationImplTest.java)
===================================================================
--- invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/InvocationImplTestCase.java	                        (rev 0)
+++ invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/InvocationImplTestCase.java	2009-11-07 22:33:43 UTC (rev 3643)
@@ -0,0 +1,444 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.container.core;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamClass;
+import java.io.ObjectStreamConstants;
+import java.io.ObjectStreamField;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.Arrays;
+import java.util.logging.Logger;
+
+import junit.framework.TestCase;
+
+import org.jboss.ejb3.container.api.Invocation;
+import org.jboss.ejb3.container.api.InvocationContext;
+import org.jboss.ejb3.container.spi.InvocationContextProvider;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * InvocationImplTest
+ * 
+ * Tests ensuring the contract of {@link Invocation} holds
+ * when using the {@link InvocationImpl}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class InvocationImplTestCase
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(InvocationImplTestCase.class.getName());
+
+   /**
+    * Original invocation constructed
+    */
+   private static Invocation original;
+
+   /**
+    * Duplicate invocation created via serialization copy
+    */
+   private static Invocation roundtrip;
+
+   /**
+    * Key for a property within the invocation context
+    */
+   private static final String KEY = "KEY";
+
+   /**
+    * Value to be associated with "KEY" in the invocation context
+    */
+   private static final String VALUE = "VALUE";
+
+   //-------------------------------------------------------------------------------------||
+   // Lifecycle --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Creates a new Invocation instance and creates a clone
+    * by roundtripping via the JDK serialization mechanism
+    * 
+    * @throws Exception
+    */
+   @BeforeClass
+   public static void initialize() throws Exception
+   {
+      // Create an Invocation
+      original = getInvocation();
+
+      // Set an InvocationContext property
+      original.getContext().setProperty(KEY, VALUE);
+
+      // Create a roundtrip instance via serialization
+      roundtrip = serializeAndDeserialize(original);
+      log.info("Created a serialized copy of the invocation: " + roundtrip);
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Ensures that {@link Invocation} instances after a serialization
+    * roundtrip are not equal by reference 
+    */
+   @Test
+   public void testEqualityByReferenceToRoundtrip()
+   {
+      TestCase.assertFalse("Roundtrip instance should not be equal to original by reference", original == roundtrip);
+   }
+
+   /**
+    * Ensures that {@link Invocation} instances after a serialization
+    * roundtrip are equal by value
+    */
+   @Test
+   public void testEqualityByValueToRoundtrip()
+   {
+      TestCase.assertTrue("Roundtrip instance should be equal to original by value", original.equals(roundtrip));
+   }
+
+   /**
+    * Ensures that {@link Invocation} instances are equal to themselves
+    */
+   @Test
+   public void testEqualityByValueToSame()
+   {
+      TestCase.assertTrue("Invocation objects must be equal by value to equal references", original.equals(original));
+   }
+
+   /**
+    * Ensures that the hash code of {@link Invocation} instances equal by 
+    * value are equal
+    */
+   @Test
+   public void testHashCode()
+   {
+      TestCase.assertTrue("Hash codes of objects equal by value must be equal", original.hashCode() == roundtrip
+            .hashCode());
+   }
+
+   /**
+    * Ensures that the target method, even after invocation serialization roundtrip, 
+    * remains intact (correct) and may be invoked upon
+    * @throws Exception
+    */
+   @Test
+   public void testRoundtripValidTargetMethod() throws Exception
+   {
+      final Addable target = new Calculator();
+      final Method targetMethod = roundtrip.getTargetMethod();
+      final int[] args =
+      {1, 2, 3};
+      final int expected = 6;
+      final int actual = ((Integer) targetMethod.invoke(target, args)).intValue();
+      log.info("Adding " + Arrays.asList(args) + ": expecting " + expected + ", got " + actual);
+      TestCase.assertEquals("Method invocation to roundtrip target method did not succeed as expected", expected,
+            actual);
+   }
+
+   /**
+    * Tests that the {@link InvocationImpl} originally released
+    * is able to be represented into the current form
+    * 
+    * @throws IOException
+    */
+   @Test
+   public void testWireProtocolOriginalToCurrent() throws IOException
+   {
+      // Make an Invocation of the form originally released
+      final Invocation originalFormat = new OriginalVersionInvocationImpl(original.getTargetMethod(), original
+            .getArgs());
+
+      // Test that the form originally released may be represented as the current type
+      this.testWireProtocol(originalFormat, original.getClass());
+   }
+
+   /**
+    * Tests that the {@link InvocationImpl} current form
+    * is able to be represented as the type originally released
+    * 
+    * @throws IOException
+    */
+   @Test
+   public void testWireProtocolCurrentToOriginal() throws IOException
+   {
+      this.testWireProtocol(original, OriginalVersionInvocationImpl.class);
+   }
+
+   /**
+    * Tests that the {@link InvocationContext} may be obtained
+    * from an {@link Invocation}
+    * 
+    * @throws IOException
+    */
+   @Test
+   public void testInvocationContext() throws IOException
+   {
+      // Obtain
+      final Invocation invocation = roundtrip;
+      final InvocationContext context = invocation.getContext();
+      log.info("Got context " + context + " from " + invocation);
+
+      // Test
+      TestCase.assertNotNull("Invocation context should not be null", context);
+
+      // Get out the property
+      final String actual = (String) context.getProperty(KEY);
+      TestCase.assertEquals("Property value was not as expected after serialization", VALUE, actual);
+   }
+
+   /**
+    * Tests that an empty {@link InvocationContext} is reinstantiated
+    * during deserialization
+    * 
+    * @throws IOException
+    */
+   @Test
+   public void testEmptyInvocationContextReinstantiated() throws Exception
+   {
+      // Make an invocation (with empty InvocationContext)
+      final Invocation invocation = getInvocation();
+
+      // Roundtrip serialization
+      final Invocation roundtrip = serializeAndDeserialize(invocation);
+
+      // Test
+      final InvocationContextProvider context = (InvocationContextProvider) roundtrip.getContext();
+      TestCase.assertNotNull("Empty invocation context after serialization should not be null", context);
+      TestCase.assertTrue("Empty invocation context after serialization should still be empty", context.isEmpty());
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Internal Helper Methods ------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Obtains a test {@link Invocation}
+    * @return
+    */
+   private static Invocation getInvocation()
+   {
+      // Obtain information needed to construct an Invocation instance
+      final Method targetMethod = Addable.class.getMethods()[0];
+      final Integer[] args =
+      {1, 2, 3};
+
+      // Create an Invocation
+      final Invocation invocation = new InvocationImpl(targetMethod, args);
+      log.info("Created invocation: " + invocation);
+
+      // Return
+      return invocation;
+   }
+
+   /**
+    * Ensures that the specified client object may be serialized to the specified type
+    * 
+    * @param clientObject The object to be serialzed
+    * @param targetType The type we should be represented as
+    * @throws IOException
+    */
+   private void testWireProtocol(final Invocation clientObject, final Class<? extends Invocation> targetType)
+         throws IOException
+   {
+      // Roundtrip the object, now representing as the target type
+      final Invocation roundtrip = serializeAndDeserialize(clientObject, targetType);
+
+      // The type of the object put through roundtrip serialization must be of the type specified
+      TestCase.assertEquals(targetType, roundtrip.getClass());
+   }
+
+   /**
+    * Roundtrip serializes/deserializes the specified {@link Invocation}
+    * 
+    * @param invocation
+    * @return
+    * @throws IOException
+    * @throws ClassNotFoundException
+    */
+   private static Invocation serializeAndDeserialize(final Invocation invocation) throws IOException,
+         ClassNotFoundException
+   {
+      assert invocation != null : "Invocation must be specified";
+      final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
+      final ObjectOutputStream out = new ObjectOutputStream(byteOut);
+      out.writeObject(invocation);
+      out.flush();
+      out.close();
+      final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray()));
+      final Invocation roundtrip = (Invocation) in.readObject();
+      in.close();
+      return roundtrip;
+   }
+
+   /**
+    * Roundtrip serializes/deserializes the specified {@link Invocation}
+    * and reconsitutes/redefines as the specified target type
+    * 
+    * @param invocation The original {@link Invocation} instance
+    * @param The new type we should cast to after deserialization 
+    * @see http://crazybob.org/2006/01/unit-testing-serialization-evolution.html
+    * @see http://crazybob.org/2006/01/unit-testing-serialization-evolution_13.html
+    * @see http://www.theserverside.com/news/thread.tss?thread_id=38398
+    * @author Bob Lee
+    */
+   private static <S extends Invocation> S serializeAndDeserialize(final Invocation invocation,
+         final Class<S> targetType) throws IOException
+   {
+      final ByteArrayOutputStream bout = new ByteArrayOutputStream();
+      final ObjectOutputStream oout = new SpoofingObjectOutputStream(bout, invocation.getClass(), targetType);
+      oout.writeObject(invocation);
+      oout.flush();
+      oout.close();
+      final ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+      final ObjectInputStream oin = new ObjectInputStream(bin);
+      try
+      {
+         final Object obj = oin.readObject();
+         oin.close();
+         log.info("Original invocation type " + invocation.getClass().getName() + " now represented as "
+               + obj.getClass().getName());
+         return targetType.cast(obj);
+      }
+      catch (ClassNotFoundException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Internal Helper Classes ------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * SpoofingObjectOutputStream
+    * 
+    * ObjectOutputStream which will replace a class name with one explicitly given
+    *
+    * @see http://crazybob.org/2006/01/unit-testing-serialization-evolution_13.html
+    * @author Bob Lee
+    * @version $Revision: $
+    */
+   static class SpoofingObjectOutputStream extends ObjectOutputStream
+   {
+
+      String oldName;
+
+      String newName;
+
+      public SpoofingObjectOutputStream(OutputStream out, Class<?> oldClass, Class<?> newClass) throws IOException
+      {
+         super(out);
+         this.oldName = oldClass.getName();
+         this.newName = newClass.getName();
+      }
+
+      @Override
+      protected void writeClassDescriptor(ObjectStreamClass descriptor) throws IOException
+      {
+         Class<?> clazz = descriptor.forClass();
+
+         boolean externalizable = Externalizable.class.isAssignableFrom(clazz);
+         boolean serializable = Serializable.class.isAssignableFrom(clazz);
+         boolean hasWriteObjectData = hasWriteObjectMethod(clazz);
+         boolean isEnum = Enum.class.isAssignableFrom(clazz);
+
+         writeUTF(replace(descriptor.getName()));
+         writeLong(descriptor.getSerialVersionUID());
+         byte flags = 0;
+         if (externalizable)
+         {
+            flags |= ObjectStreamConstants.SC_EXTERNALIZABLE;
+            flags |= ObjectStreamConstants.SC_BLOCK_DATA;
+         }
+         else if (serializable)
+         {
+            flags |= ObjectStreamConstants.SC_SERIALIZABLE;
+         }
+         if (hasWriteObjectData)
+         {
+            flags |= ObjectStreamConstants.SC_WRITE_METHOD;
+         }
+         if (isEnum)
+         {
+            flags |= ObjectStreamConstants.SC_ENUM;
+         }
+         writeByte(flags);
+
+         ObjectStreamField[] fields = descriptor.getFields();
+         writeShort(fields.length);
+         for (ObjectStreamField field : fields)
+         {
+            writeByte(field.getTypeCode());
+            writeUTF(field.getName());
+            if (!field.isPrimitive())
+            {
+               writeObject(replace(field.getTypeString()));
+            }
+         }
+      }
+
+      String replace(String className)
+      {
+         if (className.equals(newName))
+         {
+            throw new RuntimeException("Found instance of " + className + "." + " Expected instance of " + oldName
+                  + ".");
+         }
+         return className == oldName ? newName : className;
+      }
+
+      boolean hasWriteObjectMethod(Class<?> clazz)
+      {
+         try
+         {
+            Method method = clazz.getDeclaredMethod("writeObject", ObjectOutputStream.class);
+            int modifiers = method.getModifiers();
+            return method.getReturnType() == Void.TYPE && !Modifier.isStatic(modifiers)
+                  && Modifier.isPrivate(modifiers);
+         }
+         catch (NoSuchMethodException e)
+         {
+            return false;
+         }
+      }
+   }
+
+}

Modified: invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/OriginalVersionInvocationImpl.java
===================================================================
--- invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/OriginalVersionInvocationImpl.java	2009-11-07 22:26:13 UTC (rev 3642)
+++ invokablecontainer/trunk/core/src/test/java/org/jboss/ejb3/container/core/OriginalVersionInvocationImpl.java	2009-11-07 22:33:43 UTC (rev 3643)
@@ -40,7 +40,7 @@
  * OriginalVersionInvocationImpl
  * 
  * A copy of the {@link InvocationImpl} originally released.
- * Do not change this class; it is used by {@link InvocationImplTest#testWireProtocol()}
+ * Do not change this class; it is used by {@link InvocationImplTestCase#testWireProtocol()}
  * to ensure wire compatibility in both directions is preserved 
  * as the class evolves.
  *



More information about the jboss-svn-commits mailing list