01: /* CachedBdbMapTest
02: *
03: * Created on Apr 11, 2005
04: *
05: * Copyright (C) 2005 Internet Archive.
06: *
07: * This file is part of the Heritrix web crawler (crawler.archive.org).
08: *
09: * Heritrix is free software; you can redistribute it and/or modify
10: * it under the terms of the GNU Lesser Public License as published by
11: * the Free Software Foundation; either version 2.1 of the License, or
12: * any later version.
13: *
14: * Heritrix is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * GNU Lesser Public License for more details.
18: *
19: * You should have received a copy of the GNU Lesser Public License
20: * along with Heritrix; if not, write to the Free Software
21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: */
23: package org.archive.util;
24:
25: import java.io.File;
26: import java.util.HashMap;
27: import java.util.logging.Handler;
28: import java.util.logging.Level;
29: import java.util.logging.Logger;
30:
31: /**
32: * @author stack
33: * @version $Date: 2007-02-22 01:15:06 +0000 (Thu, 22 Feb 2007) $, $Revision: 4932 $
34: */
35: public class CachedBdbMapTest extends TmpDirTestCase {
36: File envDir;
37: private CachedBdbMap<String, HashMap<String, String>> cache;
38:
39: @SuppressWarnings("unchecked")
40: protected void setUp() throws Exception {
41: super .setUp();
42: this .envDir = new File(getTmpDir(), "CachedBdbMapTest");
43: this .envDir.mkdirs();
44: this .cache = new CachedBdbMap(this .envDir, this .getClass()
45: .getName(), String.class, HashMap.class);
46: }
47:
48: protected void tearDown() throws Exception {
49: this .cache.close();
50: FileUtils.deleteDir(this .envDir);
51: super .tearDown();
52: }
53:
54: public void testBackingDbGetsUpdated() {
55: // Enable all logging. Up the level on the handlers and then
56: // on the big map itself.
57: Handler[] handlers = Logger.getLogger("").getHandlers();
58: for (int index = 0; index < handlers.length; index++) {
59: handlers[index].setLevel(Level.FINEST);
60: }
61: Logger.getLogger(CachedBdbMap.class.getName()).setLevel(
62: Level.FINEST);
63: // Set up values.
64: final String value = "value";
65: final String key = "key";
66: final int upperbound = 3;
67: // First put in empty hashmap.
68: for (int i = 0; i < upperbound; i++) {
69: this .cache.put(key + Integer.toString(i),
70: new HashMap<String, String>());
71: }
72: // Now add value to hash map.
73: for (int i = 0; i < upperbound; i++) {
74: HashMap<String, String> m = this .cache.get(key
75: + Integer.toString(i));
76: m.put(key, value);
77: }
78: this .cache.sync();
79: for (int i = 0; i < upperbound; i++) {
80: HashMap<String, String> m = this .cache.get(key
81: + Integer.toString(i));
82: String v = m.get(key);
83: if (v == null || !v.equals(value)) {
84: Logger.getLogger(CachedBdbMap.class.getName()).warning(
85: "Wrong value " + i);
86: }
87: }
88: }
89:
90: public static void main(String[] args) {
91: junit.textui.TestRunner.run(CachedBdbMapTest.class);
92: }
93: }
|