KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > EnvironmentStatTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: EnvironmentStatTest.java,v 1.17 2006/10/30 21:14:41 bostic Exp $
7  */

8
9 package com.sleepycat.je;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.je.config.EnvironmentParams;
17 import com.sleepycat.je.dbi.MemoryBudget;
18 import com.sleepycat.je.util.TestUtils;
19
20 public class EnvironmentStatTest extends TestCase {
21
22     private Environment env;
23     private File JavaDoc envHome;
24
25     public EnvironmentStatTest() {
26         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
27     }
28
29     public void setUp()
30         throws IOException JavaDoc {
31
32         TestUtils.removeLogFiles("Setup", envHome, false);
33     }
34     
35     public void tearDown()
36         throws Exception JavaDoc {
37
38         /* Close down environments in case the unit test failed so that
39          * the log files can be removed.
40          */

41         try {
42             if (env != null) {
43                 env.close();
44                 env = null;
45             }
46         } catch (DatabaseException e) {
47             /* ok, the test closed it */
48         }
49
50         TestUtils.removeLogFiles("TearDown", envHome, false);
51     }
52
53     /**
54      * Test open and close of an environment.
55      */

56     public void testCacheStats()
57         throws Throwable JavaDoc {
58
59         try {
60             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
61             envConfig.setTransactional(true);
62             envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
63             envConfig.setAllowCreate(true);
64             env = new Environment(envHome, envConfig);
65             EnvironmentStats stat = env.getStats(TestUtils.FAST_STATS);
66             env.close();
67             env = null;
68             assertEquals(0, stat.getNCacheMiss());
69             assertEquals(0, stat.getNNotResident());
70
71             // Try to open and close again, now that the environment exists
72
envConfig.setAllowCreate(false);
73         envConfig.setConfigParam
74         (EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG");
75             env = new Environment(envHome, envConfig);
76             stat = env.getStats(TestUtils.FAST_STATS);
77             MemoryBudget mb =
78                 DbInternal.envGetEnvironmentImpl(env).getMemoryBudget();
79             long cacheSize = mb.getCacheMemoryUsage();
80             long bufferSize = mb.getLogBufferBudget();
81             
82             assertEquals(12, stat.getNCacheMiss());
83             assertEquals(12, stat.getNNotResident());
84
85             assertEquals(cacheSize, stat.getCacheDataBytes());
86
87             /*
88              * Buffer size may be slightly different, because the log
89              * buffer pool might do some rounding. Just check that
90              * it's within an ok margin.
91              */

92             assertTrue(Math.abs(bufferSize-stat.getBufferBytes()) < 100);
93             assertTrue(Math.abs((cacheSize + bufferSize)-
94                                 stat.getCacheTotalBytes()) < 100);
95         env.close();
96         } catch (Throwable JavaDoc t) {
97             t.printStackTrace();
98             throw t;
99         }
100     }
101 }
102
Popular Tags