View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.block;
2   
3   import org.apache.commons.jcs3.auxiliary.disk.behavior.IDiskCacheAttributes.DiskLimitType;
4   
5   
6   /*
7    * Licensed to the Apache Software Foundation (ASF) under one
8    * or more contributor license agreements.  See the NOTICE file
9    * distributed with this work for additional information
10   * regarding copyright ownership.  The ASF licenses this file
11   * to you under the Apache License, Version 2.0 (the
12   * "License"); you may not use this file except in compliance
13   * with the License.  You may obtain a copy of the License at
14   *
15   *   http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing,
18   * software distributed under the License is distributed on an
19   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20   * KIND, either express or implied.  See the License for the
21   * specific language governing permissions and limitations
22   * under the License.
23   */
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * Tests for the keyStore.
29   */
30  public class BlockDiskCacheKeyStoreUnitTest
31          extends TestCase
32  {
33      /** Directory name */
34      private final String rootDirName = "target/test-sandbox/block";
35  
36      /**
37       * Put a bunch of keys in the key store and verify that they are present.
38       *
39       * @throws Exception
40       */
41      public void testPutKeys()
42              throws Exception
43      {
44          // SETUP
45          final BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
46          attributes.setCacheName("testPutKeys");
47          attributes.setDiskPath(rootDirName);
48          attributes.setMaxKeySize(1000);
49          attributes.setBlockSizeBytes(2000);
50  
51          innerTestPutKeys(attributes);
52      }
53  
54      public void testPutKeysSize()
55              throws Exception
56      {
57          // SETUP
58          final BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
59          attributes.setCacheName("testPutKeys");
60          attributes.setDiskPath(rootDirName);
61          attributes.setMaxKeySize(100000);
62          attributes.setBlockSizeBytes(1024);
63          attributes.setDiskLimitType(DiskLimitType.SIZE);
64  
65          innerTestPutKeys(attributes);
66      }
67  
68      private void innerTestPutKeys(final BlockDiskCacheAttributes attributes)
69      {
70          final BlockDiskCache<String, String> blockDiskCache = new BlockDiskCache<>(attributes);
71          final BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<>(attributes, blockDiskCache);
72  
73          // DO WORK
74          final int numElements = 100;
75          for (int i = 0; i < numElements; i++)
76          {
77              keyStore.put(String.valueOf(i), new int[i]);
78          }
79          // System.out.println( "testPutKeys " + keyStore );
80  
81          // VERIFY
82          assertEquals("Wrong number of keys", numElements, keyStore.size());
83          for (int i = 0; i < numElements; i++)
84          {
85              final int[] result = keyStore.get(String.valueOf(i));
86              assertEquals("Wrong array returned.", i, result.length);
87          }
88      }
89  
90      /**
91       * Verify that we can load keys that we saved. Add a bunch. Save them. Clear
92       * the memory key hash. Load the keys. Verify.
93       * <p>
94       *
95       * @throws Exception
96       */
97      public void testSaveLoadKeys()
98              throws Exception
99      {
100         // SETUP
101         final BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
102         attributes.setCacheName("testSaveLoadKeys");
103         attributes.setDiskPath(rootDirName);
104         attributes.setMaxKeySize(10000);
105         attributes.setBlockSizeBytes(2000);
106 
107         testSaveLoadKeysInner(attributes);
108     }
109 
110     public void testSaveLoadKeysSize()
111             throws Exception
112     {
113         // SETUP
114         final BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
115         attributes.setCacheName("testSaveLoadKeys");
116         attributes.setDiskPath(rootDirName);
117         attributes.setMaxKeySize(10000);
118         attributes.setBlockSizeBytes(2000);
119 
120         testSaveLoadKeysInner(attributes);
121     }
122 
123     private void testSaveLoadKeysInner(final BlockDiskCacheAttributes attributes)
124     {
125         final BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<>(attributes, null);
126 
127         // DO WORK
128         final int numElements = 1000;
129         int blockIndex = 0;
130         // Random random = new Random( 89 );
131         for (int i = 0; i < numElements; i++)
132         {
133             final int blocks = i;// random.nextInt( 10 );
134 
135             // fill with reasonable data to make verify() happy
136             final int[] block1 = new int[blocks];
137             final int[] block2 = new int[blocks];
138             for (int j = 0; j < blocks; j++)
139             {
140                 block1[j] = blockIndex++;
141                 block2[j] = blockIndex++;
142             }
143             keyStore.put(String.valueOf(i), block1);
144             keyStore.put(String.valueOf(i), block2);
145         }
146         // System.out.println( "testSaveLoadKeys " + keyStore );
147 
148         // VERIFY
149         assertEquals("Wrong number of keys", numElements, keyStore.size());
150 
151         // DO WORK
152         keyStore.saveKeys();
153         keyStore.clearMemoryMap();
154 
155         // VERIFY
156         assertEquals("Wrong number of keys after clearing memory", 0, keyStore.size());
157 
158         // DO WORK
159         keyStore.loadKeys();
160 
161         // VERIFY
162         assertEquals("Wrong number of keys after loading", numElements, keyStore.size());
163         for (int i = 0; i < numElements; i++)
164         {
165             final int[] result = keyStore.get(String.valueOf(i));
166             assertEquals("Wrong array returned.", i, result.length);
167         }
168     }
169 
170     public void testObjectLargerThanMaxSize()
171     {
172         final BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
173         attributes.setCacheName("testObjectLargerThanMaxSize");
174         attributes.setDiskPath(rootDirName);
175         attributes.setMaxKeySize(1000);
176         attributes.setBlockSizeBytes(2000);
177         attributes.setDiskLimitType(DiskLimitType.SIZE);
178 
179         @SuppressWarnings({ "unchecked", "rawtypes" })
180         final
181         BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<>(attributes, new BlockDiskCache(attributes));
182 
183         keyStore.put("1", new int[1000]);
184         keyStore.put("2", new int[1000]);
185         assertNull(keyStore.get("1"));
186         assertNotNull(keyStore.get("2"));
187     }
188 }