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