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-slf4j2-impl to JUnit 5 #3080

Merged

Conversation

AlbaHerrerias
Copy link
Contributor

Hello 👋

We are from Neighbourhoodie, the implementation partner of the STF Bug Resilience Program. This work is part of our agreed Milestone 1. Upgrade from JUnit 4 to JUnit 5. This PR migrates the tests located in log4j-slf4j2-impl.

⚠️ We've found out that OverflowTest is set to run specifically with Junit 4. This behaviour was added not long ago so we wanted to ask, should we leave it like this or should we refactor OverflowTest to JUnit 5?

Thank you!

Checklist

  • Base your changes on 2.x branch if you are targeting Log4j 2; use main otherwise
  • ./mvnw verify succeeds (if it fails due to code formatting issues reported by Spotless, simply run ./mvnw spotless:apply and retry)
  • Non-trivial changes contain an entry file in the src/changelog/.2.x.x directory
  • Tests for the changes are provided
  • Commits are signed (optional, but highly recommended)

Copy link

github-actions bot commented Oct 11, 2024

Job Requested goals Build Tool Version Build Outcome Build Scan®
build-macos-latest clean install 3.9.8 Build Scan PUBLISHED
build-ubuntu-latest clean install 3.9.8 Build Scan PUBLISHED
build-windows-latest clean install 3.9.8 Build Scan PUBLISHED
Generated by gradle/develocity-actions

Copy link
Contributor

@ppkarwasz ppkarwasz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks!

Since the log4j-slf4j-impl module is mostly identical to this one (it has a couple of tests less). Could you apply the changes to that module too?

Could you also remove the junit-vintage-engine dependency in the pom.xml to show that not tests are using JUnit 4?

@@ -35,9 +35,9 @@ public void testCleanup() throws Exception {
factory.getLogger("test");
Set<LoggerContext> set = factory.getLoggerContexts();
final LoggerContext ctx1 = set.toArray(LoggerContext.EMPTY_ARRAY)[0];
assertTrue("LoggerContext is not enabled for shutdown", ctx1 instanceof LifeCycle);
assertTrue(ctx1 instanceof LifeCycle, "LoggerContext is not enabled for shutdown");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assertTrue(ctx1 instanceof LifeCycle, "LoggerContext is not enabled for shutdown");
assertInstanceOf(LifeCycle.class, ctx1, "LoggerContext is not enabled for shutdown");

Could you change the assertions on X instanceof Y to use the new assertInstanceOf assertions?

@ppkarwasz
Copy link
Contributor

ppkarwasz commented Oct 16, 2024

⚠️ We've found out that OverflowTest is set to run specifically with Junit 4. This behaviour was added not long ago so we wanted to ask, should we leave it like this or should we refactor OverflowTest to JUnit 5?

As far as I can tell OverflowTest does not depend specifically on JUnit 4. It only requires an additional dependency (log4j-to-slf4j). The module should detect that Log4j API is redirected to SLF4J and should not forward SLF4J calls back to Log4j API.
Taking advantage of new Maven Surefire Plugin capabilities I believe that such a configuration should work:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <executions>
         <!-- Separate test execution to verify that the presence of both:
            ~  * `log4j-slf4j2-impl` (bridge from SLF4J to Log4j API)
            ~  * `log4j-to-slf4j` (bridge from Log4j API to SLF4J)
            ~  does not cause a stack overflow.
            -->
          <execution>
            <id>loop-test</id>
            <goals>
              <goal>test</goal>
            </goals>
            <configuration>
             <additionalClasspathDependencies>
               <dependency>
                 <groupId>org.apache.logging.log4j</groupId>
                 <artifactId>log4j-to-slf4j</artifactId>
                 <version>${project.version}</version>
               </dependency>
             </additionalClasspathDependencies>
              <includes>
                <include>**/OverflowTest.java</include>
              </includes>
            </configuration>
          </execution>
          <execution>
            <id>default-test</id>
            <configuration>
              <includes>
                <include>**/*Test.java</include>
              </includes>
              <excludes>
                <exclude>**/OverflowTest.java</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
      </plugin>

and log4j-to-slf4j can be removed from the <dependencies> section of the project.

@AlbaHerrerias AlbaHerrerias force-pushed the log4j-slf4j2-impl-migrate-junit5 branch from c0b8144 to 4c23edd Compare October 21, 2024 14:41
@AlbaHerrerias
Copy link
Contributor Author

Thank you for the review and assistance, I have addressed now your requested changes. Please let me know if you'd like to request more.

I will make the refactor of the log4j-slf4j-impl module. My plan is to make it as a new PR (as we have been doing; a PR per module), but if you'd rather me doing it in this PR, please let me know and I will do so.

Also note that, with the removal of junit-vintage-engine, the tests of Log4j2_1482_Slf4jTest.java are not being executed (total of 2 tests). They
extend from log4j-core-test/.../Log4j2_1482_Test.java, currently written in JUnit 4. My coworker is refactoring those in #3061.

@AlbaHerrerias AlbaHerrerias force-pushed the log4j-slf4j2-impl-migrate-junit5 branch from 4c23edd to 67a954c Compare October 22, 2024 07:19
@ppkarwasz ppkarwasz self-assigned this Oct 23, 2024
Copy link
Contributor

@ppkarwasz ppkarwasz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks! 🚀

No need to port this to main, since the 3.x artifact will be removed soon (#2924).

@ppkarwasz ppkarwasz merged commit e749fa1 into apache:2.x Oct 23, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants