1 package org.apache.commons.jcs3.utils.serialization;
2
3 import java.io.IOException;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import junit.framework.TestCase;
25
26
27
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
42
43
44
45
46 public void testDeserialize_NullInput()
47 throws IOException, ClassNotFoundException
48 {
49
50 final Object result = serializer.deSerialize( null, null );
51
52
53 assertNull( "Should have nothing.", result );
54 }
55
56
57
58
59
60
61
62
63 public void testSimpleBackAndForth()
64 throws Exception
65 {
66
67 final String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231";
68 final String after = (String) serializer.deSerialize( serializer.serialize( before ), null );
69
70
71 assertEquals( "Before and after should be the same.", before, after );
72 }
73
74
75
76
77
78
79 public void testSerialize_NullInput()
80 throws Exception
81 {
82 final String before = null;
83
84
85 final byte[] serialized = serializer.serialize( before );
86 final String after = (String) serializer.deSerialize( serialized, null );
87
88
89 assertNull( "Should have nothing. after =" + after, after );
90 }
91
92
93
94
95
96
97 public void testSerialize_CompareCompressedAndUncompressed()
98 throws Exception
99 {
100
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
108 final byte[] compressed = serializer.serialize( before );
109 final byte[] nonCompressed = new StandardSerializer().serialize( before );
110
111
112 assertTrue( "Compressed should be smaller. compressed size = " + compressed.length + "nonCompressed size = "
113 + nonCompressed.length, compressed.length < nonCompressed.length );
114 }
115 }