Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate log4j-api-test to junit5 #3218

Draft
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,22 @@
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.Serializable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.Test;

/**
* Subclasses tests {@link Serializable} objects.
*/
@RunWith(Parameterized.class)
public abstract class AbstractSerializationTest {

private final Serializable serializable;

public AbstractSerializationTest(final Serializable serializable) {
this.serializable = serializable;
}
public AbstractSerializationTest() {}

@Test
public void testSerializationRoundtripEquals() {
public void testSerializationRoundtripEquals(Serializable serializable) {
assertThat(serializable, serializesRoundTrip(serializable));
}

@Test
public void testSerializationRoundtripNoException() {
public void testSerializationRoundtripNoException(Serializable serializable) {
assertThat(serializable, serializesRoundTrip());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.test.junit;

import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class SecurityManagerExtension implements BeforeEachCallback, AfterEachCallback {
private SecurityManager securityManagerBefore;
private final SecurityManager securityManager;

public SecurityManagerExtension(final SecurityManager securityManager) {
this.securityManager = securityManager;
}

public void beforeEach(ExtensionContext ctx) {
securityManagerBefore = System.getSecurityManager();
System.setSecurityManager(securityManager);
}

public void afterEach(ExtensionContext ctx) {
System.setSecurityManager(securityManagerBefore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
*/
package org.apache.logging.log4j.message;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import org.apache.logging.log4j.test.AbstractSerializationTest;
import org.junit.jupiter.api.parallel.ResourceAccessMode;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.Resources;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

@ResourceLock(value = Resources.LOCALE, mode = ResourceAccessMode.READ)
public class MessageFormatMessageSerializationTest extends AbstractSerializationTest {

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{new MessageFormatMessage("Test")},
Expand All @@ -36,7 +37,19 @@ public static Collection<Object[]> data() {
});
}

public MessageFormatMessageSerializationTest(final MessageFormatMessage message) {
super(message);
public MessageFormatMessageSerializationTest() {
super();
}

@ParameterizedTest
@MethodSource("data")
public void testSerializationRoundtripEquals(Serializable serializable) {
super.testSerializationRoundtripEquals(serializable);
}

@ParameterizedTest
@MethodSource("data")
public void testSerializationRoundtripNoException(Serializable serializable) {
super.testSerializationRoundtripNoException(serializable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package org.apache.logging.log4j.util;

import java.security.Permission;
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

/**
Expand All @@ -37,8 +37,8 @@
@ResourceLock("java.lang.SecurityManager")
public class EnvironmentPropertySourceSecurityManagerIT {

@Rule
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
@RegisterExtension
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());

/**
* Always throws a SecurityException for any environment variables permission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.security.Permission;
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

@ResourceLock("java.lang.SecurityManager")
public class LoaderUtilSecurityManagerTest {
@Rule
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
@RegisterExtension
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());

private static class TestSecurityManager extends SecurityManager {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
*/
package org.apache.logging.log4j.util;

import static org.junit.Assert.assertNull;
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.FilePermission;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.Permission;
import java.util.PropertyPermission;
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

/**
Expand All @@ -46,13 +46,13 @@
@ResourceLock("java.lang.SecurityManager")
public class PropertyFilePropertySourceSecurityManagerIT {

@BeforeClass
public static void beforeClass() {
assertTrue(TEST_FIXTURE_PATH, Files.exists(Paths.get(TEST_FIXTURE_PATH)));
@BeforeAll
public static void beforeAll() {
assertTrue(Files.exists(Paths.get(TEST_FIXTURE_PATH)), TEST_FIXTURE_PATH);
}

@Rule
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
@RegisterExtension
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());

private static final String TEST_FIXTURE_PATH = "src/test/resources/PropertiesUtilTest.properties";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import java.security.Permission;
import java.util.Deque;
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

/**
Expand All @@ -39,8 +39,8 @@
*/
@ResourceLock("java.lang.SecurityManager")
public class StackLocatorTestIT {
@Rule
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
@RegisterExtension
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());

/**
* Always throws a SecurityException for any reques to create a new SecurityManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package org.apache.logging.log4j.util;

import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.security.Permission;
import java.util.PropertyPermission;
import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.apache.logging.log4j.test.junit.SecurityManagerExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

/**
Expand All @@ -41,8 +41,8 @@
@ResourceLock("java.lang.SecurityManager")
public class SystemPropertiesPropertySourceSecurityManagerIT {

@Rule
public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
@RegisterExtension
public final SecurityManagerExtension ext = new SecurityManagerExtension(new TestSecurityManager());

/**
* Always throws a SecurityException for any environment variables permission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.util.Collection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.test.AbstractSerializationTest;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class LoggerSerializationTest extends AbstractSerializationTest {

@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{new LoggerContext("").getLogger("", null)},
Expand All @@ -35,7 +35,19 @@ public static Collection<Object[]> data() {
});
}

public LoggerSerializationTest(final Serializable serializable) {
super(serializable);
public LoggerSerializationTest() {
super();
}

@ParameterizedTest
@MethodSource("data")
public void testSerializationRoundtripEquals(Serializable serializable) {
super.testSerializationRoundtripEquals(serializable);
}

@ParameterizedTest
@MethodSource("data")
public void testSerializationRoundtripNoException(Serializable serializable) {
super.testSerializationRoundtripNoException(serializable);
}
}
Loading