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/19 17:40:57 UTC
[logging-log4j2] branch master updated: Migrate more tests to JUnit
5
This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new 16838a7 Migrate more tests to JUnit 5
16838a7 is described below
commit 16838a7155384fc253f4a22ef80989d70e36ffa5
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sat Sep 19 12:30:51 2020 -0500
Migrate more tests to JUnit 5
Signed-off-by: Matt Sicker <bo...@gmail.com>
---
.../appender/db/AbstractDatabaseAppenderTest.java | 38 ++-
.../appender/db/AbstractDatabaseManagerTest.java | 26 +-
.../core/appender/nosql/NoSqlAppenderTest.java | 24 +-
.../appender/nosql/NoSqlDatabaseManagerTest.java | 293 ++++++++++-----------
.../RandomRollingAppenderOnStartupTest.java | 58 ++--
5 files changed, 194 insertions(+), 245 deletions(-)
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppenderTest.java
index 6b5d7ef..ea84899 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppenderTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppenderTest.java
@@ -16,9 +16,18 @@
*/
package org.apache.logging.log4j.core.appender.db;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.Property;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.io.Serializable;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.BDDMockito.given;
@@ -26,18 +35,7 @@ import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
-import java.io.Serializable;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.logging.log4j.core.Filter;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.config.Property;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
+@ExtendWith(MockitoExtension.class)
public class AbstractDatabaseAppenderTest {
private static class LocalAbstractDatabaseAppender extends AbstractDatabaseAppender<LocalAbstractDatabaseManager> {
@@ -84,16 +82,16 @@ public class AbstractDatabaseAppenderTest {
public void testNameAndGetLayout01() {
setUp("testName01");
- assertEquals("The name is not correct.", "testName01", appender.getName());
- assertNull("The layout should always be null.", appender.getLayout());
+ assertEquals("testName01", appender.getName(), "The name is not correct.");
+ assertNull(appender.getLayout(), "The layout should always be null.");
}
@Test
public void testNameAndGetLayout02() {
setUp("anotherName02");
- assertEquals("The name is not correct.", "anotherName02", appender.getName());
- assertNull("The layout should always be null.", appender.getLayout());
+ assertEquals("anotherName02", appender.getName(), "The name is not correct.");
+ assertNull(appender.getLayout(), "The layout should always be null.");
}
@Test
@@ -101,7 +99,7 @@ public class AbstractDatabaseAppenderTest {
setUp("name");
final LocalAbstractDatabaseManager oldManager = appender.getManager();
- assertSame("The manager should be the same.", manager, oldManager);
+ assertSame(manager, oldManager, "The manager should be the same.");
final LocalAbstractDatabaseManager newManager = mock(LocalAbstractDatabaseManager.class);
appender.replaceManager(newManager);
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManagerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManagerTest.java
index dfb498e..f3005ef 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManagerTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManagerTest.java
@@ -16,9 +16,7 @@
*/
package org.apache.logging.log4j.core.appender.db;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.BDDMockito.then;
@@ -32,7 +30,7 @@ import static org.mockito.Mockito.when;
import java.io.Serializable;
import org.apache.logging.log4j.core.LogEvent;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
public class AbstractDatabaseManagerTest {
// this stub is provided because mocking constructors is hard
@@ -233,45 +231,45 @@ public class AbstractDatabaseManagerTest {
public void testStartupShutdown01() throws Exception {
setUp("testName01", 0);
- assertEquals("The name is not correct.", "testName01", manager.getName());
- assertFalse("The manager should not have started.", manager.isRunning());
+ assertEquals("testName01", manager.getName(), "The name is not correct.");
+ assertFalse(manager.isRunning(), "The manager should not have started.");
manager.startup();
then(manager).should().startupInternal();
- assertTrue("The manager should be running now.", manager.isRunning());
+ assertTrue(manager.isRunning(), "The manager should be running now.");
manager.shutdown();
then(manager).should().shutdownInternal();
- assertFalse("The manager should not be running anymore.", manager.isRunning());
+ assertFalse(manager.isRunning(), "The manager should not be running anymore.");
}
@Test
public void testStartupShutdown02() throws Exception {
setUp("anotherName02", 0);
- assertEquals("The name is not correct.", "anotherName02", manager.getName());
- assertFalse("The manager should not have started.", manager.isRunning());
+ assertEquals("anotherName02", manager.getName(), "The name is not correct.");
+ assertFalse(manager.isRunning(), "The manager should not have started.");
manager.startup();
then(manager).should().startupInternal();
- assertTrue("The manager should be running now.", manager.isRunning());
+ assertTrue(manager.isRunning(), "The manager should be running now.");
manager.releaseSub(-1, null);
then(manager).should().shutdownInternal();
- assertFalse("The manager should not be running anymore.", manager.isRunning());
+ assertFalse(manager.isRunning(), "The manager should not be running anymore.");
}
@Test
public void testToString01() {
setUp("someName01", 0);
- assertEquals("The string is not correct.", "someName01", manager.toString());
+ assertEquals("someName01", manager.toString(), "The string is not correct.");
}
@Test
public void testToString02() {
setUp("bufferSize=12, anotherKey02=coolValue02", 12);
- assertEquals("The string is not correct.", "bufferSize=12, anotherKey02=coolValue02", manager.toString());
+ assertEquals("bufferSize=12, anotherKey02=coolValue02", manager.toString(), "The string is not correct.");
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlAppenderTest.java
index 24e01ac..fd3e4b9 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlAppenderTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlAppenderTest.java
@@ -16,14 +16,14 @@
*/
package org.apache.logging.log4j.core.appender.nosql;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
-@RunWith(MockitoJUnitRunner.class)
+@ExtendWith(MockitoExtension.class)
public class NoSqlAppenderTest {
@Mock
@@ -33,17 +33,17 @@ public class NoSqlAppenderTest {
public void testNoProvider() {
final NoSqlAppender appender = NoSqlAppender.newBuilder().setName("myName01").build();
- assertNull("The appender should be null.", appender);
+ assertNull(appender, "The appender should be null.");
}
@Test
public void testProvider() {
final NoSqlAppender appender = NoSqlAppender.newBuilder().setName("myName01").setProvider(provider).build();
- assertNotNull("The appender should not be null.", appender);
- assertEquals("The toString value is not correct.",
+ assertNotNull(appender, "The appender should not be null.");
+ assertEquals(
"myName01{ manager=noSqlManager{ description=myName01, bufferSize=0, provider=" + provider + " } }",
- appender.toString());
+ appender.toString(), "The toString value is not correct.");
appender.stop();
}
@@ -53,11 +53,11 @@ public class NoSqlAppenderTest {
final NoSqlAppender appender = NoSqlAppender.newBuilder().setName("anotherName02").setProvider(provider)
.setBufferSize(25).build();
- assertNotNull("The appender should not be null.", appender);
- assertEquals("The toString value is not correct.",
+ assertNotNull(appender, "The appender should not be null.");
+ assertEquals(
"anotherName02{ manager=noSqlManager{ description=anotherName02, bufferSize=25, provider=" + provider
+ " } }",
- appender.toString());
+ appender.toString(), "The toString value is not correct.");
appender.stop();
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManagerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManagerTest.java
index 63ae624..84e969c 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManagerTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/nosql/NoSqlDatabaseManagerTest.java
@@ -16,22 +16,6 @@
*/
package org.apache.logging.log4j.core.appender.nosql;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.BDDMockito.given;
-import static org.mockito.BDDMockito.then;
-import static org.mockito.Mockito.mock;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.ThreadContext;
@@ -39,21 +23,31 @@ import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AppenderLoggingException;
import org.apache.logging.log4j.core.impl.ContextDataFactory;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
-import org.apache.logging.log4j.junit.ThreadContextStackRule;
+import org.apache.logging.log4j.junit.UsingThreadContextStack;
import org.apache.logging.log4j.message.Message;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.mockito.stubbing.Answer;
+import org.mockito.junit.jupiter.MockitoExtension;
-@RunWith(MockitoJUnitRunner.class)
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.BDDMockito.then;
+import static org.mockito.Mockito.mock;
+
+@ExtendWith(MockitoExtension.class)
+@UsingThreadContextStack
public class NoSqlDatabaseManagerTest {
@Mock
private NoSqlConnection<Map<String, Object>, DefaultNoSqlObject> connection;
@@ -64,26 +58,11 @@ public class NoSqlDatabaseManagerTest {
@Captor
private ArgumentCaptor<NoSqlObject<Map<String, Object>>> captor;
- @Rule
- public final ThreadContextStackRule threadContextRule = new ThreadContextStackRule();
- @Rule
- public final ExpectedException expectedException = ExpectedException.none();
-
- @Before
+ @BeforeEach
public void setUp() {
given(provider.getConnection()).willReturn(connection);
- given(connection.createObject()).willAnswer(new Answer<DefaultNoSqlObject>() {
- @Override
- public DefaultNoSqlObject answer(final InvocationOnMock invocation) throws Throwable {
- return new DefaultNoSqlObject();
- }
- });
- given(connection.createList(anyInt())).willAnswer(new Answer<DefaultNoSqlObject[]>() {
- @Override
- public DefaultNoSqlObject[] answer(final InvocationOnMock invocation) throws Throwable {
- return new DefaultNoSqlObject[invocation.<Integer>getArgument(0)];
- }
- });
+ given(connection.createObject()).willAnswer(invocation -> new DefaultNoSqlObject());
+ given(connection.createList(anyInt())).willAnswer(invocation -> new DefaultNoSqlObject[invocation.<Integer>getArgument(0)]);
}
@Test
@@ -91,7 +70,7 @@ public class NoSqlDatabaseManagerTest {
try (final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager("name", 0,
provider)) {
- assertNotNull("The manager should not be null.", manager);
+ assertNotNull(manager, "The manager should not be null.");
manager.connectAndStart();
then(provider).should().getConnection();
@@ -103,8 +82,7 @@ public class NoSqlDatabaseManagerTest {
public void testWriteInternalNotConnected01() {
try (final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager("name", 0,
provider)) {
- expectedException.expect(AppenderLoggingException.class);
- manager.writeInternal(mock(LogEvent.class), null);
+ assertThrows(AppenderLoggingException.class, () -> manager.writeInternal(mock(LogEvent.class), null));
}
}
@@ -119,8 +97,7 @@ public class NoSqlDatabaseManagerTest {
manager.connectAndStart();
then(provider).should().getConnection();
- expectedException.expect(AppenderLoggingException.class);
- manager.writeInternal(mock(LogEvent.class), null);
+ assertThrows(AppenderLoggingException.class, () -> manager.writeInternal(mock(LogEvent.class), null));
}
}
@@ -151,33 +128,33 @@ public class NoSqlDatabaseManagerTest {
then(connection).should().insertObject(captor.capture());
final NoSqlObject<Map<String, Object>> inserted = captor.getValue();
- assertNotNull("The inserted value should not be null.", inserted);
+ assertNotNull(inserted, "The inserted value should not be null.");
final Map<String, Object> object = inserted.unwrap();
- assertNotNull("The unwrapped object should not be null.", object);
+ assertNotNull(object, "The unwrapped object should not be null.");
- assertEquals("The level is not correct.", Level.WARN, object.get("level"));
- assertEquals("The logger is not correct.", "com.foo.NoSQLDbTest.testWriteInternal01",
- object.get("loggerName"));
- assertEquals("The message is not correct.", "My formatted message 01.", object.get("message"));
- assertEquals("The thread is not correct.", "MyThread-A", object.get("threadName"));
- assertEquals("The millis is not correct.", 1234567890123L, object.get("millis"));
- assertEquals("The date is not correct.", 1234567890123L, ((Date) object.get("date")).getTime());
+ assertEquals(Level.WARN, object.get("level"), "The level is not correct.");
+ assertEquals("com.foo.NoSQLDbTest.testWriteInternal01",
+ object.get("loggerName"), "The logger is not correct.");
+ assertEquals("My formatted message 01.", object.get("message"), "The message is not correct.");
+ assertEquals("MyThread-A", object.get("threadName"), "The thread is not correct.");
+ assertEquals(1234567890123L, object.get("millis"), "The millis is not correct.");
+ assertEquals(1234567890123L, ((Date) object.get("date")).getTime(), "The date is not correct.");
- assertTrue("The source should be a map.", object.get("source") instanceof Map);
+ assertTrue(object.get("source") instanceof Map, "The source should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> source = (Map<String, Object>) object.get("source");
- assertEquals("The class is not correct.", "com.foo.Bar", source.get("className"));
- assertEquals("The method is not correct.", "testMethod01", source.get("methodName"));
- assertEquals("The file name is not correct.", "Bar.java", source.get("fileName"));
- assertEquals("The line number is not correct.", 15, source.get("lineNumber"));
+ assertEquals("com.foo.Bar", source.get("className"), "The class is not correct.");
+ assertEquals("testMethod01", source.get("methodName"), "The method is not correct.");
+ assertEquals("Bar.java", source.get("fileName"), "The file name is not correct.");
+ assertEquals(15, source.get("lineNumber"), "The line number is not correct.");
- assertNull("The marker should be null.", object.get("marker"));
+ assertNull(object.get("marker"), "The marker should be null.");
- assertNull("The thrown should be null.", object.get("thrown"));
+ assertNull(object.get("thrown"), "The thrown should be null.");
- assertTrue("The context map should be empty.", ((Map<?, ?>) object.get("contextMap")).isEmpty());
+ assertTrue(((Map<?, ?>) object.get("contextMap")).isEmpty(), "The context map should be empty.");
- assertTrue("The context stack should be null.", ((Collection<?>) object.get("contextStack")).isEmpty());
+ assertTrue(((Collection<?>) object.get("contextStack")).isEmpty(), "The context stack should be null.");
}
}
@@ -223,60 +200,60 @@ public class NoSqlDatabaseManagerTest {
then(connection).should().insertObject(captor.capture());
final NoSqlObject<Map<String, Object>> inserted = captor.getValue();
- assertNotNull("The inserted value should not be null.", inserted);
+ assertNotNull(inserted, "The inserted value should not be null.");
final Map<String, Object> object = inserted.unwrap();
- assertNotNull("The unwrapped object should not be null.", object);
+ assertNotNull(object, "The unwrapped object should not be null.");
- assertEquals("The level is not correct.", Level.DEBUG, object.get("level"));
- assertEquals("The logger is not correct.", "com.foo.NoSQLDbTest.testWriteInternal02",
- object.get("loggerName"));
- assertEquals("The message is not correct.", "Another cool message 02.", object.get("message"));
- assertEquals("The thread is not correct.", "AnotherThread-B", object.get("threadName"));
- assertEquals("The millis is not correct.", 987654321564L, object.get("millis"));
- assertEquals("The date is not correct.", 987654321564L, ((Date) object.get("date")).getTime());
+ assertEquals(Level.DEBUG, object.get("level"), "The level is not correct.");
+ assertEquals("com.foo.NoSQLDbTest.testWriteInternal02",
+ object.get("loggerName"), "The logger is not correct.");
+ assertEquals("Another cool message 02.", object.get("message"), "The message is not correct.");
+ assertEquals("AnotherThread-B", object.get("threadName"), "The thread is not correct.");
+ assertEquals(987654321564L, object.get("millis"), "The millis is not correct.");
+ assertEquals(987654321564L, ((Date) object.get("date")).getTime(), "The date is not correct.");
- assertTrue("The source should be a map.", object.get("source") instanceof Map);
+ assertTrue(object.get("source") instanceof Map, "The source should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> source = (Map<String, Object>) object.get("source");
- assertEquals("The class is not correct.", "com.bar.Foo", source.get("className"));
- assertEquals("The method is not correct.", "anotherMethod03", source.get("methodName"));
- assertEquals("The file name is not correct.", "Foo.java", source.get("fileName"));
- assertEquals("The line number is not correct.", 9, source.get("lineNumber"));
+ assertEquals("com.bar.Foo", source.get("className"), "The class is not correct.");
+ assertEquals("anotherMethod03", source.get("methodName"), "The method is not correct.");
+ assertEquals("Foo.java", source.get("fileName"), "The file name is not correct.");
+ assertEquals(9, source.get("lineNumber"), "The line number is not correct.");
- assertTrue("The marker should be a map.", object.get("marker") instanceof Map);
+ assertTrue(object.get("marker") instanceof Map, "The marker should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> marker = (Map<String, Object>) object.get("marker");
- assertEquals("The marker name is not correct.", "LoneMarker", marker.get("name"));
- assertNull("The marker parent should be null.", marker.get("parent"));
+ assertEquals("LoneMarker", marker.get("name"), "The marker name is not correct.");
+ assertNull(marker.get("parent"), "The marker parent should be null.");
- assertTrue("The thrown should be a map.", object.get("thrown") instanceof Map);
+ assertTrue(object.get("thrown") instanceof Map, "The thrown should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> thrown = (Map<String, Object>) object.get("thrown");
- assertEquals("The thrown type is not correct.", "java.lang.RuntimeException", thrown.get("type"));
- assertEquals("The thrown message is not correct.", "This is something cool!", thrown.get("message"));
- assertTrue("The thrown stack trace should be a list.", thrown.get("stackTrace") instanceof List);
+ assertEquals("java.lang.RuntimeException", thrown.get("type"), "The thrown type is not correct.");
+ assertEquals("This is something cool!", thrown.get("message"), "The thrown message is not correct.");
+ assertTrue(thrown.get("stackTrace") instanceof List, "The thrown stack trace should be a list.");
@SuppressWarnings("unchecked")
final List<Map<String, Object>> stackTrace = (List<Map<String, Object>>) thrown.get("stackTrace");
- assertEquals("The thrown stack trace length is not correct.", exception.getStackTrace().length,
- stackTrace.size());
+ assertEquals(exception.getStackTrace().length,
+ stackTrace.size(), "The thrown stack trace length is not correct.");
for (int i = 0; i < exception.getStackTrace().length; i++) {
final StackTraceElement e1 = exception.getStackTrace()[i];
final Map<String, Object> e2 = stackTrace.get(i);
- assertEquals("Element class name [" + i + "] is not correct.", e1.getClassName(), e2.get("className"));
- assertEquals("Element method name [" + i + "] is not correct.", e1.getMethodName(),
- e2.get("methodName"));
- assertEquals("Element file name [" + i + "] is not correct.", e1.getFileName(), e2.get("fileName"));
- assertEquals("Element line number [" + i + "] is not correct.", e1.getLineNumber(),
- e2.get("lineNumber"));
+ assertEquals(e1.getClassName(), e2.get("className"), "Element class name [" + i + "] is not correct.");
+ assertEquals(e1.getMethodName(),
+ e2.get("methodName"), "Element method name [" + i + "] is not correct.");
+ assertEquals(e1.getFileName(), e2.get("fileName"), "Element file name [" + i + "] is not correct.");
+ assertEquals(e1.getLineNumber(),
+ e2.get("lineNumber"), "Element line number [" + i + "] is not correct.");
}
- assertNull("The thrown should have no cause.", thrown.get("cause"));
+ assertNull(thrown.get("cause"), "The thrown should have no cause.");
- assertTrue("The context map should be a map.", object.get("contextMap") instanceof Map);
- assertEquals("The context map is not correct.", context, object.get("contextMap"));
+ assertTrue(object.get("contextMap") instanceof Map, "The context map should be a map.");
+ assertEquals(context, object.get("contextMap"), "The context map is not correct.");
- assertTrue("The context stack should be list.", object.get("contextStack") instanceof List);
- assertEquals("The context stack is not correct.", stack.asList(), object.get("contextStack"));
+ assertTrue(object.get("contextStack") instanceof List, "The context stack should be list.");
+ assertEquals(stack.asList(), object.get("contextStack"), "The context stack is not correct.");
}
}
@@ -324,107 +301,107 @@ public class NoSqlDatabaseManagerTest {
then(connection).should().insertObject(captor.capture());
final NoSqlObject<Map<String, Object>> inserted = captor.getValue();
- assertNotNull("The inserted value should not be null.", inserted);
+ assertNotNull(inserted, "The inserted value should not be null.");
final Map<String, Object> object = inserted.unwrap();
- assertNotNull("The unwrapped object should not be null.", object);
+ assertNotNull(object, "The unwrapped object should not be null.");
- assertEquals("The level is not correct.", Level.DEBUG, object.get("level"));
- assertEquals("The logger is not correct.", "com.foo.NoSQLDbTest.testWriteInternal02",
- object.get("loggerName"));
- assertEquals("The message is not correct.", "Another cool message 02.", object.get("message"));
- assertEquals("The thread is not correct.", "AnotherThread-B", object.get("threadName"));
- assertEquals("The millis is not correct.", 987654321564L, object.get("millis"));
- assertEquals("The date is not correct.", 987654321564L, ((Date) object.get("date")).getTime());
+ assertEquals(Level.DEBUG, object.get("level"), "The level is not correct.");
+ assertEquals("com.foo.NoSQLDbTest.testWriteInternal02",
+ object.get("loggerName"), "The logger is not correct.");
+ assertEquals("Another cool message 02.", object.get("message"), "The message is not correct.");
+ assertEquals("AnotherThread-B", object.get("threadName"), "The thread is not correct.");
+ assertEquals(987654321564L, object.get("millis"), "The millis is not correct.");
+ assertEquals(987654321564L, ((Date) object.get("date")).getTime(), "The date is not correct.");
- assertTrue("The source should be a map.", object.get("source") instanceof Map);
+ assertTrue(object.get("source") instanceof Map, "The source should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> source = (Map<String, Object>) object.get("source");
- assertEquals("The class is not correct.", "com.bar.Foo", source.get("className"));
- assertEquals("The method is not correct.", "anotherMethod03", source.get("methodName"));
- assertEquals("The file name is not correct.", "Foo.java", source.get("fileName"));
- assertEquals("The line number is not correct.", 9, source.get("lineNumber"));
+ assertEquals("com.bar.Foo", source.get("className"), "The class is not correct.");
+ assertEquals("anotherMethod03", source.get("methodName"), "The method is not correct.");
+ assertEquals("Foo.java", source.get("fileName"), "The file name is not correct.");
+ assertEquals(9, source.get("lineNumber"), "The line number is not correct.");
- assertTrue("The marker should be a map.", object.get("marker") instanceof Map);
+ assertTrue(object.get("marker") instanceof Map, "The marker should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> marker = (Map<String, Object>) object.get("marker");
- assertEquals("The marker name is not correct.", "AnotherMarker", marker.get("name"));
+ assertEquals("AnotherMarker", marker.get("name"), "The marker name is not correct.");
- assertTrue("The marker parents should be a list.", marker.get("parents") instanceof List);
+ assertTrue(marker.get("parents") instanceof List, "The marker parents should be a list.");
@SuppressWarnings("unchecked")
final List<Object> markerParents = (List<Object>) marker.get("parents");
- assertEquals("The marker parents should contain two parents", 2, markerParents.size());
+ assertEquals(2, markerParents.size(), "The marker parents should contain two parents");
- assertTrue("The marker parents[0] should be a map.", markerParents.get(0) instanceof Map);
+ assertTrue(markerParents.get(0) instanceof Map, "The marker parents[0] should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> parent1 = (Map<String, Object>) markerParents.get(0);
- assertEquals("The first marker parent name is not correct.", "Parent1", parent1.get("name"));
+ assertEquals("Parent1", parent1.get("name"), "The first marker parent name is not correct.");
- assertTrue("The marker parents[1] should be a map.", markerParents.get(1) instanceof Map);
+ assertTrue(markerParents.get(1) instanceof Map, "The marker parents[1] should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> parent2 = (Map<String, Object>) markerParents.get(1);
- assertEquals("The second marker parent name is not correct.", "Parent2", parent2.get("name"));
- assertNull("The second marker should have no parent.", parent2.get("parent"));
+ assertEquals("Parent2", parent2.get("name"), "The second marker parent name is not correct.");
+ assertNull(parent2.get("parent"), "The second marker should have no parent.");
- assertTrue("The parent1 parents should be a list.", parent1.get("parents") instanceof List);
+ assertTrue(parent1.get("parents") instanceof List, "The parent1 parents should be a list.");
@SuppressWarnings("unchecked")
final List<Object> parent1Parents = (List<Object>) parent1.get("parents");
- assertEquals("The parent1 parents should have only one parent", 1, parent1Parents.size());
+ assertEquals(1, parent1Parents.size(), "The parent1 parents should have only one parent");
- assertTrue("The parent1Parents[0] should be a map.", parent1Parents.get(0) instanceof Map);
+ assertTrue(parent1Parents.get(0) instanceof Map, "The parent1Parents[0] should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> parent1parent = (Map<String, Object>) parent1Parents.get(0);
- assertEquals("The first parent1 parent name is not correct.", "GrandParent1", parent1parent.get("name"));
- assertNull("The parent1parent marker should have no parent.", parent1parent.get("parent"));
+ assertEquals("GrandParent1", parent1parent.get("name"), "The first parent1 parent name is not correct.");
+ assertNull(parent1parent.get("parent"), "The parent1parent marker should have no parent.");
- assertTrue("The thrown should be a map.", object.get("thrown") instanceof Map);
+ assertTrue(object.get("thrown") instanceof Map, "The thrown should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> thrown = (Map<String, Object>) object.get("thrown");
- assertEquals("The thrown type is not correct.", "java.lang.IllegalStateException", thrown.get("type"));
- assertEquals("The thrown message is not correct.", "This is the result.", thrown.get("message"));
- assertTrue("The thrown stack trace should be a list.", thrown.get("stackTrace") instanceof List);
+ assertEquals("java.lang.IllegalStateException", thrown.get("type"), "The thrown type is not correct.");
+ assertEquals("This is the result.", thrown.get("message"), "The thrown message is not correct.");
+ assertTrue(thrown.get("stackTrace") instanceof List, "The thrown stack trace should be a list.");
@SuppressWarnings("unchecked")
final List<Map<String, Object>> stackTrace = (List<Map<String, Object>>) thrown.get("stackTrace");
- assertEquals("The thrown stack trace length is not correct.", exception2.getStackTrace().length,
- stackTrace.size());
+ assertEquals(exception2.getStackTrace().length,
+ stackTrace.size(), "The thrown stack trace length is not correct.");
for (int i = 0; i < exception2.getStackTrace().length; i++) {
final StackTraceElement e1 = exception2.getStackTrace()[i];
final Map<String, Object> e2 = stackTrace.get(i);
- assertEquals("Element class name [" + i + "] is not correct.", e1.getClassName(), e2.get("className"));
- assertEquals("Element method name [" + i + "] is not correct.", e1.getMethodName(),
- e2.get("methodName"));
- assertEquals("Element file name [" + i + "] is not correct.", e1.getFileName(), e2.get("fileName"));
- assertEquals("Element line number [" + i + "] is not correct.", e1.getLineNumber(),
- e2.get("lineNumber"));
+ assertEquals(e1.getClassName(), e2.get("className"), "Element class name [" + i + "] is not correct.");
+ assertEquals(e1.getMethodName(),
+ e2.get("methodName"), "Element method name [" + i + "] is not correct.");
+ assertEquals(e1.getFileName(), e2.get("fileName"), "Element file name [" + i + "] is not correct.");
+ assertEquals(e1.getLineNumber(),
+ e2.get("lineNumber"), "Element line number [" + i + "] is not correct.");
}
- assertTrue("The thrown cause should be a map.", thrown.get("cause") instanceof Map);
+ assertTrue(thrown.get("cause") instanceof Map, "The thrown cause should be a map.");
@SuppressWarnings("unchecked")
final Map<String, Object> cause = (Map<String, Object>) thrown.get("cause");
- assertEquals("The cause type is not correct.", "java.io.IOException", cause.get("type"));
- assertEquals("The cause message is not correct.", "This is the cause.", cause.get("message"));
- assertTrue("The cause stack trace should be a list.", cause.get("stackTrace") instanceof List);
+ assertEquals("java.io.IOException", cause.get("type"), "The cause type is not correct.");
+ assertEquals("This is the cause.", cause.get("message"), "The cause message is not correct.");
+ assertTrue(cause.get("stackTrace") instanceof List, "The cause stack trace should be a list.");
@SuppressWarnings("unchecked")
final List<Map<String, Object>> causeStackTrace = (List<Map<String, Object>>) cause.get("stackTrace");
- assertEquals("The cause stack trace length is not correct.", exception1.getStackTrace().length,
- causeStackTrace.size());
+ assertEquals(exception1.getStackTrace().length,
+ causeStackTrace.size(), "The cause stack trace length is not correct.");
for (int i = 0; i < exception1.getStackTrace().length; i++) {
final StackTraceElement e1 = exception1.getStackTrace()[i];
final Map<String, Object> e2 = causeStackTrace.get(i);
- assertEquals("Element class name [" + i + "] is not correct.", e1.getClassName(), e2.get("className"));
- assertEquals("Element method name [" + i + "] is not correct.", e1.getMethodName(),
- e2.get("methodName"));
- assertEquals("Element file name [" + i + "] is not correct.", e1.getFileName(), e2.get("fileName"));
- assertEquals("Element line number [" + i + "] is not correct.", e1.getLineNumber(),
- e2.get("lineNumber"));
+ assertEquals(e1.getClassName(), e2.get("className"), "Element class name [" + i + "] is not correct.");
+ assertEquals(e1.getMethodName(),
+ e2.get("methodName"), "Element method name [" + i + "] is not correct.");
+ assertEquals(e1.getFileName(), e2.get("fileName"), "Element file name [" + i + "] is not correct.");
+ assertEquals(e1.getLineNumber(),
+ e2.get("lineNumber"), "Element line number [" + i + "] is not correct.");
}
- assertNull("The cause should have no cause.", cause.get("cause"));
+ assertNull(cause.get("cause"), "The cause should have no cause.");
- assertTrue("The context map should be a map.", object.get("contextMap") instanceof Map);
- assertEquals("The context map is not correct.", context, object.get("contextMap"));
+ assertTrue(object.get("contextMap") instanceof Map, "The context map should be a map.");
+ assertEquals(context, object.get("contextMap"), "The context map is not correct.");
- assertTrue("The context stack should be list.", object.get("contextStack") instanceof List);
- assertEquals("The context stack is not correct.", stack.asList(), object.get("contextStack"));
+ assertTrue(object.get("contextStack") instanceof List, "The context stack should be list.");
+ assertEquals(stack.asList(), object.get("contextStack"), "The context stack is not correct.");
}
}
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RandomRollingAppenderOnStartupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RandomRollingAppenderOnStartupTest.java
index c24fa50..b145be7 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RandomRollingAppenderOnStartupTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RandomRollingAppenderOnStartupTest.java
@@ -16,56 +16,33 @@
*/
package org.apache.logging.log4j.core.appender.rolling;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.junit.LoggerContextSource;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.util.Arrays;
-import java.util.Collection;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.junit.LoggerContextRule;
-import org.junit.AfterClass;
-import org.junit.Before;
-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.assertTrue;
+import static org.apache.logging.log4j.util.Unbox.box;
+import static org.junit.jupiter.api.Assertions.assertEquals;
-/**
- *
- */
-@RunWith(Parameterized.class)
+@LoggerContextSource(value = "log4j-test5.xml", timeout = 10)
public class RandomRollingAppenderOnStartupTest {
private static final String DIR = "target/onStartup";
- private Logger logger;
-
- @Parameterized.Parameters(name = "{0} \u2192 {1}")
- public static Collection<Object[]> data() {
- return Arrays.asList(new Object[][] { //
- // @formatter:off
- {"log4j-test5.xml"},
- {"log4j-test5.xml"},});
- // @formatter:on
- }
-
- @Rule
- public LoggerContextRule loggerContextRule;
-
- public RandomRollingAppenderOnStartupTest(final String configFile) {
- this.loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(configFile);
- }
+ private final Logger logger;
- @Before
- public void setUp() throws Exception {
- this.logger = this.loggerContextRule.getLogger(RandomRollingAppenderOnStartupTest.class.getName());
+ public RandomRollingAppenderOnStartupTest(final LoggerContext context) {
+ logger = context.getLogger(getClass());
}
- @BeforeClass
+ @BeforeAll
public static void beforeClass() throws Exception {
if (Files.exists(Paths.get("target/onStartup"))) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(DIR))) {
@@ -77,7 +54,7 @@ public class RandomRollingAppenderOnStartupTest {
}
}
- @AfterClass
+ @AfterAll
public static void afterClass() throws Exception {
long size = 0;
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(DIR))) {
@@ -86,8 +63,7 @@ public class RandomRollingAppenderOnStartupTest {
size = Files.size(path);
} else {
final long fileSize = Files.size(path);
- assertTrue("Expected size: " + size + " Size of " + path.getFileName() + ": " + fileSize,
- size == fileSize);
+ assertEquals(fileSize, size, "Expected size: " + size + " Size of " + path.getFileName() + ": " + fileSize);
}
Files.delete(path);
}
@@ -98,7 +74,7 @@ public class RandomRollingAppenderOnStartupTest {
@Test
public void testAppender() throws Exception {
for (int i = 0; i < 100; ++i) {
- logger.debug("This is test message number " + i);
+ logger.debug("This is test message number {}", box(i));
}
}