001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
027: */
028: package org.netbeans.lib.uihandler;
029:
030: import java.io.ByteArrayInputStream;
031: import java.io.ByteArrayOutputStream;
032: import java.io.IOException;
033: import java.io.PrintStream;
034: import java.sql.SQLException;
035: import java.util.logging.Formatter;
036: import java.util.logging.Level;
037: import java.util.logging.LogRecord;
038: import java.util.logging.XMLFormatter;
039: import javax.swing.JButton;
040: import javax.swing.JEditorPane;
041: import javax.swing.JMenuItem;
042: import org.netbeans.junit.NbTestCase;
043: import org.openide.util.HelpCtx;
044: import org.openide.util.Lookup;
045: import org.openide.util.actions.CallableSystemAction;
046: import org.openide.util.actions.CallbackSystemAction;
047: import org.openide.util.actions.SystemAction;
048:
049: /**
050: *
051: * @author Jindrich Sedek
052: */
053: public class LogFormatterTest extends NbTestCase {
054:
055: public LogFormatterTest(String testName) {
056: super (testName);
057: }
058:
059: protected void setUp() throws Exception {
060: super .setUp();
061: }
062:
063: protected void tearDown() throws Exception {
064: super .tearDown();
065: }
066:
067: public void testFormat() throws IOException {
068: LogRecord rec = new LogRecord(Level.SEVERE, "PROBLEM");
069: Throwable thrown = new NullPointerException("TESTING");
070: thrown.initCause(new AssertionError("CAUSE PROBLEM"));
071: rec.setThrown(thrown);
072: String result = new LogFormatter().format(rec);
073: assertTrue(result
074: .contains("java.lang.NullPointerException: TESTING"));
075: assertTrue(result.contains("<level>SEVERE</level>"));
076: assertTrue(result.contains("<method>testFormat</method>"));
077: assertTrue(result
078: .contains("<message>java.lang.AssertionError: CAUSE PROBLEM</message>"));
079: assertTrue(result.contains("<more>19</more>"));
080: assertTrue(result
081: .contains(" <class>junit.framework.TestSuite</class>"));
082: assertTrue(result
083: .contains("<class>sun.reflect.NativeMethodAccessorImpl</class>"));
084: assertFalse(result.contains("<more>20</more>"));
085: }
086:
087: public void testEasy() throws IOException {
088: Throwable thrown = new NullPointerException("TESTING");
089: thrown.initCause(new AssertionError("CAUSE PROBLEM"));
090: formatAndScan(thrown);
091: }
092:
093: public void testManyCausesFormat() throws IOException {
094: try {
095: generateIOException();
096: } catch (IOException exc) {
097: formatAndScan(exc);
098: }
099: }
100:
101: /**
102: * test whether the result of LogFormatter is the same as XMLFormatter
103: * if there is no nested exception
104: */
105: public void testXMLFormatterDifference() {
106: LogRecord rec = new LogRecord(Level.SEVERE, "PROBLEM");
107: LogFormatter logFormatter = new LogFormatter();
108: XMLFormatter xmlFormatter = new XMLFormatter();
109: String logResult = logFormatter.format(rec);
110: String xmlResult = xmlFormatter.format(rec);
111: assertEquals("WITHOUT THROWABLE", xmlResult, logResult);
112: rec.setThrown(new NullPointerException("TESTING EXCEPTION"));
113: rec.setResourceBundleName("MUJ BUNDLE");
114: logResult = logFormatter.format(rec);
115: //remove file names
116: logResult = logResult.replaceAll(" <file>.*</file>\n", "");
117: xmlResult = xmlFormatter.format(rec);
118: assertEquals("WITH THROWABLE", xmlResult, logResult);
119: }
120:
121: private void formatAndScan(Throwable thr) throws IOException {
122: ByteArrayOutputStream oStream = new ByteArrayOutputStream(1000);
123: LogRecord rec = new LogRecord(Level.SEVERE, "PROBLEM");
124: rec.setThrown(thr);
125: LogRecords.write(oStream, rec);//write to stream
126: ByteArrayInputStream iStream = new ByteArrayInputStream(oStream
127: .toByteArray());
128: Formatter formatter = new LogFormatter();
129: LogRecord readFromStream = new TestHandler(iStream).read();// read from stream
130: //read by handler equals the writen by formatter
131: assertEquals(formatter.format(readFromStream), formatter
132: .format(rec));
133: oStream.reset();
134: thr.printStackTrace(new PrintStream(oStream));
135: String writen = oStream.toString();
136: oStream.reset();
137: rec.getThrown().printStackTrace(new PrintStream(oStream));
138: String read = oStream.toString();
139: assertEquals(writen, read);//both stacktraces are the same
140: }
141:
142: private void generateIOException() throws IOException {
143: try {
144: generateSQL();
145: } catch (SQLException error) {
146: IOException except = new IOException("IO EXCEPTION");
147: except.initCause(error);
148: throw except;
149: }
150: }
151:
152: private void generateSQL() throws SQLException {
153: try {
154: generateClassNotFoundException();
155: } catch (ClassNotFoundException exception) {
156: SQLException except = new SQLException(
157: "SQL TESTING EXCEPTION");
158: except.initCause(exception);
159: throw except;
160: }
161: }
162:
163: private void generateClassNotFoundException()
164: throws ClassNotFoundException {
165: java.lang.Class.forName("unknown name");
166: }
167:
168: public void testFormatterDoesNotIncludeHashOnButton()
169: throws ClassNotFoundException {
170: LogRecord r = new LogRecord(Level.INFO, "BUTTON");
171: r.setParameters(new Object[] { new JButton("kuk") });
172: Formatter formatter = new LogFormatter();
173: String s = formatter.format(r);
174: assertEquals("No @\n" + s, -1, s.indexOf("@"));
175: if (s.indexOf("kuk") == -1) {
176: fail("kuk should be there:\n" + s);
177: }
178: }
179:
180: public void testFormatterDoesNotIncludeHashOnActions()
181: throws ClassNotFoundException {
182: LogRecord r = new LogRecord(Level.INFO, "ACTION");
183: SA sa = SA.get(SA.class);
184: r.setParameters(new Object[] { sa });
185: Formatter formatter = new LogFormatter();
186: String s = formatter.format(r);
187: assertEquals("No @\n" + s, -1, s.indexOf("@"));
188: if (s.indexOf("SomeName") == -1) {
189: fail("SomeName should be there:\n" + s);
190: }
191: if (s.indexOf("LogFormatterTest$SA") == -1) {
192: fail("LogFormatterTest$SA should be there:\n" + s);
193: }
194: }
195:
196: public void testFormatterDoesNotIncludeHashOnActionsClone()
197: throws ClassNotFoundException {
198: LogRecord r = new LogRecord(Level.INFO, "ACTION_CLONE");
199: SA sa = SA.get(SA.class);
200: r.setParameters(new Object[] { sa
201: .createContextAwareInstance(Lookup.EMPTY) });
202: Formatter formatter = new LogFormatter();
203: String s = formatter.format(r);
204: assertEquals("No @\n" + s, -1, s.indexOf("@"));
205: if (s.indexOf("SomeName") == -1) {
206: fail("SomeName should be there:\n" + s);
207: }
208: if (s.indexOf("LogFormatterTest$SA") == -1) {
209: fail("LogFormatterTest$SA should be there:\n" + s);
210: }
211: }
212:
213: public void testFormatterDoesNotIncludeHashOnMenu()
214: throws ClassNotFoundException {
215: LogRecord r = new LogRecord(Level.INFO, "MENU");
216: SA sa = SA.get(SA.class);
217: r.setParameters(new Object[] { new JMenuItem(sa) });
218: Formatter formatter = new LogFormatter();
219: String s = formatter.format(r);
220: assertEquals("No @\n" + s, -1, s.indexOf("@"));
221: if (s.indexOf("SomeName") == -1) {
222: fail("SomeName should be there:\n" + s);
223: }
224: if (s.indexOf("LogFormatterTest$SA") == -1) {
225: fail("LogFormatterTest$SA should be there:\n" + s);
226: }
227: }
228:
229: public void testFormatterDoesNotIncludeHashOnEditor()
230: throws ClassNotFoundException {
231: LogRecord r = new LogRecord(Level.INFO, "EDIT");
232: JEditorPane ep = new javax.swing.JEditorPane();
233: ep.setName("SomeName");
234: r.setParameters(new Object[] { ep });
235: Formatter formatter = new LogFormatter();
236: String s = formatter.format(r);
237: assertEquals("No @\n" + s, -1, s.indexOf("@"));
238: if (s.indexOf("SomeName") == -1) {
239: fail("SomeName should be there:\n" + s);
240: }
241: }
242:
243: public static class SA extends CallbackSystemAction {
244:
245: public String getName() {
246: return "SomeName";
247: }
248:
249: public HelpCtx getHelpCtx() {
250: throw new UnsupportedOperationException(
251: "Not supported yet.");
252: }
253:
254: }
255: }
|