001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: DebugRecordTest.java,v 1.41.2.3 2008/01/07 15:14:34 cwl Exp $
007: */
008:
009: package com.sleepycat.je.util;
010:
011: import java.io.BufferedReader;
012: import java.io.File;
013: import java.io.FileReader;
014: import java.io.IOException;
015: import java.util.ArrayList;
016: import java.util.List;
017: import java.util.StringTokenizer;
018: import java.util.logging.Level;
019:
020: import junit.framework.TestCase;
021:
022: import com.sleepycat.je.DatabaseException;
023: import com.sleepycat.je.DbInternal;
024: import com.sleepycat.je.EnvironmentConfig;
025: import com.sleepycat.je.config.EnvironmentParams;
026: import com.sleepycat.je.dbi.EnvironmentImpl;
027: import com.sleepycat.je.log.LogEntryType;
028: import com.sleepycat.je.log.FileManager;
029: import com.sleepycat.je.log.SearchFileReader;
030: import com.sleepycat.je.recovery.RecoveryInfo;
031: import com.sleepycat.je.utilint.DbLsn;
032: import com.sleepycat.je.utilint.Tracer;
033:
034: public class DebugRecordTest extends TestCase {
035: private File envHome;
036: private EnvironmentImpl env;
037:
038: public DebugRecordTest() {
039: envHome = new File(System.getProperty(TestUtils.DEST_DIR));
040: env = null;
041: }
042:
043: public void setUp() throws IOException {
044:
045: TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
046: TestUtils.removeFiles(envHome, new InfoFileFilter());
047: }
048:
049: public void tearDown() throws IOException {
050:
051: TestUtils.removeFiles("TearDown", envHome,
052: FileManager.JE_SUFFIX);
053: TestUtils.removeFiles(envHome, new InfoFileFilter());
054: }
055:
056: public void testDebugLogging() throws DatabaseException,
057: IOException {
058:
059: try {
060: // turn on the txt file and db log logging, turn off the console
061: EnvironmentConfig envConfig = TestUtils.initEnvConfig();
062: envConfig.setConfigParam(EnvironmentParams.JE_LOGGING_FILE
063: .getName(), "true");
064: envConfig.setConfigParam(
065: EnvironmentParams.JE_LOGGING_CONSOLE.getName(),
066: "false");
067: envConfig.setConfigParam(EnvironmentParams.JE_LOGGING_LEVEL
068: .getName(), "CONFIG");
069: envConfig.setConfigParam(EnvironmentParams.JE_LOGGING_DBLOG
070: .getName(), "true");
071: envConfig.setConfigParam(EnvironmentParams.NODE_MAX
072: .getName(), "6");
073: envConfig.setAllowCreate(true);
074: /* Disable noisy UtilizationProfile database creation. */
075: DbInternal.setCreateUP(envConfig, false);
076: /* Don't run the cleaner without a UtilizationProfile. */
077: envConfig.setConfigParam(EnvironmentParams.ENV_RUN_CLEANER
078: .getName(), "false");
079:
080: env = new EnvironmentImpl(envHome, envConfig);
081:
082: List expectedRecords = new ArrayList();
083:
084: // Recovery itself will log two messages
085: RecoveryInfo info = new RecoveryInfo();
086: expectedRecords.add(new Tracer("Recovery w/no files."));
087: expectedRecords.add(new Tracer(
088: "Checkpoint 1: source=recovery"
089: + " success=true nFullINFlushThisRun=0"
090: + " nDeltaINFlushThisRun=0"));
091: expectedRecords.add(new Tracer("Recovery finished: "
092: + info.toString()));
093:
094: // Log a message
095: Tracer.trace(Level.INFO, env, "hi there");
096: expectedRecords.add(new Tracer("hi there"));
097:
098: // Log an exception
099: DatabaseException e = new DatabaseException(
100: "fake exception");
101: Tracer.trace(env, "DebugRecordTest", "testException",
102: "foo", e);
103: expectedRecords.add(new Tracer("foo\n"
104: + Tracer.getStackTrace(e)));
105:
106: // Log a split
107: // Flush the log to disk
108: env.getLogManager().flush();
109: env.getFileManager().clear();
110: env.closeLogger();
111:
112: // Verify
113: checkDatabaseLog(expectedRecords);
114: checkTextFile(expectedRecords);
115:
116: } finally {
117: if (env != null) {
118: env.close();
119: }
120: }
121: }
122:
123: /**
124: * Check what's in the database log
125: */
126: private void checkDatabaseLog(List expectedList)
127: throws DatabaseException, IOException {
128:
129: SearchFileReader searcher = new SearchFileReader(env, 1000,
130: true, DbLsn.NULL_LSN, DbLsn.NULL_LSN,
131: LogEntryType.LOG_TRACE);
132:
133: int numSeen = 0;
134: while (searcher.readNextEntry()) {
135: Tracer dRec = (Tracer) searcher.getLastObject();
136: assertEquals("Should see this as " + numSeen + " record: ",
137: ((Tracer) expectedList.get(numSeen)).getMessage(),
138: dRec.getMessage());
139: numSeen++;
140: }
141:
142: assertEquals("Should see this many debug records", expectedList
143: .size(), numSeen);
144: }
145:
146: /**
147: * Check what's in the text file
148: */
149: private void checkTextFile(List expectedList) throws IOException {
150:
151: FileReader fr = null;
152: BufferedReader br = null;
153: try {
154: String textFileName = envHome + File.separator
155: + "je.info.0";
156: fr = new FileReader(textFileName);
157: br = new BufferedReader(fr);
158:
159: String line = br.readLine();
160: int numSeen = 0;
161:
162: // Read the file, checking only lines that start with valid Levels
163: while (line != null) {
164: int firstColon = line.indexOf(':');
165: firstColon = firstColon > 0 ? firstColon : 0;
166: String possibleLevel = line.substring(0, firstColon);
167: try {
168: Level.parse(possibleLevel);
169: String expected = ((Tracer) expectedList
170: .get(numSeen)).getMessage();
171: StringBuffer seen = new StringBuffer();
172: /*
173: * Assemble the log message by reading the right number
174: * of lines
175: */
176: StringTokenizer st = new StringTokenizer(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 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
191: + " should be the same", expected, seen
192: .toString());
193: }
194: numSeen++;
195: } catch (Exception e) {
196: // skip this line, not a message
197: }
198: line = br.readLine();
199: }
200: assertEquals("Should see this many debug records",
201: expectedList.size(), numSeen);
202: } finally {
203: if (br != null) {
204: br.close();
205: }
206: if (fr != null) {
207: fr.close();
208: }
209: }
210: }
211: }
|