Skip to content

Commit

Permalink
Fixing broken tests due to new Maven Resolution Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tkvangorder committed Feb 8, 2022
1 parent 4ded662 commit 2a64e71
Show file tree
Hide file tree
Showing 5 changed files with 522 additions and 537 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.maven.search.FindPlugin;
import org.openrewrite.maven.tree.Maven;
import org.openrewrite.xml.XmlVisitor;
import org.openrewrite.xml.search.FindTags;
import org.openrewrite.xml.tree.Xml;
Expand All @@ -38,32 +37,31 @@ public String getDescription() {

@Override
protected TreeVisitor<?, ExecutionContext> getApplicableTest() {
return new MavenVisitor() {
return new MavenVisitor<ExecutionContext>() {

@Override
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
if (FindPlugin.find(maven, "org.springframework.boot", "spring-boot-maven-plugin").stream().noneMatch(plugin -> FindTags.find(plugin, "//configuration/agent").isEmpty())) {
maven = maven.withMarkers(maven.getMarkers().searchResult());
public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
if (FindPlugin.find(document, "org.springframework.boot", "spring-boot-maven-plugin").stream().noneMatch(plugin -> FindTags.find(plugin, "//configuration/agent").isEmpty())) {
document = document.withMarkers(document.getMarkers().searchResult());
}
return super.visitMaven(maven, ctx);
return document;
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new SpringBootMavenPluginMigrateAgentToAgentsVisitor();
}

private static class SpringBootMavenPluginMigrateAgentToAgentsVisitor extends MavenVisitor {
@Override
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
FindPlugin.find(maven, "org.springframework.boot", "spring-boot-maven-plugin").forEach(plugin ->
FindTags.find(plugin, "//configuration/agent").forEach(agentTag ->
doAfterVisit(new ChangeTagKeyVisitor<>(agentTag, "agents"))
)
);
return super.visitMaven(maven, ctx);
}
return new MavenVisitor<ExecutionContext>() {
@Override
public Xml visitDocument(Xml.Document document, ExecutionContext executionContext) {
FindPlugin.find(document, "org.springframework.boot", "spring-boot-maven-plugin").forEach(plugin ->
FindTags.find(plugin, "//configuration/agent").forEach(agentTag ->
doAfterVisit(new ChangeTagKeyVisitor<>(agentTag, "agents"))
)
);
return super.visitDocument(document, executionContext);
}
};
}

private static class ChangeTagKeyVisitor<P> extends XmlVisitor<P> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.marker.JavaProject;
import org.openrewrite.java.tree.Comment;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TextComment;
import org.openrewrite.marker.Markers;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.maven.tree.Maven;
import org.openrewrite.maven.internal.MavenParsingException;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.Pom;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.properties.PropertiesVisitor;
import org.openrewrite.properties.search.FindProperties;
import org.openrewrite.properties.tree.Properties;
import org.openrewrite.semver.DependencyMatcher;
import org.openrewrite.xml.tree.Xml;
import org.openrewrite.yaml.YamlIsoVisitor;
import org.openrewrite.yaml.search.FindProperty;
import org.openrewrite.yaml.tree.Yaml;
Expand All @@ -48,8 +50,8 @@
*/
public class IntegrationSchedulerPoolRecipe extends Recipe {

private static Pattern APP_PROPS_FILE_REGEX = Pattern.compile("^application.*\\.properties$");
private static Pattern APP_YAML_FILE_REGEX = Pattern.compile("^application.*\\.ya?ml$");
private static final Pattern APP_PROPS_FILE_REGEX = Pattern.compile("^application.*\\.properties$");
private static final Pattern APP_YAML_FILE_REGEX = Pattern.compile("^application.*\\.ya?ml$");

private static final String PROPERTY_KEY = "spring.task.scheduling.pool.size";

Expand All @@ -69,17 +71,24 @@ public String getDescription() {
" default of 10 threads, use the spring.task.scheduling.pool.size property.";
}

private boolean isApplicableMavenProject(Maven maven) {
private boolean isApplicableMavenProject(Xml.Document maven) {
DependencyMatcher boot25Matcher = DependencyMatcher.build("org.springframework.boot:spring-boot:2.4.X").getValue();
DependencyMatcher integrationMatcher = DependencyMatcher.build("org.springframework.integration:spring-integration-core").getValue();
Collection<Pom.Dependency> deps = maven.getModel().getDependencies(Scope.Compile);

List<ResolvedDependency> deps = maven.getMarkers().findFirst(MavenResolutionResult.class)
.orElseThrow(() -> new IllegalStateException("Maven visitors should not be visiting XML documents without a Maven marker"))
.getDependencies().getOrDefault(Scope.Compile, Collections.emptyList());

boolean boot25 = false;
boolean si = false;
for (Pom.Dependency d : deps) {

for (ResolvedDependency d : deps) {
if (!boot25) {
assert boot25Matcher != null;
boot25 = boot25Matcher.matches(d.getGroupId(), d.getArtifactId(), d.getVersion());
}
if (!si) {
assert integrationMatcher != null;
si = integrationMatcher.matches(d.getGroupId(), d.getArtifactId());
}
if (boot25 && si) {
Expand All @@ -90,23 +99,25 @@ private boolean isApplicableMavenProject(Maven maven) {
}

@Override
protected MavenVisitor getApplicableTest() {
return new MavenVisitor() {
protected MavenVisitor<ExecutionContext> getApplicableTest() {
return new MavenVisitor<ExecutionContext>() {

@Override
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
if (isApplicableMavenProject(maven)) {
return maven.withMarkers(maven.getMarkers().searchResult());
public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {

if (isApplicableMavenProject(document)) {
return document.withMarkers(document.getMarkers().searchResult());
}
return maven;
return document;
}
};
}

@Override
protected List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx) {
Set<JavaProject> javaProjects = before.stream()
.filter(Maven.class::isInstance)
.map(Maven.class::cast)
.filter(s -> s.getMarkers().findFirst(MavenResolutionResult.class).isPresent())
.map(Xml.Document.class::cast)
.filter(this::isApplicableMavenProject)
.map(m -> m.getMarkers().findFirst(JavaProject.class))
.filter(Optional::isPresent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.SourceFile;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.search.FindAnnotations;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.maven.tree.Maven;
import org.openrewrite.maven.tree.Pom;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.semver.DependencyMatcher;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Collections;

/**
* Mark POM's of projects where logging shutdown hook may need to be disabled
Expand Down Expand Up @@ -62,21 +58,23 @@ public String getDescription() {

@Override
protected TreeVisitor<?, ExecutionContext> getApplicableTest() {
return new MavenVisitor() {
return new MavenVisitor<ExecutionContext>() {

@Override
public Maven visitMaven(Maven maven, ExecutionContext ctx) {
if(!maven.getModel().getPackaging().equals("jar")) {
return maven;
public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
MavenResolutionResult model = getResolutionResult();
//Default packaging, if not specified is "jar"
if (model.getPom().getPackaging() != null && !"jar".equals(model.getPom().getPackaging())) {
return document;
}

DependencyMatcher matcher = DependencyMatcher.build("org.springframework.boot:spring-boot:2.4.X").getValue();
assert matcher != null;
for (Pom.Dependency d : maven.getModel().getDependencies(Scope.Compile)) {
for (ResolvedDependency d : getResolutionResult().getDependencies().getOrDefault(Scope.Compile, Collections.emptyList())) {
if (matcher.matches(d.getGroupId(), d.getArtifactId(), d.getVersion())) {
return maven.withMarkers(maven.getMarkers().searchResult());
return document.withMarkers(document.getMarkers().searchResult());
}
}
return maven;
return document;
}
};
}
Expand Down
Loading

0 comments on commit 2a64e71

Please sign in to comment.