[jboss-svn-commits] JBL Code SVN: r9654 - in labs/jbosstm/trunk/common: tests/com/hp/mwtests/commonlogging/testlevels and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Feb 20 10:55:56 EST 2007
Author: jhalliday
Date: 2007-02-20 10:55:56 -0500 (Tue, 20 Feb 2007)
New Revision: 9654
Modified:
labs/jbosstm/trunk/common/build.xml
labs/jbosstm/trunk/common/tests/com/hp/mwtests/commonlogging/testlevels/TestLevels.java
Log:
Updated TestLevels to use JUnit and build.xml to cause test to be run by default.
Modified: labs/jbosstm/trunk/common/build.xml
===================================================================
--- labs/jbosstm/trunk/common/build.xml 2007-02-20 14:25:27 UTC (rev 9653)
+++ labs/jbosstm/trunk/common/build.xml 2007-02-20 15:55:56 UTC (rev 9654)
@@ -58,8 +58,8 @@
<property name="com.hp.mwlabs.ts.common.tests.destdir" location="${com.hp.mwlabs.ts.common.jar.dest}/tests/"/>
- <property name="com.hp.mw.ts.common.tests.compile" value="no"/>
- <property name="com.hp.mw.ts.common.tests.install" value="no"/>
+ <property name="com.hp.mw.ts.common.tests.compile" value="yes"/>
+ <property name="com.hp.mw.ts.common.tests.install" value="yes"/>
<property name="com.hp.mw.ts.common.log4j.compile" value="yes"/>
<property name="com.hp.mw.ts.common.csf.compile" value="yes"/>
@@ -218,13 +218,32 @@
debug="${com.hp.mwlabs.ts.common.debug}">
<classpath>
<path location="${com.hp.mwlabs.ts.common.dest}"/>
- </classpath>
+ <path location="../ext/junit.jar"/>
+ </classpath>
</javac>
</target>
- <target name="com.hp.mwlabs.ts.common.tests.jar" depends="com.hp.mw.ts.common.tests.compile" if="com.hp.mwlabs.ts.common.tests.compile">
+ <target name="com.hp.mw.ts.common.tests.run" depends="com.hp.mw.ts.common.tests.compile">
+ <!-- if this taskdef is blowing up, it's probably because you need ant 1.7.0 in order to use
+ the embedded classpath method of finding junit.jar. -->
+ <junit haltonerror="yes" haltonfailure="yes" fork="yes"
+ printsummary="yes">
+ <formatter type="plain"/>
+ <test name="com.hp.mwtests.commonlogging.testlevels.TestLevels"/>
+ <classpath>
+ <pathelement location="${com.hp.mwlabs.ts.common.tests.dest}"/>
+ <pathelement location="../ext/junit.jar"/>
+ <path location="${com.hp.mwlabs.ts.common.dest}"/>
+ <pathelement path="${build.classpath}"/>
+ <pathelement location="etc"/>
+ </classpath>
+ </junit>
+ </target>
+
+ <target name="com.hp.mwlabs.ts.common.tests.jar" depends="com.hp.mw.ts.common.tests.run" if="com.hp.mwlabs.ts.common.tests.compile">
+
<mkdir dir="${com.hp.mwlabs.ts.common.tests.destdir}"/>
<jar basedir="${com.hp.mwlabs.ts.common.tests.dest}"
jarfile="${com.hp.mwlabs.ts.common.tests.destdir}/${com.arjuna.common.modulename}_tests.jar"/>
@@ -321,7 +340,8 @@
<!-- Short target names -->
<target name="compile" depends="com.arjuna.common.compile"/>
- <target name="jar" depends="com.hp.mw.ts.common.jar"/>
+ <target name="jar" depends="com.arjuna.common.build"/>
+ <!-- <target name="jar" depends="com.hp.mw.ts.common.jar"/> -->
<target name="clean" depends="com.hp.mw.ts.common.clean"/>
<target name="htdocs" depends="com.hp.mw.ts.common.htdocs"/>
<target name="install" depends="com.hp.mw.ts.common.install"/>
Modified: labs/jbosstm/trunk/common/tests/com/hp/mwtests/commonlogging/testlevels/TestLevels.java
===================================================================
--- labs/jbosstm/trunk/common/tests/com/hp/mwtests/commonlogging/testlevels/TestLevels.java 2007-02-20 14:25:27 UTC (rev 9653)
+++ labs/jbosstm/trunk/common/tests/com/hp/mwtests/commonlogging/testlevels/TestLevels.java 2007-02-20 15:55:56 UTC (rev 9654)
@@ -22,9 +22,26 @@
import com.arjuna.common.util.logging.Logi18n;
import com.arjuna.common.util.logging.LogFactory;
+import com.arjuna.common.internal.util.logging.commonPropertyManager;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
-public class TestLevels
+import junit.framework.TestCase;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * JUnit test that verifies log statments made through the CLF appear at the expexted log level.
+ * It does this by replacing System.out with a memory backed buffer, writing some log messages
+ * and then checking the buffer contents for the expected regexps.
+ *
+ * Note: due to the way logging is initialized, this is somewhat fragile.
+ * In general you can run only one such test in a given JVM without risking interference.
+ */
+public class TestLevels extends TestCase
{
/**
* for logging purposes.
@@ -32,14 +49,7 @@
public static final String CLASS = TestLevels.class.getName();
/**
- * CLF logger for this class
*
- * the resource bundle to use is the fully qualified class name if no additional parameters are given.
- */
- private static Logi18n log = LogFactory.getLogi18n(CLASS, "TestLevels");
-
- /**
- *
* @message testMessage This is the {0} message, logged at level {1}.
*
* @param args
@@ -49,13 +59,54 @@
// CommonLogging-properties.xml: <common><properties><property name= value=>
// cd common/install/lib
// java -cp jbossts-common.jar:tests/common_tests.jar:../../../ext/commons-logging.jar:../../../ext/log4j-1.2.8.jar:../../etc/ com.hp.mwtests.commonlogging.testlevels.TestLevels
+ junit.textui.TestRunner.run(suite());
+ }
- System.out.println("running test");
- log.debug("testMessage", new Object[] {"1st", "debug"});
- log.info("testMessage", new Object[] {"1st", "info"});
- log.warn("testMessage", new Object[] {"1st", "warn"});
- log.error("testMessage", new Object[] {"1st", "error"});
- log.fatal("testMessage", new Object[] {"1st", "fatal"});
- }
+ public static Test suite() {
+ return new TestSuite(TestLevels.class);
+ }
+
+
+ public void testLog4j() {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ PrintStream bufferedStream = new PrintStream(buffer);
+ PrintStream originalStream = System.out;
+
+ // test the releveling for AS integration:
+ // TODO: how to configure this on a per-test (not per-JVM) basis?
+ commonPropertyManager.propertyManager.setProperty(LogFactory.LOGGER_PROPERTY, "log4j_releveler");
+
+ System.setOut(bufferedStream);
+ writeLogMessages();
+ System.setOut(originalStream);
+ verifyResult(buffer.toString(), true);
+ }
+
+ public static void writeLogMessages() {
+ // Don't init the log in a member variable - it must be done AFTER System.out is changed.
+ // TODO: this needs to be cleaner, or we need each test in it's own JVM!
+ Logi18n log = LogFactory.getLogi18n(CLASS, "TestLevels");
+ log.debug("testMessage", new Object[] {"1st", "debug"});
+ log.info("testMessage", new Object[] {"1st", "info"});
+ log.warn("testMessage", new Object[] {"1st", "warn"});
+ log.error("testMessage", new Object[] {"1st", "error"});
+ log.fatal("testMessage", new Object[] {"1st", "fatal"});
+ }
+
+ public static void verifyResult(String result, boolean expectReleveling) {
+ String[] lines = result.split("[\r\n]+?");
+ assertNotNull(lines);
+ assertEquals(5, lines.length);
+ assertTrue("Got actual value: "+lines[0], lines[0].matches("\\s*DEBUG \\[main\\] \\(TestLevels.java.*"));
+
+ if(expectReleveling) {
+ assertTrue("Got actual value: "+lines[1], lines[1].matches("\\s*DEBUG \\[main\\] \\(TestLevels.java.*"));
+ } else {
+ assertTrue("Got actual value: "+lines[1], lines[1].matches("\\s*INFO \\[main\\] \\(TestLevels.java.*"));
+ }
+ assertTrue("Got actual value: "+lines[2], lines[2].matches("\\s*WARN \\[main\\] \\(TestLevels.java.*"));
+ assertTrue("Got actual value: "+lines[3], lines[3].matches("\\s*ERROR \\[main\\] \\(TestLevels.java.*"));
+ assertTrue("Got actual value: "+lines[4], lines[4].matches("\\s*FATAL \\[main\\] \\(TestLevels.java.*"));
+ }
}
More information about the jboss-svn-commits
mailing list