View Javadoc
1   package org.apache.commons.jcs.utils.serialization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Tests the serialization conversion util.
34   * <p>
35   * @author Aaron Smuts
36   */
37  public class SerializationConversionUtilUnitTest
38      extends TestCase
39  {
40      /**
41       * Verify null for null.
42       * <p>
43       * @throws IOException
44       */
45      public void testgGetSerializedCacheElement_null()
46          throws IOException
47      {
48          // SETUP
49          IElementSerializer elementSerializer = new StandardSerializer();
50          ICacheElement<String, String> before = null;
51  
52          // DO WORK
53          ICacheElementSerialized<String, String> result =
54              SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
55  
56          // VERIFY
57          assertNull( "Should get null for null", result );
58      }
59  
60      /**
61       * Verify null for null.
62       * <p>
63       * @throws Exception
64       */
65      public void testgGetDeSerializedCacheElement_null()
66          throws Exception
67      {
68          // SETUP
69          IElementSerializer elementSerializer = new StandardSerializer();
70          ICacheElementSerialized<String, String> before = null;
71  
72          // DO WORK
73          ICacheElement<String, String> result =
74              SerializationConversionUtil.getDeSerializedCacheElement( before, elementSerializer );
75  
76          // VERIFY
77          assertNull( "Should get null for null", result );
78      }
79  
80      /**
81       * Verify that we can go back and forth with the simplest of objects.
82       * <p>
83       * @throws Exception
84       */
85      public void testSimpleConversion()
86          throws Exception
87      {
88          // SETUP
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         // DO WORK
102         ICacheElementSerialized<String, String> serialized =
103             SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
104 
105         // VERIFY
106         assertNotNull( "Should have a serialized object.", serialized );
107 
108         // DO WORK
109         ICacheElement<String, String> after =
110             SerializationConversionUtil.getDeSerializedCacheElement( serialized, elementSerializer );
111 
112         // VERIFY
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      * Verify that we can go back and forth with the simplest of objects.
123      *<p>
124      * @throws Exception
125      */
126     public void testAccidentalDoubleConversion()
127         throws Exception
128     {
129         // SETUP
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         // DO WORK
143         ICacheElementSerialized<String, String> alreadySerialized =
144             SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
145         ICacheElementSerialized<String, String> serialized =
146             SerializationConversionUtil.getSerializedCacheElement( alreadySerialized, elementSerializer );
147 
148         // VERIFY
149         assertNotNull( "Should have a serialized object.", serialized );
150 
151         // DO WORK
152         ICacheElement<String, String> after =
153             SerializationConversionUtil.getDeSerializedCacheElement( serialized, elementSerializer );
154 
155         // VERIFY
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      * Verify that we get an IOException for a null serializer.
166      */
167     public void testNullSerializerConversion()
168     {
169         // SETUP
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;// new StandardSerializer();
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         // DO WORK
183         try
184         {
185             SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
186 
187             // VERIFY
188             fail( "We should have received an IOException." );
189         }
190         catch ( IOException e )
191         {
192             // expected
193         }
194     }
195 }