-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from jamezp/LOGMGR-257
[LOGMGR-257] Add an error manager which asserts no errors were found …
- Loading branch information
Showing
15 changed files
with
269 additions
and
20 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
core/src/test/java/org/jboss/logmanager/AssertingErrorManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.