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

JUnit 5 migration: Replace params with subclassing #3256

Closed
Closed
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 @@ -16,55 +16,41 @@
*/
package org.apache.logging.log4j.core.appender.routing;

import static org.junit.Assert.assertEquals;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.AppenderControl;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.apache.logging.log4j.core.util.Constants;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
*
*/
@RunWith(Parameterized.class)
public class DefaultRouteScriptAppenderTest {

@Parameterized.Parameters(name = "{0} {1}")
public static Object[][] getParameters() {
// @formatter:off
return new Object[][] {
{"log4j-routing-default-route-script-groovy.xml", false},
{"log4j-routing-default-route-script-javascript.xml", false},
{"log4j-routing-script-staticvars-javascript.xml", true},
{"log4j-routing-script-staticvars-groovy.xml", true},
};
// @formatter:on
}
public abstract class DefaultRouteScriptAppenderTest {

@BeforeClass
@BeforeAll
public static void beforeClass() {
System.setProperty(Constants.SCRIPT_LANGUAGES, "Groovy, Javascript");
}

@Rule
public final LoggerContextRule loggerContextRule;
public final LoggerContext loggerContext;

private final boolean expectBindingEntries;

public DefaultRouteScriptAppenderTest(final String configLocation, final boolean expectBindingEntries) {
this.loggerContextRule = new LoggerContextRule(configLocation);
public DefaultRouteScriptAppenderTest(final LoggerContext context, boolean expectBindingEntries) {
this.loggerContext = context;
this.expectBindingEntries = expectBindingEntries;
}

Expand All @@ -80,60 +66,59 @@ private void checkStaticVars() {
private ListAppender getListAppender() {
final String key = "Service2";
final RoutingAppender routingAppender = getRoutingAppender();
Assert.assertTrue(routingAppender.isStarted());
assertTrue(routingAppender.isStarted());
final Map<String, AppenderControl> appenders = routingAppender.getAppenders();
final AppenderControl appenderControl = appenders.get(key);
assertNotNull("No appender control generated for '" + key + "'; appenders = " + appenders, appenderControl);
assertNotNull(appenderControl, "No appender control generated for '" + key + "'; appenders = " + appenders);
final ListAppender listAppender = (ListAppender) appenderControl.getAppender();
return listAppender;
}

private RoutingAppender getRoutingAppender() {
return loggerContextRule.getRequiredAppender("Routing", RoutingAppender.class);
return (RoutingAppender) loggerContext.getConfiguration().getAppenders().get("Routing");
}

private void logAndCheck() {
final Marker marker = MarkerManager.getMarker("HEXDUMP");
final Logger logger = loggerContextRule.getLogger(DefaultRouteScriptAppenderTest.class);
final Logger logger = (Logger) loggerContext.getLogger(DefaultRouteScriptAppenderTest.class);
logger.error("Hello");
final ListAppender listAppender = getListAppender();
assertEquals("Incorrect number of events", 1, listAppender.getEvents().size());
assertEquals(1, listAppender.getEvents().size(), "Incorrect number of events");
logger.error("World");
assertEquals("Incorrect number of events", 2, listAppender.getEvents().size());
assertEquals(2, listAppender.getEvents().size(), "Incorrect number of events");
logger.error(marker, "DEADBEEF");
assertEquals("Incorrect number of events", 3, listAppender.getEvents().size());
assertEquals(3, listAppender.getEvents().size(), "Incorrect number of events");
}

@Test(expected = AssertionError.class)
public void testAppenderAbsence() {
loggerContextRule.getListAppender("List1");
@Test
public void testNotListAppender() {
Appender appender = loggerContext.getConfiguration().getAppender("List1");
assertFalse(appender instanceof ListAppender);
}

@Test
public void testListAppenderPresence() {
// No appender until an event is routed, even thought we initialized the default route on startup.
Assert.assertNull(
"No appender control generated",
getRoutingAppender().getAppenders().get("Service2"));
assertNull(getRoutingAppender().getAppenders().get("Service2"), "No appender control generated");
}

@Test
public void testNoPurgePolicy() {
// No PurgePolicy in this test
Assert.assertNull("Unexpected PurgePolicy", getRoutingAppender().getPurgePolicy());
assertNull(getRoutingAppender().getPurgePolicy(), "Unexpected PurgePolicy");
}

@Test
public void testNoRewritePolicy() {
// No RewritePolicy in this test
Assert.assertNull("Unexpected RewritePolicy", getRoutingAppender().getRewritePolicy());
assertNull(getRoutingAppender().getRewritePolicy(), "Unexpected RewritePolicy");
}

@Test
public void testRoutingAppenderDefaultRouteKey() {
final RoutingAppender routingAppender = getRoutingAppender();
Assert.assertNotNull(routingAppender.getDefaultRouteScript());
Assert.assertNotNull(routingAppender.getDefaultRoute());
assertNotNull(routingAppender.getDefaultRouteScript());
assertNotNull(routingAppender.getDefaultRoute());
assertEquals("Service2", routingAppender.getDefaultRoute().getKey());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.routing;

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.ReconfigurationPolicy;

@LoggerContextSource(value = "log4j-routing-default-route-script-groovy.xml", reconfigure = ReconfigurationPolicy.BEFORE_EACH)
public class DefaultRouteScriptGroovyAppenderTest extends DefaultRouteScriptAppenderTest {
public DefaultRouteScriptGroovyAppenderTest(LoggerContext context) {
super(context, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.routing;

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.ReconfigurationPolicy;

@LoggerContextSource(value = "log4j-routing-default-route-script-javascript.xml", reconfigure = ReconfigurationPolicy.BEFORE_EACH)
public class DefaultRouteScriptJavaScriptAppenderTest extends DefaultRouteScriptAppenderTest {
public DefaultRouteScriptJavaScriptAppenderTest(LoggerContext context) {
super(context, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.routing;

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.ReconfigurationPolicy;

@LoggerContextSource(value = "log4j-routing-script-staticvars-groovy.xml", reconfigure = ReconfigurationPolicy.BEFORE_EACH)
public class ScriptStaticVarsGroovyAppenderTest extends DefaultRouteScriptAppenderTest {
public ScriptStaticVarsGroovyAppenderTest(LoggerContext context) {
super(context, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.routing;

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.ReconfigurationPolicy;

@LoggerContextSource(value = "log4j-routing-script-staticvars-javascript.xml", reconfigure = ReconfigurationPolicy.BEFORE_EACH)
public class ScriptStaticVarsJavaScriptAppenderTest extends DefaultRouteScriptAppenderTest {
public ScriptStaticVarsJavaScriptAppenderTest(LoggerContext context) {
super(context, true);
}
}