View Javadoc
1   package org.apache.commons.jcs.utils.serialization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  
24  /**
25   * Tests the standard serializer.
26   *<p>
27   * @author Aaron Smuts
28   */
29  public class StandardSerializerUnitTest
30      extends TestCase
31  {
32      /**
33       * Test simple back and forth with a string.
34       *<p>
35       * @throws Exception
36       */
37      public void testSimpleBackAndForth()
38          throws Exception
39      {
40          // SETUP
41          StandardSerializer serializer = new StandardSerializer();
42  
43          String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
44  
45          // DO WORK
46          String after = (String) serializer.deSerialize( serializer.serialize( before ), null );
47  
48          // VERIFY
49          assertEquals( "Before and after should be the same.", before, after );
50      }
51  
52      /**
53       * Test serialization with a null object. Verify that we don't get an error.
54       *<p>
55       * @throws Exception
56       */
57      public void testNullInput()
58          throws Exception
59      {
60          // SETUP
61          StandardSerializer serializer = new StandardSerializer();
62  
63          String before = null;
64  
65          // DO WORK
66          byte[] serialized = serializer.serialize( before );
67          //System.out.println( "testNullInput " + serialized );
68  
69          String after = (String) serializer.deSerialize( serialized, null );
70          //System.out.println( "testNullInput " + after );
71  
72          // VERIFY
73          assertNull( "Should have nothing.", after );
74      }
75  
76      /**
77       * Test simple back and forth with a string.
78       *<p>
79       * @throws Exception
80       */
81      public void testBigStringBackAndForth()
82          throws Exception
83      {
84          // SETUP
85          StandardSerializer serializer = new StandardSerializer();
86  
87          String string = "This is my big string ABCDEFGH";
88          StringBuilder sb = new StringBuilder();
89          sb.append( string );
90          for ( int i = 0; i < 4; i++ )
91          {
92              sb.append( " " + i + sb.toString() ); // big string
93          }
94          String before = sb.toString();
95  
96          // DO WORK
97          String after = (String) serializer.deSerialize( serializer.serialize( before ), null );
98  
99          // VERIFY
100         assertEquals( "Before and after should be the same.", before, after );
101     }
102 }