Skip to content

Commit

Permalink
Convert modules to junit 5
Browse files Browse the repository at this point in the history
Signed-off-by: Ninette Adhikari <[email protected]>
  • Loading branch information
ninetteadhikari committed Sep 30, 2024
1 parent 9d3ddd0 commit d18b772
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/
package org.apache.logging.log4j.core.async;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -29,26 +29,25 @@
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.impl.DefaultLogEventFactory;
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
import org.apache.logging.log4j.message.AsynchronouslyFormattable;
import org.apache.logging.log4j.message.Message;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Category(AsyncLoggers.class)
@Tag("AsyncLoggers")
public class AsyncLoggerConfigErrorOnFormat {

@BeforeClass
@BeforeAll
public static void beforeClass() {
System.setProperty("log4j2.enableThreadlocals", "true");
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConfigErrorOnFormat.xml");
// Log4jLogEvent.toString invokes message.toString
System.setProperty("log4j2.logEventFactory", DefaultLogEventFactory.class.getName());
}

@AfterClass
@AfterAll
public static void afterClass() {
System.clearProperty("log4j2.enableThreadlocals");
System.clearProperty("log4j2.logEventFactory");
Expand All @@ -57,7 +56,7 @@ public static void afterClass() {
@Test
public void testError() throws Exception {
final File file = new File("target", "AsyncLoggerConfigErrorOnFormat.log");
assertTrue("Deleted old file before test", !file.exists() || file.delete());
assertTrue(!file.exists() || file.delete(), "Deleted old file before test");

final Logger log = LogManager.getLogger("com.foo.Bar");
log.info(new ThrowsErrorOnFormatMessage());
Expand All @@ -71,7 +70,7 @@ public void testError() throws Exception {
file.delete();

assertThat(line1, containsString("Second message"));
assertNull("Expected only one line", line2);
assertNull(line2, "Expected only one line");
}

@AsynchronouslyFormattable // Shouldn't call getFormattedMessage early
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@
package org.apache.logging.log4j.core.config.plugins.processor;

import static org.apache.logging.log4j.util.Strings.toRootLowerCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAliases;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@RunWith(JUnit4.class)
@ExtendWith(MockitoExtension.class)
public class PluginProcessorTest {

private static final PluginCache pluginCache = new PluginCache();

private final Plugin p = FakePlugin.class.getAnnotation(Plugin.class);

@BeforeClass
@BeforeAll
public static void setUpClass() throws Exception {
final Enumeration<URL> resources =
PluginProcessor.class.getClassLoader().getResources(PluginProcessor.PLUGIN_CACHE_FILE);
Expand All @@ -48,10 +48,10 @@ public static void setUpClass() throws Exception {

@Test
public void testTestCategoryFound() throws Exception {
assertNotNull("No plugin annotation on FakePlugin.", p);
assertNotNull(p, "No plugin annotation on FakePlugin.");
final Map<String, PluginEntry> testCategory = pluginCache.getCategory(p.category());
assertNotEquals("No plugins were found.", 0, pluginCache.size());
assertNotNull("The category '" + p.category() + "' was not found.", testCategory);
assertNotEquals(0, pluginCache.size(), "No plugins were found.");
assertNotNull(testCategory, "The category '" + p.category() + "' was not found.");
assertFalse(testCategory.isEmpty());
}

Expand All @@ -71,7 +71,7 @@ public void testFakePluginAliasesContainSameInformation() throws Exception {
}

private void verifyFakePluginEntry(final String name, final PluginEntry fake) {
assertNotNull("The plugin '" + toRootLowerCase(name) + "' was not found.", fake);
assertNotNull(fake, "The plugin '" + toRootLowerCase(name) + "' was not found.");
assertEquals(FakePlugin.class.getName(), fake.getClassName());
assertEquals(toRootLowerCase(name), fake.getKey());
assertEquals(Plugin.EMPTY, p.elementType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.IOException;
import org.apache.logging.log4j.core.test.categories.Layouts;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Category(Layouts.Json.class)
@Tag("Layouts.Json")
public class StackTraceElementMixInTest {

@Test
Expand All @@ -53,7 +52,7 @@ private void roundtrip(final ObjectMapper mapper)
new StackTraceElement("package.SomeClass", "someMethod", "SomeClass.java", 123);
final String s = mapper.writeValueAsString(expected);
final StackTraceElement actual = mapper.readValue(s, StackTraceElement.class);
Assert.assertEquals(expected, actual);
Assertions.assertEquals(expected, actual);
}

@Test
Expand All @@ -76,7 +75,7 @@ public void testFromJsonWithSimpleModule() throws Exception {
final String s = this.aposToQuotes(
"{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':123}");
final StackTraceElement actual = mapper.readValue(s, StackTraceElement.class);
Assert.assertEquals(expected, actual);
Assertions.assertEquals(expected, actual);
}

@Test
Expand All @@ -91,6 +90,6 @@ public void testFromJsonWithLog4jModule() throws Exception {
final String s = this.aposToQuotes(
"{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':123}");
final StackTraceElement actual = mapper.readValue(s, StackTraceElement.class);
Assert.assertEquals(expected, actual);
Assertions.assertEquals(expected, actual);
}
}

0 comments on commit d18b772

Please sign in to comment.