Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp/1002 mavenartifactdownloader should interpret relocations #1003

Draft
wants to merge 2 commits into
base: version/revamp
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.springframework.sbm.parsers.JavaParserBuilder;
import org.springframework.sbm.project.resource.TestProjectContext;

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

/**
* @author Fabian Krüger
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
import org.springframework.sbm.java.api.Member;
import org.springframework.sbm.java.impl.DependenciesChangedEventHandler;
import org.springframework.sbm.java.impl.DependencyChangeHandler;
import org.springframework.sbm.java.impl.RewriteJavaParser;
import org.springframework.sbm.parsers.RewriteExecutionContext;
import org.springframework.sbm.parsers.JavaParserBuilder;
import org.springframework.sbm.project.resource.SbmApplicationProperties;
import org.springframework.sbm.project.resource.TestProjectContext;

import java.nio.file.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
*/
package org.springframework.sbm.boot.upgrade_27_30.report.helper;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.sbm.build.util.PomBuilder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Fabian Krüger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@
*/
package org.openrewrite.maven;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.SourceFile;
import org.openrewrite.maven.cache.LocalMavenArtifactCache;
import org.openrewrite.maven.cache.ReadOnlyLocalMavenArtifactCache;
import org.openrewrite.maven.internal.MavenPomDownloader;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.maven.utilities.MavenArtifactDownloader;
import org.springframework.sbm.parsers.maven.RewriteMavenArtifactDownloader;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Stream;

Expand All @@ -33,6 +45,63 @@
* @author Fabian Krüger
*/
public class MavenParserTest {

@Test
@DisplayName("parse pom with relocated dependency")
void parsePomWithRelocatedDependency() {
@Language("xml")
String pom = """
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.acme</groupId>
<artifactId>app</artifactId>
<version>0.1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
</project>
""";

List<SourceFile> parse = MavenParser.builder().build().parse(pom).toList();
assertThat(parse).isNotNull();

// MavenArtifactDownloader mavenArtifactDownloader = new MavenArtifactDownloader(new ReadOnlyLocalMavenArtifactCache(Path.of(System.getProperty("user.home")).resolve(".m2/repository")), null, e -> {
// throw new RuntimeException(e);
// });

MavenResolutionResult mavenResolutionResult = parse.get(0).getMarkers().findFirst(MavenResolutionResult.class).get();
List<ResolvedDependency> resolvedDependencies = mavenResolutionResult.getDependencies().get(Scope.Compile);

MavenArtifactDownloader mavenArtifactDownloader = new RewriteMavenArtifactDownloader(
new LocalMavenArtifactCache(Paths.get(System.getProperty("user.home"), ".m2", "repository"))
.orElse(
new LocalMavenArtifactCache(Paths.get(System.getProperty("user.home"), ".rewrite", "cache", "artifacts")
)
),
null,
t -> {throw new RuntimeException(t);}
);
List<Path> list = resolvedDependencies
// FIXME: 945 - deal with dependencies to projects in reactor
//
.stream()
.filter(rd -> rd.getRepository() != null)
.map(rd -> mavenArtifactDownloader.downloadArtifact(rd))
.filter(Objects::nonNull)
.distinct()
.toList();

assertThat(list).isNotNull();
}

@Test
@DisplayName("Should Read .mvn/maven.config")
@ExpectedToFail("See https://github.com/openrewrite/rewrite/issues/3409")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@
*/
class RewriteProjectParserParityTest {

@Test
@DisplayName("pom")
void pom(@TempDir Path tempDir) {

@Language("xml")
String pom = """
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.acme</groupId>
<artifactId>app</artifactId>
<version>0.1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
</project>
""";
TestProjectHelper.createTestProject(tempDir).withResources(new DummyResource(tempDir.resolve("pom.xml"), pom)).writeToFilesystem();
ParserParityTestHelper
.scanProjectDir(tempDir)
.parseSequentially()
.verifyParity();

}

@Test
@DisplayName("Parsing Simplistic Maven Project ")
void parsingSimplisticMavenProject(@TempDir Path tempDir) throws GitAPIException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.openrewrite.style.Style;
import org.springframework.sbm.parsers.ParserProperties;
import org.springframework.sbm.parsers.RewriteProjectParsingResult;
import org.springframework.util.StopWatch;

import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -100,7 +101,15 @@ public void verifyParity(CustomParserResultParityChecker customParserResultParit
expectedParserResult = result.expectedParsingResult();
actualParserResult = result.actualParsingResult();
} else {
StopWatch sw1 = new StopWatch("Parsers");
sw1.start("actual");
actualParserResult = parserExecutionHelper.parseWithRewriteProjectParser(baseDir, parserProperties);
sw1.stop();
System.out.println(sw1.shortSummary());

sw1.start("expectd");
sw1.stop();
System.out.println(sw1.shortSummary());
expectedParserResult = parserExecutionHelper.parseWithComparingParser(baseDir, parserProperties, executionContext);
}

Expand Down
Loading