KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > store > impl > ClearJispFilesystemStoreTestCase


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

17 package org.apache.excalibur.store.impl;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.apache.avalon.framework.logger.ConsoleLogger;
25 import org.apache.avalon.framework.logger.Logger;
26 import org.apache.avalon.framework.logger.NullLogger;
27 import org.apache.avalon.framework.parameters.Parameters;
28
29 /**
30  * This TestCase fills a Jisp store with <code>MAX_ENTRIES</code>
31  * and then tests the remove() and clear() methods.
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  */

35 public class ClearJispFilesystemStoreTestCase extends TestCase {
36     
37     /** permanent Jisp store */
38     private JispFilesystemStore m_store;
39     
40     /** logger for this test */
41     private final Logger m_logger =
42         new ConsoleLogger(ConsoleLogger.LEVEL_DEBUG);
43     
44     /** max entries to fill the store */
45     private int MAX_ENTRIES = 100;
46     
47     /** temp dir for this test */
48     private File JavaDoc m_tempDir;
49     
50     /**
51      * Set up the the jisp database before running each test
52      */

53     public void setUp() throws Exception JavaDoc {
54         m_logger.debug("-BGN--> setUp() fixture for " + getName());
55         
56         m_tempDir = File.createTempFile("jisp", "test");
57         m_tempDir.delete();
58         m_tempDir.mkdir();
59         
60         m_store = new JispFilesystemStore();
61         
62         //enable logging
63
m_store.enableLogging(new NullLogger());
64         
65         //parameters
66
final Parameters params = new Parameters();
67         params.setParameter("directory", m_tempDir.toString());
68         params.makeReadOnly();
69         
70         //parameterize it
71
m_store.parameterize(params);
72         
73         //fill the store
74

75         fillStore();
76         
77         m_logger.debug("-END--> setUp() fixture for " + getName());
78         
79     }
80     
81     /**
82      * Fills the store
83      * @throws IOException
84      */

85     private void fillStore() throws IOException JavaDoc {
86         String JavaDoc key = null;
87         String JavaDoc value = null;
88         m_logger.debug("filling the database...");
89         for (int i = 0; i < MAX_ENTRIES; i++) {
90             key = "key" + i;
91             value = "value" + i;
92             m_store.store(key, value);
93         }
94         m_logger.debug("filling the database...OK");
95     }
96     
97     /**
98      * Test remove() on <code>JispFilesystemStore</code>
99      * This test succeeds if the a removed item points to null.
100      */

101     public void testRemove() {
102         m_logger.debug("-BGN--> testRemove()");
103         //get a key to remove
104
final String JavaDoc key = "key" + (MAX_ENTRIES - 10);
105         m_logger.debug("removing key: " + key);
106         m_store.remove(key);
107         // check that the item for the removed key is null
108
Object JavaDoc item = m_store.get(key);
109         if (item != null) {
110             assertTrue("The store item for key=" + key + " has value of " + item.toString(), item == null);
111         } else {
112             assertTrue(item == null);
113         }
114         m_logger.debug("-END--> testRemove()");
115         
116     }
117     
118     /**
119      * Test clear() on <code>JispFilesystemStore</code>
120      * This test succeeds if the store size after cleaning is 0.
121      * @throws Exception
122      */

123     public void testClear() throws Exception JavaDoc {
124         m_logger.debug("-BGN--> testClear()");
125         final int sizeBefore = m_store.size();
126         m_logger.debug("store size before clear:" + sizeBefore);
127         m_logger.debug("index count before clear:" + m_store.m_Index.count());
128         m_store.clear();
129         final int sizeAfter = m_store.size();
130         m_logger.debug("store size after clear:" + sizeAfter);
131         m_logger.debug("index count after clear:" + m_store.m_Index.count());
132         assertTrue(sizeAfter == 0);
133         m_logger.debug("-END--> testClear()");
134     }
135     
136     /**
137      * Clean the resources after running each test
138      */

139     protected void tearDown() throws Exception JavaDoc {
140         m_logger.debug("-BGN--> tearUp() fixture for test " + getName());
141         m_logger.debug("deleting index and database");
142         deleteAll(m_tempDir);
143         m_logger.debug("-END--> tearUp() fixture for test " + getName());
144     }
145     
146     /**
147      * Deletes files in directory recursively
148      * @param f
149      */

150     private void deleteAll(File JavaDoc f) {
151         if (f.isDirectory()) {
152             File JavaDoc[] children = f.listFiles();
153             for (int i = 0; i < children.length; i++) {
154                 deleteAll(children[i]);
155             }
156         }
157         
158         f.delete();
159     }
160 }
161
162
163
Popular Tags