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  import java.io.IOException;
25  
26  /**
27   * Tests the compressing serializer.
28   */
29  public class CompressingSerializerUnitTest
30      extends TestCase
31  {
32      /**
33       * Verify that we don't get any erorrs for null input.
34       * <p>
35       * @throws ClassNotFoundException
36       * @throws IOException
37       */
38      public void testDeserialize_NullInput()
39          throws IOException, ClassNotFoundException
40      {
41          // SETUP
42          CompressingSerializer serializer = new CompressingSerializer();
43  
44          // DO WORK
45          Object result = serializer.deSerialize( null, null );
46  
47          // VERIFY
48          assertNull( "Should have nothing.", result );
49      }
50  
51      /**
52       * Test simple back and forth with a string.
53       * <p>
54       * ))&lt;=&gt;((
55       * <p>
56       * @throws Exception on error
57       */
58      public void testSimpleBackAndForth()
59          throws Exception
60      {
61          // SETUP
62          CompressingSerializer serializer = new CompressingSerializer();
63  
64          // DO WORK
65          String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
66          String after = (String) serializer.deSerialize( serializer.serialize( before ), null );
67  
68          // VERIFY
69          assertEquals( "Before and after should be the same.", before, after );
70      }
71  
72      /**
73       * Test serialization with a null object. Verify that we don't get an error.
74       * <p>
75       * @throws Exception on error
76       */
77      public void testSerialize_NullInput()
78          throws Exception
79      {
80          // SETUP
81          CompressingSerializer serializer = new CompressingSerializer();
82  
83          String before = null;
84  
85          // DO WORK
86          byte[] serialized = serializer.serialize( before );
87          String after = (String) serializer.deSerialize( serialized, null );
88  
89          // VERIFY
90          assertNull( "Should have nothing. after =" + after, after );
91      }
92  
93      /**
94       * Verify that the compressed is smaller.
95       * <p>
96       * @throws Exception on error
97       */
98      public void testSerialize_CompareCompressedAndUncompressed()
99          throws Exception
100     {
101         // SETUP
102         CompressingSerializer serializer = new CompressingSerializer();
103 
104         // I hate for loops.
105         String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdssaf dsaf sadf dsaf dsaf dsaf "
106             + "dsafdsa fdsaf dsaf dsafdsa dsaf dsaf dsaf dsaf dsafdsa76f dsa798f dsa6fdsa 087f  "
107             + "gh 987dsahb dsahbuhbfnui nufdsa hbv87 f8vhdsgbnfv h8fdg8dfjvn8fdwgj fdsgjb9fdsjbv"
108             + "jvhjv hg98f-dsaghj j9fdsb gfsb 9fdshjbgb987fdsbfdwgh ujbhjbhb hbfdsgh fdshb "
109             + "Ofdsgyfesgyfdsafdsafsa333 31231";
110 
111         // DO WORK
112         byte[] compressed = serializer.serialize( before );
113         byte[] nonCompressed = serializer.serializeObject( before );
114 
115         // VERIFY
116         assertTrue( "Compressed should be smaller. compressed size = " + compressed.length + "nonCompressed size = "
117             + nonCompressed.length, compressed.length < nonCompressed.length );
118     }
119 }