View Javadoc
1   package org.apache.commons.jcs3;
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.util.HashMap;
23  import java.util.LinkedList;
24  import java.util.Random;
25  
26  import org.apache.commons.jcs3.access.CacheAccess;
27  
28  import junit.framework.TestCase;
29  
30  /**
31   * Simple test for the JCS class.
32   */
33  public class JCSUnitTest
34      extends TestCase
35  {
36      /** A random for key generation. */
37      Random random = new Random();
38  
39      /**
40       * @throws Exception
41       */
42      public void testJCS()
43          throws Exception
44      {
45          final CacheAccess<String, LinkedList<HashMap<String, String>>> jcs = JCS.getInstance( "testCache1" );
46  
47          final LinkedList<HashMap<String, String>> list = buildList();
48  
49          jcs.put( "some:key", list );
50  
51          assertEquals( list, jcs.get( "some:key" ) );
52      }
53  
54      /**
55       * @return builds a list
56       */
57      private LinkedList<HashMap<String, String>> buildList()
58      {
59          final LinkedList<HashMap<String, String>> list = new LinkedList<>();
60  
61          for ( int i = 0; i < 100; i++ )
62          {
63              list.add( buildMap() );
64          }
65  
66          return list;
67      }
68  
69      /**
70       * @return a map
71       */
72      private HashMap<String, String> buildMap()
73      {
74          final HashMap<String, String> map = new HashMap<>();
75  
76          final byte[] keyBytes = new byte[32];
77          final byte[] valBytes = new byte[128];
78  
79          for ( int i = 0; i < 10; i++ )
80          {
81              random.nextBytes( keyBytes );
82              random.nextBytes( valBytes );
83  
84              map.put( new String( keyBytes ), new String( valBytes ) );
85          }
86  
87          return map;
88      }
89  }