KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > util > DebugRecordTest


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

8
9 package com.sleepycat.je.util;
10
11 import java.io.BufferedReader JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.FileReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18 import java.util.logging.Level JavaDoc;
19
20 import junit.framework.TestCase;
21
22 import com.sleepycat.je.DatabaseException;
23 import com.sleepycat.je.DbInternal;
24 import com.sleepycat.je.EnvironmentConfig;
25 import com.sleepycat.je.config.EnvironmentParams;
26 import com.sleepycat.je.dbi.EnvironmentImpl;
27 import com.sleepycat.je.log.LogEntryType;
28 import com.sleepycat.je.log.FileManager;
29 import com.sleepycat.je.log.SearchFileReader;
30 import com.sleepycat.je.recovery.RecoveryInfo;
31 import com.sleepycat.je.utilint.DbLsn;
32 import com.sleepycat.je.utilint.Tracer;
33
34 public class DebugRecordTest extends TestCase {
35     private File JavaDoc envHome;
36     private EnvironmentImpl env;
37     
38     public DebugRecordTest() {
39         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
40         env = null;
41     }
42
43     public void setUp()
44     throws IOException JavaDoc {
45
46         TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
47         TestUtils.removeFiles(envHome, new InfoFileFilter());
48     }
49     
50     public void tearDown()
51     throws IOException JavaDoc {
52
53         TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
54         TestUtils.removeFiles(envHome, new InfoFileFilter());
55     }
56
57     
58     public void testDebugLogging()
59     throws DatabaseException, IOException JavaDoc {
60
61         try {
62             // turn on the txt file and db log logging, turn off the console
63
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
64             envConfig.setConfigParam
65         (EnvironmentParams.JE_LOGGING_FILE.getName(), "true");
66             envConfig.setConfigParam
67         (EnvironmentParams.JE_LOGGING_CONSOLE.getName(),
68          "false");
69             envConfig.setConfigParam
70         (EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG");
71             envConfig.setConfigParam
72         (EnvironmentParams.JE_LOGGING_DBLOG.getName(), "true");
73             envConfig.setConfigParam
74         (EnvironmentParams.NODE_MAX.getName(), "6");
75         envConfig.setAllowCreate(true);
76             /* Disable noisy UtilizationProfile database creation. */
77             DbInternal.setCreateUP(envConfig, false);
78             /* Don't run the cleaner without a UtilizationProfile. */
79             envConfig.setConfigParam
80                 (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
81     
82             env = new EnvironmentImpl(envHome, envConfig);
83         
84             List JavaDoc expectedRecords = new ArrayList JavaDoc();
85             
86             // Recovery itself will log two messages
87
RecoveryInfo info = new RecoveryInfo();
88             expectedRecords.add(new Tracer("Recovery w/no files."));
89             expectedRecords.add(new Tracer
90                 ("Checkpoint 1: source=recovery" +
91                  " success=true nFullINFlushThisRun=0" +
92                  " nDeltaINFlushThisRun=0"));
93             expectedRecords.add(new Tracer("Recovery finished: " +
94                        info.toString()));
95             
96             // Log a message
97
Tracer.trace(Level.INFO, env, "hi there");
98             expectedRecords.add(new Tracer("hi there"));
99
100             // Log an exception
101
DatabaseException e = new DatabaseException("fake exception");
102             Tracer.trace(env, "DebugRecordTest", "testException", "foo", e);
103             expectedRecords.add(new Tracer("foo\n" + Tracer.getStackTrace(e)));
104                             
105             // Log a split
106
// Flush the log to disk
107
env.getLogManager().flush();
108             env.getFileManager().clear();
109             env.closeLogger();
110
111             // Verify
112
checkDatabaseLog(expectedRecords);
113             checkTextFile(expectedRecords);
114
115         } finally {
116             if (env != null) {
117                 env.close();
118             }
119         }
120     }
121     
122     /**
123      * Check what's in the database log
124      */

125     private void checkDatabaseLog(List JavaDoc expectedList)
126         throws DatabaseException, IOException JavaDoc {
127
128         SearchFileReader searcher =
129             new SearchFileReader(env, 1000, true, DbLsn.NULL_LSN,
130                  DbLsn.NULL_LSN, LogEntryType.LOG_TRACE);
131
132         int numSeen = 0;
133         while (searcher.readNextEntry()) {
134             Tracer dRec = (Tracer) searcher.getLastObject();
135             assertEquals("Should see this as " + numSeen + " record: ",
136              ((Tracer) expectedList.get(numSeen)).getMessage(),
137                          dRec.getMessage());
138             numSeen++;
139         }
140         
141         assertEquals("Should see this many debug records",
142                      expectedList.size(), numSeen);
143     }
144
145     /**
146      * Check what's in the text file
147      */

148     private void checkTextFile(List JavaDoc expectedList)
149         throws IOException JavaDoc {
150
151         FileReader JavaDoc fr = null;
152         BufferedReader JavaDoc br = null;
153         try {
154             String JavaDoc textFileName = envHome + File.separator + "je.info.0";
155             fr = new FileReader JavaDoc(textFileName);
156             br = new BufferedReader JavaDoc(fr);
157
158             String JavaDoc line = br.readLine();
159             int numSeen = 0;
160
161             // Read the file, checking only lines that start with valid Levels
162
while (line != null) {
163                 int firstColon = line.indexOf(':');
164                 firstColon = firstColon > 0? firstColon : 0;
165                 String JavaDoc possibleLevel = line.substring(0, firstColon);
166                 try {
167                     Level.parse(possibleLevel);
168                     String JavaDoc expected =
169                         ((Tracer) expectedList.get(numSeen)).getMessage();
170                     StringBuffer JavaDoc seen = new StringBuffer JavaDoc();
171                     /*
172                      * Assemble the log message by reading the right number
173                      * of lines
174                      */

175                     StringTokenizer JavaDoc st =
176                         new StringTokenizer JavaDoc(expected,
177                                             Character.toString('\n'), false);
178
179                     seen.append(line.substring(firstColon + 2));
180                     for (int i = 1; i < st.countTokens(); i++) {
181                         seen.append('\n');
182                         String JavaDoc l = br.readLine();
183                         seen.append(l);
184                         if (i == (st.countTokens() -1)) {
185                             seen.append('\n');
186                         }
187                     }
188                     // XXX, diff of multiline stuff isn't right yet
189
if (st.countTokens() == 1) {
190                         assertEquals("Line " + numSeen + " should be the same",
191                                      expected, seen.toString());
192                     }
193                     numSeen++;
194                 } catch (Exception JavaDoc e) {
195                     // skip this line, not a message
196
}
197                 line = br.readLine();
198             }
199             assertEquals("Should see this many debug records",
200                          expectedList.size(), numSeen);
201         } finally {
202         if (br != null) {
203         br.close();
204         }
205         if (fr != null) {
206         fr.close();
207         }
208     }
209     }
210 }
211
Popular Tags