diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java b/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java index cca14c30994..a6a38e4e68f 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java @@ -27,8 +27,6 @@ import java.util.function.Consumer; -import static org.junit.jupiter.api.Assertions.assertEquals; - public class Assertions { private Assertions() { } @@ -67,9 +65,11 @@ public static SourceSpecs pomXml(@Language("xml") @Nullable String before, @Lang } private static SourceFile pomResolvedSuccessfully(SourceFile sourceFile, TypeValidation typeValidation) { - sourceFile.getMarkers() - .findFirst(MavenResolutionResult.class) - .orElseThrow(() -> new IllegalStateException("No MavenResolutionResult found")); + if (typeValidation.dependencyModel()) { + sourceFile.getMarkers() + .findFirst(MavenResolutionResult.class) + .orElseThrow(() -> new IllegalStateException("No MavenResolutionResult found")); + } return sourceFile; } } diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenDependencyFailuresTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenDependencyFailuresTest.java index 5e284f274ec..1a179b991a4 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenDependencyFailuresTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenDependencyFailuresTest.java @@ -25,6 +25,7 @@ import org.openrewrite.maven.tree.MavenResolutionResult; import org.openrewrite.maven.tree.Scope; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; import org.openrewrite.xml.tree.Xml; import java.io.IOException; @@ -222,7 +223,11 @@ void unresolvableTransitiveDependencyDueToInvalidPom(@TempDir Path localReposito @Test void unresolvableDependency() { rewriteRun( - spec -> spec.executionContext(new InMemoryExecutionContext()), + spec -> spec.executionContext(new InMemoryExecutionContext()) + .typeValidationOptions(TypeValidation.builder() + // Skip test framework validation that a MavenResolutionResult is present since this tests exercises error handling + .dependencyModel(false) + .build()), pomXml( """ @@ -247,7 +252,8 @@ void unresolvableDependency() { """, - spec -> spec.afterRecipe(after -> { + spec -> spec + .afterRecipe(after -> { Optional maybeParseException = after.getMarkers().findFirst(ParseExceptionResult.class); assertThat(maybeParseException).hasValueSatisfying(per -> assertThat(per.getMessage()).contains("Unable to download POM: com.google.guava:guava:doesnotexist. Tried repositories")); }) @@ -326,17 +332,15 @@ void unresolvableTransitiveDependencyDueToInvalidPom() { """, spec -> spec.afterRecipe(pom -> - { - assertThat(pom.getMarkers().findFirst(MavenResolutionResult.class)) - .isPresent() - .get() - .extracting(mrr -> mrr.getDependencies().get(Scope.Compile)) - .matches(deps -> deps.size() == 1) - .extracting(deps -> deps.get(0)) - .matches(dep -> dep.getGroupId().equals("org.jvnet.staxex") && - dep.getArtifactId().equals("stax-ex") && - dep.getVersion().equals("1.0")); - })) + assertThat(pom.getMarkers().findFirst(MavenResolutionResult.class)) + .isPresent() + .get() + .extracting(mrr -> mrr.getDependencies().get(Scope.Compile)) + .matches(deps -> deps.size() == 1) + .extracting(deps -> deps.get(0)) + .matches(dep -> dep.getGroupId().equals("org.jvnet.staxex") && + dep.getArtifactId().equals("stax-ex") && + dep.getVersion().equals("1.0")))) ); } diff --git a/rewrite-test/src/main/java/org/openrewrite/test/TypeValidation.java b/rewrite-test/src/main/java/org/openrewrite/test/TypeValidation.java index 4c51773c841..6afde8de32e 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/TypeValidation.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/TypeValidation.java @@ -21,6 +21,12 @@ import lombok.NoArgsConstructor; import lombok.experimental.Accessors; +/** + * Controls the test framework's validation of invariants which are expected to hold true in an LST both before and + * after the recipe run. Originally this applied only to validating the well-formedness of type metadata in Java LSTs + * (hence the name TypeValidation), but has since expanded to include concepts relevant to other types of sources. + * "InvariantValidation" would be a more accurate name, but "TypeValidation" is kept for backwards compatibility. + */ @Data @NoArgsConstructor @AllArgsConstructor @@ -28,30 +34,63 @@ @Builder public class TypeValidation { + /** + * Controls whether class declarations are validated to have valid type metadata. + */ @Builder.Default private boolean classDeclarations = true; + /** + * Controls whether identifiers declarations are validated to have valid type metadata. + * Within even a well-formed Java LST not all identifiers typically have type metadata, so even when enabled some + * identifiers are allowed to have null type. + */ @Builder.Default private boolean identifiers = true; + /** + * Controls whether method declarations are validated to have valid type metadata. + */ @Builder.Default private boolean methodDeclarations = true; + /** + * Controls whether field declarations are validated to have valid type metadata. + */ @Builder.Default private boolean variableDeclarations = true; + /** + * Controls whether method invocations are validated to have valid type metadata. + */ @Builder.Default private boolean methodInvocations = true; + /** + * Controls whether constructor invocations are validated to have valid type metadata. + */ @Builder.Default private boolean constructorInvocations = true; + /** + * Controls whether sources expected to have dependency resolution metadata in a model marker are validated to have + * that model attached. For example, a Maven pom is expected to have a MavenResolutionResult model attached. + */ + @Builder.Default + private boolean dependencyModel = true; + + /** + * Enable all invariant validation checks. + */ public static TypeValidation all() { return new TypeValidation(); } + /** + * Skip all invariant validation checks. + */ public static TypeValidation none() { - return new TypeValidation(false,false,false,false,false,false); + return new TypeValidation(false, false, false, false, false, false, false); } static TypeValidation before(RecipeSpec testMethodSpec, RecipeSpec testClassSpec) {