1 8 9 package com.sleepycat.je.util; 10 11 import java.io.BufferedReader ; 12 import java.io.File ; 13 import java.io.FileReader ; 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 import java.util.StringTokenizer ; 18 import java.util.logging.Level ; 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 envHome; 36 private EnvironmentImpl env; 37 38 public DebugRecordTest() { 39 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 40 env = null; 41 } 42 43 public void setUp() 44 throws IOException { 45 46 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 47 TestUtils.removeFiles(envHome, new InfoFileFilter()); 48 } 49 50 public void tearDown() 51 throws IOException { 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 { 60 61 try { 62 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 77 DbInternal.setCreateUP(envConfig, false); 78 79 envConfig.setConfigParam 80 (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false"); 81 82 env = new EnvironmentImpl(envHome, envConfig); 83 84 List expectedRecords = new ArrayList (); 85 86 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 Tracer.trace(Level.INFO, env, "hi there"); 98 expectedRecords.add(new Tracer("hi there")); 99 100 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 env.getLogManager().flush(); 108 env.getFileManager().clear(); 109 env.closeLogger(); 110 111 checkDatabaseLog(expectedRecords); 113 checkTextFile(expectedRecords); 114 115 } finally { 116 if (env != null) { 117 env.close(); 118 } 119 } 120 } 121 122 125 private void checkDatabaseLog(List expectedList) 126 throws DatabaseException, IOException { 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 148 private void checkTextFile(List expectedList) 149 throws IOException { 150 151 FileReader fr = null; 152 BufferedReader br = null; 153 try { 154 String textFileName = envHome + File.separator + "je.info.0"; 155 fr = new FileReader (textFileName); 156 br = new BufferedReader (fr); 157 158 String line = br.readLine(); 159 int numSeen = 0; 160 161 while (line != null) { 163 int firstColon = line.indexOf(':'); 164 firstColon = firstColon > 0? firstColon : 0; 165 String possibleLevel = line.substring(0, firstColon); 166 try { 167 Level.parse(possibleLevel); 168 String expected = 169 ((Tracer) expectedList.get(numSeen)).getMessage(); 170 StringBuffer seen = new StringBuffer (); 171 175 StringTokenizer st = 176 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 if (st.countTokens() == 1) { 190 assertEquals("Line " + numSeen + " should be the same", 191 expected, seen.toString()); 192 } 193 numSeen++; 194 } catch (Exception e) { 195 } 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 |