1 package org.apache.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 java.io.IOException;
23
24 import junit.framework.TestCase;
25
26 import org.apache.jcs.engine.CacheElement;
27 import org.apache.jcs.engine.ElementAttributes;
28 import org.apache.jcs.engine.behavior.ICacheElement;
29 import org.apache.jcs.engine.behavior.ICacheElementSerialized;
30 import org.apache.jcs.engine.behavior.IElementAttributes;
31 import org.apache.jcs.engine.behavior.IElementSerializer;
32
33 /***
34 * Tests the serialization conversion util.
35 *
36 * @author Aaron Smuts
37 *
38 */
39 public class SerializationConversionUtilUnitTest
40 extends TestCase
41 {
42
43 /***
44 * Verify that we can go back and forth with the simplest of objects.
45 *
46 * @throws Exception
47 */
48 public void testSimpleConversion()
49 throws Exception
50 {
51 String cacheName = "testName";
52 String key = "key";
53 String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
54
55 IElementSerializer elementSerializer = new StandardSerializer();
56
57 IElementAttributes attr = new ElementAttributes();
58 attr.setMaxLifeSeconds( 34 );
59
60 ICacheElement before = new CacheElement( cacheName, key, value );
61 before.setElementAttributes( attr );
62
63 ICacheElementSerialized serialized = SerializationConversionUtil.getSerializedCacheElement( before,
64 elementSerializer );
65 assertNotNull( "Should have a serialized object.", serialized );
66 System.out.println( "testSimpleConversion, " + serialized );
67
68 ICacheElement after = SerializationConversionUtil.getDeSerializedCacheElement( serialized, elementSerializer );
69
70 assertNotNull( "Should have a deserialized object.", after );
71 assertEquals( "Values should be the same.", before.getVal(), after.getVal() );
72 assertEquals( "Attributes should be the same.", before.getElementAttributes().getMaxLifeSeconds(), after
73 .getElementAttributes().getMaxLifeSeconds() );
74 assertEquals( "Keys should be the same.", before.getKey(), after.getKey() );
75 assertEquals( "Cache name should be the same.", before.getCacheName(), after.getCacheName() );
76
77 }
78
79 /***
80 * Verify that we get an IOException for a null serializer.
81 *
82 * @throws Exception
83 */
84 public void testNullSerializerConversion()
85 {
86 String cacheName = "testName";
87 String key = "key";
88 String value = "value fdsadf dsafdsa fdsaf dsafdsaf dsafdsaf dsaf dsaf dsaf dsafa dsaf dsaf dsafdsaf";
89
90 IElementSerializer elementSerializer = null;
91
92 IElementAttributes attr = new ElementAttributes();
93 attr.setMaxLifeSeconds( 34 );
94
95 ICacheElement before = new CacheElement( cacheName, key, value );
96 before.setElementAttributes( attr );
97
98 try
99 {
100 SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
101 fail( "We should have received an IOException." );
102 }
103 catch ( IOException e )
104 {
105
106 }
107
108 }
109
110 }