Package org.jboss.test.ha.framework.server

Source Code of org.jboss.test.ha.framework.server.SimpleCachableMarshalledValueUnitTestCase$FragileObject

/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, 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.test.ha.framework.server;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.jboss.ha.framework.server.SimpleCachableMarshalledValue;
import org.jboss.util.id.GUID;

import junit.framework.TestCase;

/**
* Unit tests for SimpleCachableMarshalledValue.
*
* @author Brian Stansberry
*/
public class SimpleCachableMarshalledValueUnitTestCase extends TestCase
{

   /**
    * Create a new SimpleCachableMarshalledValueUnitTestCase.
    *
    * @param name
    */
   public SimpleCachableMarshalledValueUnitTestCase(String name)
   {
      super(name);
   }
  
   public void testReplication() throws Exception
   {
      GUID guid = new GUID();
      SimpleCachableMarshalledValue mv = new SimpleCachableMarshalledValue(guid);
     
      SimpleCachableMarshalledValue copy = replicate(mv);
      assertNull(copy.peekUnderlyingObject());
      byte[] serialized = copy.peekSerializedForm();
      assertNotNull(serialized);
     
      assertNull(mv.peekSerializedForm());
      assertSame(guid, mv.peekUnderlyingObject());
     
      Object guid2 =  unmarshall(serialized);
      assertNotSame(guid, guid2);
      assertEquals(guid, guid2);
   }

   /**
    * Test method for {@link org.jboss.ha.framework.server.SimpleCachableMarshalledValue#get()}.
    */
   public void testGet() throws Exception
   {
      GUID guid = new GUID();
      SimpleCachableMarshalledValue mv = new SimpleCachableMarshalledValue(guid);
     
      assertSame(guid, mv.get());
     
      SimpleCachableMarshalledValue copy = replicate(mv);
     
      Object guid2 =  copy.get();
      assertNotSame(guid, guid2);
      assertEquals(guid, guid2);
     
      copy.toByteArray();
      SimpleCachableMarshalledValue triplet = replicate(copy);
      guid2 = triplet.get();
      assertEquals(guid, guid2);
     
      mv = new SimpleCachableMarshalledValue(null);
      assertNull(mv.get());
   }

   /**
    * Test method for {@link org.jboss.ha.framework.server.SimpleCachableMarshalledValue#toByteArray()}.
    */
   public void testToByteArray() throws Exception
   {
      GUID guid = new GUID();
      SimpleCachableMarshalledValue mv = new SimpleCachableMarshalledValue(guid);
     
      assertNull(mv.peekSerializedForm());
     
      byte[] bytes = mv.toByteArray();
     
      assertNull(mv.peekUnderlyingObject());
     
      assertSame(bytes, mv.peekSerializedForm());
     
      byte[] bytes2 = mv.toByteArray();
     
      assertSame(bytes, bytes2);
     
      SimpleCachableMarshalledValue copy = replicate(mv);
      assertEquals(copy.peekSerializedForm(), copy.toByteArray());
     
      mv = new SimpleCachableMarshalledValue(null);
      assertNull(mv.toByteArray());
   }

   /**
    * Test method for {@link org.jboss.ha.framework.server.SimpleCachableMarshalledValue#equals(java.lang.Object)}.
    */
   public void testEquals() throws Exception
   {
      GUID guid = new GUID();
      SimpleCachableMarshalledValue mv = new SimpleCachableMarshalledValue(guid);
     
      assertTrue(mv.equals(mv));
      assertFalse(mv.equals(null));
     
      SimpleCachableMarshalledValue dup = new SimpleCachableMarshalledValue(guid);
      assertTrue(mv.equals(dup));
      assertTrue(dup.equals(mv));
     
      SimpleCachableMarshalledValue replica = replicate(mv);
      assertTrue(mv.equals(replica));
      assertTrue(replica.equals(mv));
     
      SimpleCachableMarshalledValue nulled = new SimpleCachableMarshalledValue(null);
      assertFalse(mv.equals(nulled));
      assertFalse(nulled.equals(mv));
      assertFalse(replica.equals(nulled));
      assertFalse(nulled.equals(replica));
      assertTrue(nulled.equals(nulled));
      assertFalse(nulled.equals(null));
      assertTrue(nulled.equals(new SimpleCachableMarshalledValue(null)));
   }

   /**
    * Test method for {@link org.jboss.ha.framework.server.SimpleCachableMarshalledValue#hashCode()}.
    */
   public void testHashCode() throws Exception
   {
      GUID guid = new GUID();
      SimpleCachableMarshalledValue mv = new SimpleCachableMarshalledValue(guid);
      assertEquals(guid.hashCode(), mv.hashCode());
     
      SimpleCachableMarshalledValue copy = replicate(mv);
      assertEquals(guid.hashCode(), copy.hashCode());
     
      mv = new SimpleCachableMarshalledValue(null);
      assertEquals(0, mv.hashCode());
     
      FragileObject fragile = new FragileObject();
      mv = new SimpleCachableMarshalledValue(fragile, true);
     
      assertEquals(SimpleCachableMarshalledValue.OUR_HASH_CODE, mv.hashCode());
     
      copy = replicate(mv);
      assertEquals(mv.hashCode(), copy.hashCode());
     
      mv = new SimpleCachableMarshalledValue(null);
     
      assertEquals(SimpleCachableMarshalledValue.NULL_HASH_CODE, mv.hashCode());
     
      copy = replicate(mv);
      assertEquals(mv.hashCode(), copy.hashCode());
      mv = new SimpleCachableMarshalledValue(null, true);
     
      assertEquals(SimpleCachableMarshalledValue.OUR_HASH_CODE, mv.hashCode());
     
      copy = replicate(mv);
      assertEquals(mv.hashCode(), copy.hashCode());
   }

   /**
    * Test method for {@link java.lang.Object#toString()}.
    */
   public void testToString() throws Exception
   {
      GUID guid = new GUID();
      SimpleCachableMarshalledValue mv = new SimpleCachableMarshalledValue(guid);
      assertNotNull(mv.toString());
     
      SimpleCachableMarshalledValue copy = replicate(mv);
      assertNotNull(copy.toString());
     
      FragileObject fragile = new FragileObject();
      mv = new SimpleCachableMarshalledValue(fragile, true);
      assertNotNull(mv.toString());
     
      copy = replicate(mv);
      assertNotNull(copy.toString());
     
      mv = new SimpleCachableMarshalledValue(null);
      assertNotNull(mv.toString());
   }

   private SimpleCachableMarshalledValue replicate(SimpleCachableMarshalledValue mv)
      throws IOException, ClassNotFoundException
   {
      return (SimpleCachableMarshalledValue) unmarshall(marshall(mv));
   }
  
   private byte[] marshall(SimpleCachableMarshalledValue mv) throws IOException
   {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(baos);
      oos.writeObject(mv);
      oos.close();
      return baos.toByteArray();
   }
  
   private Object unmarshall(byte[] bytes)
      throws IOException, ClassNotFoundException
   {
      ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
      ObjectInputStream ois = new ObjectInputStream(bais);
      try
      {
         return ois.readObject();
      }
      finally
      {
         ois.close();
      }
   }
  
   /**
    * A class that throws exceptions if any of its methods are invoked. Used
    * to test that such methods are not invoked.
    */
   private static class FragileObject implements Serializable
   {
      /** The serialVersionUID */
      private static final long serialVersionUID = 1L;

      @Override
      public int hashCode()
      {
         throw new RuntimeException("Someone called hashCode()!!");
      }

      @Override
      public boolean equals(Object obj)
      {
         throw new RuntimeException("Someone called equals()!!");
      }

      @Override
      public String toString()
      {
         throw new RuntimeException("Someone called toString()!!");
      }
     
   }
}
TOP

Related Classes of org.jboss.test.ha.framework.server.SimpleCachableMarshalledValueUnitTestCase$FragileObject

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.