diff --git a/core/src/test/java/org/jboss/logmanager/AssertingErrorManager.java b/core/src/test/java/org/jboss/logmanager/AssertingErrorManager.java new file mode 100644 index 00000000..b5f0e603 --- /dev/null +++ b/core/src/test/java/org/jboss/logmanager/AssertingErrorManager.java @@ -0,0 +1,104 @@ +/* + * JBoss, Home of Professional Open Source. + * + * Copyright 2019 Red Hat, Inc., and individual contributors + * as indicated by the @author tags. + * + * 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.jboss.logmanager; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.logging.ErrorManager; + +import org.junit.Assert; + +/** + * @author James R. Perkins + */ +public class AssertingErrorManager extends ErrorManager { + + private final int[] allowedCodes; + + private AssertingErrorManager() { + this.allowedCodes = null; + } + + private AssertingErrorManager(final int... allowedCodes) { + this.allowedCodes = allowedCodes; + } + + public static AssertingErrorManager of() { + return new AssertingErrorManager(); + } + + public static AssertingErrorManager of(final int... allowedCodes) { + return new AssertingErrorManager(allowedCodes); + } + + @Override + public void error(final String msg, final Exception ex, final int code) { + if (notAllowed(code)) { + final String codeStr; + switch (code) { + case CLOSE_FAILURE: + codeStr = "CLOSE_FAILURE"; + break; + case FLUSH_FAILURE: + codeStr = "FLUSH_FAILURE"; + break; + case FORMAT_FAILURE: + codeStr = "FORMAT_FAILURE"; + break; + case GENERIC_FAILURE: + codeStr = "GENERIC_FAILURE"; + break; + case OPEN_FAILURE: + codeStr = "OPEN_FAILURE"; + break; + case WRITE_FAILURE: + codeStr = "WRITE_FAILURE"; + break; + default: + codeStr = "INVALID (" + code + ")"; + break; + } + try ( + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw) + ) { + pw.printf("LogManager error of type %s: %s%n", codeStr, msg); + ex.printStackTrace(pw); + Assert.fail(sw.toString()); + } catch (IOException e) { + // This shouldn't happen, but just fail if it does + e.printStackTrace(); + Assert.fail(String.format("Failed to print error message: %s", e.getMessage())); + } + } + } + + private boolean notAllowed(final int code) { + if (allowedCodes != null) { + for (int allowedCode : allowedCodes) { + if (code == allowedCode) { + return false; + } + } + } + return true; + } +} diff --git a/core/src/test/java/org/jboss/logmanager/FileHandlerPerformanceTests.java b/core/src/test/java/org/jboss/logmanager/FileHandlerPerformanceTests.java index a81de9a1..d3116865 100644 --- a/core/src/test/java/org/jboss/logmanager/FileHandlerPerformanceTests.java +++ b/core/src/test/java/org/jboss/logmanager/FileHandlerPerformanceTests.java @@ -38,6 +38,7 @@ private static void initHandler(ExtHandler handler) throws UnsupportedEncodingEx handler.setLevel(Level.ALL); handler.setAutoFlush(true); handler.setEncoding("utf-8"); + handler.setErrorManager(AssertingErrorManager.of()); } private static void publish(final ExtHandler handler, final String msg) { diff --git a/core/src/test/java/org/jboss/logmanager/HandlerTests.java b/core/src/test/java/org/jboss/logmanager/HandlerTests.java index 541e9daa..ed3760d1 100644 --- a/core/src/test/java/org/jboss/logmanager/HandlerTests.java +++ b/core/src/test/java/org/jboss/logmanager/HandlerTests.java @@ -171,6 +171,7 @@ public void testHandlerDelegation() throws Throwable { static class MultiHandler extends ExtHandler { protected MultiHandler() { + setErrorManager(AssertingErrorManager.of()); } public void publish(final LogRecord record) { diff --git a/core/src/test/java/org/jboss/logmanager/handlers/DelayedHandlerTests.java b/core/src/test/java/org/jboss/logmanager/handlers/DelayedHandlerTests.java index 95a4368d..6246dea9 100644 --- a/core/src/test/java/org/jboss/logmanager/handlers/DelayedHandlerTests.java +++ b/core/src/test/java/org/jboss/logmanager/handlers/DelayedHandlerTests.java @@ -30,6 +30,7 @@ import java.util.logging.Logger; import java.util.stream.Collectors; +import org.jboss.logmanager.AssertingErrorManager; import org.jboss.logmanager.ExtHandler; import org.jboss.logmanager.ExtLogRecord; import org.jboss.logmanager.LogContext; @@ -56,6 +57,7 @@ public void testQueuedMessages() { final Logger rootLogger = logContext.getLogger(""); final DelayedHandler delayedHandler = new DelayedHandler(); + delayedHandler.setErrorManager(AssertingErrorManager.of()); rootLogger.addHandler(delayedHandler); rootLogger.info("Test message 1"); rootLogger.fine("Test message 2"); @@ -90,6 +92,7 @@ public void testAllLoggedAfterActivation() throws Exception { final LogContext logContext = LogContext.create(); final DelayedHandler handler = new DelayedHandler(); + handler.setErrorManager(AssertingErrorManager.of()); final Logger rootLogger = logContext.getLogger(""); rootLogger.addHandler(handler); @@ -130,6 +133,7 @@ public void testAllLoggedMidActivation() throws Exception { final LogContext logContext = LogContext.create(); final DelayedHandler handler = new DelayedHandler(); + handler.setErrorManager(AssertingErrorManager.of()); final Logger rootLogger = logContext.getLogger(""); rootLogger.addHandler(handler); final Random r = new Random(); @@ -175,6 +179,7 @@ public void testOrder() throws Exception { final LogContext logContext = LogContext.create(); final DelayedHandler handler = new DelayedHandler(); + handler.setErrorManager(AssertingErrorManager.of()); final Logger rootLogger = logContext.getLogger(""); rootLogger.addHandler(handler); final Random r = new Random(); @@ -230,6 +235,11 @@ private static ExecutorService createExecutor() { public static class TestHandler extends ExtHandler { static final List MESSAGES = new ArrayList<>(); + @SuppressWarnings("WeakerAccess") + public TestHandler() { + setErrorManager(AssertingErrorManager.of()); + } + @Override protected synchronized void doPublish(final ExtLogRecord record) { MESSAGES.add(record); diff --git a/core/src/test/java/org/jboss/logmanager/handlers/ExtHandlerTests.java b/core/src/test/java/org/jboss/logmanager/handlers/ExtHandlerTests.java index 4cfdb724..9450471e 100644 --- a/core/src/test/java/org/jboss/logmanager/handlers/ExtHandlerTests.java +++ b/core/src/test/java/org/jboss/logmanager/handlers/ExtHandlerTests.java @@ -21,6 +21,7 @@ import java.util.logging.SimpleFormatter; +import org.jboss.logmanager.AssertingErrorManager; import org.jboss.logmanager.ExtHandler; import org.jboss.logmanager.formatters.PatternFormatter; import org.junit.Assert; @@ -140,6 +141,10 @@ public void testCallerCalculationNonExtFormatter() throws Exception { static class CloseHandler extends ExtHandler { private boolean closed = false; + CloseHandler() { + setErrorManager(AssertingErrorManager.of()); + } + @Override public void close() { closed = true; diff --git a/core/src/test/java/org/jboss/logmanager/handlers/OutputStreamHandlerTest.java b/core/src/test/java/org/jboss/logmanager/handlers/OutputStreamHandlerTest.java index 9854184c..d0e79ef1 100644 --- a/core/src/test/java/org/jboss/logmanager/handlers/OutputStreamHandlerTest.java +++ b/core/src/test/java/org/jboss/logmanager/handlers/OutputStreamHandlerTest.java @@ -18,75 +18,79 @@ */ package org.jboss.logmanager.handlers; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + import java.io.IOException; import java.io.StringWriter; import java.util.logging.Formatter; import java.util.logging.LogRecord; -import static org.hamcrest.core.Is.is; +import org.jboss.logmanager.AssertingErrorManager; import org.jboss.logmanager.ExtLogRecord; import org.jboss.logmanager.Level; import org.jboss.logmanager.handlers.ConsoleHandler.Target; import org.junit.After; -import static org.junit.Assert.assertThat; import org.junit.Before; import org.junit.Test; /** - * * @author Emmanuel Hugonnet (c) 2013 Red Hat, inc. */ public class OutputStreamHandlerTest { private StringWriter out; private OutputStreamHandler handler; - + private static final Formatter NO_FORMATTER = new Formatter() { public String format(final LogRecord record) { return record.getMessage(); } }; - + public OutputStreamHandlerTest() { } - + @Before public void prepareBuffer() { out = new StringWriter(); } - + @After public void cleanAll() throws IOException { - handler.flush(); - handler.close(); - out.close(); + handler.flush(); + handler.close(); + out.close(); } - + @Test public void testSetEncoding() throws Exception { handler = new OutputStreamHandler(); + handler.setErrorManager(AssertingErrorManager.of()); handler.setEncoding("UTF-8"); assertThat(handler.getEncoding(), is("UTF-8")); } - - @Test - public void testSetEncodingOnOutputStream() throws Exception { - handler = new ConsoleHandler(Target.CONSOLE, NO_FORMATTER); + + @Test + public void testSetEncodingOnOutputStream() throws Exception { + handler = new ConsoleHandler(Target.CONSOLE, NO_FORMATTER); + handler.setErrorManager(AssertingErrorManager.of()); handler.setWriter(out); handler.setEncoding("UTF-8"); assertThat(handler.getEncoding(), is("UTF-8")); - handler.publish(new ExtLogRecord(Level.INFO, "Hello World", getClass().getName())); + handler.publish(new ExtLogRecord(Level.INFO, "Hello World", getClass().getName())); assertThat(out.toString(), is("Hello World")); } - + @Test public void testSetNullEncodingOnOutputStream() throws Exception { handler = new OutputStreamHandler(NO_FORMATTER); + handler.setErrorManager(AssertingErrorManager.of()); handler.setWriter(out); handler.setEncoding(null); handler.publish(new ExtLogRecord(Level.INFO, "Hello World", getClass().getName())); - assertThat(out.toString(), is("Hello World")); + assertThat(out.toString(), is("Hello World")); } - - + + } diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/AbstractHandlerTest.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/AbstractHandlerTest.java index da05986a..d1fae457 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/AbstractHandlerTest.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/AbstractHandlerTest.java @@ -105,6 +105,7 @@ static boolean deleteChildrenRecursively(final File dir) { protected static void configureHandlerDefaults(final ExtHandler handler) { handler.setAutoFlush(true); handler.setFormatter(FORMATTER); + handler.setErrorManager(AssertingErrorManager.of()); } protected ExtLogRecord createLogRecord(final String msg) { diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/AssertingErrorManager.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/AssertingErrorManager.java new file mode 100644 index 00000000..0e3b85f2 --- /dev/null +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/AssertingErrorManager.java @@ -0,0 +1,104 @@ +/* + * JBoss, Home of Professional Open Source. + * + * Copyright 2019 Red Hat, Inc., and individual contributors + * as indicated by the @author tags. + * + * 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.jboss.logmanager.ext.handlers; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.logging.ErrorManager; + +import org.junit.Assert; + +/** + * @author James R. Perkins + */ +public class AssertingErrorManager extends ErrorManager { + + private final int[] allowedCodes; + + private AssertingErrorManager() { + this.allowedCodes = null; + } + + private AssertingErrorManager(final int... allowedCodes) { + this.allowedCodes = allowedCodes; + } + + public static AssertingErrorManager of() { + return new AssertingErrorManager(); + } + + public static AssertingErrorManager of(final int... allowedCodes) { + return new AssertingErrorManager(allowedCodes); + } + + @Override + public void error(final String msg, final Exception ex, final int code) { + if (notAllowed(code)) { + final String codeStr; + switch (code) { + case CLOSE_FAILURE: + codeStr = "CLOSE_FAILURE"; + break; + case FLUSH_FAILURE: + codeStr = "FLUSH_FAILURE"; + break; + case FORMAT_FAILURE: + codeStr = "FORMAT_FAILURE"; + break; + case GENERIC_FAILURE: + codeStr = "GENERIC_FAILURE"; + break; + case OPEN_FAILURE: + codeStr = "OPEN_FAILURE"; + break; + case WRITE_FAILURE: + codeStr = "WRITE_FAILURE"; + break; + default: + codeStr = "INVALID (" + code + ")"; + break; + } + try ( + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw) + ) { + pw.printf("LogManager error of type %s: %s%n", codeStr, msg); + ex.printStackTrace(pw); + Assert.fail(sw.toString()); + } catch (IOException e) { + // This shouldn't happen, but just fail if it does + e.printStackTrace(); + Assert.fail(String.format("Failed to print error message: %s", e.getMessage())); + } + } + } + + private boolean notAllowed(final int code) { + if (allowedCodes != null) { + for (int allowedCode : allowedCodes) { + if (code == allowedCode) { + return false; + } + } + } + return true; + } +} diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/AsyncHandlerTests.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/AsyncHandlerTests.java index 1eab456a..54c1b38d 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/AsyncHandlerTests.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/AsyncHandlerTests.java @@ -108,6 +108,7 @@ static class BlockingQueueHandler extends ExtHandler { BlockingQueueHandler() { queue = new LinkedBlockingDeque(); + setErrorManager(AssertingErrorManager.of()); } @Override diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicRotatingFileHandlerTests.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicRotatingFileHandlerTests.java index 1f6e500d..06d5144a 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicRotatingFileHandlerTests.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicRotatingFileHandlerTests.java @@ -29,6 +29,7 @@ import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.List; +import java.util.logging.ErrorManager; import org.jboss.byteman.contrib.bmunit.BMRule; import org.jboss.byteman.contrib.bmunit.BMUnitRunner; @@ -59,6 +60,7 @@ public void createHandler() throws FileNotFoundException { handler.setFormatter(FORMATTER); // Set append to true to ensure the rotated file is overwritten handler.setAppend(true); + handler.setErrorManager(AssertingErrorManager.of()); } @After @@ -112,6 +114,7 @@ public void testFailedRotate() throws Exception { final int nextDay = currentDay + 1; // Set to false for this specific test handler.setAppend(false); + handler.setErrorManager(AssertingErrorManager.of(ErrorManager.GENERIC_FAILURE)); final String currentDate = sdf.format(cal.getTime()); diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicSizeRotatingFileHandlerTests.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicSizeRotatingFileHandlerTests.java index a535804d..a8692128 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicSizeRotatingFileHandlerTests.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/PeriodicSizeRotatingFileHandlerTests.java @@ -31,6 +31,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.logging.ErrorManager; import org.jboss.byteman.contrib.bmunit.BMRule; import org.jboss.byteman.contrib.bmunit.BMUnitRunner; @@ -217,6 +218,7 @@ public void testArchiveRotateSizeOnlyZip() throws Exception { public void testFailedRotate() throws Exception { final PeriodicSizeRotatingFileHandler handler = new PeriodicSizeRotatingFileHandler(); configureHandlerDefaults(handler); + handler.setErrorManager(AssertingErrorManager.of(ErrorManager.GENERIC_FAILURE)); handler.setRotateSize(1024L); handler.setMaxBackupIndex(5); handler.setFile(logFile); diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/QueueHandlerTests.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/QueueHandlerTests.java index 495524d4..ec3e4cb8 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/QueueHandlerTests.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/QueueHandlerTests.java @@ -38,6 +38,7 @@ public class QueueHandlerTests extends AbstractHandlerTest { @Test public void testQueueSize() throws Exception { final QueueHandler handler = new QueueHandler(5); + handler.setErrorManager(AssertingErrorManager.of()); // Log 6 records and ensure only 5 are available for (int i = 0; i < 6; i++) { @@ -62,6 +63,7 @@ public void testNestedHandlers() throws Exception { nestedHandler.setLevel(Level.INFO); final QueueHandler handler = new QueueHandler(10); + handler.setErrorManager(AssertingErrorManager.of()); handler.setLevel(Level.ERROR); handler.addHandler(nestedHandler); @@ -84,6 +86,10 @@ public void testNestedHandlers() throws Exception { static class NestedHandler extends ExtHandler { private final List records = new ArrayList(); + NestedHandler() { + setErrorManager(AssertingErrorManager.of()); + } + @Override protected void doPublish(final ExtLogRecord record) { records.add(record); diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/SizeRotatingFileHandlerTests.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/SizeRotatingFileHandlerTests.java index 075dca9d..f14d98d3 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/SizeRotatingFileHandlerTests.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/SizeRotatingFileHandlerTests.java @@ -26,6 +26,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import java.util.logging.ErrorManager; import org.jboss.byteman.contrib.bmunit.BMRule; import org.jboss.byteman.contrib.bmunit.BMUnitRunner; @@ -181,6 +182,7 @@ public void testArchiveRotateZip() throws Exception { public void testFailedRotate() throws Exception { final SizeRotatingFileHandler handler = new SizeRotatingFileHandler(); configureHandlerDefaults(handler); + handler.setErrorManager(AssertingErrorManager.of(ErrorManager.GENERIC_FAILURE)); handler.setRotateSize(1024L); handler.setMaxBackupIndex(5); handler.setFile(logFile); diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/SocketHandlerTests.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/SocketHandlerTests.java index 827ed55f..77945516 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/SocketHandlerTests.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/SocketHandlerTests.java @@ -24,6 +24,7 @@ import java.net.UnknownHostException; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; +import java.util.logging.ErrorManager; import javax.net.ssl.SSLContext; import org.jboss.logmanager.ExtLogRecord; @@ -145,6 +146,7 @@ public void testProtocolChange() throws Exception { @Test public void testTcpReconnect() throws Exception { try (SocketHandler handler = createHandler(Protocol.TCP)) { + handler.setErrorManager(AssertingErrorManager.of(ErrorManager.FLUSH_FAILURE)); // Publish a record to a running server try ( @@ -193,6 +195,7 @@ public void testTlsConfig() throws Exception { socketHandler.setAutoFlush(true); socketHandler.setEncoding("utf-8"); socketHandler.setFormatter(patternFormatter); + socketHandler.setErrorManager(AssertingErrorManager.of()); // Create the root logger final Logger rootLogger = logContext.getLogger(""); rootLogger.addHandler(socketHandler); @@ -208,6 +211,7 @@ private SocketHandler createHandler(final Protocol protocol) throws UnsupportedE handler.setAutoFlush(true); handler.setEncoding("utf-8"); handler.setFormatter(new PatternFormatter("%s\n")); + handler.setErrorManager(AssertingErrorManager.of()); return handler; } diff --git a/ext/src/test/java/org/jboss/logmanager/ext/handlers/SyslogHandlerTests.java b/ext/src/test/java/org/jboss/logmanager/ext/handlers/SyslogHandlerTests.java index 067dfe3c..1f23d80d 100644 --- a/ext/src/test/java/org/jboss/logmanager/ext/handlers/SyslogHandlerTests.java +++ b/ext/src/test/java/org/jboss/logmanager/ext/handlers/SyslogHandlerTests.java @@ -49,6 +49,7 @@ public class SyslogHandlerTests { public void setupHandler() throws Exception { handler = new SyslogHandler(HOSTNAME, PORT); handler.setFormatter(new PatternFormatter("%s")); + handler.setErrorManager(AssertingErrorManager.of()); } @After