From 459288958ee9aab2951c4d50bd8d3881a953a295 Mon Sep 17 00:00:00 2001 From: John Gibson Date: Mon, 15 Feb 2016 18:05:54 -0500 Subject: [PATCH 1/5] Added a test for issue #39 (missing configuration files). --- pom.xml | 1 + spring/pom.xml | 14 ++ .../ext/spring/MissingConfigurationTest.java | 145 ++++++++++++++++++ .../ch/qos/logback/ext/spring/fakeLogger.xml | 33 ++++ 4 files changed, 193 insertions(+) create mode 100644 spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java create mode 100644 spring/src/test/resources/ch/qos/logback/ext/spring/fakeLogger.xml diff --git a/pom.xml b/pom.xml index 9fc05f9..e2d18b7 100644 --- a/pom.xml +++ b/pom.xml @@ -90,6 +90,7 @@ UTF-8 1.1.1 + 1.7.6 diff --git a/spring/pom.xml b/spring/pom.xml index 2b28c94..edc59d3 100644 --- a/spring/pom.xml +++ b/spring/pom.xml @@ -72,6 +72,20 @@ true + + + org.springframework + spring-test + ${spring.version} + test + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + test + + diff --git a/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java b/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java new file mode 100644 index 0000000..1cf688a --- /dev/null +++ b/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java @@ -0,0 +1,145 @@ +/** + * Copyright (C) 2016 The MITRE Corporation + * + * 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 ch.qos.logback.ext.spring; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.UnsynchronizedAppenderBase; +import ch.qos.logback.core.joran.spi.JoranException; +import ch.qos.logback.ext.spring.web.WebLogbackConfigurer; +import java.io.File; +import java.util.ArrayList; +import java.util.Random; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestRule; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.impl.StaticLoggerBinder; +import org.springframework.mock.web.MockServletContext; + +/** + * Tests the behavior of the logging configuration if the configuration files are missing. + * @author John Gibson + */ +public class MissingConfigurationTest { + /** + * Parameter specifying the location of the logback config file + */ + private static final String CONFIG_LOCATION_PARAM = "logbackConfigLocation"; + + private static Random rng = new Random(); + + @ClassRule + public static final TemporaryFolder PLAYGROUND = new TemporaryFolder(); + + @Before + public void clearFakeLogs() { + FakeAppender.logs.clear(); + } + + /** + * Aside from printing the test name this also ensures that the logging system is in the known default state. + */ + @Rule + public TestRule ensureLoggingInitialized = new TestWatcher() { + @Override + protected void starting(Description description) { + Logger log = LoggerFactory.getLogger(MissingConfigurationTest.class); + log.info("Starting test: " + description.getMethodName()); + } + }; + + @After + public void cleanupLogging() { + LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory(); + loggerContext.reset(); + } + + @Test(timeout = 1000L) + public void testMissingConfiguration() throws Exception { + MockServletContext context = new MockServletContext(); + File missingConfig = null; + do { + missingConfig = new File("missingConfigTest" + rng.nextInt() + ".xml"); + } while (missingConfig.exists()); + + context.addInitParameter(CONFIG_LOCATION_PARAM, missingConfig.toURI().toURL().toString()); + + WebLogbackConfigurer.initLogging(context); + } + + @Test + public void testInvalidConfiguration() throws Exception { + MockServletContext context = new MockServletContext(); + File empty = PLAYGROUND.newFile("empty.xml"); + context.addInitParameter(CONFIG_LOCATION_PARAM, empty.toURI().toURL().toString()); + try { + WebLogbackConfigurer.initLogging(context); + fail("Error expected."); + } catch (RuntimeException e) { + assertEquals("Unexpected error while configuring logback", e.getMessage()); + assertTrue("Expected a RuntimeException wrapping a JoranException, but it was a different cause: " + e.getCause(), e.getCause() instanceof JoranException); + } + } + + @Test(timeout = 1000L) + public void testMissingFollowedByNormalConfiguration() throws Exception { + MockServletContext context = new MockServletContext(); + File missingConfig = null; + do { + missingConfig = new File("missingConfigTest" + rng.nextInt() + ".xml"); + } while (missingConfig.exists()); + + context.addInitParameter(CONFIG_LOCATION_PARAM, missingConfig.toURI().toURL().toString() + "," + getClass().getResource("fakeLogger.xml")); + WebLogbackConfigurer.initLogging(context); + Logger log = LoggerFactory.getLogger(MissingConfigurationTest.class); + String key = "missingConfig" + rng.nextInt(); + log.error(key); + boolean found = false; + ArrayList logs; + synchronized (FakeAppender.class) { + logs = new ArrayList(FakeAppender.logs); + } + for (ILoggingEvent evt : logs) { + if (key.equals(evt.getMessage())) { + found = true; + } + } + assertTrue("Logging event was not found: " + logs, found); + } + + public static class FakeAppender extends UnsynchronizedAppenderBase { + private static final ArrayList logs = new ArrayList(); + + @Override + protected void append(ILoggingEvent eventObject) { + synchronized(FakeAppender.class) { + logs.add(eventObject); + } + } + } +} diff --git a/spring/src/test/resources/ch/qos/logback/ext/spring/fakeLogger.xml b/spring/src/test/resources/ch/qos/logback/ext/spring/fakeLogger.xml new file mode 100644 index 0000000..07cdb0b --- /dev/null +++ b/spring/src/test/resources/ch/qos/logback/ext/spring/fakeLogger.xml @@ -0,0 +1,33 @@ + + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %throwable{5}%n + + + + + + + + + + + + \ No newline at end of file From 29410265a239e99e5b3d1d5b912c0c034031bc26 Mon Sep 17 00:00:00 2001 From: John Gibson Date: Tue, 16 Feb 2016 09:43:29 -0500 Subject: [PATCH 2/5] Added classpath tests to MissingConfigurationTest (issue #39). --- .../ext/spring/MissingConfigurationTest.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java b/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java index 1cf688a..be42b8f 100644 --- a/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java +++ b/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java @@ -80,7 +80,7 @@ public void cleanupLogging() { } @Test(timeout = 1000L) - public void testMissingConfiguration() throws Exception { + public void testMissingFileConfiguration() throws Exception { MockServletContext context = new MockServletContext(); File missingConfig = null; do { @@ -92,6 +92,13 @@ public void testMissingConfiguration() throws Exception { WebLogbackConfigurer.initLogging(context); } + @Test + public void testMissingClasspathConfiguration() throws Exception { + MockServletContext context = new MockServletContext(); + context.addInitParameter(CONFIG_LOCATION_PARAM, "classpath:ch/qos/logback/ext/spring/does/not/exist/" + rng.nextInt() + ".xml"); + WebLogbackConfigurer.initLogging(context); + } + @Test public void testInvalidConfiguration() throws Exception { MockServletContext context = new MockServletContext(); @@ -132,6 +139,33 @@ public void testMissingFollowedByNormalConfiguration() throws Exception { assertTrue("Logging event was not found: " + logs, found); } + @Test(timeout = 1000L) + public void testMissingFollowedByNormalClasspathConfiguration() throws Exception { + MockServletContext context = new MockServletContext(); + File missingConfig = null; + do { + missingConfig = new File("missingConfigTest" + rng.nextInt() + ".xml"); + } while (missingConfig.exists()); + + context.addInitParameter(CONFIG_LOCATION_PARAM, missingConfig.toURI().toURL().toString() + "," + + "classpath:ch/qos/logback/ext/spring/fakeLogger.xml"); + WebLogbackConfigurer.initLogging(context); + Logger log = LoggerFactory.getLogger(MissingConfigurationTest.class); + String key = "missingConfig" + rng.nextInt(); + log.error(key); + boolean found = false; + ArrayList logs; + synchronized (FakeAppender.class) { + logs = new ArrayList(FakeAppender.logs); + } + for (ILoggingEvent evt : logs) { + if (key.equals(evt.getMessage())) { + found = true; + } + } + assertTrue("Logging event was not found: " + logs, found); + } + public static class FakeAppender extends UnsynchronizedAppenderBase { private static final ArrayList logs = new ArrayList(); From 266fdb98bd4038b154189ef60f4dd3849261cac3 Mon Sep 17 00:00:00 2001 From: John Gibson Date: Mon, 15 Feb 2016 18:06:21 -0500 Subject: [PATCH 3/5] Fixes #39 (missing configuration files in Spring). --- .../logback/ext/spring/LogbackConfigurer.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/spring/src/main/java/ch/qos/logback/ext/spring/LogbackConfigurer.java b/spring/src/main/java/ch/qos/logback/ext/spring/LogbackConfigurer.java index 787d375..f79dee6 100644 --- a/spring/src/main/java/ch/qos/logback/ext/spring/LogbackConfigurer.java +++ b/spring/src/main/java/ch/qos/logback/ext/spring/LogbackConfigurer.java @@ -17,6 +17,8 @@ import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.net.URL; import org.slf4j.impl.StaticLoggerBinder; @@ -62,13 +64,29 @@ private LogbackConfigurer() { * (e.g. "classpath:logback.xml"), an absolute file URL * (e.g. "file:C:/logback.xml), or a plain absolute path in the file system * (e.g. "C:/logback.xml") - * @throws java.io.FileNotFoundException if the location specifies an invalid file path + * @throws java.io.FileNotFoundException if the location is not found or if the location specifies an invalid file path * @throws ch.qos.logback.core.joran.spi.JoranException * Thrown */ public static void initLogging(String location) throws FileNotFoundException, JoranException { String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location); URL url = ResourceUtils.getURL(resolvedLocation); + InputStream check = null; + try { + check = url.openStream(); + } catch (FileNotFoundException e) { + throw e; + } catch (IOException e) { + // Ignore and let the configuration continue in case Logback can handle it successfully + } finally { + if (check != null) { + try { + check.close(); + } catch (IOException e) { + // We can probably eat this safely and let Logback trip the error + } + } + } LoggerContext loggerContext = (LoggerContext)StaticLoggerBinder.getSingleton().getLoggerFactory(); // in the current version logback automatically configures at startup the context, so we have to reset it From 903e18e5d40bf645383d92372873ae8f275ad34e Mon Sep 17 00:00:00 2001 From: John Gibson Date: Fri, 28 Oct 2016 20:21:25 -0400 Subject: [PATCH 4/5] Improved missing logging configuration tests (issue #39). --- pom.xml | 7 + spring/pom.xml | 6 + .../ext/spring/MissingConfigurationTest.java | 145 ++++++++++++++---- 3 files changed, 127 insertions(+), 31 deletions(-) diff --git a/pom.xml b/pom.xml index e2d18b7..41db717 100644 --- a/pom.xml +++ b/pom.xml @@ -91,6 +91,7 @@ 1.1.1 1.7.6 + 1.3 @@ -116,6 +117,12 @@ 4.11 test + + org.hamcrest + hamcrest-core + ${hamcrest.version} + test + diff --git a/spring/pom.xml b/spring/pom.xml index edc59d3..0729047 100644 --- a/spring/pom.xml +++ b/spring/pom.xml @@ -85,6 +85,12 @@ ${slf4j.version} test + + org.hamcrest + hamcrest-library + ${hamcrest.version} + test + diff --git a/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java b/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java index be42b8f..62ebe61 100644 --- a/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java +++ b/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java @@ -15,18 +15,20 @@ */ package ch.qos.logback.ext.spring; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - +import ch.qos.logback.classic.BasicConfigurator; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.UnsynchronizedAppenderBase; import ch.qos.logback.core.joran.spi.JoranException; import ch.qos.logback.ext.spring.web.WebLogbackConfigurer; import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; import java.util.Random; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Matchers; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; @@ -40,6 +42,7 @@ import org.slf4j.LoggerFactory; import org.slf4j.impl.StaticLoggerBinder; import org.springframework.mock.web.MockServletContext; +import static org.junit.Assert.*; /** * Tests the behavior of the logging configuration if the configuration files are missing. @@ -56,6 +59,8 @@ public class MissingConfigurationTest { @ClassRule public static final TemporaryFolder PLAYGROUND = new TemporaryFolder(); + private LogSensitiveMockServletContext context = new LogSensitiveMockServletContext(); + @Before public void clearFakeLogs() { FakeAppender.logs.clear(); @@ -68,20 +73,26 @@ public void clearFakeLogs() { public TestRule ensureLoggingInitialized = new TestWatcher() { @Override protected void starting(Description description) { + LoggerContext loggerContext = doCleanup(); + BasicConfigurator.configure(loggerContext); Logger log = LoggerFactory.getLogger(MissingConfigurationTest.class); log.info("Starting test: " + description.getMethodName()); } }; - @After - public void cleanupLogging() { + private LoggerContext doCleanup() { LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory(); loggerContext.reset(); + return loggerContext; + } + + @After + public void cleanupLogging() { + doCleanup(); } - @Test(timeout = 1000L) + @Test(timeout = 3000L) public void testMissingFileConfiguration() throws Exception { - MockServletContext context = new MockServletContext(); File missingConfig = null; do { missingConfig = new File("missingConfigTest" + rng.nextInt() + ".xml"); @@ -90,18 +101,21 @@ public void testMissingFileConfiguration() throws Exception { context.addInitParameter(CONFIG_LOCATION_PARAM, missingConfig.toURI().toURL().toString()); WebLogbackConfigurer.initLogging(context); + assertThat("Missing configuration file wasn't seen.", context.messages, + Matchers.hasItem((Matchers.containsString(missingConfig.getName())))); } @Test public void testMissingClasspathConfiguration() throws Exception { - MockServletContext context = new MockServletContext(); - context.addInitParameter(CONFIG_LOCATION_PARAM, "classpath:ch/qos/logback/ext/spring/does/not/exist/" + rng.nextInt() + ".xml"); + final String fakeEntry = "classpath:ch/qos/logback/ext/spring/does/not/exist/" + rng.nextInt() + ".xml"; + context.addInitParameter(CONFIG_LOCATION_PARAM, fakeEntry); WebLogbackConfigurer.initLogging(context); + assertThat("Missing configuration classpath wasn't seen.", context.messages, + Matchers.hasItem((Matchers.containsString(fakeEntry)))); } @Test public void testInvalidConfiguration() throws Exception { - MockServletContext context = new MockServletContext(); File empty = PLAYGROUND.newFile("empty.xml"); context.addInitParameter(CONFIG_LOCATION_PARAM, empty.toURI().toURL().toString()); try { @@ -113,57 +127,75 @@ public void testInvalidConfiguration() throws Exception { } } - @Test(timeout = 1000L) + @Test(timeout = 3000L) public void testMissingFollowedByNormalConfiguration() throws Exception { - MockServletContext context = new MockServletContext(); File missingConfig = null; do { missingConfig = new File("missingConfigTest" + rng.nextInt() + ".xml"); } while (missingConfig.exists()); - context.addInitParameter(CONFIG_LOCATION_PARAM, missingConfig.toURI().toURL().toString() + "," + getClass().getResource("fakeLogger.xml")); + File fileConfig = PLAYGROUND.newFile("fakeLogger.xml"); + URL u = getClass().getResource("fakeLogger.xml"); + InputStream is = null; + FileOutputStream os = null; + try { + is = u.openStream(); + os = new FileOutputStream(fileConfig); + byte[] buff = new byte[2048]; + for (int read = is.read(buff); read != -1; read = is.read(buff)) { + os.write(buff, 0, read); + } + } finally { + if (is != null) { + is.close(); + } + if (os != null) { + os.close(); + } + } + + context.addInitParameter(CONFIG_LOCATION_PARAM, missingConfig.toURI().toURL().toString() + "," + + fileConfig.toURI().toURL().toString()); WebLogbackConfigurer.initLogging(context); + assertThat("Missing configuration file wasn't seen.", context.messages, + Matchers.hasItem((Matchers.containsString(missingConfig.getName())))); + assertThat("Actual configuration file wasn't seen.", context.messages, + Matchers.hasItem((Matchers.containsString(fileConfig.toURI().toURL().toString())))); + Logger log = LoggerFactory.getLogger(MissingConfigurationTest.class); String key = "missingConfig" + rng.nextInt(); log.error(key); - boolean found = false; ArrayList logs; synchronized (FakeAppender.class) { logs = new ArrayList(FakeAppender.logs); } - for (ILoggingEvent evt : logs) { - if (key.equals(evt.getMessage())) { - found = true; - } - } - assertTrue("Logging event was not found: " + logs, found); + assertThat(logs, Matchers.hasItem(new LoggingEventMatcher(key))); } - @Test(timeout = 1000L) + @Test(timeout = 3000L) public void testMissingFollowedByNormalClasspathConfiguration() throws Exception { - MockServletContext context = new MockServletContext(); File missingConfig = null; do { missingConfig = new File("missingConfigTest" + rng.nextInt() + ".xml"); } while (missingConfig.exists()); + String cpConfig = "classpath:ch/qos/logback/ext/spring/fakeLogger.xml"; context.addInitParameter(CONFIG_LOCATION_PARAM, missingConfig.toURI().toURL().toString() + "," + - "classpath:ch/qos/logback/ext/spring/fakeLogger.xml"); + cpConfig); WebLogbackConfigurer.initLogging(context); + assertThat("Missing configuration file wasn't seen.", context.messages, + Matchers.hasItem((Matchers.containsString(missingConfig.getName())))); + assertThat("Actual configuration file wasn't seen.", context.messages, + Matchers.hasItem((Matchers.containsString(cpConfig)))); + Logger log = LoggerFactory.getLogger(MissingConfigurationTest.class); String key = "missingConfig" + rng.nextInt(); log.error(key); - boolean found = false; ArrayList logs; synchronized (FakeAppender.class) { logs = new ArrayList(FakeAppender.logs); } - for (ILoggingEvent evt : logs) { - if (key.equals(evt.getMessage())) { - found = true; - } - } - assertTrue("Logging event was not found: " + logs, found); + assertThat(logs, Matchers.hasItem(new LoggingEventMatcher(key))); } public static class FakeAppender extends UnsynchronizedAppenderBase { @@ -176,4 +208,55 @@ protected void append(ILoggingEvent eventObject) { } } } + + private static class LoggingEventMatcher extends BaseMatcher { + private final String message; + + public LoggingEventMatcher(String message) { + this.message = message; + } + + @Override + public boolean matches(Object item) { + if (!(item instanceof ILoggingEvent)) { + return false; + } + return message.equals(((ILoggingEvent) item).getMessage()); + } + + @Override + public void describeTo(org.hamcrest.Description description) { + description.appendText("Logging event with message").appendValue(message); + } + } + + /** + * Spring's MockServletContext class uses a logger to write messages. This interferes with testing the actual + * logging system. In addition it makes it difficult to capture messages that we should expect to be logged to the + * servlet. + */ + private static class LogSensitiveMockServletContext extends MockServletContext { + public final ArrayList messages = new ArrayList(); + private static final String PREFIX = "MockServlet: "; + + @Override + public void log(String message) { + System.out.println(PREFIX + message); + messages.add(message); + } + + @Override + public void log(Exception ex, String message) { + log(message, ex); + } + + @Override + public void log(String message, Throwable ex) { + System.err.println(PREFIX + message); + messages.add(message); + System.err.println(PREFIX + ex.toString()); + messages.add(ex.toString()); + ex.printStackTrace(); + } + } } From 97c67c2c2f56f7cc0acdf947041bbbc57df9ea58 Mon Sep 17 00:00:00 2001 From: John Gibson Date: Mon, 15 Feb 2016 20:22:04 -0500 Subject: [PATCH 5/5] A hack to let MITRE's copyright information to pass the license checks. --- spring/pom.xml | 40 +++++++++++++++++++ .../ext/spring/MissingConfigurationTest.java | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/spring/pom.xml b/spring/pom.xml index 0729047..982a480 100644 --- a/spring/pom.xml +++ b/spring/pom.xml @@ -110,6 +110,46 @@ + + + com.mycila + license-maven-plugin + 2.6 + + + default + + check + + + + **/README* + **/LICENSE* + **/AUTHORS* + src/test/resources/** + src/main/resources/** + src/test/java/**/MissingConfigurationTest* + + + + + mitre-license + + check + + + + The MITRE Corporation + 2016 + jgibson@mitre.org + + + src/test/java/**/MissingConfigurationTest* + + + + + diff --git a/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java b/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java index 62ebe61..8a792d8 100644 --- a/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java +++ b/spring/src/test/java/ch/qos/logback/ext/spring/MissingConfigurationTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2016 The MITRE Corporation + * Copyright (C) 2016 The MITRE Corporation (jgibson@mitre.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.