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