[jboss-cvs] jboss-serialization/tests/org/jboss/serial/immutable ...

Clebert Suconic csuconic at jboss.com
Tue Apr 10 13:21:49 EDT 2007


  User: csuconic
  Date: 07/04/10 13:21:49

  Modified:    tests/org/jboss/serial/immutable 
                        ImmutableClassesTestCase.java
  Log:
  http://jira.jboss.org/jira/browse/JBSER-84 - Implementation of User Based Immutables
  
  Revision  Changes    Path
  1.2       +128 -0    jboss-serialization/tests/org/jboss/serial/immutable/ImmutableClassesTestCase.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ImmutableClassesTestCase.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-serialization/tests/org/jboss/serial/immutable/ImmutableClassesTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- ImmutableClassesTestCase.java	9 Apr 2007 23:05:21 -0000	1.1
  +++ ImmutableClassesTestCase.java	10 Apr 2007 17:21:49 -0000	1.2
  @@ -3,7 +3,9 @@
   import junit.framework.TestCase;
   
   import java.io.*;
  +import java.util.Random;
   
  +import org.jboss.serial.io.Immutable;
   import org.jboss.serial.io.JBossObjectInputStream;
   import org.jboss.serial.io.JBossObjectOutputStream;
   
  @@ -34,6 +36,73 @@
   		
   	}
   	
  +	public void testUserImmutableGraph() throws Exception
  +	{
  +		UserGraph graph = new UserGraph();
  +		graph.str1 = new String("str1");
  +		graph.str2 = new String("str1");
  +		
  +		
  +		int value = new Random().nextInt();
  +		
  +		UserImmutable immutables[] = new UserImmutable[50];
  +		String strs[] = new String[50];
  +		for (int i=0;i<50;i++)
  +		{
  +			immutables[i] = new UserImmutable(value);
  +			strs[i] = new String("Test" + value);
  +		}
  +		
  +		graph.stringArray = strs;
  +		assertEquals(graph.str1, graph.str2);
  +		assertNotSame(graph.str1, graph.str2);
  +		graph.immutables = immutables;
  +		
  +		graph.anotherImmutableReference = new UserImmutable(value);
  +		
  +		assertImmutables(graph, false);
  +		
  +		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
  +		JBossObjectOutputStream jbout = new JBossObjectOutputStream(byteOut);
  +		jbout.writeObject(graph);
  +		jbout.close();
  +		
  +		
  +		JBossObjectInputStream jbinput = new JBossObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray()));
  +		UserGraph graph2 = (UserGraph)jbinput.readObject();
  +		
  +		assertNotSame (graph, graph2);
  +		assertEquals(graph2.str1, graph2.str2);
  +		assertSame(graph2.str1, graph2.str2);
  +
  +		assertImmutables(graph2, true);
  +		
  +		assertEquals(graph2.anotherImmutableReference, graph2.immutables[0]);
  +		assertSame(graph2.anotherImmutableReference, graph2.immutables[0]);
  +		
  +		assertNull(graph2.immutableMeantToBeNull);
  +		assertNull(graph2.stringMeantToBeNull);
  +		
  +	}
  +
  +	private void assertImmutables(UserGraph parameterGraph, boolean shouldBeSame) {
  +		for (int i=1;i<parameterGraph.immutables.length;i++)
  +		{
  +			assertEquals(parameterGraph.immutables[0], parameterGraph.immutables[i]);
  +			assertEquals(parameterGraph.stringArray[0], parameterGraph.stringArray[i]);
  +			if (shouldBeSame)
  +			{
  +				assertSame(parameterGraph.immutables[0], parameterGraph.immutables[i]);
  +				assertSame(parameterGraph.stringArray[0], parameterGraph.stringArray[i]);
  +			}
  +			else
  +			{
  +				assertNotSame(parameterGraph.immutables[0], parameterGraph.immutables[i]);
  +				assertNotSame(parameterGraph.stringArray[0], parameterGraph.stringArray[i]);
  +			}
  +		}
  +	}
  +	
   	
   	
   	static class Graph
  @@ -46,4 +115,63 @@
   		String str2;
   	}
   
  +	static class UserGraph extends Graph
  +	{
  +		public UserGraph()
  +		{
  +		}
  +
  +		UserImmutable immutables[];
  +		
  +		UserImmutable anotherImmutableReference;
  +		
  +		String stringArray[];
  +		
  +		UserImmutable immutableMeantToBeNull;
  +		String stringMeantToBeNull;
  +	}
  +	
  +	static class UserImmutable implements Immutable
  +	{
  +		private static final long serialVersionUID = -8930841034106320740L;
  +		
  +		private int id;
  +		
  +		public int getId()
  +		{
  +			return id;
  +		}
  +		
  +		public UserImmutable(int id)
  +		{
  +			this.id=id;
  +		}
  +
  +		public int hashCode() {
  +			final int PRIME = 31;
  +			int result = 1;
  +			result = PRIME * result + id;
  +			return result;
  +		}
  +
  +		public boolean equals(Object obj) {
  +			if (this == obj)
  +				return true;
  +			if (obj == null)
  +				return false;
  +			if (getClass() != obj.getClass())
  +				return false;
  +			final UserImmutable other = (UserImmutable) obj;
  +			if (id != other.id)
  +				return false;
  +			return true;
  +		}
  +		
  +		public String toString()
  +		{
  +			return "Immutable " + id + " identityHashCode=" + System.identityHashCode(this);
  +		}
  +	}
  +	
  +
   }
  
  
  



More information about the jboss-cvs-commits mailing list