KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > index > hash > HashTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
4  * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
5  * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  */

14
15 package org.apache.activemq.kaha.impl.index.hash;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import junit.framework.TestCase;
20 import org.apache.activemq.kaha.Store;
21 import org.apache.activemq.kaha.impl.index.IndexItem;
22 import org.apache.activemq.kaha.impl.index.IndexManager;
23 import org.apache.activemq.util.IOHelper;
24
25 /**
26  * Test a HashIndex
27  *
28  */

29 public class HashTest extends TestCase{
30
31     private static int COUNT=1000;
32     private HashIndex hashIndex;
33     private File JavaDoc directory;
34     private IndexManager indexManager;
35    
36
37     /**
38      * @throws java.lang.Exception
39      * @see junit.framework.TestCase#setUp()
40      */

41     protected void setUp() throws Exception JavaDoc{
42         super.setUp();
43         directory=new File JavaDoc(IOHelper.getDefaultDataDirectory());
44         directory.mkdirs();
45         indexManager=new IndexManager(directory,"im-hash-test","rw",null);
46         this.hashIndex=new HashIndex(directory,"testHash",indexManager);
47         this.hashIndex.setKeyMarshaller(Store.StringMarshaller);
48     }
49
50    
51     
52     public void testHashIndex() throws Exception JavaDoc{
53         doTest(300);
54         hashIndex.clear();
55         hashIndex.unload();
56         doTest(600);
57         hashIndex.clear();
58         hashIndex.unload();
59         doTest(1024*4);
60     }
61
62     public void doTest(int pageSize) throws Exception JavaDoc{
63         String JavaDoc keyRoot="key:";
64         hashIndex.setPageSize(pageSize);
65         this.hashIndex.load();
66         doInsert(keyRoot);
67         checkRetrieve(keyRoot);
68         doRemove(keyRoot);
69         doInsert(keyRoot);
70         doRemoveBackwards(keyRoot);
71     }
72
73     void doInsert(String JavaDoc keyRoot) throws Exception JavaDoc{
74         for(int i=0;i<COUNT;i++){
75             IndexItem value=indexManager.createNewIndex();
76             indexManager.storeIndex(value);
77             hashIndex.store(keyRoot+i,value);
78            
79         }
80     }
81
82     void checkRetrieve(String JavaDoc keyRoot) throws IOException JavaDoc{
83         for(int i=0;i<COUNT;i++){
84             IndexItem item=(IndexItem)hashIndex.get(keyRoot+i);
85             assertNotNull(item);
86         }
87     }
88
89     void doRemove(String JavaDoc keyRoot) throws Exception JavaDoc{
90         for(int i=0;i<COUNT;i++){
91             hashIndex.remove(keyRoot+i);
92         }
93         for(int i=0;i<COUNT;i++){
94             IndexItem item=(IndexItem)hashIndex.get(keyRoot+i);
95             assertNull(item);
96         }
97     }
98
99     void doRemoveBackwards(String JavaDoc keyRoot) throws Exception JavaDoc{
100         for(int i=COUNT-1;i>=0;i--){
101             hashIndex.remove(keyRoot+i);
102         }
103         for(int i=0;i<COUNT;i++){
104             IndexItem item=(IndexItem)hashIndex.get(keyRoot+i);
105             assertNull(item);
106         }
107     }
108
109     /**
110      * @throws java.lang.Exception
111      * @see junit.framework.TestCase#tearDown()
112      */

113     protected void tearDown() throws Exception JavaDoc{
114         super.tearDown();
115         File JavaDoc[] files=directory.listFiles();
116         for(File JavaDoc file:files){
117             file.delete();
118         }
119     }
120 }
121
Popular Tags