View Javadoc
1   package org.apache.commons.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  import org.apache.commons.jcs.auxiliary.disk.behavior.IDiskCacheAttributes.DiskLimitType;
25  
26  /**
27   * Tests for the keyStore.
28   * <p>
29   *
30   * @author Aaron Smuts
31   */
32  public class BlockDiskCacheKeyStoreUnitTest
33          extends TestCase
34  {
35      /** Directory name */
36      private final String rootDirName = "target/test-sandbox/block";
37  
38      /**
39       * Put a bunch of keys in the key store and verify that they are present.
40       * <p>
41       *
42       * @throws Exception
43       */
44      public void testPutKeys()
45              throws Exception
46      {
47          // SETUP
48          BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
49          attributes.setCacheName("testPutKeys");
50          attributes.setDiskPath(rootDirName);
51          attributes.setMaxKeySize(1000);
52          attributes.setBlockSizeBytes(2000);
53  
54          innerTestPutKeys(attributes);
55      }
56  
57      public void testPutKeysSize()
58              throws Exception
59      {
60          // SETUP
61          BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
62          attributes.setCacheName("testPutKeys");
63          attributes.setDiskPath(rootDirName);
64          attributes.setMaxKeySize(100000);
65          attributes.setBlockSizeBytes(1024);
66          attributes.setDiskLimitType(DiskLimitType.SIZE);
67  
68          innerTestPutKeys(attributes);
69      }
70  
71      private void innerTestPutKeys(BlockDiskCacheAttributes attributes)
72      {
73          BlockDiskCache<String, String> blockDiskCache = new BlockDiskCache<String, String>(attributes);
74          BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<String>(attributes, blockDiskCache);
75  
76          // DO WORK
77          int numElements = 100;
78          for (int i = 0; i < numElements; i++)
79          {
80              keyStore.put(String.valueOf(i), new int[i]);
81          }
82          // System.out.println( "testPutKeys " + keyStore );
83  
84          // VERIFY
85          assertEquals("Wrong number of keys", numElements, keyStore.size());
86          for (int i = 0; i < numElements; i++)
87          {
88              int[] result = keyStore.get(String.valueOf(i));
89              assertEquals("Wrong array returned.", i, result.length);
90          }
91      }
92  
93      /**
94       * Verify that we can load keys that we saved. Add a bunch. Save them. Clear
95       * the memory key hash. Load the keys. Verify.
96       * <p>
97       *
98       * @throws Exception
99       */
100     public void testSaveLoadKeys()
101             throws Exception
102     {
103         // SETUP
104         BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
105         attributes.setCacheName("testSaveLoadKeys");
106         attributes.setDiskPath(rootDirName);
107         attributes.setMaxKeySize(10000);
108         attributes.setBlockSizeBytes(2000);
109 
110         testSaveLoadKeysInner(attributes);
111     }
112 
113     public void testSaveLoadKeysSize()
114             throws Exception
115     {
116         // SETUP
117         BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
118         attributes.setCacheName("testSaveLoadKeys");
119         attributes.setDiskPath(rootDirName);
120         attributes.setMaxKeySize(10000);
121         attributes.setBlockSizeBytes(2000);
122 
123         testSaveLoadKeysInner(attributes);
124     }
125 
126     private void testSaveLoadKeysInner(BlockDiskCacheAttributes attributes)
127     {
128         BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<String>(attributes, null);
129 
130         // DO WORK
131         int numElements = 1000;
132         int blockIndex = 0;
133         // Random random = new Random( 89 );
134         for (int i = 0; i < numElements; i++)
135         {
136             int blocks = i;// random.nextInt( 10 );
137 
138             // fill with reasonable data to make verify() happy
139             int[] block1 = new int[blocks];
140             int[] block2 = new int[blocks];
141             for (int j = 0; j < blocks; j++)
142             {
143                 block1[j] = blockIndex++;
144                 block2[j] = blockIndex++;
145             }
146             keyStore.put(String.valueOf(i), block1);
147             keyStore.put(String.valueOf(i), block2);
148         }
149         // System.out.println( "testSaveLoadKeys " + keyStore );
150 
151         // VERIFY
152         assertEquals("Wrong number of keys", numElements, keyStore.size());
153 
154         // DO WORK
155         keyStore.saveKeys();
156         keyStore.clearMemoryMap();
157 
158         // VERIFY
159         assertEquals("Wrong number of keys after clearing memory", 0, keyStore.size());
160 
161         // DO WORK
162         keyStore.loadKeys();
163 
164         // VERIFY
165         assertEquals("Wrong number of keys after loading", numElements, keyStore.size());
166         for (int i = 0; i < numElements; i++)
167         {
168             int[] result = keyStore.get(String.valueOf(i));
169             assertEquals("Wrong array returned.", i, result.length);
170         }
171     }
172 
173     public void testObjectLargerThanMaxSize()
174     {
175         BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
176         attributes.setCacheName("testObjectLargerThanMaxSize");
177         attributes.setDiskPath(rootDirName);
178         attributes.setMaxKeySize(1000);
179         attributes.setBlockSizeBytes(2000);
180         attributes.setDiskLimitType(DiskLimitType.SIZE);
181 
182         @SuppressWarnings({ "unchecked", "rawtypes" })
183         BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<String>(attributes, new BlockDiskCache(attributes));
184 
185         keyStore.put("1", new int[1000]);
186         keyStore.put("2", new int[1000]);
187         assertNull(keyStore.get("1"));
188         assertNotNull(keyStore.get("2"));
189     }
190 }