Skip to content

Commit

Permalink
Reorder Log4j1ToLog4j2 recipes after report of incomplete migration (
Browse files Browse the repository at this point in the history
…#148)

* Reorder Log4j1ToLog4j2 recipes after report of incomplete migration

Fixes #147

* Apply formatter
  • Loading branch information
timtebeek authored May 19, 2024
1 parent 2c4c3c7 commit 44f9092
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 37 deletions.
10 changes: 5 additions & 5 deletions src/main/resources/META-INF/rewrite/log4j.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ tags:
recipeList:
- org.openrewrite.java.logging.ChangeLombokLogAnnotation:
loggingFramework: Log4J2
- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.log4j
newPackageName: org.apache.logging.log4j
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: org.apache.logging.log4j.Logger getLogger(..)
methodPattern: org.apache.log4j.Logger getLogger(..)
fullyQualifiedTargetTypeName: org.apache.logging.log4j.LogManager
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: org.apache.logging.log4j.Logger getRootLogger()
methodPattern: org.apache.log4j.Logger getRootLogger()
fullyQualifiedTargetTypeName: org.apache.logging.log4j.LogManager
- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.log4j
newPackageName: org.apache.logging.log4j
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.apache.logging.log4j.Category getEffectiveLevel()
newMethodName: getLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand All @@ -26,16 +27,14 @@
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.mavenProject;
import static org.openrewrite.java.Assertions.srcMainJava;
import static org.openrewrite.java.Assertions.*;
import static org.openrewrite.maven.Assertions.pomXml;

class Log4j1ToLog4j2Test implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipeFromResource("/META-INF/rewrite/log4j.yml","org.openrewrite.java.logging.log4j.Log4j1ToLog4j2")
spec.recipeFromResource("/META-INF/rewrite/log4j.yml", "org.openrewrite.java.logging.log4j.Log4j1ToLog4j2")
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "log4j-1.2"));
}
Expand All @@ -54,8 +53,8 @@ class Test {
}
""",
"""
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
class Test {
Logger logger = LogManager.getLogger(Test.class);
Expand All @@ -72,18 +71,16 @@ void getRootLoggerToLogManager() {
java(
"""
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
class Test {
Logger logger0 = Logger.getRootLogger();
Logger logger1 = LogManager.getRootLogger();
}
""",
"""
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
class Test {
Logger logger0 = LogManager.getRootLogger();
Logger logger1 = LogManager.getRootLogger();
}
"""
)
Expand Down Expand Up @@ -115,6 +112,39 @@ static void method(Logger logger) {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-logging-frameworks/issues/147")
void matchUnknownTypes() {
//language=java
rewriteRun(
java(
"""
import org.apache.log4j.Logger;
class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class);
public static void main(String[] args) {
LOGGER.info("Hello world");
}
}
""",
"""
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
class Main {
private static final Logger LOGGER = LogManager.getLogger(Main.class);
public static void main(String[] args) {
LOGGER.info("Hello world");
}
}
"""
)
);
}

@Test
void mavenProjectDependenciesUpdated() {
rewriteRun(
Expand All @@ -130,8 +160,8 @@ class Test {
}
""",
"""
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
class Test {
Logger logger = LogManager.getLogger(Test.class);
Expand All @@ -141,28 +171,28 @@ class Test {
//language=xml
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
</project>
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
</project>
""",
sourceSpecs -> sourceSpecs.after(after -> {
Matcher matcher = Pattern.compile("<version>(2\\.2\\d\\.\\d)</version>").matcher(after);
Expand Down Expand Up @@ -196,7 +226,7 @@ class Test {
</dependency>
</dependencies>
</project>
""".formatted(version);
""".formatted(version);
})
)
)
Expand Down

0 comments on commit 44f9092

Please sign in to comment.