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 import org.apache.commons.jcs.engine.CacheElement;
24 import org.apache.commons.jcs.engine.ElementAttributes;
25 import org.apache.commons.jcs.engine.behavior.ICacheElement;
26 import org.apache.commons.jcs.engine.behavior.ICacheElementSerialized;
27 import org.apache.commons.jcs.engine.behavior.IElementAttributes;
28 import org.apache.commons.jcs.engine.behavior.IElementSerializer;
29
30 import java.io.IOException;
31
32
33
34
35
36
37 public class SerializationConversionUtilUnitTest
38 extends TestCase
39 {
40
41
42
43
44
45 public void testgGetSerializedCacheElement_null()
46 throws IOException
47 {
48
49 IElementSerializer elementSerializer = new StandardSerializer();
50 ICacheElement<String, String> before = null;
51
52
53 ICacheElementSerialized<String, String> result =
54 SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
55
56
57 assertNull( "Should get null for null", result );
58 }
59
60
61
62
63
64
65 public void testgGetDeSerializedCacheElement_null()
66 throws Exception
67 {
68
69 IElementSerializer elementSerializer = new StandardSerializer();
70 ICacheElementSerialized<String, String> before = null;
71
72
73 ICacheElement<String, String> result =
74 SerializationConversionUtil.getDeSerializedCacheElement( before, elementSerializer );
75
76
77 assertNull( "Should get null for null", result );
78 }
79
80
81
82
83
84
85 public void testSimpleConversion()
86 throws Exception
87 {
88
89 String cacheName = "testName";
90 String key = "key";
91 String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
92
93 IElementSerializer elementSerializer = new StandardSerializer();
94
95 IElementAttributes attr = new ElementAttributes();
96 attr.setMaxLife(34);
97
98 ICacheElement<String, String> before = new CacheElement<String, String>( cacheName, key, value );
99 before.setElementAttributes( attr );
100
101
102 ICacheElementSerialized<String, String> serialized =
103 SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
104
105
106 assertNotNull( "Should have a serialized object.", serialized );
107
108
109 ICacheElement<String, String> after =
110 SerializationConversionUtil.getDeSerializedCacheElement( serialized, elementSerializer );
111
112
113 assertNotNull( "Should have a deserialized object.", after );
114 assertEquals( "Values should be the same.", before.getVal(), after.getVal() );
115 assertEquals( "Attributes should be the same.", before.getElementAttributes().getMaxLife(), after
116 .getElementAttributes().getMaxLife() );
117 assertEquals( "Keys should be the same.", before.getKey(), after.getKey() );
118 assertEquals( "Cache name should be the same.", before.getCacheName(), after.getCacheName() );
119 }
120
121
122
123
124
125
126 public void testAccidentalDoubleConversion()
127 throws Exception
128 {
129
130 String cacheName = "testName";
131 String key = "key";
132 String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
133
134 IElementSerializer elementSerializer = new StandardSerializer();
135
136 IElementAttributes attr = new ElementAttributes();
137 attr.setMaxLife(34);
138
139 ICacheElement<String, String> before = new CacheElement<String, String>( cacheName, key, value );
140 before.setElementAttributes( attr );
141
142
143 ICacheElementSerialized<String, String> alreadySerialized =
144 SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
145 ICacheElementSerialized<String, String> serialized =
146 SerializationConversionUtil.getSerializedCacheElement( alreadySerialized, elementSerializer );
147
148
149 assertNotNull( "Should have a serialized object.", serialized );
150
151
152 ICacheElement<String, String> after =
153 SerializationConversionUtil.getDeSerializedCacheElement( serialized, elementSerializer );
154
155
156 assertNotNull( "Should have a deserialized object.", after );
157 assertEquals( "Values should be the same.", before.getVal(), after.getVal() );
158 assertEquals( "Attributes should be the same.", before.getElementAttributes().getMaxLife(), after
159 .getElementAttributes().getMaxLife() );
160 assertEquals( "Keys should be the same.", before.getKey(), after.getKey() );
161 assertEquals( "Cache name should be the same.", before.getCacheName(), after.getCacheName() );
162 }
163
164
165
166
167 public void testNullSerializerConversion()
168 {
169
170 String cacheName = "testName";
171 String key = "key";
172 String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
173
174 IElementSerializer elementSerializer = null;
175
176 IElementAttributes attr = new ElementAttributes();
177 attr.setMaxLife(34);
178
179 ICacheElement<String, String> before = new CacheElement<String, String>( cacheName, key, value );
180 before.setElementAttributes( attr );
181
182
183 try
184 {
185 SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
186
187
188 fail( "We should have received an IOException." );
189 }
190 catch ( IOException e )
191 {
192
193 }
194 }
195 }