View Javadoc
1   package org.apache.commons.jcs3.utils.serialization;
2   
3   
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * Tests the standard serializer.
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          final StandardSerializer serializer = new StandardSerializer();
42  
43          final String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
44  
45          // DO WORK
46          final 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          final StandardSerializer serializer = new StandardSerializer();
62  
63          final String before = null;
64  
65          // DO WORK
66          final byte[] serialized = serializer.serialize( before );
67          //System.out.println( "testNullInput " + serialized );
68  
69          final 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          final StandardSerializer serializer = new StandardSerializer();
86  
87          final String string = "This is my big string ABCDEFGH";
88          final 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          final String before = sb.toString();
95  
96          // DO WORK
97          final 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 }