Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LOGMGR-257] Add an error manager which asserts no errors were found … #262

Merged
merged 1 commit into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions core/src/test/java/org/jboss/logmanager/AssertingErrorManager.java
Original file line number Diff line number Diff line change
@@ -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 <a href="mailto:[email protected]">James R. Perkins</a>
*/
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions core/src/test/java/org/jboss/logmanager/HandlerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -230,6 +235,11 @@ private static ExecutorService createExecutor() {
public static class TestHandler extends ExtHandler {
static final List<ExtLogRecord> MESSAGES = new ArrayList<>();

@SuppressWarnings("WeakerAccess")
public TestHandler() {
setErrorManager(AssertingErrorManager.of());
}

@Override
protected synchronized void doPublish(final ExtLogRecord record) {
MESSAGES.add(record);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="mailto:[email protected]">Emmanuel Hugonnet</a> (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"));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading