Skip to content

Commit

Permalink
Unit test framework now asserts that poms have a MavenResolutionResult
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Sep 11, 2024
1 parent 13cd2e9 commit 1e2e9f2
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
import org.intellij.lang.annotations.Language;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.SourceFile;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.test.SourceSpec;
import org.openrewrite.test.SourceSpecs;
import org.openrewrite.test.TypeValidation;
import org.openrewrite.xml.tree.Xml;

import java.util.function.Consumer;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Assertions {
private Assertions() {
}
Expand All @@ -41,7 +46,7 @@ public static SourceSpecs pomXml(@Language("xml") @Nullable String before) {

public static SourceSpecs pomXml(@Language("xml") @Nullable String before, Consumer<SourceSpec<Xml.Document>> spec) {
SourceSpec<Xml.Document> maven = new SourceSpec<>(Xml.Document.class, "maven", MavenParser.builder(), before,
SourceSpec.ValidateSource.noop, Assertions::customizeExecutionContext);
Assertions::pomResolvedSuccessfully, Assertions::customizeExecutionContext);
maven.path("pom.xml");
spec.accept(maven);
return maven;
Expand All @@ -55,10 +60,16 @@ public static SourceSpecs pomXml(@Language("xml") @Nullable String before, @Lang
public static SourceSpecs pomXml(@Language("xml") @Nullable String before, @Language("xml") @Nullable String after,
Consumer<SourceSpec<Xml.Document>> spec) {
SourceSpec<Xml.Document> maven = new SourceSpec<>(Xml.Document.class, "maven", MavenParser.builder(), before,
SourceSpec.ValidateSource.noop, Assertions::customizeExecutionContext).after(s -> after);
Assertions::pomResolvedSuccessfully, Assertions::customizeExecutionContext).after(s -> after);
maven.path("pom.xml");
spec.accept(maven);
return maven;
}

private static SourceFile pomResolvedSuccessfully(SourceFile sourceFile, TypeValidation typeValidation) {
sourceFile.getMarkers()
.findFirst(MavenResolutionResult.class)
.orElseThrow(() -> new IllegalStateException("No MavenResolutionResult found"));
return sourceFile;
}
}

0 comments on commit 1e2e9f2

Please sign in to comment.