01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.util.log;
17:
18: import com.google.gwt.core.ext.TreeLogger;
19:
20: import junit.framework.TestCase;
21:
22: import java.io.PrintWriter;
23: import java.io.StringWriter;
24:
25: /**
26: * Tests the <code>AbstractTreeLogger</code>.
27: */
28: public class AbstractTreeLoggerTest extends TestCase {
29:
30: /**
31: * We handle out-of-memory conditions specially in the logger to provide more
32: * useful log output. It does some slightly weird stuff like turning a regular
33: * log() into a branch(), so this test makes sure that doesn't break anything.
34: */
35: public void testOutOfMemoryLoggerCommitOrderForLog() {
36: StringWriter sw = new StringWriter();
37: PrintWriter pw = new PrintWriter(sw, true);
38: PrintWriterTreeLogger logger = new PrintWriterTreeLogger(pw);
39: logger.setMaxDetail(TreeLogger.WARN);
40:
41: final String tstDbgStr = "TEST-DEBUG-STRING";
42: final String tstErrStr = "TEST-ERROR-STRING";
43:
44: // Emit something that's low-priority and wouldn't show up normally unless
45: // it had a higher-priority child log event.
46: TreeLogger branch = logger.branch(TreeLogger.DEBUG, tstDbgStr,
47: null);
48: assertEquals(-1, sw.toString().indexOf(tstDbgStr));
49:
50: // Emit something that's low-priority but that also has a OOM.
51: branch.log(TreeLogger.ERROR, tstErrStr, new OutOfMemoryError());
52:
53: // Make sure both are now there, in the right order.
54: int posTstDbgStr = sw.toString().indexOf(tstDbgStr);
55: int posTstErrStr = sw.toString().indexOf(tstErrStr);
56: int posOutOfMemory = sw.toString().indexOf(
57: AbstractTreeLogger.OUT_OF_MEMORY_MSG);
58: assertTrue(posTstDbgStr != -1);
59: assertTrue(posTstErrStr != -1);
60: assertTrue(posOutOfMemory != -1);
61: assertTrue(posTstDbgStr < posTstErrStr);
62: assertTrue(posTstErrStr < posOutOfMemory);
63: }
64:
65: /**
66: * Low-priority branch points don't actually show low-priority messages unless
67: * they (later) get a child that is loggable.
68: */
69: public void testLazyBranchCommit() {
70: StringWriter sw = new StringWriter();
71: PrintWriter pw = new PrintWriter(sw, true);
72: PrintWriterTreeLogger logger = new PrintWriterTreeLogger(pw);
73: logger.setMaxDetail(TreeLogger.WARN);
74:
75: final String tstDbgStr = "TEST-DEBUG-STRING";
76: final String tstErrStr = "TEST-ERROR-STRING";
77:
78: // Emit something that's low-priority and wouldn't show up normally unless
79: // it had a higher-priority child log event.
80: TreeLogger branch = logger.branch(TreeLogger.DEBUG, tstDbgStr,
81: null);
82: assertEquals(-1, sw.toString().indexOf(tstDbgStr));
83:
84: // Emit something that's high-priority and will cause both to show up.
85: branch.log(TreeLogger.ERROR, tstErrStr, null);
86:
87: // Make sure both are now there, in the right order.
88: int posTstDbgStr = sw.toString().indexOf(tstDbgStr);
89: int posTstErrStr = sw.toString().indexOf(tstErrStr);
90: assertTrue(posTstDbgStr != -1);
91: assertTrue(posTstErrStr != -1);
92: assertTrue(posTstDbgStr < posTstErrStr);
93: }
94:
95: }
|