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 org.apache.commons.jcs3.JCS;
23  import org.apache.commons.jcs3.access.CacheAccess;
24  
25  /*
26   * Licensed to the Apache Software Foundation (ASF) under one
27   * or more contributor license agreements.  See the NOTICE file
28   * distributed with this work for additional information
29   * regarding copyright ownership.  The ASF licenses this file
30   * to you under the Apache License, Version 2.0 (the
31   * "License"); you may not use this file except in compliance
32   * with the License.  You may obtain a copy of the License at
33   *
34   *   http://www.apache.org/licenses/LICENSE-2.0
35   *
36   * Unless required by applicable law or agreed to in writing,
37   * software distributed under the License is distributed on an
38   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
39   * KIND, either express or implied.  See the License for the
40   * specific language governing permissions and limitations
41   * under the License.
42   */
43  
44  import junit.framework.TestCase;
45  
46  /**
47   * Verify that serializer functionality works.
48   */
49  public class SerializerUnitTest
50      extends TestCase
51  {
52      /**
53       * Test setup
54       * <p>
55       * @throws Exception
56       */
57      @Override
58      public void setUp()
59          throws Exception
60      {
61          JCS.setConfigFilename( "/TestElementSerializer.ccf" );
62      }
63  
64      @Override
65      protected void tearDown() throws Exception
66      {
67          JCS.shutdown();
68      }
69  
70      /**
71       * Verify that object reading and writing works
72       * <p>
73       * @throws Exception
74       */
75      public void testReadWrite()
76          throws Exception
77      {
78          final int count = 500; // 100 fit in memory
79          // CompressingSerializer
80          final CacheAccess<String, String> jcs1 = JCS.getInstance( "blockRegion1" );
81  
82          for ( int i = 0; i < count; i++ )
83          {
84              jcs1.put( "key:" + i, "data" + i );
85          }
86  
87          for ( int i = 0; i < count; i++ )
88          {
89              final String res = jcs1.get( "key:" + i );
90              assertNotNull( "[key:" + i + "] should not be null, " + jcs1.getStats(), res );
91          }
92  
93          // EncryptingSerializer
94          final CacheAccess<String, String> jcs2 = JCS.getInstance( "blockRegion2" );
95  
96          for ( int i = 0; i < count; i++ )
97          {
98              jcs2.put( "key:" + i, "data" + i );
99          }
100 
101         for ( int i = 0; i < count; i++ )
102         {
103             final String res = jcs2.get( "key:" + i );
104             assertNotNull( "[key:" + i + "] should not be null, " + jcs2.getStats(), res );
105         }
106 
107         JCS.shutdown();
108 
109         // Re-init
110         // EncryptingSerializer
111         final CacheAccess<String, String> jcs3 = JCS.getInstance( "blockRegion2" );
112 
113         for ( int i = 0; i < count; i++ )
114         {
115             final String res = jcs3.get( "key:" + i );
116             assertNotNull( "[key:" + i + "] should not be null, " + jcs3.getStats(), res );
117         }
118     }
119 }