1   package org.apache.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 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;// new StandardSerializer();
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             // expected
106         }
107 
108     }
109 
110 }