Index: src/java/org/apache/log4j/concurrent/ConcurrentAppender.java =================================================================== --- src/java/org/apache/log4j/concurrent/ConcurrentAppender.java (revision 463475) +++ src/java/org/apache/log4j/concurrent/ConcurrentAppender.java (working copy) @@ -169,7 +169,7 @@ */ public boolean isAsSevereAsThreshold(final Priority level) { Priority copy = threshold; - return ((copy == null) || copy.isGreaterOrEqual(level)); + return ((copy == null) || level.isGreaterOrEqual(copy)); } /** @@ -424,7 +424,7 @@ if (f != null) sb.append(','); } - return f.toString(); + return sb.toString(); } } Index: tests/src/java/org/apache/log4j/concurrent/SynchronizedBooleanTest.java =================================================================== --- tests/src/java/org/apache/log4j/concurrent/SynchronizedBooleanTest.java (revision 0) +++ tests/src/java/org/apache/log4j/concurrent/SynchronizedBooleanTest.java (revision 0) @@ -0,0 +1,39 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.log4j.concurrent; + +import junit.framework.TestCase; + + +/** + * Tests of SynchronizedBoolean. + */ +public class SynchronizedBooleanTest extends TestCase { + + /** + * Tests SynchronizedBoolean get set and toString. + */ + public void testBasic() { + SynchronizedBoolean sb = new SynchronizedBoolean(true); + assertEquals(true, sb.get()); + sb.set(false); + assertEquals(false, sb.get()); + assertEquals("false", sb.toString()); + } + +} + Property changes on: tests/src/java/org/apache/log4j/concurrent/SynchronizedBooleanTest.java ___________________________________________________________________ Name: svn:executable + * Index: tests/src/java/org/apache/log4j/concurrent/ConcurrentAppenderTest.java =================================================================== --- tests/src/java/org/apache/log4j/concurrent/ConcurrentAppenderTest.java (revision 0) +++ tests/src/java/org/apache/log4j/concurrent/ConcurrentAppenderTest.java (revision 0) @@ -0,0 +1,145 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.log4j.concurrent; + +import junit.framework.TestCase; +import org.apache.log4j.Logger; +import org.apache.log4j.Level; +import org.apache.log4j.spi.LoggingEvent; +import org.apache.log4j.SimpleLayout; +import org.apache.log4j.filter.DenyAllFilter; +import org.apache.log4j.filter.StringMatchFilter; + +/** + * Tests of ConcurrentAppender. + */ +public class ConcurrentAppenderTest extends TestCase { + + private Logger log = Logger.getLogger(ConcurrentAppenderTest.class); + private MyConcurrentAppender a = new MyConcurrentAppender(); + private String name = "name"; + private String msg = "Hello, World"; + private SimpleLayout layout = new SimpleLayout(); + private DenyAllFilter denyFilter = new DenyAllFilter(); + private StringMatchFilter stringMatchFilter = new StringMatchFilter(); + { + stringMatchFilter.setStringToMatch("yo"); + } + + /** + * Tests set and get methods. + */ + public void testSetGet() { + assertEquals(false, a.isActive()); + assertEquals(false, a.isClosed()); + assertEquals(false, a.getClosed()); + assertEquals(null, a.getName()); + a.setName(name); + assertEquals(name, a.getName()); + assertEquals(null, a.getThreshold()); + a.setThreshold(Level.INFO); + assertEquals(Level.INFO, a.getThreshold()); + assertEquals(null, a.getLayout()); + a.setLayout(layout); + assertEquals(layout, a.getLayout()); + + assertNotNull(a.getErrorHandler()); + a.setErrorHandler(null); + assertNotNull(a.toString()); + } + + /** + * Tests log methods, threshold, filter. + */ + public void testLog() { + log.addAppender(a); + log.debug(msg); + assertEquals("not activated", null, a.event); + a.activateOptions(); + log.debug(msg); + assertNotNull(a.event); + + a.setThreshold(Level.INFO); + a.event = null; + log.debug(msg); + assertEquals("filtered", null, a.event); + log.fatal(msg); + assertNotNull(a.event); + + a.event = null; + stringMatchFilter.setStringToMatch("yo"); + a.addFilter(stringMatchFilter); + a.addFilter(denyFilter); + log.fatal("Not y and o"); + assertEquals("filtered", null, a.event); + log.fatal("yo yo yo"); + assertNotNull(a.event); + assertEquals(stringMatchFilter, a.getFilter()); + + a.clearFilters(); + a.event = null; + log.fatal("Not y and o"); + assertNotNull("no longer filtered", a.event); + } + + /** + * Tests active and close methods. + */ + public void testClose() { + log.addAppender(a); + log.debug("not active"); + a.activateOptions(); + assertEquals(true, a.isActive()); + a.close(); + assertEquals(true, a.internalClosed); + assertEquals(true, a.isClosed()); + assertEquals(true, a.getClosed()); + log.debug("not logged"); + assertEquals("closed", null, a.event); + a.close(); // shouldn't call internalClose() twice + assertEquals(true, a.internalClosed); + assertEquals(true, a.isClosed()); + } + + class MyConcurrentAppender extends ConcurrentAppender { + + LoggingEvent event; + boolean internalClosed; + + MyConcurrentAppender() { + super(false); + } + + protected void append(LoggingEvent event) { + this.event = event; + } + + public boolean requiresLayout() { + return true; + } + + protected void internalClose() { + if (internalClosed) + throw new IllegalStateException(); + internalClosed = true; + } + + } + +} + + Property changes on: tests/src/java/org/apache/log4j/concurrent/ConcurrentAppenderTest.java ___________________________________________________________________ Name: svn:executable + * Index: tests/src/java/org/apache/log4j/concurrent/ConsoleAppenderTest.java =================================================================== --- tests/src/java/org/apache/log4j/concurrent/ConsoleAppenderTest.java (revision 0) +++ tests/src/java/org/apache/log4j/concurrent/ConsoleAppenderTest.java (revision 0) @@ -0,0 +1,74 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.log4j.concurrent; + +import java.io.PrintStream; +import java.io.ByteArrayOutputStream; +import junit.framework.TestCase; +import org.apache.log4j.Layout; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.SimpleLayout; + +/** + * Tests of ConsoleAppender. + */ +public class ConsoleAppenderTest extends TestCase { + + private Logger log = Logger.getLogger(ConsoleAppender.class); + private ConsoleAppender appender = new ConsoleAppender(); + private SimpleLayout layout = new SimpleLayout(); + + private ByteArrayOutputStream bo1 = new ByteArrayOutputStream(); + private ByteArrayOutputStream bo2 = new ByteArrayOutputStream(); + private PrintStream err = new PrintStream(bo1); + private PrintStream out = new PrintStream(bo2); + { + appender.setLayout(layout); + log.addAppender(appender); + } + + /** + * Tests ConsoleAppender get set and toString. + */ + public void testBasic() { + assertEquals(false, appender.isActive()); + System.setErr(err); + appender.setTarget("System.err"); + assertEquals(false, appender.isActive()); + appender.activateOptions(); + assertEquals(true, appender.isActive()); + log.debug("HI"); + assertEquals("DEBUG - HI", bo1.toString().trim()); + assertEquals("", bo2.toString()); + + appender.setTarget("System.out"); + appender.activateOptions(); + System.setOut(out); + log.debug("HI"); + assertEquals("not following", "", bo2.toString().trim()); + + appender.setFollow(true); + appender.activateOptions(); + log.debug("HI"); + assertEquals("DEBUG - HI", bo2.toString().trim()); + } + +} + + + Property changes on: tests/src/java/org/apache/log4j/concurrent/ConsoleAppenderTest.java ___________________________________________________________________ Name: svn:executable + * Index: tests/src/java/org/apache/log4j/concurrent/WriterAppenderTest.java =================================================================== --- tests/src/java/org/apache/log4j/concurrent/WriterAppenderTest.java (revision 0) +++ tests/src/java/org/apache/log4j/concurrent/WriterAppenderTest.java (revision 0) @@ -0,0 +1,133 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.log4j.concurrent; + +import java.io.Writer; +import java.io.IOException; +import junit.framework.TestCase; +import org.apache.log4j.Layout; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.SimpleLayout; + +/** + * Tests of WriterAppender. + */ +public class WriterAppenderTest extends TestCase { + + private Logger log = Logger.getLogger(ConcurrentAppenderTest.class); + private WriterAppender appender = new WriterAppender(); + private SimpleLayout layout = new SimpleLayout(); + + { + log.addAppender(appender); + layout.setFooter("F"); + layout.setHeader("H"); + } + + private MyStringWriter sw = new MyStringWriter(); + + private static class MyStringWriter extends Writer { + public boolean closed = false; + public boolean flushed = false; + public boolean toss = false; + StringBuffer sb = new StringBuffer(); + + public void write(char[] cbuf, int off, int len) throws IOException { + sb.append(cbuf, off, len); + if (toss) + throw new IOException(); + } + + public void flush() { + flushed = true; + } + + public void close() throws IOException { + closed = true; + } + + public String toString() { + return sb.toString(); + } + + } + + /** + * Tests WriterAppender get set and toString. + */ + public void testBasic() { + assertEquals(true, appender.getImmediateFlush()); + appender.setImmediateFlush(false); + assertEquals(false, appender.getImmediateFlush()); + appender.activateOptions(); + assertEquals(false, appender.isActive()); + appender.setLayout(layout); + appender.activateOptions(); + assertEquals(false, appender.isActive()); + appender.setWriter(sw); + appender.activateOptions(); + assertEquals(true, appender.isActive()); + appender.close(); + assertEquals(true, sw.closed); + assertEquals(true, sw.flushed); + assertEquals(true, appender.requiresLayout()); + appender.setEncoding("ASCII"); + assertEquals("ASCII", appender.getEncoding()); + } + + /** + * Tests WriterAppender output. + */ + public void testOutput() { + appender.setLayout(layout); + appender.setWriter(sw); + appender.activateOptions(); + log.debug("HI"); + assertEquals(true, sw.flushed); + assertEquals("HDEBUG - HI", sw.toString().trim()); + appender.close(); + log.debug("HI"); + assertEquals("HDEBUG - HI" + Layout.LINE_SEP + "F", sw.toString().trim()); + } + + /** + * Tests Throwable output. + */ + public void testThrowable() { + appender.setLayout(layout); + appender.setWriter(sw); + appender.activateOptions(); + appender.setImmediateFlush(false); + sw.flushed = false; + log.debug("HI", new Throwable()); + assertEquals(false, sw.flushed); + String s = sw.toString(); + assertTrue(":" + s, s.startsWith("HDEBUG - HI")); + assertTrue("has a stack trace", s.length() > 40); + + appender.setImmediateFlush(true); + assertEquals(true, appender.isActive()); + sw.toss = true; + log.debug("HI"); + log.debug("HI"); + assertEquals(false, appender.isActive()); + } + +} + + Property changes on: tests/src/java/org/apache/log4j/concurrent/WriterAppenderTest.java ___________________________________________________________________ Name: svn:executable + * Index: tests/build.xml =================================================================== --- tests/build.xml (revision 463475) +++ tests/build.xml (working copy) @@ -225,6 +225,7 @@ CachedDateFormat, Encoding, Syslog, + Concurrent, NTEventLogAppender, RFA, ERFA, @@ -810,6 +811,16 @@ + + + + + + + + + +