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:58:00 UTC
[logging-log4j2] 07/10: Migrate some rolling 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 a125b79d31a8fc4a6d86a0c706cb092a111081e7
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sun Sep 13 20:12:11 2020 -0500
Migrate some rolling tests to JUnit 5
Backported from 3.x.
Signed-off-by: Matt Sicker <bo...@gmail.com>
---
.../appender/rolling/CronTriggeringPolicyTest.java | 11 ++--
.../core/appender/rolling/EligibleFilesTest.java | 10 +--
.../log4j/core/appender/rolling/FileSizeTest.java | 11 ++--
.../rolling/OnStartupTriggeringPolicyTest.java | 73 ++++++++++------------
.../appender/rolling/PatternProcessorTest.java | 32 +++++-----
5 files changed, 68 insertions(+), 69 deletions(-)
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java
index 9fce567..efcadd9 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/CronTriggeringPolicyTest.java
@@ -22,9 +22,10 @@ import org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.NullConfiguration;
import org.apache.logging.log4j.core.layout.PatternLayout;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
public class CronTriggeringPolicyTest {
@@ -36,7 +37,7 @@ public class CronTriggeringPolicyTest {
//@Rule
//public CleanFiles cleanFiles = new CleanFiles("testcmd1.log", "testcmd2.log", "testcmd3.log");
- @Before
+ @BeforeEach
public void before() {
configuration = new NullConfiguration();
}
@@ -59,7 +60,7 @@ public class CronTriggeringPolicyTest {
.setConfiguration(configuration)
.build();
// @formatter:on
- Assert.assertNotNull(raf);
+ assertNotNull(raf);
}
/**
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/EligibleFilesTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/EligibleFilesTest.java
index 51fae36..4b99248 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/EligibleFilesTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/EligibleFilesTest.java
@@ -20,9 +20,9 @@ import java.nio.file.Path;
import java.util.Map;
import org.apache.logging.log4j.core.pattern.NotANumber;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
/**
* Test getEligibleFiles method.
@@ -34,11 +34,11 @@ public class EligibleFilesTest {
final String path = "target/test-classes/rolloverPath/log4j.txt.20170112_09-" + NotANumber.VALUE + ".gz";
final TestRolloverStrategy strategy = new TestRolloverStrategy();
final Map<Integer, Path> files = strategy.findFilesInPath(path);
- assertTrue("No files found", files.size() > 0);
- assertTrue("Incorrect number of files found. Should be 30, was " + files.size(), files.size() == 30);
+ assertTrue(files.size() > 0, "No files found");
+ assertEquals(30, files.size(), "Incorrect number of files found. Should be 30, was " + files.size());
}
- private class TestRolloverStrategy extends AbstractRolloverStrategy {
+ private static class TestRolloverStrategy extends AbstractRolloverStrategy {
public TestRolloverStrategy() {
super(null);
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java
index 9b9bf45..35e2199 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/FileSizeTest.java
@@ -16,8 +16,9 @@
*/
package org.apache.logging.log4j.core.appender.rolling;
-import org.junit.Test;
-import static org.junit.Assert.*;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
/**
* Tests {@link FileSize}.
@@ -27,10 +28,10 @@ public class FileSizeTest {
private final static long EXPECTED = 10 * 1024;
@Test
- public void testFileSize() throws Exception {
+ public void testFileSize() {
long value = FileSize.parse("10KB", 0);
- assertTrue("unexpected value " + value, value == EXPECTED);
+ assertEquals(EXPECTED, value, "unexpected value " + value);
value = FileSize.parse("10 KB", 0);
- assertTrue("unexpected value " + value, value == EXPECTED);
+ assertEquals(EXPECTED, value, "unexpected value " + value);
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
index 1ea3aa2..ef31ba2 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
@@ -16,60 +16,50 @@
*/
package org.apache.logging.log4j.core.appender.rolling;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.DefaultConfiguration;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import java.io.ByteArrayInputStream;
-import java.io.File;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileTime;
-import java.util.Arrays;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.DefaultConfiguration;
-import org.apache.logging.log4j.core.layout.PatternLayout;
-import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
-import org.apache.logging.log4j.junit.CleanFolders;
-import org.junit.Assert;
-import org.junit.Rule;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Tests {@link OnStartupTriggeringPolicy}.
*/
-//@Ignore
public class OnStartupTriggeringPolicyTest {
- private static final String TARGET_FOLDER = "target/rollOnStartup";
- private static final String TARGET_FILE = TARGET_FOLDER + "/testfile";
- private static final String TARGET_PATTERN = TARGET_FOLDER + "/test1-%d{MM-dd-yyyy}-%i.log";
- private static final String ROLLED_FILE_PREFIX = TARGET_FOLDER + "/test1-";
- private static final String ROLLED_FILE_SUFFIX = "-1.log";
+ private static final String TARGET_PATTERN = "/test1-%d{MM-dd-yyyy}-%i.log";
private static final String TEST_DATA = "Hello world!";
private static final FastDateFormat formatter = FastDateFormat.getInstance("MM-dd-yyyy");
- @Rule
- public CleanFolders rule = new CleanFolders(TARGET_FOLDER);
+ @TempDir
+ Path tempDir;
@Test
public void testPolicy() throws Exception {
- //System.setProperty("log4j2.debug", "true");
- //System.setProperty("log4j2.StatusLogger.level", "trace");
final Configuration configuration = new DefaultConfiguration();
- final Path target = Paths.get(TARGET_FILE);
- Assert.assertFalse(Files.exists(target));
- target.toFile().getParentFile().mkdirs();
- final long timeStamp = System.currentTimeMillis() - (1000 * 60 * 60 * 24);
+ final Path target = tempDir.resolve("testfile");
+ final long timeStamp = Instant.now().minus(Duration.ofDays(1)).toEpochMilli();
final String expectedDate = formatter.format(timeStamp);
- final String rolledFileName = ROLLED_FILE_PREFIX + expectedDate + ROLLED_FILE_SUFFIX;
- final Path rolled = Paths.get(rolledFileName);
+ final Path rolled = tempDir.resolve("test1-" + expectedDate + "-1.log");
final long copied;
- try (final InputStream is = new ByteArrayInputStream(TEST_DATA.getBytes("UTF-8"))) {
+ try (final InputStream is = new ByteArrayInputStream(TEST_DATA.getBytes(StandardCharsets.UTF_8))) {
copied = Files.copy(is, target, StandardCopyOption.REPLACE_EXISTING);
}
final long size = Files.size(target);
@@ -79,20 +69,23 @@ public class OnStartupTriggeringPolicyTest {
final FileTime fileTime = FileTime.fromMillis(timeStamp);
final BasicFileAttributeView attrs = Files.getFileAttributeView(target, BasicFileAttributeView.class);
attrs.setTimes(fileTime, fileTime, fileTime);
-
final PatternLayout layout = PatternLayout.newBuilder().withPattern("%msg").withConfiguration(configuration)
.build();
- final RolloverStrategy strategy = DefaultRolloverStrategy.createStrategy(null, null, null, "0", null, true,
- configuration);
+ final RolloverStrategy strategy = DefaultRolloverStrategy.newBuilder().withCompressionLevelStr("0")
+ .withStopCustomActionsOnError(true).withConfig(configuration).build();
final OnStartupTriggeringPolicy policy = OnStartupTriggeringPolicy.createPolicy(1);
- try (final RollingFileManager manager = RollingFileManager.getFileManager(TARGET_FILE, TARGET_PATTERN, true, false,
- policy, strategy, null, layout, 8192, true, false, null, null, null, configuration)) {
+
+ try (final RollingFileManager manager = RollingFileManager.getFileManager(target.toString(), tempDir.toString() + TARGET_PATTERN, true,
+ false, policy, strategy, null, layout, 8192, true, false, null, null, null, configuration)) {
manager.initialize();
- final String files = Arrays.toString(new File(TARGET_FOLDER).listFiles());
- assertTrue(target.toString() + ", files = " + files, Files.exists(target));
- assertEquals(target.toString(), 0, Files.size(target));
- assertTrue("Missing: " + rolled.toString() + ", files on disk = " + files, Files.exists(rolled));
- assertEquals(rolled.toString(), size, Files.size(rolled));
+ final String files;
+ try (Stream<Path> contents = Files.list(tempDir)) {
+ files = contents.map(Path::toString).collect(Collectors.joining(", ", "[", "]"));
+ }
+ assertTrue(Files.exists(target), target.toString() + ", files = " + files);
+ assertEquals(0, Files.size(target), target.toString());
+ assertTrue(Files.exists(rolled), "Missing: " + rolled.toString() + ", files on disk = " + files);
+ assertEquals(size, Files.size(rolled), rolled.toString());
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java
index 556268d..427d845 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessorTest.java
@@ -22,9 +22,10 @@ import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
-import org.junit.Test;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
/**
* Tests the PatternProcessor class.
@@ -40,7 +41,7 @@ public class PatternProcessorTest {
final PatternProcessor pp = new PatternProcessor("c:\\test\\new/app-%d{HH-mm-ss}.log");
final Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 16);
- cal.set(Calendar.MINUTE, 02);
+ cal.set(Calendar.MINUTE, 2);
cal.set(Calendar.SECOND, 15);
final StringBuilder buf = new StringBuilder();
@@ -57,7 +58,7 @@ public class PatternProcessorTest {
// expect Wed, March 4, 2014, 11:00
final Calendar expected = Calendar.getInstance();
- expected.set(2014, Calendar.MARCH, 4, 11, 00, 00);
+ expected.set(2014, Calendar.MARCH, 4, 11, 0, 0);
expected.set(Calendar.MILLISECOND, 0);
assertEquals(format(expected.getTimeInMillis()), format(actual));
}
@@ -71,7 +72,7 @@ public class PatternProcessorTest {
// expect Wed, March 5, 2014, 00:00
final Calendar expected = Calendar.getInstance();
- expected.set(2014, Calendar.MARCH, 5, 00, 00, 00);
+ expected.set(2014, Calendar.MARCH, 5, 0, 0, 0);
expected.set(Calendar.MILLISECOND, 0);
assertEquals(format(expected.getTimeInMillis()), format(actual));
}
@@ -116,7 +117,7 @@ public class PatternProcessorTest {
// expect Tue, March 4, 2014, 10:32
final Calendar expected = Calendar.getInstance();
- expected.set(2014, Calendar.MARCH, 4, 10, 32, 00);
+ expected.set(2014, Calendar.MARCH, 4, 10, 32, 0);
expected.set(Calendar.MILLISECOND, 0);
assertEquals(format(expected.getTimeInMillis()), format(actual));
}
@@ -130,7 +131,7 @@ public class PatternProcessorTest {
// We expect 1st day of next month
final Calendar expected = Calendar.getInstance();
- expected.set(2014, Calendar.NOVEMBER, 1, 00, 00, 00);
+ expected.set(2014, Calendar.NOVEMBER, 1, 0, 0, 0);
expected.set(Calendar.MILLISECOND, 0);
assertEquals(format(expected.getTimeInMillis()), format(actual));
}
@@ -144,7 +145,7 @@ public class PatternProcessorTest {
// Expect 1st of next month: 2014 Feb 1st
final Calendar expected = Calendar.getInstance();
- expected.set(2014, Calendar.FEBRUARY, 1, 00, 00, 00);
+ expected.set(2014, Calendar.FEBRUARY, 1, 0, 0, 0);
expected.set(Calendar.MILLISECOND, 0);
assertEquals(format(expected.getTimeInMillis()), format(actual));
}
@@ -158,7 +159,7 @@ public class PatternProcessorTest {
// Expect 1st of next month: 2015 Jan 1st
final Calendar expected = Calendar.getInstance();
- expected.set(2015, Calendar.JANUARY, 1, 00, 00, 00);
+ expected.set(2015, Calendar.JANUARY, 1, 0, 0, 0);
expected.set(Calendar.MILLISECOND, 0);
assertEquals(format(expected.getTimeInMillis()), format(actual));
}
@@ -172,7 +173,7 @@ public class PatternProcessorTest {
// We expect 1st day of next month
final Calendar expected = Calendar.getInstance();
- expected.set(2016, Calendar.JANUARY, 1, 00, 00, 00);
+ expected.set(2016, Calendar.JANUARY, 1, 0, 0, 0);
expected.set(Calendar.MILLISECOND, 0);
assertEquals(format(expected.getTimeInMillis()), format(actual));
}
@@ -194,6 +195,7 @@ public class PatternProcessorTest {
}
@Test
+ @Tag("locale")
public void testGetNextTimeWeeklyReturnsFirstDayOfNextWeek_FRANCE() {
final Locale old = Locale.getDefault();
Locale.setDefault(Locale.FRANCE); // force 1st day of the week to be Monday
@@ -206,7 +208,7 @@ public class PatternProcessorTest {
// expect Monday, March 10, 2014
final Calendar expected = Calendar.getInstance();
- expected.set(2014, Calendar.MARCH, 10, 00, 00, 00);
+ expected.set(2014, Calendar.MARCH, 10, 0, 0, 0);
expected.set(Calendar.MILLISECOND, 0);
assertEquals(format(expected.getTimeInMillis()), format(actual));
} finally {
@@ -215,6 +217,7 @@ public class PatternProcessorTest {
}
@Test
+ @Tag("locale")
public void testGetNextTimeWeeklyReturnsFirstDayOfNextWeek_US() {
final Locale old = Locale.getDefault();
Locale.setDefault(Locale.US); // force 1st day of the week to be Sunday
@@ -227,7 +230,7 @@ public class PatternProcessorTest {
// expect Sunday, March 9, 2014
final Calendar expected = Calendar.getInstance();
- expected.set(2014, Calendar.MARCH, 9, 00, 00, 00);
+ expected.set(2014, Calendar.MARCH, 9, 0, 0, 0);
expected.set(Calendar.MILLISECOND, 0);
assertEquals(format(expected.getTimeInMillis()), format(actual));
} finally {
@@ -239,18 +242,19 @@ public class PatternProcessorTest {
* Tests https://issues.apache.org/jira/browse/LOG4J2-1232
*/
@Test
+ @Tag("locale")
public void testGetNextTimeWeeklyReturnsFirstWeekInYear_US() {
final Locale old = Locale.getDefault();
Locale.setDefault(Locale.US); // force 1st day of the week to be Sunday
try {
final PatternProcessor pp = new PatternProcessor("logs/market_data_msg.log-%d{yyyy-MM-'W'W}");
final Calendar initial = Calendar.getInstance();
- initial.set(2015, Calendar.DECEMBER, 28, 00, 00, 00); // Monday, December 28, 2015
+ initial.set(2015, Calendar.DECEMBER, 28, 0, 0, 0); // Monday, December 28, 2015
final long actual = pp.getNextTime(initial.getTimeInMillis(), 1, false);
// expect Sunday January 3, 2016
final Calendar expected = Calendar.getInstance();
- expected.set(2016, Calendar.JANUARY, 3, 00, 00, 00);
+ expected.set(2016, Calendar.JANUARY, 3, 0, 0, 0);
expected.set(Calendar.MILLISECOND, 0);
assertEquals(format(expected.getTimeInMillis()), format(actual));
} finally {