Skip to content

Commit 1435e01

Browse files
committed
Instance main should not be used for certain test cases
1 parent 0663445 commit 1435e01

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

src/test/java/org/openrewrite/java/migrate/lang/MigrateMainMethodToInstanceMainTest.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.java.JavaParser;
2021
import org.openrewrite.test.RecipeSpec;
2122
import org.openrewrite.test.RewriteTest;
2223

@@ -288,4 +289,88 @@ void main() {
288289
)
289290
);
290291
}
292+
293+
@Test
294+
void doNotMigrateMainUsedAsMethodReference() {
295+
//language=java
296+
rewriteRun(
297+
java(
298+
"""
299+
interface MainMethod {
300+
void run(String[] args);
301+
}
302+
"""
303+
),
304+
java(
305+
"""
306+
class Application {
307+
public static void main(String[] args) {
308+
System.out.println("Hello from main");
309+
}
310+
}
311+
312+
class Runner {
313+
void executeMain() {
314+
MainMethod foo = Application::main;
315+
foo.run(null);
316+
}
317+
}
318+
"""
319+
)
320+
);
321+
}
322+
323+
@Test
324+
void doNotMigrateMainWithNonDefaultConstructor() {
325+
//language=java
326+
rewriteRun(
327+
java(
328+
"""
329+
class Application {
330+
public static void main(String[] args) {
331+
System.out.println("Hello!");
332+
}
333+
334+
public Application(String config) {
335+
// Non-default constructor
336+
}
337+
}
338+
"""
339+
)
340+
);
341+
}
342+
343+
@Test
344+
void doNotMigrateMainInSpringBootApplication() {
345+
//language=java
346+
rewriteRun(
347+
spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(
348+
"""
349+
package org.springframework.boot.autoconfigure;
350+
public @interface SpringBootApplication {}
351+
""",
352+
"""
353+
package org.springframework.boot;
354+
public class SpringApplication {
355+
public static void run(Class<?> primarySource, String... args) {}
356+
}
357+
"""
358+
)),
359+
java(
360+
"""
361+
package com.example.demo;
362+
363+
import org.springframework.boot.SpringApplication;
364+
import org.springframework.boot.autoconfigure.SpringBootApplication;
365+
366+
@SpringBootApplication
367+
class DemoApplication {
368+
public static void main(String[] args) {
369+
SpringApplication.run(DemoApplication.class, args);
370+
}
371+
}
372+
"""
373+
)
374+
);
375+
}
291376
}

0 commit comments

Comments
 (0)