1   /* CachedBdbMapTest
2    * 
3    * Created on Apr 11, 2005
4    *
5    * Copyright (C) 2005 Internet Archive.
6    * 
7    * This file is part of the Heritrix web crawler (crawler.archive.org).
8    * 
9    * 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,
45              this.getClass().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()).
62              setLevel(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), new HashMap<String,String>());
70          }
71          // Now add value to hash map.
72          for (int i = 0; i < upperbound; i++) {
73              HashMap<String,String> m = this.cache.get(key + Integer.toString(i));
74              m.put(key, value);
75          }
76          this.cache.sync();
77          for (int i = 0; i < upperbound; i++) {
78              HashMap<String,String> m = this.cache.get(key + Integer.toString(i));
79              String v = m.get(key);
80              if (v == null || !v.equals(value)) {
81                  Logger.getLogger(CachedBdbMap.class.getName()).
82                      warning("Wrong value " + i);
83              }
84          }
85      }
86      
87      public static void main(String [] args) {
88          junit.textui.TestRunner.run(CachedBdbMapTest.class);
89      }
90  }