From d78854d6bc246df9215ca606e01e4e41b2a60f5c Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Wed, 4 Dec 2024 20:02:42 +0100 Subject: [PATCH 1/3] Add provided testcase --- .../java/logging/SystemErrToLoggingTest.java | 78 +++++++++++++++++-- 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java b/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java index 4be8e25..d6344d7 100644 --- a/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java +++ b/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java @@ -49,7 +49,7 @@ void useSlf4j() { class Test { int n; Logger logger; - + void test() { try { } catch(Throwable t) { @@ -64,7 +64,7 @@ void test() { class Test { int n; Logger logger; - + void test() { try { } catch(Throwable t) { @@ -97,10 +97,10 @@ void test() { """ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - + class Test { private static final Logger LOGGER = LoggerFactory.getLogger(Test.class); - + void test() { try { } catch(Throwable t) { @@ -143,7 +143,7 @@ void inRunnable() { import org.slf4j.Logger; class Test { Logger logger; - + void test() { Runnable r = () -> System.err.println("single"); } @@ -153,7 +153,7 @@ void test() { import org.slf4j.Logger; class Test { Logger logger; - + void test() { Runnable r = () -> logger.error("single"); } @@ -176,7 +176,7 @@ void supportLombokLogAnnotations() { @Slf4j class Test { int n; - + void test() { try { } catch(Throwable t) { @@ -191,7 +191,7 @@ void test() { @Slf4j class Test { int n; - + void test() { try { } catch(Throwable t) { @@ -203,4 +203,66 @@ void test() { ) ); } + + @Test + void replaceSystemErr() { + rewriteRun( + spec -> spec.recipe(new SystemErrToLogging(false, "logger", "SLF4J")), + //language=java + java( + """ + public class A { + org.slf4j.Logger logger = null; + + public void m() { + int cnt = 1; + String name = ""; + switch (cnt) { + case 1: + java.util.List numbers = new java.util.ArrayList<>(); + numbers.forEach(o -> System.err.println(String.valueOf(o))); + break; + case 2: + break; + case 3: + break; + default: + break; + } + } + + public String m2(String s1) { + return null; + } + } + """, + """ + public class A { + org.slf4j.Logger logger = null; + + public void m() { + int cnt = 1; + String name = ""; + switch (cnt) { + case 1: + java.util.List numbers = new java.util.ArrayList<>(); + numbers.forEach(o -> logger.error(String.valueOf(o))); + break; + case 2: + break; + case 3: + break; + default: + break; + } + } + + public String m2(String s1) { + return null; + } + } + """ + )); + } + } From f23d33ed1b802645d2033e792f9dfd967ba995df Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 4 Dec 2024 22:20:07 +0100 Subject: [PATCH 2/3] Minimize test case --- .../java/logging/SystemErrToLoggingTest.java | 29 ++++--------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java b/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java index d6344d7..93c18f1 100644 --- a/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java +++ b/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java @@ -211,58 +211,41 @@ void replaceSystemErr() { //language=java java( """ - public class A { + class A { org.slf4j.Logger logger = null; - public void m() { - int cnt = 1; - String name = ""; + void m(int cnt) { switch (cnt) { case 1: java.util.List numbers = new java.util.ArrayList<>(); numbers.forEach(o -> System.err.println(String.valueOf(o))); break; case 2: - break; - case 3: - break; default: break; } } - - public String m2(String s1) { - return null; - } } """, """ - public class A { + class A { org.slf4j.Logger logger = null; - public void m() { - int cnt = 1; - String name = ""; + void m(int cnt) { switch (cnt) { case 1: java.util.List numbers = new java.util.ArrayList<>(); numbers.forEach(o -> logger.error(String.valueOf(o))); break; case 2: - break; - case 3: - break; default: break; } } - - public String m2(String s1) { - return null; - } } """ - )); + ) + ); } } From 57f3aa62adca475f157d60a84eeb65ad6f8217c6 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 4 Dec 2024 22:22:37 +0100 Subject: [PATCH 3/3] Show that `yield` works as well, and link to issue --- .../java/logging/SystemErrToLoggingTest.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java b/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java index 93c18f1..a264ba4 100644 --- a/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java +++ b/src/test/java/org/openrewrite/java/logging/SystemErrToLoggingTest.java @@ -204,8 +204,9 @@ void test() { ); } + @Issue("https://github.com/openrewrite/rewrite-logging-frameworks/issues/192") @Test - void replaceSystemErr() { + void switchCaseStatements() { rewriteRun( spec -> spec.recipe(new SystemErrToLogging(false, "logger", "SLF4J")), //language=java @@ -244,6 +245,43 @@ void m(int cnt) { } } """ + ), + //language=java + java( + """ + class B { + org.slf4j.Logger logger = null; + + void m(int cnt) { + int val = switch (cnt) { + case 1: + java.util.List numbers = new java.util.ArrayList<>(); + numbers.forEach(o -> System.err.println(String.valueOf(o))); + yield 1; + case 2: + default: + yield 2; + }; + } + } + """, + """ + class B { + org.slf4j.Logger logger = null; + + void m(int cnt) { + int val = switch (cnt) { + case 1: + java.util.List numbers = new java.util.ArrayList<>(); + numbers.forEach(o -> logger.error(String.valueOf(o))); + yield 1; + case 2: + default: + yield 2; + }; + } + } + """ ) ); }