Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fabapp2 committed Nov 20, 2023
1 parent a3850af commit 033f1cc
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.sbm.build.api;

import org.junit.jupiter.api.Test;
import org.springframework.sbm.build.util.PomBuilder;
import org.springframework.sbm.test.util.PomBuilder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.api.Dependency;
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
import org.springframework.sbm.build.util.PomBuilder;
import org.springframework.sbm.test.util.PomBuilder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.context.ProjectContextHolder;
import org.springframework.sbm.parsers.RewriteExecutionContext;
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 @@ -39,7 +39,7 @@
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.api.Dependency;
import org.springframework.sbm.build.impl.RewriteMavenParser;
import org.springframework.sbm.build.util.PomBuilder;
import org.springframework.sbm.test.util.PomBuilder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.context.ProjectContextFactory;
import org.springframework.sbm.engine.context.ProjectContextHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@
import org.springframework.sbm.build.api.DependenciesChangedEvent;
import org.springframework.sbm.build.api.Dependency;
import org.springframework.sbm.build.api.Plugin;
import org.springframework.sbm.build.util.PomBuilder;
import org.springframework.sbm.test.util.PomBuilder;
import org.springframework.sbm.build.impl.OpenRewriteMavenPlugin;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.context.ProjectContextHolder;
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 @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.build.util;
package org.springframework.sbm.test.util;

import org.springframework.sbm.project.parser.DependencyHelper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.junit.jupiter.api.Test;
import org.springframework.sbm.boot.upgrade_27_30.report.yaml.SpringBootUpgradeReportActionDeserializer;
import org.springframework.sbm.build.util.PomBuilder;
import org.springframework.sbm.test.util.PomBuilder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.recipe.Recipe;
import org.springframework.sbm.project.resource.TestProjectContext;
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.test.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 @@ -18,7 +18,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportTestSupport;
import org.springframework.sbm.build.util.PomBuilder;
import org.springframework.sbm.test.util.PomBuilder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.springframework.sbm.build.api.Module;
import org.springframework.sbm.build.impl.OpenRewriteMavenPlugin;
import org.springframework.sbm.build.migration.actions.OpenRewriteMavenBuildFileTestSupport;
import org.springframework.sbm.build.util.PomBuilder;
import org.springframework.sbm.test.util.PomBuilder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

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

0 comments on commit 033f1cc

Please sign in to comment.