Skip to content

Commit 9dfb111

Browse files
committed
Improve validation for AsyncWaitStrategyFactoryConfig for null/empty 'factoryClassName' (apache#3159)
1 parent c59fdd4 commit 9dfb111

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryConfigTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import static org.hamcrest.MatcherAssert.assertThat;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertNull;
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
2324

2425
import com.lmax.disruptor.WaitStrategy;
2526
import com.lmax.disruptor.YieldingWaitStrategy;
2627
import org.apache.logging.log4j.core.LoggerContext;
28+
import org.apache.logging.log4j.core.config.Configuration;
2729
import org.apache.logging.log4j.core.test.appender.ListAppender;
2830
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
2931
import org.apache.logging.log4j.core.test.junit.Named;
@@ -69,6 +71,37 @@ void testIncorrectConfigWaitStrategyFactory(final LoggerContext context) {
6971
assertNull(asyncWaitStrategyFactory);
7072
}
7173

74+
/**
75+
* Test that when XML element {@code AsyncWaitFactory} has no 'class' attribute.
76+
*
77+
* @param configuration the configuration
78+
*/
79+
@Test
80+
@LoggerContextSource("log4j2-asyncwaitfactoryconfig-3159-nok.xml")
81+
void testInvalidBuilderConfiguration3159(final Configuration configuration) {
82+
assertNull(configuration.getAsyncWaitStrategyFactory(), "The AsyncWaitStrategyFactory should be null.");
83+
}
84+
85+
/**
86+
* Test that when programmatically building a {@link AsyncWaitStrategyFactoryConfig} a {@code null}
87+
* factory class-name throws an exception.
88+
*/
89+
@Test
90+
void testInvalidProgrammaticConfiguration3159WithNullFactoryClassName() {
91+
assertThrows(IllegalArgumentException.class, () -> AsyncWaitStrategyFactoryConfig.newBuilder()
92+
.withFactoryClassName(null));
93+
}
94+
95+
/**
96+
* Test that when programmatically building a {@link AsyncWaitStrategyFactoryConfig} a blank ({@code ""})
97+
* factory class-name throws an exception.
98+
*/
99+
@Test
100+
void testInvalidProgrammaticConfiguration3159WithEmptyFactoryClassName() {
101+
assertThrows(IllegalArgumentException.class, () -> AsyncWaitStrategyFactoryConfig.newBuilder()
102+
.withFactoryClassName(""));
103+
}
104+
72105
@Test
73106
@LoggerContextSource("AsyncWaitStrategyIncorrectFactoryConfigTest.xml")
74107
void testIncorrectWaitStrategyFallsBackToDefault(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration status="warn">
19+
<AsyncWaitStrategyFactory/> <!-- no 'class' attribute -->
20+
</Configuration>

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryConfig.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19-
import java.util.Objects;
2019
import org.apache.logging.log4j.core.Core;
2120
import org.apache.logging.log4j.core.config.plugins.Plugin;
2221
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
2322
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
2423
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
24+
import org.apache.logging.log4j.core.util.Assert;
2525
import org.apache.logging.log4j.status.StatusLogger;
2626
import org.apache.logging.log4j.util.LoaderUtil;
2727

@@ -41,7 +41,7 @@ public class AsyncWaitStrategyFactoryConfig {
4141
private final String factoryClassName;
4242

4343
public AsyncWaitStrategyFactoryConfig(final String factoryClassName) {
44-
this.factoryClassName = Objects.requireNonNull(factoryClassName, "factoryClassName");
44+
this.factoryClassName = Assert.requireNonEmpty(factoryClassName, "factoryClassName");
4545
}
4646

4747
@PluginBuilderFactory
@@ -67,12 +67,16 @@ public String getFactoryClassName() {
6767
}
6868

6969
public B withFactoryClassName(final String className) {
70-
this.factoryClassName = className;
70+
this.factoryClassName =
71+
Assert.requireNonEmpty(className, "The 'className' argument must not be null or empty.");
7172
return asBuilder();
7273
}
7374

7475
@Override
7576
public AsyncWaitStrategyFactoryConfig build() {
77+
if (!isValid()) {
78+
return null;
79+
}
7680
return new AsyncWaitStrategyFactoryConfig(factoryClassName);
7781
}
7882

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="changed">
6+
<issue id="3159" link="https://github.com/apache/logging-log4j2/issues/3159"/>
7+
<description format="asciidoc">Add improved validation to AsyncWaitStrategyFactoryConfig for null/empty factoryClassName. GitHub issue #3159.</description>
8+
</entry>

0 commit comments

Comments
 (0)