-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JCL to Log4j API migration (#129)
* Add JCL to Log4j API migration This PR adds a recipe to migrate from Apache Commons Logging to Log4j 2.x API. * Minor formatting changes to CommonsLoggingToLog4jTest --------- Co-authored-by: Tim te Beek <[email protected]>
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/test/java/org/openrewrite/java/logging/log4j/CommonsLoggingToLog4jTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* <p> | ||
* Licensed 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.openrewrite.java.logging.log4j; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
import org.openrewrite.test.TypeValidation; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class CommonsLoggingToLog4jTest implements RewriteTest { | ||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipeFromResource("/META-INF/rewrite/log4j.yml", | ||
"org.openrewrite.java.logging.log4j.CommonsLoggingToLog4j") | ||
.parser(JavaParser.fromJavaVersion().classpath("log4j-api", "commons-logging", "lombok")); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void loggerFactoryToLogManager() { | ||
// language=java | ||
rewriteRun( | ||
java( | ||
""" | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.commons.logging.Log; | ||
class Test { | ||
Log log1 = LogFactory.getLog(Test.class); | ||
Log log2 = LogFactory.getLog("Test"); | ||
Log log3 = LogFactory.getFactory().getInstance(Test.class); | ||
Log log4 = LogFactory.getFactory().getInstance("Test"); | ||
} | ||
""", | ||
""" | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
class Test { | ||
Logger log1 = LogManager.getLogger(Test.class); | ||
Logger log2 = LogManager.getLogger("Test"); | ||
Logger log3 = LogManager.getLogger(Test.class); | ||
Logger log4 = LogManager.getLogger("Test"); | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void changeLombokLogAnnotation() { | ||
// language=java | ||
rewriteRun( | ||
spec -> spec.typeValidationOptions(TypeValidation.builder() | ||
.identifiers(false) | ||
.methodInvocations(false) | ||
.build()), | ||
java( | ||
""" | ||
import lombok.extern.apachecommons.CommonsLog; | ||
@CommonsLog | ||
class Test { | ||
void method() { | ||
log.info("uh oh"); | ||
} | ||
} | ||
""", | ||
""" | ||
import lombok.extern.log4j.Log4j2; | ||
@Log4j2 | ||
class Test { | ||
void method() { | ||
log.info("uh oh"); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |