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