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 2020/08/01 22:38:39 UTC
[logging-log4j2] branch release-2.x updated: LOG4J2-2883 - Add unit
test
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new 1252c53 LOG4J2-2883 - Add unit test
1252c53 is described below
commit 1252c531844e108c4c6316ecbcda9ae9f2d1aeb4
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sat Aug 1 15:38:19 2020 -0700
LOG4J2-2883 - Add unit test
---
.../FailoverFailedPrimaryAppenderTest.java | 90 ++++++++++++++++++++++
.../rolling/RollingDirectTimeNewDirectoryTest.java | 73 ++++++++++++++++++
.../src/test/resources/log4j-rollOnStartup.json | 2 +-
.../test/resources/log4j-rolling-folder-direct.xml | 39 ++++++++++
4 files changed, 203 insertions(+), 1 deletion(-)
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FailoverFailedPrimaryAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FailoverFailedPrimaryAppenderTest.java
new file mode 100644
index 0000000..93903a2
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FailoverFailedPrimaryAppenderTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.appender;
+
+import java.util.List;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.apache.logging.log4j.test.appender.FailOnceAppender;
+import org.apache.logging.log4j.test.appender.ListAppender;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ *
+ */
+public class FailoverFailedPrimaryAppenderTest {
+ private ListAppender app;
+ private FailOnceAppender foApp;
+ private Logger logger;
+ private Logger onceLogger;
+
+ @ClassRule
+ public static LoggerContextRule init = new LoggerContextRule("log4j-failover.xml");
+
+ @Before
+ public void setUp() throws Exception {
+ app = init.getListAppender("List");
+ foApp = (FailOnceAppender) init.getAppender("Once");
+ logger = init.getLogger("LoggerTest");
+ onceLogger = init.getLogger("Once");
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ if (app != null) {
+ app.clear();
+ }
+ }
+
+ @Test
+ public void testFailover() {
+ logger.error("This is a test");
+ List<LogEvent> events = app.getEvents();
+ assertNotNull(events);
+ assertEquals("Incorrect number of events. Should be 1 is " + events.size(), events.size(), 1);
+ app.clear();
+ logger.error("This is a test");
+ events = app.getEvents();
+ assertNotNull(events);
+ assertEquals("Incorrect number of events. Should be 1 is " + events.size(), events.size(), 1);
+ }
+
+ @Test
+ public void testRecovery() throws Exception {
+ onceLogger.error("Fail once");
+ onceLogger.error("Fail again");
+ List<LogEvent> events = app.getEvents();
+ assertNotNull(events);
+ assertEquals("Incorrect number of events. Should be 2 is " + events.size(), events.size(), 2);
+ app.clear();
+ Thread.sleep(1100);
+ onceLogger.error("Fail after recovery interval");
+ onceLogger.error("Second log message");
+ events = app.getEvents();
+ assertEquals("Did not recover", events.size(), 0);
+ events = foApp.getEvents();
+ assertEquals("Incorrect number of events in primary appender", events.size(), 2);
+ }
+}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingDirectTimeNewDirectoryTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingDirectTimeNewDirectoryTest.java
new file mode 100644
index 0000000..8372a7e
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingDirectTimeNewDirectoryTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.appender.rolling;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+
+import java.io.File;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Tests
+ */
+public class RollingDirectTimeNewDirectoryTest {
+ private static final String CONFIG = "log4j-rolling-folder-direct.xml";
+
+ private static final String DIR = "target/rolling-folder-direct";
+
+ public static LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
+
+ @Rule
+ public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR);
+
+ private Logger logger;
+
+ @Before
+ public void setUp() throws Exception {
+ this.logger = loggerContextRule.getLogger(RollingDirectTimeNewDirectoryTest.class.getName());
+ }
+
+
+ @Test
+ public void streamClosedError() throws Exception {
+ for (int i = 0; i < 1000; i++) {
+ logger.info("nHq6p9kgfvWfjzDRYbZp");
+ }
+ Thread.sleep(1500);
+ for (int i = 0; i < 1000; i++) {
+ logger.info("nHq6p9kgfvWfjzDRYbZp");
+ }
+
+ File tempDirectoryAsFile = new File(DIR);
+ File[] loggingFolders = tempDirectoryAsFile.listFiles();
+ assertNotNull(loggingFolders);
+ // Check if two folders were created
+ assertTrue("Not enough directories created", loggingFolders.length >= 2);
+ for (File dir : loggingFolders) {
+ File[] files = dir.listFiles();
+ assertTrue("No files in directory " + dir.toString(), files != null && files.length > 0);
+ }
+ }
+}
diff --git a/log4j-core/src/test/resources/log4j-rollOnStartup.json b/log4j-core/src/test/resources/log4j-rollOnStartup.json
index 85e9203..5d158b7 100644
--- a/log4j-core/src/test/resources/log4j-rollOnStartup.json
+++ b/log4j-core/src/test/resources/log4j-rollOnStartup.json
@@ -1,5 +1,5 @@
{
- "configuration": { "status": "debug",
+ "configuration": { "status": "warn",
"appenders": {
"Console": {
"name": "Console",
diff --git a/log4j-core/src/test/resources/log4j-rolling-folder-direct.xml b/log4j-core/src/test/resources/log4j-rolling-folder-direct.xml
new file mode 100644
index 0000000..9ded6a3
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-rolling-folder-direct.xml
@@ -0,0 +1,39 @@
+<?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" name="XMLConfigTest">
+ <Properties>
+ <Property name="logDir">target/rolling-folder-direct</Property>
+ </Properties>
+ <Appenders>
+ <RollingFile name="RollingFile" filePattern="${logDir}/%d{MM-dd-yy-HH-mm-ss}/processor.log">
+ <PatternLayout>
+ <Pattern>%d} %p %C{1.} [%t] %m%n</Pattern>
+ </PatternLayout>
+ <TimeBasedTriggeringPolicy />
+ <DirectWriteRolloverStrategy/>
+ </RollingFile>
+ </Appenders>
+
+ <Loggers>
+ <Root level="info">
+ <AppenderRef ref="RollingFile"/>
+ </Root>
+ </Loggers>
+
+</Configuration>
\ No newline at end of file