You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2016/04/26 06:31:07 UTC
[11/34] logging-log4j2 git commit: LOG4J2-1342 added tests to verify
parameters are passed correctly with async logging
LOG4J2-1342 added tests to verify parameters are passed correctly with async logging
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/873e19b4
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/873e19b4
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/873e19b4
Branch: refs/heads/LOG4j2-494
Commit: 873e19b4258032a253cc5baee03b4919fc170397
Parents: 6ef68fa
Author: rpopma <rp...@apache.org>
Authored: Fri Apr 22 12:08:38 2016 +0900
Committer: Ralph Goers <rg...@nextiva.com>
Committed: Mon Apr 25 21:30:28 2016 -0700
----------------------------------------------------------------------
.../layout/CsvParameterLayoutAllAsyncTest.java | 69 ++++++++++++++++++
.../core/layout/CsvParameterLayoutTest.java | 73 ++++++++++++--------
.../src/test/resources/csvParamsMixedAsync.xml | 13 ++++
log4j-core/src/test/resources/csvParamsSync.xml | 29 ++++++++
4 files changed, 156 insertions(+), 28 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/873e19b4/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutAllAsyncTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutAllAsyncTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutAllAsyncTest.java
new file mode 100644
index 0000000..0ef2e1d
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutAllAsyncTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.logging.log4j.core.layout;
+
+import org.apache.commons.csv.CSVFormat;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.async.AsyncLoggerContextSelector;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.util.Constants;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.apache.logging.log4j.util.Strings;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Tests {@link AbstractCsvLayout} with all loggers async.
+ *
+ * @since 2.6
+ */
+public class CsvParameterLayoutAllAsyncTest {
+
+ @BeforeClass
+ public static void beforeClass() {
+ System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR,
+ AsyncLoggerContextSelector.class.getName());
+ System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
+ "AsyncLoggerTest.xml");
+ }
+
+ @AfterClass
+ public static void afterClass() {
+ System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, Strings.EMPTY);
+ }
+
+
+ @Test
+ public void testLayoutDefaultNormal() throws Exception {
+ Logger root = (Logger) LogManager.getRootLogger();
+ CsvParameterLayoutTest.testLayoutNormalApi(root, CsvParameterLayout.createDefaultLayout(), false);
+ }
+
+ @Test
+ public void testLayoutDefaultObjectArrayMessage() throws Exception {
+ Logger root = (Logger) LogManager.getRootLogger();
+ CsvParameterLayoutTest.testLayoutNormalApi(root, CsvParameterLayout.createDefaultLayout(), true);
+ }
+
+ @Test
+ public void testLayoutTab() throws Exception {
+ Logger root = (Logger) LogManager.getRootLogger();
+ CsvParameterLayoutTest.testLayoutNormalApi(root, CsvParameterLayout.createLayout(CSVFormat.TDF), true);
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/873e19b4/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutTest.java
index 5f58d79..ce3abfc 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/CsvParameterLayoutTest.java
@@ -16,9 +16,9 @@
*/
package org.apache.logging.log4j.core.layout;
-import static org.junit.Assert.assertEquals;
-
import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -26,43 +26,49 @@ import org.apache.commons.csv.CSVFormat;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.Appender;
-import org.apache.logging.log4j.core.BasicConfigurationFactory;
import org.apache.logging.log4j.core.Logger;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.junit.LoggerContextRule;
import org.apache.logging.log4j.message.ObjectArrayMessage;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.AfterClass;
import org.junit.Assert;
-import org.junit.BeforeClass;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.junit.Assert.*;
/**
* Tests {@link AbstractCsvLayout}.
*
* @since 2.4
*/
+@RunWith(value = Parameterized.class)
public class CsvParameterLayoutTest {
- static ConfigurationFactory cf = new BasicConfigurationFactory();
+ @Parameterized.Parameters(name = "{0}")
+ public static Collection<Object[]> data() {
+ return Arrays.asList(
+ new Object[][]{
+ { new LoggerContextRule("csvParamsSync.xml"), },
+ { new LoggerContextRule("csvParamsMixedAsync.xml"), },
+ }
+ );
+ }
- @AfterClass
- public static void cleanupClass() {
- ConfigurationFactory.removeConfigurationFactory(cf);
- ThreadContext.clearAll();
+ @Rule
+ public final LoggerContextRule init;
+
+ public CsvParameterLayoutTest(final LoggerContextRule contextRule) {
+ this.init = contextRule;
}
- @BeforeClass
- public static void setupClass() {
+ @AfterClass
+ public static void cleanupClass() {
+// System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, null);
ThreadContext.clearAll();
- ConfigurationFactory.setConfigurationFactory(cf);
- final LoggerContext ctx = LoggerContext.getContext();
- ctx.reconfigure();
}
- private final LoggerContext ctx = LoggerContext.getContext();
-
- private final Logger root = ctx.getLogger("");
-
@Test
public void testCustomCharset() {
final AbstractCsvLayout layout = CsvParameterLayout.createLayout(null, "Excel", null, null, null, null, null,
@@ -82,7 +88,7 @@ public class CsvParameterLayoutTest {
assertEquals("text/csv; charset=UTF-8", layout.getContentType());
}
- private void testLayoutNormalApi(final AbstractCsvLayout layout, boolean messageApi) throws Exception {
+ static void testLayoutNormalApi(final Logger root, final AbstractCsvLayout layout, boolean messageApi) throws Exception {
final Map<String, Appender> appenders = root.getAppenders();
for (final Appender appender : appenders.values()) {
root.removeAppender(appender);
@@ -97,11 +103,19 @@ public class CsvParameterLayoutTest {
// output messages
if (messageApi) {
- logDebugObjectArrayMessage();
+ logDebugObjectArrayMessage(root);
} else {
- logDebugNormalApi();
+ logDebugNormalApi(root);
+ }
+
+ // wait until background thread finished processing
+ int maxTries = 50;
+ while (appender.getMessages().size() < 4 && maxTries-- > 0) {
+ Thread.sleep(1L);
}
+ assertEquals("Background thread did not finish processing: msg count", 4, appender.getMessages().size());
+ // don't stop appender until background thread is done
appender.stop();
final List<String> list = appender.getMessages();
@@ -112,14 +126,14 @@ public class CsvParameterLayoutTest {
Assert.assertEquals("7" + d + "8" + d + "9" + d + "10", list.get(3));
}
- private void logDebugNormalApi() {
+ private static void logDebugNormalApi(final Logger root) {
root.debug("with placeholders: {}{}{}", 1, 2, 3);
root.debug("without placeholders", 2, 3);
root.debug(null, 5, 6);
root.debug("invalid placeholder count {}", 7, 8, 9, 10);
}
- private void logDebugObjectArrayMessage() {
+ private static void logDebugObjectArrayMessage(final Logger root) {
root.debug(new ObjectArrayMessage(1, 2, 3));
root.debug(new ObjectArrayMessage(2, 3));
root.debug(new ObjectArrayMessage(5, 6));
@@ -128,16 +142,19 @@ public class CsvParameterLayoutTest {
@Test
public void testLayoutDefaultNormal() throws Exception {
- testLayoutNormalApi(CsvParameterLayout.createDefaultLayout(), false);
+ Logger root = this.init.getLogger("");
+ testLayoutNormalApi(root, CsvParameterLayout.createDefaultLayout(), false);
}
@Test
public void testLayoutDefaultObjectArrayMessage() throws Exception {
- testLayoutNormalApi(CsvParameterLayout.createDefaultLayout(), true);
+ Logger root = this.init.getLogger("");
+ testLayoutNormalApi(root, CsvParameterLayout.createDefaultLayout(), true);
}
@Test
public void testLayoutTab() throws Exception {
- testLayoutNormalApi(CsvParameterLayout.createLayout(CSVFormat.TDF), true);
+ Logger root = this.init.getLogger("");
+ testLayoutNormalApi(root, CsvParameterLayout.createLayout(CSVFormat.TDF), true);
}
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/873e19b4/log4j-core/src/test/resources/csvParamsMixedAsync.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/csvParamsMixedAsync.xml b/log4j-core/src/test/resources/csvParamsMixedAsync.xml
new file mode 100644
index 0000000..4d0811a
--- /dev/null
+++ b/log4j-core/src/test/resources/csvParamsMixedAsync.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="OFF">
+ <Appenders>
+ <List name="List">
+ <PatternLayout pattern="[%-5level] %c{1.} %msg%n" />
+ </List>
+ </Appenders>
+ <Loggers>
+ <AsyncRoot level="debug" includeLocation="false">
+ <appender-ref ref="List"/>
+ </AsyncRoot>
+ </Loggers>
+</Configuration>
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/873e19b4/log4j-core/src/test/resources/csvParamsSync.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/csvParamsSync.xml b/log4j-core/src/test/resources/csvParamsSync.xml
new file mode 100644
index 0000000..c2a92c3
--- /dev/null
+++ b/log4j-core/src/test/resources/csvParamsSync.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You 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.
+ -->
+<Configuration status="WARN">
+ <Appenders>
+ <List name="List">
+ <PatternLayout pattern="[%-5level] %c{1.} %msg%n" />
+ </List>
+ </Appenders>
+ <Loggers>
+ <Root level="debug">
+ <AppenderRef ref="List" />
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file