From 74df1fc5357ae75cd3239e65222485882b1d712a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Kr=C3=BCger?= Date: Mon, 6 Nov 2023 18:52:19 +0100 Subject: [PATCH] Apply sorcery to temp. move Maven for test --- sbm-support-rewrite/.gitignore | 11 +- sbm-support-rewrite/pom.xml | 6 + .../autoconfigure/ScopeConfiguration.java | 12 +- .../parsers/RewriteParserConfiguration.java | 6 +- .../maven/MavenSettingsInitializer.java | 12 +- .../RewriteParserMavenConfiguration.java | 2 +- .../sbm/PrivateArtifactRepositoryTest.java | 95 ++++++++++++++- .../sbm/parsers/BuildFileParserTest.java | 3 +- .../RewriteProjectParserParityTest.java | 35 +----- .../sbm/parsers/RewriteProjectParserTest.java | 3 +- .../maven/MavenSettingsInitializerTest.java | 3 +- .../sbm/test/util/ParserParityTestHelper.java | 111 ++++++++++++++++-- .../reposilite-data/shared.configuration.json | 2 +- .../reposilite-data/tokens.json | 94 --------------- .../user.home/.m2/settings.xml | 13 -- 15 files changed, 235 insertions(+), 173 deletions(-) delete mode 100644 sbm-support-rewrite/testcode/reposilite-test/reposilite-data/tokens.json delete mode 100644 sbm-support-rewrite/testcode/reposilite-test/user.home/.m2/settings.xml diff --git a/sbm-support-rewrite/.gitignore b/sbm-support-rewrite/.gitignore index 770fbad63..a50bd2883 100644 --- a/sbm-support-rewrite/.gitignore +++ b/sbm-support-rewrite/.gitignore @@ -1,7 +1,12 @@ +/testcode/reposilite-test/dependency-project/pom.xml +/testcode/reposilite-test/dependency-project/target +/testcode/reposilite-test/dependent-project/pom.xml +/testcode/reposilite-test/dependent-project/target /testcode/reposilite-test/user.home/** -!/testcode/reposilite-test/user.home/.m2/settings.xml.template -!/testcode/reposilite-test/user.home/.m2/settings-security.xml +testcode/reposilite-test/user.home/.m2/settings.xml /testcode/reposilite-test/reposilite-data/** -!testcode/reposilite-test/reposilite-data/shared.configuration.json /testcode/reposilite-test/user.home/apache-maven-3.9.5/ /testcode/**/target/** +!/testcode/reposilite-test/user.home/.m2/settings.xml.template +!/testcode/reposilite-test/user.home/.m2/settings-security.xml +!testcode/reposilite-test/reposilite-data/shared.configuration.json diff --git a/sbm-support-rewrite/pom.xml b/sbm-support-rewrite/pom.xml index a73632866..51ceb7c57 100644 --- a/sbm-support-rewrite/pom.xml +++ b/sbm-support-rewrite/pom.xml @@ -264,6 +264,12 @@ 1.10.0 test + + org.powermock + powermock-core + 2.0.9 + test + diff --git a/sbm-support-rewrite/src/main/java/org/springframework/sbm/boot/autoconfigure/ScopeConfiguration.java b/sbm-support-rewrite/src/main/java/org/springframework/sbm/boot/autoconfigure/ScopeConfiguration.java index bf83a1bd9..582f6530c 100644 --- a/sbm-support-rewrite/src/main/java/org/springframework/sbm/boot/autoconfigure/ScopeConfiguration.java +++ b/sbm-support-rewrite/src/main/java/org/springframework/sbm/boot/autoconfigure/ScopeConfiguration.java @@ -19,6 +19,7 @@ import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.java.AddOrUpdateAnnotationAttribute; import org.openrewrite.maven.MavenExecutionContextView; +import org.openrewrite.maven.MavenSettings; import org.openrewrite.maven.cache.MavenPomCache; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.boot.autoconfigure.AutoConfiguration; @@ -28,6 +29,8 @@ import org.springframework.sbm.scopes.ProjectMetadata; import org.springframework.sbm.scopes.ScanScope; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.function.Supplier; /** @@ -61,7 +64,8 @@ public static BeanFactoryPostProcessor beanFactoryPostProcessor(ExecutionScope e @Bean @org.springframework.sbm.scopes.annotations.ScanScope ProjectMetadata projectMetadata() { - return new ProjectMetadata(); + ProjectMetadata projectMetadata = new ProjectMetadata(); + return projectMetadata; } @Bean @@ -74,9 +78,9 @@ Supplier executionContextSupplier() { @org.springframework.sbm.scopes.annotations.ScanScope ExecutionContext executionContext(ProjectMetadata projectMetadata, Supplier executionContextSupplier, MavenPomCache mavenPomCache) { ExecutionContext executionContext = executionContextSupplier.get(); - MavenExecutionContextView contextView = MavenExecutionContextView.view(executionContext); - contextView.setMavenSettings(projectMetadata.getMavenSettings()); - contextView.setPomCache(mavenPomCache); +// MavenExecutionContextView contextView = MavenExecutionContextView.view(executionContext); +// contextView.setMavenSettings(projectMetadata.getMavenSettings()); +// contextView.setPomCache(mavenPomCache); return executionContext; } diff --git a/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/RewriteParserConfiguration.java b/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/RewriteParserConfiguration.java index 4c160c3c3..9c2e6dbf7 100644 --- a/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/RewriteParserConfiguration.java +++ b/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/RewriteParserConfiguration.java @@ -74,11 +74,11 @@ Consumer artifactDownloaderErrorConsumer() { @Bean ModuleParser moduleParser() { return new ModuleParser(); - }< + } @Bean - MavenModuleParser mavenModuleParser(ParserProperties parserPropeties, ModuleParser moduleParser) { - return new MavenModuleParser(parserPropeties, moduleParser); + MavenModuleParser mavenModuleParser(ParserProperties parserProperties, ModuleParser moduleParser) { + return new MavenModuleParser(parserProperties, moduleParser); } @Bean diff --git a/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/maven/MavenSettingsInitializer.java b/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/maven/MavenSettingsInitializer.java index 5ce1fddd0..020b3707a 100644 --- a/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/maven/MavenSettingsInitializer.java +++ b/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/maven/MavenSettingsInitializer.java @@ -20,8 +20,10 @@ import org.openrewrite.Parser; import org.openrewrite.maven.MavenExecutionContextView; import org.openrewrite.maven.MavenSettings; +import org.openrewrite.maven.tree.MavenRepository; import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher; import org.springframework.core.io.Resource; +import org.springframework.sbm.scopes.ProjectMetadata; import org.springframework.sbm.utils.ResourceUtil; import org.springframework.stereotype.Component; @@ -37,18 +39,22 @@ public class MavenSettingsInitializer { private final MavenPasswordDecrypter mavenPasswordDecrypter; private final ExecutionContext executionContext; + private final ProjectMetadata projectMetadata; /** * @deprecated initialization in ExecutionoContext is done in ProjectParser */ public void initializeMavenSettings() { + String repo = "file://" + Path.of(System.getProperty("user.home")).resolve(".m2/repository") + "/"; + MavenSettings mavenSettings = new MavenSettings(repo, null, null, null, null); // Read .m2/settings.xml // TODO: Add support for global Maven settings (${maven.home}/conf/settings.xml). - MavenExecutionContextView mavenExecutionContextView = MavenExecutionContextView.view(executionContext); Path mavenSettingsFile = Path.of(System.getProperty("user.home")).resolve(".m2/settings.xml"); + MavenExecutionContextView mavenExecutionContextView = MavenExecutionContextView.view(executionContext); if (Files.exists(mavenSettingsFile)) { - MavenSettings mavenSettings = MavenSettings.parse(mavenSettingsFile, mavenExecutionContextView); - mavenExecutionContextView.setMavenSettings(mavenSettings); + mavenSettings = MavenSettings.parse(mavenSettingsFile, mavenExecutionContextView); } + mavenExecutionContextView.setMavenSettings(mavenSettings); + projectMetadata.setMavenSettings(mavenSettings); } public MavenSettings initializeMavenSettings(Resource mavenSettingsFile, Path securitySettingsFilePath) { diff --git a/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/maven/RewriteParserMavenConfiguration.java b/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/maven/RewriteParserMavenConfiguration.java index 88b888db5..1560f0bea 100644 --- a/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/maven/RewriteParserMavenConfiguration.java +++ b/sbm-support-rewrite/src/main/java/org/springframework/sbm/parsers/maven/RewriteParserMavenConfiguration.java @@ -68,7 +68,7 @@ MavenSettingsInitializer mavenSettingsInitializer(ExecutionContext executionCont try { SecDispatcher secDispatcher = new DefaultSecDispatcher(new DefaultPlexusCipher()); MavenPasswordDecrypter mavenPasswordDecrypter = new MavenPasswordDecrypter(secDispatcher); - return new MavenSettingsInitializer(mavenPasswordDecrypter, executionContext); + return new MavenSettingsInitializer(mavenPasswordDecrypter, executionContext, new ProjectMetadata()); } catch (PlexusCipherException e) { throw new RuntimeException(e); } diff --git a/sbm-support-rewrite/src/test/java/org/springframework/sbm/PrivateArtifactRepositoryTest.java b/sbm-support-rewrite/src/test/java/org/springframework/sbm/PrivateArtifactRepositoryTest.java index ac02d5a33..eeadb538e 100644 --- a/sbm-support-rewrite/src/test/java/org/springframework/sbm/PrivateArtifactRepositoryTest.java +++ b/sbm-support-rewrite/src/test/java/org/springframework/sbm/PrivateArtifactRepositoryTest.java @@ -19,27 +19,42 @@ import org.apache.maven.shared.invoker.*; import org.junit.jupiter.api.*; import org.junit.jupiter.api.io.TempDir; +import org.junitpioneer.jupiter.SetSystemProperty; +import org.mockito.Mockito; import org.openrewrite.java.marker.JavaSourceSet; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; +import org.openrewrite.maven.cache.LocalMavenArtifactCache; +import org.openrewrite.maven.cache.MavenArtifactCache; +import org.openrewrite.maven.tree.MavenRepository; +import org.powermock.reflect.Whitebox; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.sbm.boot.autoconfigure.SbmSupportRewriteConfiguration; import org.springframework.sbm.parsers.RewriteProjectParser; import org.springframework.sbm.parsers.RewriteProjectParsingResult; import org.springframework.sbm.parsers.maven.RewriteMavenProjectParser; import org.springframework.sbm.parsers.maven.SbmTestConfiguration; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.util.ReflectionTestUtils; import org.springframework.util.FileSystemUtils; import org.testcontainers.containers.GenericContainer; +import org.testcontainers.images.builder.Transferable; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.MountableFile; import java.io.*; +import java.lang.reflect.Field; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; @@ -53,14 +68,29 @@ /** * @author Fabian Krüger */ -@SpringBootTest(classes = {SbmSupportRewriteConfiguration.class, SbmTestConfiguration.class}) +@SpringBootTest(classes = {MyTEstCOnfig.class, SbmSupportRewriteConfiguration.class, SbmTestConfiguration.class}) +@DirtiesContext // clear beans caching Maven settings @Testcontainers @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class PrivateArtifactRepositoryTest { + + + private static MavenRepository originalMavenRepository; + // localRepository.getUri() will differ from '"file:" + userHome + "/.m2/repository/"' because + // MavenRepository.MAVEN_LOCAL_DEFAULT gets returned and this field is statically initialized. + // For this test it means that running the test in isolation succeeds but running it in combination + // with a test that loads MavenRepository before 'user.home' was changed in this test, it fails. + // And maybe even worse, running this test before others would set the local maven repository to the + // dummy dir used in this test. + // + // To prevent this it will be set to the original settings with this line: + MavenRepository mavenLocalDefault = MavenRepository.MAVEN_LOCAL_DEFAULT; + public static final String $USER_HOME_PLACEHOLDER = "${user.home}"; public static final String $PORT_PLACEHOLDER = "${port}"; public static final String TESTCODE_DIR = "testcode/reposilite-test"; public static final String DEPENDENCY_CLASS_FQNAME = "com.example.dependency.DependencyClass"; + @Container static GenericContainer reposilite = new GenericContainer(DockerImageName.parse("dzikoysk/reposilite:3.4.10")) .withExposedPorts(8080) @@ -68,6 +98,42 @@ public class PrivateArtifactRepositoryTest { MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-data/shared.configuration.json"), "/app/data/shared.configuration.json" ) + .withCopyFileToContainer( + MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-test/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/maven-metadata.xml"), + "/app/data/repositories/snapshots/com/example/dependency/dependency-project/maven-metadata.xml" + ) + .withCopyFileToContainer( + MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-test/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/maven-metadata.xml.md5"), + "/app/data/repositories/snapshots/com/example/dependency/dependency-project/maven-metadata.xml.md5" + ) + .withCopyFileToContainer( + MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-test/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/maven-metadata.xml.sha1"), + "/app/data/repositories/snapshots/com/example/dependency/dependency-project/maven-metadata.xml.sha1" + ) + .withCopyFileToContainer( + MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-test/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.jar"), + "/app/data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.jar" + ) + .withCopyFileToContainer( + MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-test/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.jar.md5"), + "/app/data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.jar.md5" + ) + .withCopyFileToContainer( + MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-test/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.jar.sha1"), + "/app/data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.jar.sha1" + ) + .withCopyFileToContainer( + MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-test/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.pom"), + "/app/data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.pom" + ) + .withCopyFileToContainer( + MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-test/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.pom.md5"), + "/app/data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.pom.md5" + ) + .withCopyFileToContainer( + MountableFile.forHostPath("./" + TESTCODE_DIR + "/reposilite-test/reposilite-data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.pom.sha1"), + "/app/data/repositories/snapshots/com/example/dependency/dependency-project/1.0-SNAPSHOT/dependency-project-1.0-20231105.102337-1.pom.sha1" + ) // Create temp user 'user' with password 'secret' .withEnv("REPOSILITE_OPTS", "--token user:secret --shared-config shared.configuration.json"); @@ -89,17 +155,20 @@ static void beforeAll(@TempDir Path tempDir) { newUserHome = Path.of(".").resolve(TESTCODE_DIR + "/user.home").toAbsolutePath().normalize().toString(); System.setProperty("user.home", newUserHome); installMavenForTestIfNotExists(tempDir); + originalMavenRepository = MavenRepository.MAVEN_LOCAL_DEFAULT; + MavenRepository mavenRepository = new MavenRepository("local", new File(System.getProperty("user.home") + "/.m2/repository").toURI().toString(), "true", "true", true, null, null, false); + Whitebox.setInternalState(MavenRepository.class, "MAVEN_LOCAL_DEFAULT", mavenRepository); } @AfterAll static void afterAll() { System.setProperty("user.home", originalUserHome); + Whitebox.setInternalState(MavenRepository.class, "MAVEN_LOCAL_DEFAULT", originalMavenRepository); } @Test @Order(1) @DisplayName("Maven settings should be read") -// @SetSystemProperty(key = "user.home", value = "testcode/reposilite-test/user.home") void mavenSettingsShouldBeRead() throws IOException, MavenInvocationException, InterruptedException { Integer port = reposilite.getMappedPort(8080); System.out.println("Reposilite: http://localhost:" + port + " login with user:secret"); @@ -119,13 +188,17 @@ void mavenSettingsShouldBeRead() throws IOException, MavenInvocationException, I deployDependency(dependencyPomPath); // the project 'testcode/reposilite-test/reposilite-test' has been deployed to reposilite + clearDependencyFromLocalMavenRepo(); + InvocationRequest request = new DefaultInvocationRequest(); request.setPomFile(dependentPomPath.toFile()); request.setShowErrors(true); request.setUserSettingsFile(Path.of(TESTCODE_DIR + "/user.home/.m2/settings.xml").toFile()); - request.setGoals(List.of("-v", "clean", "package")); + request.setGoals(List.of("clean", "package")); request.setLocalRepositoryDirectory(localMavenRepository); request.setBatchMode(true); + request.setGlobalChecksumPolicy(InvocationRequest.CheckSumPolicy.Warn); + request.setOutputHandler(s -> System.out.println(s)); Invoker invoker = new DefaultInvoker(); invoker.setMavenHome(Path.of(TESTCODE_DIR + "/user.home/apache-maven-3.9.5").toFile()); InvocationResult result = invoker.execute(request); @@ -148,7 +221,7 @@ void mavenSettingsShouldBeRead() throws IOException, MavenInvocationException, I void verifyDependenciesFromPrivateRepoWereResolved() { // verify dependency does not exist in local Maven repo Path dependencyArtifactDir = dependencyPathInLocalMavenRepo.getParent(); - assertThat(Files.isDirectory(dependencyArtifactDir)).isTrue(); + assertThat(dependencyArtifactDir).isDirectory(); assertThat(dependencyArtifactDir.toFile().listFiles()).isEmpty(); // scan a project that depends on this dependency @@ -156,7 +229,7 @@ void verifyDependenciesFromPrivateRepoWereResolved() { RewriteProjectParsingResult parsingResult = parser.parse(migrateApplication); // verify dependency was downloaded - Path snapshotDir = dependencyPathInLocalMavenRepo.resolve("1.0-SNAPSHOT"); + Path snapshotDir = dependencyPathInLocalMavenRepo.resolve("1.0-SNAPSHOT").toAbsolutePath().normalize(); assertThat(snapshotDir).isDirectory(); assertThat(Arrays.stream(snapshotDir.toFile().listFiles()).map(f -> f.getName()).findFirst().get()).matches("dependency-project-1.0-.*\\.jar"); @@ -303,3 +376,15 @@ private void deployDependency(Path pomXmlPath) throws MavenInvocationException { } } + + +@Configuration +class MyTEstCOnfig { + @Bean + MavenArtifactCache mavenArtifactCache() { + MavenArtifactCache mavenArtifactCache = new LocalMavenArtifactCache(Paths.get(System.getProperty("user.home"), ".m2", "repository")).orElse( + new LocalMavenArtifactCache(Paths.get(System.getProperty("user.home"), ".rewrite", "cache", "artifacts")) + ); + return mavenArtifactCache; + } +} \ No newline at end of file diff --git a/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/BuildFileParserTest.java b/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/BuildFileParserTest.java index 04a16cea1..2b5564b88 100644 --- a/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/BuildFileParserTest.java +++ b/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/BuildFileParserTest.java @@ -32,6 +32,7 @@ import org.springframework.sbm.parsers.maven.BuildFileParser; import org.springframework.sbm.parsers.maven.MavenPasswordDecrypter; import org.springframework.sbm.parsers.maven.MavenSettingsInitializer; +import org.springframework.sbm.scopes.ProjectMetadata; import org.springframework.sbm.test.util.DummyResource; import org.springframework.sbm.utils.ResourceUtil; @@ -110,7 +111,7 @@ public class GivenSimpleMavenMultiModuleProject { @BeforeEach void beforeEach() throws PlexusCipherException { ExecutionContext executionContext = new InMemoryExecutionContext(t -> {throw new RuntimeException(t);}); - sut = new BuildFileParser(new MavenSettingsInitializer(new MavenPasswordDecrypter(new DefaultSecDispatcher(new DefaultPlexusCipher())), executionContext)); + sut = new BuildFileParser(new MavenSettingsInitializer(new MavenPasswordDecrypter(new DefaultSecDispatcher(new DefaultPlexusCipher())), executionContext, new ProjectMetadata())); } @Test diff --git a/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/RewriteProjectParserParityTest.java b/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/RewriteProjectParserParityTest.java index 9f42c1ab5..d8f5b79af 100644 --- a/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/RewriteProjectParserParityTest.java +++ b/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/RewriteProjectParserParityTest.java @@ -53,9 +53,12 @@ import org.springframework.core.io.Resource; import org.springframework.sbm.parsers.*; import org.springframework.sbm.parsers.events.RewriteParsingEventListenerAdapter; +import org.springframework.sbm.parsers.maven.ComparingParserFactory; +import org.springframework.sbm.parsers.maven.RewriteMavenProjectParser; import org.springframework.sbm.scopes.ScanScope; import org.springframework.sbm.test.util.DummyResource; import org.springframework.sbm.test.util.ParserParityTestHelper; +import org.springframework.sbm.test.util.TestProjectHelper; import org.springframework.test.util.ReflectionTestUtils; import java.nio.charset.Charset; @@ -136,37 +139,6 @@ public static void main(String[] args){ } """; - - List classpath = List.of( - Path.of("/Users/fkrueger/.m2/repository/org/springframework/boot/spring-boot-starter/3.1.1/spring-boot-starter-3.1.1.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/springframework/boot/spring-boot/3.1.1/spring-boot-3.1.1.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/springframework/spring-context/6.0.10/spring-context-6.0.10.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/springframework/spring-aop/6.0.10/spring-aop-6.0.10.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/springframework/spring-beans/6.0.10/spring-beans-6.0.10.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/springframework/spring-expression/6.0.10/spring-expression-6.0.10.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/3.1.1/spring-boot-autoconfigure-3.1.1.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/springframework/boot/spring-boot-starter-logging/3.1.1/spring-boot-starter-logging-3.1.1.jar"), - Path.of("/Users/fkrueger/.m2/repository/ch/qos/logback/logback-classic/1.4.8/logback-classic-1.4.8.jar"), - Path.of("/Users/fkrueger/.m2/repository/ch/qos/logback/logback-core/1.4.8/logback-core-1.4.8.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/slf4j/slf4j-api/2.0.7/slf4j-api-2.0.7.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.20.0/log4j-to-slf4j-2.20.0.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/apache/logging/log4j/log4j-api/2.20.0/log4j-api-2.20.0.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/slf4j/jul-to-slf4j/2.0.7/jul-to-slf4j-2.0.7.jar"), - Path.of("/Users/fkrueger/.m2/repository/jakarta/annotation/jakarta.annotation-api/2.1.1/jakarta.annotation-api-2.1.1.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/springframework/spring-core/6.0.10/spring-core-6.0.10.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/springframework/spring-jcl/6.0.10/spring-jcl-6.0.10.jar"), - Path.of("/Users/fkrueger/.m2/repository/org/yaml/snakeyaml/1.33/snakeyaml-1.33.jar") - ); - JavaTypeCache javaTypeCache = new JavaTypeCache(); - SourceFile sourceFile = JavaParser.fromJavaVersion().classpath(classpath).typeCache(javaTypeCache) - .build() - .parse(javaClass) - .toList() - .get(0); - - JavaSourceSet.build("main", classpath, javaTypeCache, true); - - TestProjectHelper.createTestProject(tempDir) .withResources( new DummyResource(tempDir.resolve("src/main/java/com/example/MyMain.java"), javaClass), @@ -183,7 +155,6 @@ public static void main(String[] args){ ParserParityTestHelper .scanProjectDir(tempDir) .withParserProperties(comparingParserProperties) - .parseSequentially() .verifyParity(); } diff --git a/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/RewriteProjectParserTest.java b/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/RewriteProjectParserTest.java index 3db5afebf..8f1cecdf8 100644 --- a/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/RewriteProjectParserTest.java +++ b/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/RewriteProjectParserTest.java @@ -33,6 +33,7 @@ import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.sbm.parsers.maven.*; +import org.springframework.sbm.scopes.ProjectMetadata; import org.springframework.sbm.scopes.ScanScope; import org.springframework.sbm.test.util.DummyResource; import org.springframework.sbm.utils.ResourceUtil; @@ -97,7 +98,7 @@ void parseSimpleMavenProject(@TempDir Path tempDir) throws PlexusCipherException MavenModuleParser mavenModuleParser = new MavenModuleParser(parserProperties, mavenMojoParserPrivateMethods); RewriteProjectParser projectParser = new RewriteProjectParser( new ProvenanceMarkerFactory(new MavenProvenanceMarkerFactory()), - new BuildFileParser(new MavenSettingsInitializer(new MavenPasswordDecrypter(new DefaultSecDispatcher(new DefaultPlexusCipher())), executionContext)), + new BuildFileParser(new MavenSettingsInitializer(new MavenPasswordDecrypter(new DefaultSecDispatcher(new DefaultPlexusCipher())), executionContext, new ProjectMetadata())), new SourceFileParser(mavenModuleParser), new StyleDetector(), parserProperties, diff --git a/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/maven/MavenSettingsInitializerTest.java b/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/maven/MavenSettingsInitializerTest.java index c8fa16e4f..8c82e7dcd 100644 --- a/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/maven/MavenSettingsInitializerTest.java +++ b/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/maven/MavenSettingsInitializerTest.java @@ -25,6 +25,7 @@ import org.sonatype.plexus.components.cipher.PlexusCipherException; import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher; import org.springframework.sbm.parsers.RewriteExecutionContext; +import org.springframework.sbm.scopes.ProjectMetadata; import java.net.URI; import java.net.URISyntaxException; @@ -52,7 +53,7 @@ class MavenSettingsInitializerTest { void mavenParserMustAdhereToSettingsXmlTest() throws URISyntaxException, PlexusCipherException { RewriteExecutionContext executionContext = new RewriteExecutionContext(); - MavenSettingsInitializer sut = new MavenSettingsInitializer(new MavenPasswordDecrypter(new DefaultSecDispatcher(new DefaultPlexusCipher())), executionContext); + MavenSettingsInitializer sut = new MavenSettingsInitializer(new MavenPasswordDecrypter(new DefaultSecDispatcher(new DefaultPlexusCipher())), executionContext, new ProjectMetadata()); sut.initializeMavenSettings(); MavenExecutionContextView mavenExecutionContextView = MavenExecutionContextView.view(executionContext); diff --git a/sbm-support-rewrite/src/test/java/org/springframework/sbm/test/util/ParserParityTestHelper.java b/sbm-support-rewrite/src/test/java/org/springframework/sbm/test/util/ParserParityTestHelper.java index 509b6a5c8..dfcb8788e 100644 --- a/sbm-support-rewrite/src/test/java/org/springframework/sbm/test/util/ParserParityTestHelper.java +++ b/sbm-support-rewrite/src/test/java/org/springframework/sbm/test/util/ParserParityTestHelper.java @@ -15,6 +15,7 @@ */ package org.springframework.sbm.test.util; +import org.assertj.core.api.SoftAssertions; import org.openrewrite.ExecutionContext; import org.openrewrite.SourceFile; import org.openrewrite.java.marker.JavaSourceSet; @@ -22,6 +23,9 @@ import org.openrewrite.marker.Marker; import org.openrewrite.marker.Markers; import org.openrewrite.maven.MavenSettings; +import org.openrewrite.maven.tree.MavenResolutionResult; +import org.openrewrite.maven.tree.ResolvedDependency; +import org.openrewrite.maven.tree.Scope; import org.openrewrite.style.Style; import org.springframework.sbm.parsers.ParserProperties; import org.springframework.sbm.parsers.RewriteProjectParsingResult; @@ -134,23 +138,43 @@ private void verifyMarkersAreTheSame(RewriteProjectParsingResult comparingParser Markers testedMarkers = curTestedSourceFile.getMarkers(); List testedMarkersList = testedMarkers.getMarkers(); - assertThat(comparingMarkersList) + testedMarkersList.sort(Comparator.comparing(o -> o.getClass().getName())); + comparingMarkersList.sort(Comparator.comparing(o -> o.getClass().getName())); + + SoftAssertions softAssertions = new SoftAssertions(); + + // Compare + assertThat(comparingMarkersList.get(0)) .usingRecursiveComparison() - .withStrictTypeChecking() - .ignoringCollectionOrder() + .ignoringCollectionOrderInFields("parent.modules") .ignoringFields( - // classpath compared further down - "classpath", - // FIXME: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/982 - "styles" + "dependencies", + "parent.modules" ) .ignoringFieldsOfTypes( UUID.class, // FIXME: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/880 - MavenSettings.class, - // FIXME: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/982 - Style.class) - .isEqualTo(testedMarkersList); + MavenSettings.class) + .isEqualTo(testedMarkersList.get(0)); + + + comparingMarkersList.forEach(comparingMarker -> { + int i = comparingMarkersList.indexOf(comparingMarker); + Marker testedMarker = testedMarkersList.get(i); + + assertThat(testedMarker).isInstanceOf(comparingMarker.getClass()); + + if(MavenResolutionResult.class.isInstance(testedMarker)) { + MavenResolutionResult comparing = (MavenResolutionResult) comparingMarker; + MavenResolutionResult tested = (MavenResolutionResult) testedMarker; + compareMavenResolutionResultMarker(softAssertions, comparing, tested); + } else { + compareMarker(softAssertions, comparingMarker, testedMarker); + } + + }); + + softAssertions.assertAll(); if (curComparingSourceFile.getMarkers().findFirst(JavaSourceSet.class).isPresent()) { // Tested parser must have JavaSourceSet marker when comparing parser has it @@ -188,6 +212,71 @@ private void verifyMarkersAreTheSame(RewriteProjectParsingResult comparingParser } } + private void compareMarker(SoftAssertions softAssertions, Marker comparingMarker, Marker testedMarker) { + softAssertions.assertThat(testedMarker) + .usingRecursiveComparison() + .withStrictTypeChecking() + .ignoringCollectionOrder() + .ignoringFields( + // classpath compared further down + "classpath", + // FIXME: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/982 + "styles" + ) + .ignoringFieldsOfTypes( + UUID.class, + // FIXME: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/880 + MavenSettings.class, + // FIXME: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/982 + Style.class) + .isEqualTo(comparingMarker); + } + + private void compareMavenResolutionResultMarker(SoftAssertions softAssertions, MavenResolutionResult comparing, MavenResolutionResult tested) { + softAssertions.assertThat(tested) + .usingRecursiveComparison() + .ignoringFields( + "modules", // TODO: extra test + "dependencies", // TODO: extra test + "parent.modules" // TODO: extra test + ) + .ignoringFieldsOfTypes( + UUID.class, + // FIXME: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/880 + MavenSettings.class) + .isEqualTo(comparing); + + List comparingModules = comparing.getModules(); + List testedModules = tested.getModules(); + + + // bring modules in same order + comparingModules.sort(Comparator.comparing(o -> o.getPom().getGav().toString())); + testedModules.sort(Comparator.comparing(o -> o.getPom().getGav().toString())); + // test modules + comparingModules.forEach(cm -> { + MavenResolutionResult testedMavenResolutionResult = testedModules.get(comparingModules.indexOf(cm)); + compareMavenResolutionResultMarker(softAssertions, cm, testedMavenResolutionResult); + }); + + Set keys = comparing.getDependencies().keySet(); + keys.forEach(k -> { + List comparingDependencies = comparing.getDependencies().get(k); + List testedDependencies = tested.getDependencies().get(k); + + // same order + comparingDependencies.sort(Comparator.comparing(o -> o.getGav().toString())); + testedDependencies.sort(Comparator.comparing(o -> o.getGav().toString())); + + softAssertions.assertThat(testedDependencies) + .usingRecursiveComparison() + .ignoringFieldsOfTypes( + UUID.class + ) + .isEqualTo(comparingDependencies); + }); + } + public ParserParityTestHelper withExecutionContextForComparingParser(ExecutionContext executionContext) { this.executionContext = executionContext; return this; diff --git a/sbm-support-rewrite/testcode/reposilite-test/reposilite-data/shared.configuration.json b/sbm-support-rewrite/testcode/reposilite-test/reposilite-data/shared.configuration.json index 8fb5145bb..90c3c48d0 100644 --- a/sbm-support-rewrite/testcode/reposilite-test/reposilite-data/shared.configuration.json +++ b/sbm-support-rewrite/testcode/reposilite-test/reposilite-data/shared.configuration.json @@ -52,7 +52,7 @@ "storageProvider": { "type": "fs", "quota": "100%", - "mount": "./repositories/snapshots" + "mount": "./snapshots" }, "storagePolicy": "PRIORITIZE_UPSTREAM_METADATA", "proxied": [] diff --git a/sbm-support-rewrite/testcode/reposilite-test/reposilite-data/tokens.json b/sbm-support-rewrite/testcode/reposilite-test/reposilite-data/tokens.json deleted file mode 100644 index 0ab628653..000000000 --- a/sbm-support-rewrite/testcode/reposilite-test/reposilite-data/tokens.json +++ /dev/null @@ -1,94 +0,0 @@ -[ - { - "accessToken": { - "identifier": { - "type": "PERSISTENT", - "value": 1 - }, - "name": "test-token", - "encryptedSecret": "$2a$10$FX20ttuMkkYZOaMXoLOjf.zf9v4MGqMxsaTC3zs28DLelaUeFDmI6", - "createdAt": [ - 2023, - 11, - 2 - ], - "description": "" - }, - "permissions": [ - { - "identifier": "access-token:manager", - "shortcut": "m" - } - ], - "routes": [] - }, - { - "accessToken": { - "identifier": { - "type": "PERSISTENT", - "value": 3 - }, - "name": "admin:secret", - "encryptedSecret": "$2a$10$lMwrb9kiWFVzNSTDm2.U7uRHZAze3WDmemqmwf58LAFYhV7Z9fXAq", - "createdAt": [ - 2023, - 11, - 2 - ], - "description": "" - }, - "permissions": [ - { - "identifier": "access-token:manager", - "shortcut": "m" - } - ], - "routes": [] - }, - { - "accessToken": { - "identifier": { - "type": "PERSISTENT", - "value": 4 - }, - "name": "admin", - "encryptedSecret": "$2a$10$LsZrKkSz9D6FtHPwOHykR.r.vYjosOiBZqR/CS/GesY6.3ecyABcm", - "createdAt": [ - 2023, - 11, - 2 - ], - "description": "" - }, - "permissions": [ - { - "identifier": "access-token:manager", - "shortcut": "m" - } - ], - "routes": [] - }, - { - "accessToken": { - "identifier": { - "type": "PERSISTENT", - "value": 6 - }, - "name": "user", - "encryptedSecret": "$2a$10$djPW0e6jwLv.X8jSW/PwC.i5.bOWl.vCVW4AwLGdNDD96Glfbu0Zm", - "createdAt": [ - 2023, - 11, - 2 - ], - "description": "" - }, - "permissions": [ - { - "identifier": "access-token:manager", - "shortcut": "m" - } - ], - "routes": [] - } -] \ No newline at end of file diff --git a/sbm-support-rewrite/testcode/reposilite-test/user.home/.m2/settings.xml b/sbm-support-rewrite/testcode/reposilite-test/user.home/.m2/settings.xml deleted file mode 100644 index c6d3e0fdc..000000000 --- a/sbm-support-rewrite/testcode/reposilite-test/user.home/.m2/settings.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - reposilite-repository-releases - user - - {iKYxpFKiVu0HTAe4w0RAzev3TAav0DG8wEom2qNoRws=} - - -