1   package org.apache.jcs.auxiliary.disk.block;
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  /***
25   * Tests for the keyStore.
26   * <p>
27   * @author Aaron Smuts
28   */
29  public class BlockDiskCacheKeyStoreUnitTest
30      extends TestCase
31  {
32      /*** Directory name */
33      private String rootDirName = "target/test-sandbox/block";
34  
35      /***
36       * Put a bunch of keys inthe key store and verify that they are present.
37       * <p>
38       * @throws Exception
39       */
40      public void testPutKeys()
41          throws Exception
42      {
43          // SETUP
44          String regionName = "testPutKeys";
45          int maxKeys = 1000;
46          int bytesPerBlock = 2000;
47  
48          BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
49          attributes.setCacheName( regionName );
50          attributes.setDiskPath( rootDirName );
51          attributes.setMaxKeySize( maxKeys );
52          attributes.setBlockSizeBytes( bytesPerBlock );
53  
54          BlockDiskCache blockDiskCache = new BlockDiskCache( attributes );
55  
56          BlockDiskKeyStore keyStore = new BlockDiskKeyStore( attributes, blockDiskCache );
57  
58          // DO WORK
59          int numElements = 100;
60          for ( int i = 0; i < numElements; i++ )
61          {
62              keyStore.put( String.valueOf( i ), new int[i] );
63          }
64          System.out.println( "testPutKeys " + keyStore );
65  
66          // VERIFY
67          assertEquals( "Wrong number of keys", numElements, keyStore.size() );
68          for ( int i = 0; i < numElements; i++ )
69          {
70              int[] result = keyStore.get( String.valueOf( i ) );
71              assertEquals( "Wrong array returned.", i, result.length );
72          }
73      }
74  
75      /***
76       * Verify that we can load keys that we saved. Add a bunch. Save them. Clear the memory keyhash.
77       * Load the keys. Verify.
78       * <p>
79       * @throws Exception
80       */
81      public void testSaveLoadKeys()
82          throws Exception
83      {
84          // SETUP
85          String regionName = "testSaveLoadKeys";
86          int maxKeys = 10000;
87          int bytesPerBlock = 2000;
88  
89          BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
90          attributes.setCacheName( regionName );
91          attributes.setDiskPath( rootDirName );
92          attributes.setMaxKeySize( maxKeys );
93          attributes.setBlockSizeBytes( bytesPerBlock );
94  
95          BlockDiskCache blockDiskCache = new BlockDiskCache( attributes );
96  
97          BlockDiskKeyStore keyStore = new BlockDiskKeyStore( attributes, blockDiskCache );
98  
99          // DO WORK
100         int numElements = 1000;
101         //Random random = new Random( 89 );
102         for ( int i = 0; i < numElements; i++ )
103         {
104             int blocks = i;//random.nextInt( 10 );
105             keyStore.put( String.valueOf( i ), new int[blocks] );
106             keyStore.put( String.valueOf( i ), new int[i] );
107         }
108         System.out.println( "testSaveLoadKeys " + keyStore );
109 
110         // VERIFY
111         assertEquals( "Wrong number of keys", numElements, keyStore.size() );
112 
113         // DO WORK
114         keyStore.saveKeys();
115         keyStore.clearMemoryMap();
116 
117         // VERIFY
118         assertEquals( "Wrong number of keys after clearing memory", 0, keyStore.size() );
119 
120         // DO WORK
121         keyStore.loadKeys();
122 
123         // VERIFY
124         assertEquals( "Wrong number of keys after loading", numElements, keyStore.size() );
125         for ( int i = 0; i < numElements; i++ )
126         {
127             int[] result = keyStore.get( String.valueOf( i ) );
128             assertEquals( "Wrong array returned.", i, result.length );
129         }
130     }
131 }