From 023a0f45792fc9fda0977a4a5faae726af2121d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Merlin=20B=C3=B6gershausen?= Date: Wed, 6 Sep 2023 09:49:27 +0200 Subject: [PATCH] add reproducing tests for moderne.io failures --- .../java/migrate/lang/UseVarKeywordTest.java | 233 +++++++++++++++++- 1 file changed, 227 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/openrewrite/java/migrate/lang/UseVarKeywordTest.java b/src/test/java/org/openrewrite/java/migrate/lang/UseVarKeywordTest.java index 52244cfa85..343d21f9d2 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/UseVarKeywordTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/UseVarKeywordTest.java @@ -37,6 +37,227 @@ public void defaults(RecipeSpec spec) { .activateRecipes("org.openrewrite.java.migrate.lang.UseVar")); } + @Nested + class BugFixing { + + @Test + void anonymousClass() { + // spring-projects/spring-data-commons @ main: src/test/java/org/springframework/data/domain/ManagedTypesUnitTests.java + // solving: Expected a template that would generate exactly one statement to replace one statement, but generated 2. Template: + //var typesSupplier = __P__./*__p1__*/p() + //language=java + rewriteRun( + version( + java(""" + package com.example.app; + + import java.util.Collections; + import java.util.function.Supplier; + + class ManagedTypesUnitTests { + void supplierBasedManagedTypesAreEvaluatedLazily() { + Supplier>> typesSupplier = spy(new Supplier>>() { + @Override + public Iterable> get() { + return Collections.singleton(Object.class); + } + }); + } + + // mock for mockito method + private Supplier>> spy(Supplier>> supplier) { + return null; + } + } + """, """ + package com.example.app; + + import java.util.Collections; + import java.util.function.Supplier; + + class ManagedTypesUnitTests { + void supplierBasedManagedTypesAreEvaluatedLazily() { + var typesSupplier = spy(new Supplier>>() { + @Override + public Iterable> get() { + return Collections.singleton(Object.class); + } + }); + } + + // mock for mockito method + private Supplier>> spy(Supplier>> supplier) { + return null; + } + } + """), + 10 + ) + ); + } + + @Test + void multiGenerics() { + // spring-cloud/spring-cloud-contract @ main: spring-cloud-contract-verifier/src/test/resources/contractsToCompile/contract_multipart.java + // solving java.lang.IllegalArgumentException: Unable to parse expression from JavaType Unknown + //language=java + rewriteRun( + version( + java(""" + import java.util.Collection; + import java.util.HashMap; + import java.util.Map; + import java.util.function.Supplier; + + class contract_multipart implements Supplier> { \s + private static Map namedProps(HttpSender.Request r) { + Map map = new HashMap<>(); + return map; + } + + @Override + public Collection get() { return null; } + } + // replacements + class Contract{} + class DslProperty{} + class HttpSender { + static class Request {} + } + """, """ + import java.util.Collection; + import java.util.HashMap; + import java.util.Map; + import java.util.function.Supplier; + + class contract_multipart implements Supplier> { \s + private static Map namedProps(HttpSender.Request r) { + var map = new HashMap(); + return map; + } + + @Override + public Collection get() { return null; } + } + // replacements + class Contract{} + class DslProperty{} + class HttpSender { + static class Request {} + } + """), + 10 + ) + ); + } + + @Test + void duplicateTemplate() { + // spring-projects/spring-hateoas @ main src/test/java/org/springframework/hateoas/mediatype/html/HtmlInputTypeUnitTests.java + // solving Expected a template that would generate exactly one statement to replace one statement, but generated 2. Template: + //var numbers = __P__.>/*__p1__*/p() + //language=java + rewriteRun( + version( + java(""" + import java.math.BigDecimal; + import java.util.Arrays; + import java.util.Collection; + import java.util.stream.Stream; + + class HtmlInputTypeUnitTests { + Stream derivesInputTypesFromType() { + Stream<$> numbers = HtmlInputType.NUMERIC_TYPES.stream() // + .map(it -> $.of(it, HtmlInputType.NUMBER)); + return null; + } + + static class HtmlInputType { + static final Collection> NUMERIC_TYPES = Arrays.asList(int.class, long.class, float.class, + double.class, short.class, Integer.class, Long.class, Float.class, Double.class, Short.class, BigDecimal.class); + + public static final HtmlInputType NUMBER = new HtmlInputType(); + + public static HtmlInputType from(Class type) { return null; } + } + + static class $ { + + Class type; + HtmlInputType expected; + + static $ of(Class it, HtmlInputType number){ return null; }\s + + public void verify() { + assertThat(HtmlInputType.from(type)).isEqualTo(expected); + } + + + @Override + public String toString() { + return String.format("Derives %s from %s.", expected, type); + } + //mocking + private > AbstractBigDecimalAssert assertThat(HtmlInputType from) { + return null; + } + } + } + // replacement + class DynamicTest {} + class AbstractBigDecimalAssert { + public void isEqualTo(Object expected) {} + } + """, """ + import java.math.BigDecimal; + import java.util.Arrays; + import java.util.Collection; + import java.util.stream.Stream; + + import static org.assertj.core.api.Assertions.assertThat; + + class HtmlInputTypeUnitTests { + Stream derivesInputTypesFromType() { + var numbers = HtmlInputType.NUMERIC_TYPES.stream() // + .map(it -> $.of(it, HtmlInputType.NUMBER)); + return null; + } + + static class HtmlInputType { + static final Collection> NUMERIC_TYPES = Arrays.asList(int.class, long.class, float.class, + double.class, short.class, Integer.class, Long.class, Float.class, Double.class, Short.class, BigDecimal.class); + + public static final HtmlInputType NUMBER = new HtmlInputType(); + + public static HtmlInputType from(Class type) { return null; } + } + + static class $ { + + Class type; + HtmlInputType expected; + + static $ of(Class it, HtmlInputType number){ return null; }\s + + public void verify() { + assertThat(HtmlInputType.from(type)).isEqualTo(expected); + } + + @Override + public String toString() { + return String.format("Derives %s from %s.", expected, type); + } + } + } + // replacement + class DynamicTest {} + """), + 10 + ) + ); + } + } + @Nested class GeneralNotApplicable { @@ -143,10 +364,10 @@ void withTernary() { version( java(""" package com.example.app; - + \s class A { void m() { - String o = true ? "isTrue" : "Test"; + String o = "isTrue"; } } """), @@ -225,18 +446,18 @@ void withTernary() { version( java(""" package com.example.app; - + \s class A { void m() { - String o = true ? "isTrue" : "Test"; + String o = "isTrue"; } } """, """ package com.example.app; - + \s class A { void m() { - var o = true ? "isTrue" : "Test"; + var o = "isTrue"; } } """),