You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2020/09/15 18:57:54 UTC
[logging-log4j2] 01/10: Migrate MemoryMappedFileAppender tests to
JUnit 5
This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 0f5b9ebf3b40d2bc8b1cb82c0037e7e3b4a4add2
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sun Sep 13 16:29:19 2020 -0500
Migrate MemoryMappedFileAppender tests to JUnit 5
Backported from 3.x.
Signed-off-by: Matt Sicker <bo...@gmail.com>
---
.../MemoryMappedFileAppenderLocationTest.java | 95 ----------------
.../MemoryMappedFileAppenderRemapTest.java | 95 ----------------
.../MemoryMappedFileAppenderSimpleTest.java | 86 ---------------
.../appender/MemoryMappedFileAppenderTest.java | 120 +++++++++++++++++++++
.../MemoryMappedFileAppenderLocationTest.xml | 4 +-
.../MemoryMappedFileAppenderRemapTest.xml | 4 +-
.../resources/MemoryMappedFileAppenderTest.xml | 4 +-
7 files changed, 126 insertions(+), 282 deletions(-)
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java
deleted file mode 100644
index 8f2c4b7..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * 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.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.CoreLoggerContexts;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.apache.logging.log4j.core.util.Integers;
-import org.junit.Before;
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.*;
-
-import static org.junit.Assert.*;
-
-/**
- * Tests that logged strings and their location appear in the file,
- * that the file size is the next power of two of the specified mapped region length
- * and that the file is shrunk to its actual usage when done.
- *
- * @since 2.1
- */
-public class MemoryMappedFileAppenderLocationTest {
-
- final String LOGFILE = "target/MemoryMappedFileAppenderLocationTest.log";
-
- @Before
- public void before() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
- "MemoryMappedFileAppenderLocationTest.xml");
- }
-
- @Test
- public void testMemMapLocation() throws Exception {
- final File f = new File(LOGFILE);
- if (f.exists()) {
- assertTrue("deleted ok", f.delete());
- }
- assertTrue(!f.exists());
-
- final int expectedFileLength = Integers.ceilingNextPowerOfTwo(32000);
- assertEquals(32768, expectedFileLength);
-
- final Logger log = LogManager.getLogger();
- try {
- log.warn("Test log1");
- assertTrue(f.exists());
- assertEquals("initial length", expectedFileLength, f.length());
-
- log.warn("Test log2");
- assertEquals("not grown", expectedFileLength, f.length());
- } finally {
- CoreLoggerContexts.stopLoggerContext(false);
- }
- final int LINESEP = System.lineSeparator().length();
- assertEquals("Shrunk to actual used size", 474 + 2 * LINESEP, f.length());
-
- String line1, line2, line3;
- try (final BufferedReader reader = new BufferedReader(new FileReader(LOGFILE))) {
- line1 = reader.readLine();
- line2 = reader.readLine();
- line3 = reader.readLine();
- }
- assertNotNull(line1);
- assertThat(line1, containsString("Test log1"));
- final String location1 = "org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderLocationTest.testMemMapLocation(MemoryMappedFileAppenderLocationTest.java:65)";
- assertThat(line1, containsString(location1));
-
- assertNotNull(line2);
- assertThat(line2, containsString("Test log2"));
- final String location2 = "org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderLocationTest.testMemMapLocation(MemoryMappedFileAppenderLocationTest.java:69)";
- assertThat(line2, containsString(location2));
-
- assertNull("only two lines were logged", line3);
- }
-}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java
deleted file mode 100644
index c4d647b..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * 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.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.util.Arrays;
-
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.CoreLoggerContexts;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.junit.Before;
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.*;
-
-import static org.junit.Assert.*;
-
-/**
- * Tests that logged strings appear in the file, that the initial file size is the specified specified region length,
- * that the file is extended by region length when necessary, and that the file is shrunk to its actual usage when done.
- *
- * @since 2.1
- */
-public class MemoryMappedFileAppenderRemapTest {
-
- final String LOGFILE = "target/MemoryMappedFileAppenderRemapTest.log";
-
- @Before
- public void before() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "MemoryMappedFileAppenderRemapTest.xml");
- }
-
- @Test
- public void testMemMapExtendsIfNeeded() throws Exception {
- final File f = new File(LOGFILE);
- if (f.exists()) {
- assertTrue(f.delete());
- }
- assertTrue(!f.exists());
-
- final Logger log = LogManager.getLogger();
- final char[] text = new char[200];
- Arrays.fill(text, 'A');
- try {
- log.warn("Test log1");
- assertTrue(f.exists());
- assertEquals("initial length", 256, f.length());
-
- log.warn(new String(text));
- assertEquals("grown", 256 * 2, f.length());
-
- log.warn(new String(text));
- assertEquals("grown again", 256 * 3, f.length());
- } finally {
- CoreLoggerContexts.stopLoggerContext(false);
- }
- final int LINESEP = System.lineSeparator().length();
- assertEquals("Shrunk to actual used size", 658 + 3 * LINESEP, f.length());
-
- String line1, line2, line3, line4;
- try (final BufferedReader reader = new BufferedReader(new FileReader(LOGFILE))) {
- line1 = reader.readLine();
- line2 = reader.readLine();
- line3 = reader.readLine();
- line4 = reader.readLine();
- }
- assertNotNull(line1);
- assertThat(line1, containsString("Test log1"));
-
- assertNotNull(line2);
- assertThat(line2, containsString(new String(text)));
-
- assertNotNull(line3);
- assertThat(line3, containsString(new String(text)));
-
- assertNull("only three lines were logged", line4);
- }
-}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java
deleted file mode 100644
index ebab4cd..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * 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.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.CoreLoggerContexts;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.junit.Before;
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.*;
-
-import static org.junit.Assert.*;
-
-/**
- * Tests that logged strings appear in the file,
- * that the default file size is used if not specified
- * and that the file is shrunk to its actual usage when done.
- *
- * @since 2.1
- */
-public class MemoryMappedFileAppenderSimpleTest {
-
- final String LOGFILE = "target/MemoryMappedFileAppenderTest.log";
-
- @Before
- public void before() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "MemoryMappedFileAppenderTest.xml");
- }
-
- @Test
- public void testMemMapBasics() throws Exception {
- final File f = new File(LOGFILE);
- if (f.exists()) {
- assertTrue(f.delete());
- }
- assertTrue(!f.exists());
-
- final Logger log = LogManager.getLogger();
- try {
- log.warn("Test log1");
- assertTrue(f.exists());
- assertEquals("initial length", MemoryMappedFileManager.DEFAULT_REGION_LENGTH, f.length());
-
- log.warn("Test log2");
- assertEquals("not grown", MemoryMappedFileManager.DEFAULT_REGION_LENGTH, f.length());
- } finally {
- CoreLoggerContexts.stopLoggerContext(false);
- }
- final int LINESEP = System.lineSeparator().length();
- assertEquals("Shrunk to actual used size", 186 + 2 * LINESEP, f.length());
-
- String line1, line2, line3;
- try (final BufferedReader reader = new BufferedReader(new FileReader(LOGFILE))) {
- line1 = reader.readLine();
- line2 = reader.readLine();
- line3 = reader.readLine();
- }
- assertNotNull(line1);
- assertThat(line1, containsString("Test log1"));
-
- assertNotNull(line2);
- assertThat(line2, containsString("Test log2"));
-
- assertNull("only two lines were logged", line3);
- }
-}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderTest.java
new file mode 100644
index 0000000..9dcf88e
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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 org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.util.Integers;
+import org.apache.logging.log4j.junit.CleanUpFiles;
+import org.apache.logging.log4j.junit.LoggerContextSource;
+import org.junit.jupiter.api.Test;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests that logged strings appear in the file, that the initial file size is the specified specified region length,
+ * that the file is extended by region length when necessary, and that the file is shrunk to its actual usage when done.
+ *
+ * @since 2.1
+ */
+@CleanUpFiles({
+ "target/MemoryMappedFileAppenderTest.log",
+ "target/MemoryMappedFileAppenderRemapTest.log",
+ "target/MemoryMappedFileAppenderLocationTest.log"
+})
+public class MemoryMappedFileAppenderTest {
+
+ @Test
+ @LoggerContextSource("MemoryMappedFileAppenderTest.xml")
+ public void testMemMapBasics(final LoggerContext context) throws Exception {
+ final Logger log = context.getLogger(getClass());
+ final Path logFile = Paths.get("target", "MemoryMappedFileAppenderTest.log");
+ try {
+ log.warn("Test log1");
+ assertTrue(Files.exists(logFile));
+ assertEquals(MemoryMappedFileManager.DEFAULT_REGION_LENGTH, Files.size(logFile));
+ log.warn("Test log2");
+ assertEquals(MemoryMappedFileManager.DEFAULT_REGION_LENGTH, Files.size(logFile));
+ } finally {
+ context.stop();
+ }
+ final int LINESEP = System.lineSeparator().length();
+ assertEquals(18 + 2 * LINESEP, Files.size(logFile));
+
+ final List<String> lines = Files.readAllLines(logFile);
+ assertThat(lines, both(hasSize(2)).and(contains("Test log1", "Test log2")));
+ }
+
+ @Test
+ @LoggerContextSource("MemoryMappedFileAppenderRemapTest.xml")
+ public void testMemMapExtendsIfNeeded(final LoggerContext context) throws Exception {
+ final Logger log = context.getLogger(getClass());
+ final Path logFile = Paths.get("target", "MemoryMappedFileAppenderRemapTest.log");
+ final char[] text = new char[256];
+ Arrays.fill(text, 'A');
+ final String str = new String(text);
+ try {
+ log.warn("Test log1");
+ assertTrue(Files.exists(logFile));
+ assertEquals(256, Files.size(logFile));
+ log.warn(str);
+ assertEquals(2 * 256, Files.size(logFile));
+ log.warn(str);
+ assertEquals(3 * 256, Files.size(logFile));
+ } finally {
+ context.stop();
+ }
+ assertEquals(521 + 3 * System.lineSeparator().length(), Files.size(logFile), "Expected file size to shrink");
+
+ final List<String> lines = Files.readAllLines(logFile);
+ assertThat(lines, both(hasSize(3)).and(contains("Test log1", str, str)));
+ }
+
+ @Test
+ @LoggerContextSource("MemoryMappedFileAppenderLocationTest.xml")
+ void testMemMapLocation(final LoggerContext context) throws Exception {
+ final Logger log = context.getLogger(getClass());
+ final Path logFile = Paths.get("target", "MemoryMappedFileAppenderLocationTest.log");
+ final int expectedFileLength = Integers.ceilingNextPowerOfTwo(32000);
+ assertEquals(32768, expectedFileLength);
+ try {
+ log.warn("Test log1");
+ assertTrue(Files.exists(logFile));
+ assertEquals(expectedFileLength, Files.size(logFile));
+ log.warn("Test log2");
+ assertEquals(expectedFileLength, Files.size(logFile));
+ } finally {
+ context.stop();
+ }
+ assertEquals(272 + 2 * System.lineSeparator().length(), Files.size(logFile), "Expected file size to shrink");
+
+ final List<String> lines = Files.readAllLines(logFile);
+ assertThat(lines, both(hasSize(2)).and(contains(
+ "org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderTest.testMemMapLocation(MemoryMappedFileAppenderTest.java:104): Test log1",
+ "org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderTest.testMemMapLocation(MemoryMappedFileAppenderTest.java:107): Test log2"
+ )));
+ }
+}
diff --git a/log4j-core/src/test/resources/MemoryMappedFileAppenderLocationTest.xml b/log4j-core/src/test/resources/MemoryMappedFileAppenderLocationTest.xml
index 14b6b25..b192995 100644
--- a/log4j-core/src/test/resources/MemoryMappedFileAppenderLocationTest.xml
+++ b/log4j-core/src/test/resources/MemoryMappedFileAppenderLocationTest.xml
@@ -5,7 +5,7 @@
fileName="target/MemoryMappedFileAppenderLocationTest.log"
regionLength="32000" immediateFlush="true" append="false">
<PatternLayout>
- <Pattern>%d %p %c{1.} [%t] %X{aKey} %m %location %ex%n</Pattern>
+ <Pattern>%location: %m%n</Pattern>
</PatternLayout>
</MemoryMappedFile>
</Appenders>
@@ -15,4 +15,4 @@
<AppenderRef ref="MemoryMappedFile"/>
</Root>
</Loggers>
-</Configuration>
\ No newline at end of file
+</Configuration>
diff --git a/log4j-core/src/test/resources/MemoryMappedFileAppenderRemapTest.xml b/log4j-core/src/test/resources/MemoryMappedFileAppenderRemapTest.xml
index 02799a5..d46b902 100644
--- a/log4j-core/src/test/resources/MemoryMappedFileAppenderRemapTest.xml
+++ b/log4j-core/src/test/resources/MemoryMappedFileAppenderRemapTest.xml
@@ -5,7 +5,7 @@
fileName="target/MemoryMappedFileAppenderRemapTest.log"
regionLength="256" immediateFlush="true" append="false">
<PatternLayout>
- <Pattern>%d %p %c{1.} [%t] %X{aKey} %m%ex%n</Pattern>
+ <Pattern>%m%n</Pattern>
</PatternLayout>
</MemoryMappedFile>
</Appenders>
@@ -15,4 +15,4 @@
<AppenderRef ref="MemoryMappedFile"/>
</Root>
</Loggers>
-</Configuration>
\ No newline at end of file
+</Configuration>
diff --git a/log4j-core/src/test/resources/MemoryMappedFileAppenderTest.xml b/log4j-core/src/test/resources/MemoryMappedFileAppenderTest.xml
index 430c8b1..209459d 100644
--- a/log4j-core/src/test/resources/MemoryMappedFileAppenderTest.xml
+++ b/log4j-core/src/test/resources/MemoryMappedFileAppenderTest.xml
@@ -5,7 +5,7 @@
fileName="target/MemoryMappedFileAppenderTest.log"
immediateFlush="false" append="false">
<PatternLayout>
- <Pattern>%d %p %c{1.} [%t] %X{aKey} %m%ex%n</Pattern>
+ <Pattern>%m%n</Pattern>
</PatternLayout>
</MemoryMappedFile>
</Appenders>
@@ -15,4 +15,4 @@
<AppenderRef ref="MemoryMappedFile"/>
</Root>
</Loggers>
-</Configuration>
\ No newline at end of file
+</Configuration>