# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: /doma/jarda/netbeans-src/xtest/nbjunit # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 endcoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: src/org/netbeans/junit/Log.java *** /doma/jarda/netbeans-src/xtest/nbjunit/src/org/netbeans/junit/Log.java No Base Revision --- /doma/jarda/netbeans-src/xtest/nbjunit/src/org/netbeans/junit/Log.java Locally New *************** *** 1,0 **** --- 1,68 ---- + /* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun + * Microsystems, Inc. All Rights Reserved. + */ + + package org.netbeans.junit; + + import java.io.ByteArrayInputStream; + import java.io.IOException; + import java.util.logging.Handler; + import java.util.logging.Level; + import java.util.logging.LogManager; + import java.util.logging.LogRecord; + import junit.framework.Assert; + + /** Collects log messages. + * + * @author Jaroslav Tulach + */ + public class Log extends Handler { + private static NbTestCase current; + + /** Creates a new instance of Log */ + public Log() { + } + + static void configure(Level lev, NbTestCase current) throws IOException { + String s = "handlers=" + Log.class.getName() + "\n" + + ".level=" + lev.intValue() + "\n"; + ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes("utf-8")); + LogManager.getLogManager().readConfiguration(is); + + Log.current = current; + } + + public void publish(LogRecord record) { + Assert.assertNotNull("There is a case: ", current); + + StringBuffer sb = new StringBuffer(); + sb.append('['); + sb.append(record.getLoggerName()); + sb.append("] THREAD: "); + sb.append(Thread.currentThread().getName()); + sb.append(" MSG: "); + sb.append(record.getMessage()); + + current.getLog().println(sb.toString()); + + if (record.getThrown() != null) { + record.getThrown().printStackTrace(current.getLog()); + } + } + + public void flush() { + } + + public void close() throws SecurityException { + } + + } Index: test/unit/src/org/netbeans/junit/LoggingTest.java *** /doma/jarda/netbeans-src/xtest/nbjunit/test/unit/src/org/netbeans/junit/LoggingTest.java Base (1.3) --- /doma/jarda/netbeans-src/xtest/nbjunit/test/unit/src/org/netbeans/junit/LoggingTest.java Locally Modified (Based On 1.3) *************** *** 14,21 **** --- 14,24 ---- package org.netbeans.junit; import java.io.File; + import java.io.FileInputStream; import java.io.IOException; import java.io.PrintStream; + import java.util.logging.Level; + import java.util.logging.Logger; /** Checks that we can do proper logging. * *************** *** 83,89 **** --- 86,118 ---- } } } + + protected Level logLevel() { + return Level.WARNING; } + + public void testLoggingUtil() throws Exception { + Logger log = Logger.getLogger(getName()); + log.log(Level.SEVERE, "Ahoj"); + log.log(Level.FINE, "Jardo"); + + File f = new File(getWorkDir(), getName() + ".log"); + assertEquals("Log file exists", true, f.exists()); + + byte[] arr = new byte[(int)f.length()]; + FileInputStream is = new FileInputStream(f); + int l = is.read(arr); + assertEquals(l, arr.length); + + String s = new String(arr); + if (s.indexOf("Ahoj") == -1) { + fail("There should be Ahoj\n" + s); + } + assertEquals("Not logged for FINE: " + s, -1, s.indexOf("Jardo")); + } + + } Index: src/org/netbeans/junit/NbTestCase.java *** /doma/jarda/netbeans-src/xtest/nbjunit/src/org/netbeans/junit/NbTestCase.java Base (1.34) --- /doma/jarda/netbeans-src/xtest/nbjunit/src/org/netbeans/junit/NbTestCase.java Locally Modified (Based On 1.34) *************** *** 17,22 **** --- 17,23 ---- import java.io.*; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Field; + import java.util.logging.Level; import junit.framework.TestCase; import junit.framework.TestResult; import junit.framework.AssertionFailedError; *************** *** 92,97 **** --- 93,108 ---- } /** + * Allows easy collecting of log messages send thru java.util.logging API. + * Overwrite and return the log level to collect logs to logging file. + * @see #getLog() + * @return null + */ + protected Level logLevel() { + return null; + } + + /** * Runs the test case and collects the results in TestResult. * overrides JUnit run, because filter check * and handling of {@link #runInEQ} *************** *** 130,135 **** --- 141,152 ---- */ public void runBare() throws Throwable { setUp(); + + Level lev = logLevel(); + if (lev != null) { + Log.configure(lev, this); + } + long now = System.currentTimeMillis(); try { runTest();