View Javadoc
1   package org.apache.commons.jcs3.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  
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   * Tests the serialization conversion util.
35   */
36  public class SerializationConversionUtilUnitTest
37      extends TestCase
38  {
39      /**
40       * Verify null for null.
41       * <p>
42       * @throws IOException
43       */
44      public void testgGetSerializedCacheElement_null()
45          throws IOException
46      {
47          // SETUP
48          final IElementSerializer elementSerializer = new StandardSerializer();
49          final ICacheElement<String, String> before = null;
50  
51          // DO WORK
52          final ICacheElementSerialized<String, String> result =
53              SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
54  
55          // VERIFY
56          assertNull( "Should get null for null", result );
57      }
58  
59      /**
60       * Verify null for null.
61       * <p>
62       * @throws Exception
63       */
64      public void testgGetDeSerializedCacheElement_null()
65          throws Exception
66      {
67          // SETUP
68          final IElementSerializer elementSerializer = new StandardSerializer();
69          final ICacheElementSerialized<String, String> before = null;
70  
71          // DO WORK
72          final ICacheElement<String, String> result =
73              SerializationConversionUtil.getDeSerializedCacheElement( before, elementSerializer );
74  
75          // VERIFY
76          assertNull( "Should get null for null", result );
77      }
78  
79      /**
80       * Verify that we can go back and forth with the simplest of objects.
81       * <p>
82       * @throws Exception
83       */
84      public void testSimpleConversion()
85          throws Exception
86      {
87          // SETUP
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         // DO WORK
101         final ICacheElementSerialized<String, String> serialized =
102             SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
103 
104         // VERIFY
105         assertNotNull( "Should have a serialized object.", serialized );
106 
107         // DO WORK
108         final ICacheElement<String, String> after =
109             SerializationConversionUtil.getDeSerializedCacheElement( serialized, elementSerializer );
110 
111         // VERIFY
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      * Verify that we can go back and forth with the simplest of objects.
122      *<p>
123      * @throws Exception
124      */
125     public void testAccidentalDoubleConversion()
126         throws Exception
127     {
128         // SETUP
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         // DO WORK
142         final ICacheElementSerialized<String, String> alreadySerialized =
143             SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
144         final ICacheElementSerialized<String, String> serialized =
145             SerializationConversionUtil.getSerializedCacheElement( alreadySerialized, elementSerializer );
146 
147         // VERIFY
148         assertNotNull( "Should have a serialized object.", serialized );
149 
150         // DO WORK
151         final ICacheElement<String, String> after =
152             SerializationConversionUtil.getDeSerializedCacheElement( serialized, elementSerializer );
153 
154         // VERIFY
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      * Verify that we get an IOException for a null serializer.
165      */
166     public void testNullSerializerConversion()
167     {
168         // SETUP
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;// new StandardSerializer();
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         // DO WORK
182         try
183         {
184             SerializationConversionUtil.getSerializedCacheElement( before, elementSerializer );
185 
186             // VERIFY
187             fail( "We should have received an IOException." );
188         }
189         catch ( final IOException e )
190         {
191             // expected
192         }
193     }
194 }