All log messages are written to System.out. System.out is a shared resource among all other - * threads. For this reason, we run tests in this class in the same thread (disabling parallelism). - * This approach prevents log messages from other threads from interfering. Since all the tests in - * this class are relatively fast, it does not significantly impact overall performance. - * We disable parallelism by using the {@link Execution} annotation with - * {@link ExecutionMode#SAME_THREAD}. DO NOT REMOVE THAT ANNOTATION!
- * - * @since 0.28.11 - */ -@Execution(ExecutionMode.SAME_THREAD) -@SuppressWarnings("JTCOP.RuleAllTestsHaveProductionClass") -final class LogFormatTest { - - /** - * Expected log message format. - */ - private static final String FORMAT = - "^\\d{2}:\\d{2}:\\d{2} \\[INFO] org.eolang.maven.LogFormatTest: Wake up, Neo...\\R"; - - @Test - @CaptureLogs - void printsFormattedMessage(final Logs out) { - final String msg = "Wake up, Neo..."; - Logger.info(this, msg); - final String actual = out.waitForMessage(msg); - MatcherAssert.assertThat( - String.format( - "Actual log output is '%s', but expected pattern is: '%s'", - actual, - LogFormatTest.FORMAT - ), - actual, - Matchers.matchesPattern(LogFormatTest.FORMAT) - ); - } - - @Test - void matchesCorrectly() { - MatcherAssert.assertThat( - CatalogsTest.TO_ADD_MESSAGE, - "16:02:08 [INFO] org.eolang.maven.LogFormatTest: Wake up, Neo...\n", - Matchers.matchesPattern(LogFormatTest.FORMAT) - ); - } -} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java index 0c00b1d444..8a87f54711 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java @@ -44,13 +44,10 @@ import org.eolang.maven.hash.ChRemote; import org.eolang.maven.hash.ChText; import org.eolang.maven.hash.CommitHash; -import org.eolang.maven.log.CaptureLogs; -import org.eolang.maven.log.Logs; import org.eolang.maven.util.HmBase; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.hamcrest.io.FileMatchers; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -183,31 +180,6 @@ void doesNotPullInOfflineMode(@Mktmp final Path tmp) throws IOException { ); } - @Test - @CaptureLogs - void showsWhereNotFoundWasDiscoveredAt(@Mktmp final Path tmp, final Logs out) - throws IOException { - final FakeMaven mvn = new FakeMaven(tmp) - .withProgram( - "+package com.example\n", - "# No comments.", - "[] > main", - " org.eolang.org > @" - ) - .with("objectionary", new OyRemote(new ChRemote("master"))); - Assertions.assertThrows( - Exception.class, - () -> mvn.execute(new FakeMaven.Pull()), - "Pull mojo should fail, but it does not" - ); - Assertions.assertTrue( - out.captured().stream().anyMatch( - line -> line.contains("Failed to pull 'org.eolang.org' earlier discovered at") - ), - "Log should contain info where failed to pull object was discovered at, but it does not" - ); - } - @Test void skipsAlreadyPulled(@Mktmp final Path temp) throws IOException { final FakeMaven maven = new FakeMaven(temp) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/SafeMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/SafeMojoTest.java deleted file mode 100644 index 2dc643690c..0000000000 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/SafeMojoTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2025 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.eolang.maven; - -import com.yegor256.Mktmp; -import com.yegor256.MktmpResolver; -import java.nio.file.Path; -import org.eolang.maven.log.CaptureLogs; -import org.eolang.maven.log.Logs; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; - -/** - * Test case for {@link SafeMojo}. - * - * @since 0.1 - */ -@ExtendWith(MktmpResolver.class) -final class SafeMojoTest { - - @Test - @CaptureLogs - void logsStackTrace(final Logs out, @Mktmp final Path temp) { - Assertions.assertDoesNotThrow( - () -> new FakeMaven(temp) - .withProgram("something > is definitely wrong here") - .execute(new FakeMaven.Parse()), - CatalogsTest.TO_ADD_MESSAGE - ); - MatcherAssert.assertThat( - CatalogsTest.TO_ADD_MESSAGE, - String.join("\n", out.captured()), - Matchers.containsString("Failed to parse") - ); - } - -} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/log/CaptureLogs.java b/eo-maven-plugin/src/test/java/org/eolang/maven/log/CaptureLogs.java deleted file mode 100644 index fb88560d00..0000000000 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/log/CaptureLogs.java +++ /dev/null @@ -1,178 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2025 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.eolang.maven.log; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; -import java.util.Enumeration; -import org.apache.log4j.Appender; -import org.apache.log4j.ConsoleAppender; -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; -import org.apache.log4j.spi.LoggingEvent; -import org.junit.jupiter.api.extension.AfterEachCallback; -import org.junit.jupiter.api.extension.BeforeEachCallback; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.ParameterContext; -import org.junit.jupiter.api.extension.ParameterResolver; - -/** - * Captured logs annotation for tests. - * @todo #2896:90min Make '@CaptureLogs' thread-safe. - * 'Logs' should contain only messages related to the test. - * Currently, '@CaptureLogs' appends all messages that - * were logged via 'Logger' to 'Logs', so messages from - * other tests (run in parallel) are also included, which causes - * problems when tests are run in parallel. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) -@ExtendWith(CaptureLogs.CaptureLogsExtension.class) -@SuppressWarnings("JTCOP.RuleAllTestsHaveProductionClass") -public @interface CaptureLogs { - - /** - * JUnit extension to capture log messages. - * - * @since 0.30 - */ - final class CaptureLogsExtension implements - ParameterResolver, BeforeEachCallback, AfterEachCallback { - - /** - * Logs. - */ - private final Logs logs; - - /** - * Appender. - */ - private final CaptureLogsAppender appender; - - /** - * Ctor. - */ - CaptureLogsExtension() { - this(new Logs()); - } - - /** - * Ctor. - * @param logs Where to save logs from the appender. - */ - private CaptureLogsExtension(final Logs logs) { - this(logs, new CaptureLogsAppender(logs)); - } - - /** - * Ctor. - * @param logs Where to save logs from the appender. - * @param appender Appender to use. - */ - private CaptureLogsExtension(final Logs logs, final CaptureLogsAppender appender) { - this.logs = logs; - this.appender = appender; - } - - @Override - public void beforeEach(final ExtensionContext context) { - this.appender.init(); - } - - @Override - public void afterEach(final ExtensionContext context) { - this.appender.destroy(); - } - - @Override - public boolean supportsParameter( - final ParameterContext param, - final ExtensionContext extension - ) { - return param.getParameter().getType() == Logs.class; - } - - @Override - public Object resolveParameter( - final ParameterContext param, - final ExtensionContext extension - ) { - return this.logs; - } - } - - /** - * Log4j logger appender. - * - * @since 0.30 - */ - final class CaptureLogsAppender extends ConsoleAppender { - - /** - * Where to save logs from the appender. - */ - private final Logs logs; - - /** - * Ctor. - * @param logs Where to save logs from the appender. - */ - CaptureLogsAppender(final Logs logs) { - this.logs = logs; - } - - @Override - public void append(final LoggingEvent event) { - this.logs.append(this.getLayout().format(event)); - } - - /** - * Initialize the appender. - * Adds appender to the root logger. - */ - void init() { - final Logger logger = LogManager.getRootLogger(); - final Enumeration> appenders = logger.getAllAppenders(); - if (appenders.hasMoreElements()) { - final Object next = appenders.nextElement(); - if (next instanceof ConsoleAppender) { - this.setLayout(((Appender) next).getLayout()); - } - } - logger.addAppender(this); - this.logs.waitForInit(); - } - - /** - * Destroy the appender. - * Removes appender from the root logger. - */ - void destroy() { - LogManager.getRootLogger().removeAppender(this); - } - } -} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/log/Logs.java b/eo-maven-plugin/src/test/java/org/eolang/maven/log/Logs.java deleted file mode 100644 index d660589705..0000000000 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/log/Logs.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2025 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.eolang.maven.log; - -import com.jcabi.log.Logger; -import java.util.Collection; -import java.util.Collections; -import java.util.Optional; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -/** - * Captured logs. - * - * @since 0.30 - */ -@SuppressWarnings({"JTCOP.RuleAllTestsHaveProductionClass", "JTCOP.RuleCorrectTestName"}) -public final class Logs { - - /** - * Captured logs. - */ - private final Collection