FrankChen021 commented on code in PR #19940:
URL: https://github.com/apache/druid/pull/19940#discussion_r3742745368
##########
processing/src/test/java/org/apache/druid/java/util/common/StringUtilsTest.java:
##########
@@ -150,165 +145,167 @@
final ByteBuffer bytes = bufferHolder.get();
bytes.put(new byte[]{'a', 'b', 'c', 'd'});
bytes.rewind();
- Assert.assertEquals("abcd", StringUtils.fromUtf8(bytes, 4));
+ Assertions.assertEquals("abcd", StringUtils.fromUtf8(bytes, 4));
bytes.rewind();
- Assert.assertEquals("abcd", StringUtils.fromUtf8(bytes));
+ Assertions.assertEquals("abcd", StringUtils.fromUtf8(bytes));
}
}
@SuppressWarnings("MalformedFormatString")
@Test
public void testNonStrictFormat()
{
- Assert.assertEquals("test%d; format",
StringUtils.nonStrictFormat("test%d", "format"));
- Assert.assertEquals("test%s%s; format",
StringUtils.nonStrictFormat("test%s%s", "format"));
+ Assertions.assertEquals("test%d; format",
StringUtils.nonStrictFormat("test%d", "format"));
+ Assertions.assertEquals("test%s%s; format",
StringUtils.nonStrictFormat("test%s%s", "format"));
Review Comment:
Evaluated this finding. It is pre-existing in the intentional
nonStrictFormat test case, which verifies that one supplied argument is
retained while the second format token remains literal. This PR only changed
the JUnit assertion API, so no code change was made for this finding.
##########
processing/src/test/java/org/apache/druid/java/util/common/CompressionUtilsTest.java:
##########
@@ -610,49 +610,49 @@
);
}
catch (ISE ise) {
- Assert.assertTrue(ise.getMessage().contains("does not start with
outDir"));
- Assert.assertFalse("Zip exploit triggered, /tmp/evil.txt was written.",
evilResult.exists());
+ Assertions.assertTrue(ise.getMessage().contains("does not start with
outDir"));
+ Assertions.assertFalse(evilResult.exists(), "Zip exploit triggered,
/tmp/evil.txt was written.");
return;
}
- Assert.fail("Exception was not thrown for malicious zip file");
+ Assertions.fail("Exception was not thrown for malicious zip file");
}
@Test
// Sanity check to make sure the test class works as expected
public void testZeroRemainingInputStream() throws IOException
{
try (OutputStream outputStream = new FileOutputStream(testFile)) {
- Assert.assertEquals(
+ Assertions.assertEquals(
GZ_BYTES.length,
ByteStreams.copy(
new ZeroRemainingInputStream(new ByteArrayInputStream(GZ_BYTES)),
outputStream
)
);
- Assert.assertEquals(
+ Assertions.assertEquals(
GZ_BYTES.length,
ByteStreams.copy(
new ZeroRemainingInputStream(new ByteArrayInputStream(GZ_BYTES)),
outputStream
)
);
- Assert.assertEquals(
+ Assertions.assertEquals(
GZ_BYTES.length,
ByteStreams.copy(
new ZeroRemainingInputStream(new ByteArrayInputStream(GZ_BYTES)),
outputStream
)
);
}
- Assert.assertEquals(GZ_BYTES.length * 3, testFile.length());
+ Assertions.assertEquals(GZ_BYTES.length * 3, testFile.length());
Review Comment:
Evaluated this finding. The GZ_BYTES.length * 3 expression was already
present before this PR; only the assertion API changed. It is unrelated to the
JUnit 5 migration and was left unchanged.
##########
processing/src/test/java/org/apache/druid/utils/CloseableUtilsTest.java:
##########
@@ -79,7 +80,7 @@
MatcherAssert.assertThat(e, CoreMatchers.instanceOf(IOException.class));
// Second exception
- Assert.assertEquals(1, e.getSuppressed().length);
+ Assertions.assertEquals(1, e.getSuppressed().length);
Review Comment:
Evaluated this finding. The nullable local is intentional test setup:
closeAll is expected to throw and the test checks the captured exception. This
code predates PR #19940; the migration only changed the assertion API, so it
was left unchanged.
##########
processing/src/test/java/org/apache/druid/utils/CloseableUtilsTest.java:
##########
@@ -108,7 +109,7 @@
MatcherAssert.assertThat(e, CoreMatchers.instanceOf(IOException.class));
// Second exception
- Assert.assertEquals(1, e.getSuppressed().length);
+ Assertions.assertEquals(1, e.getSuppressed().length);
Review Comment:
Evaluated this finding. The nullable local is intentional test setup:
closeAll is expected to throw and the test checks the captured exception. This
code predates PR #19940; the migration only changed the assertion API, so it
was left unchanged.
##########
processing/src/test/java/org/apache/druid/testing/junit/LoggerCaptureExtension.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.druid.testing.junit;
+
+import com.google.common.collect.ImmutableList;
+import com.google.errorprone.annotations.concurrent.GuardedBy;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * JUnit 5 extension that captures Log4j events emitted by a target class.
+ * Register it with {@code new LoggerCaptureExtension(TargetClass.class)} and
inspect {@link #getLogEvents()}.
+ */
+public class LoggerCaptureExtension implements BeforeEachCallback,
AfterEachCallback
+{
+ private final Class<?> targetClass;
+ private InMemoryAppender inMemoryAppender;
+ private LoggerConfig targetClassLoggerConfig;
+
+ public LoggerCaptureExtension(final Class<?> targetClass)
+ {
+ this.targetClass = targetClass;
+ }
+
+ @Override
+ public void beforeEach(final ExtensionContext context)
+ {
+ inMemoryAppender = new InMemoryAppender(targetClass);
+ final LoggerContext loggerContext = (LoggerContext)
LogManager.getContext(false);
+ final Configuration configuration = loggerContext.getConfiguration();
+ targetClassLoggerConfig =
configuration.getLoggerConfig(targetClass.getName());
+ targetClassLoggerConfig.addAppender(inMemoryAppender, Level.ALL, null);
+ }
+
+ @Override
+ public void afterEach(final ExtensionContext context)
+ {
+ clearLogEvents();
+ targetClassLoggerConfig.removeAppender(InMemoryAppender.NAME);
+ }
+
+ public List<LogEvent> getLogEvents()
+ {
+ return inMemoryAppender.getLogEvents();
+ }
+
+ public void clearLogEvents()
+ {
+ inMemoryAppender.clearLogEvents();
+ }
+
+ public void awaitLogEvents() throws InterruptedException
+ {
+ inMemoryAppender.awaitLogEvents();
+ }
+
+ private static class InMemoryAppender extends AbstractAppender
+ {
+ private static final String NAME = InMemoryAppender.class.getName();
+ private final String targetLoggerName;
+ @GuardedBy("logEvents")
+ private final List<LogEvent> logEvents = new ArrayList<>();
+
+ InMemoryAppender(final Class<?> targetClass)
+ {
+ super(NAME, null, null);
Review Comment:
Fixed in commit 3b92250d3c by replacing the deprecated three-argument
AbstractAppender constructor with the supported five-argument overload and
Property.EMPTY_ARRAY. Processing test compilation passed with mvn -ntp -pl
processing test-compile -Dweb.console.skip=true -T1C.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]