View Javadoc
1   package org.apache.commons.jcs;
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.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  import org.apache.commons.jcs.access.CacheAccess;
26  
27  import java.util.HashMap;
28  import java.util.LinkedList;
29  import java.util.Random;
30  
31  /**
32   * Simple test for the JCS class.
33   */
34  public class JCSUnitTest
35      extends TestCase
36  {
37      /** A random for key generation. */
38      Random random = new Random();
39  
40      /**
41       * @param testName
42       */
43      public JCSUnitTest( String testName )
44      {
45          super( testName );
46      }
47  
48      /**
49       * @return Test
50       */
51      public static Test suite()
52      {
53          return new TestSuite( JCSUnitTest.class );
54      }
55  
56      /**
57       * @param args
58       */
59      public static void main( String args[] )
60      {
61          String[] testCaseName = { JCSUnitTest.class.getName() };
62          junit.textui.TestRunner.main( testCaseName );
63      }
64  
65      /**
66       * @throws Exception
67       */
68      public void testJCS()
69          throws Exception
70      {
71          CacheAccess<String, LinkedList<HashMap<String, String>>> jcs = JCS.getInstance( "testCache1" );
72  
73          LinkedList<HashMap<String, String>> list = buildList();
74  
75          jcs.put( "some:key", list );
76  
77          assertEquals( list, jcs.get( "some:key" ) );
78      }
79  
80      /**
81       * @return builds a list
82       */
83      private LinkedList<HashMap<String, String>> buildList()
84      {
85          LinkedList<HashMap<String, String>> list = new LinkedList<HashMap<String,String>>();
86  
87          for ( int i = 0; i < 100; i++ )
88          {
89              list.add( buildMap() );
90          }
91  
92          return list;
93      }
94  
95      /**
96       * @return a map
97       */
98      private HashMap<String, String> buildMap()
99      {
100         HashMap<String, String> map = new HashMap<String, String>();
101 
102         byte[] keyBytes = new byte[32];
103         byte[] valBytes = new byte[128];
104 
105         for ( int i = 0; i < 10; i++ )
106         {
107             random.nextBytes( keyBytes );
108             random.nextBytes( valBytes );
109 
110             map.put( new String( keyBytes ), new String( valBytes ) );
111         }
112 
113         return map;
114     }
115 }