Skip to content

Commit

Permalink
Cleaned up ParserParityTestHelper
Browse files Browse the repository at this point in the history
Fixed MavenSettingsInitializer
  • Loading branch information
fabapp2 committed Nov 9, 2023
1 parent e9e25cb commit 76d5220
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@

import lombok.RequiredArgsConstructor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.maven.MavenExecutionContextView;
import org.openrewrite.maven.MavenSettings;
import org.openrewrite.maven.internal.RawRepositories;
import org.openrewrite.maven.tree.MavenRepository;
import org.openrewrite.maven.tree.ProfileActivation;
import org.springframework.sbm.scopes.ProjectMetadata;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
* Initialize {@link MavenSettings} with information from {@code ~/.m2/settings.xml} and {@code settings-security.xml}.
Expand All @@ -47,12 +51,19 @@ public MavenSettingsInitializer(ExecutionContext executionContext, ProjectMetada

public void initializeMavenSettings() {
Path userHome = Path.of(System.getProperty("user.home"));
String repo = "file://" + userHome.resolve(".m2/repository") + "/";
String m2RepoPath = userHome.resolve(".m2/repository") + "/";
String repo = "file://" + m2RepoPath;
Path mavenSettingsFile = userHome.resolve(".m2/settings.xml");
Path mavenSecuritySettingsFile = userHome.resolve(".m2/settings-security.xml");

MavenRepository mavenRepository = new MavenRepository("local", repo, "true", "true", true, null, null, false);
MavenSettings mavenSettings = new MavenSettings(repo, mavenRepository, null, null, null, null);
MavenRepository mavenRepository = new MavenRepository("local", repo, null, null, true, null, null, null);
MavenSettings.Profile defaultProfile = new MavenSettings.Profile("default", null, new RawRepositories());
MavenSettings.@Nullable Profiles profiles = new MavenSettings.Profiles(List.of(defaultProfile));
MavenSettings.@Nullable ActiveProfiles activeProfiles = new MavenSettings.ActiveProfiles(List.of("default"));
MavenSettings.@Nullable Mirrors mirrors = new MavenSettings.Mirrors();
MavenSettings.Servers servers = new MavenSettings.Servers();
MavenSettings mavenSettings = new MavenSettings(m2RepoPath, mavenRepository, profiles, activeProfiles, mirrors, servers);

// TODO: Add support for global Maven settings (${maven.home}/conf/settings.xml).
MavenExecutionContextView mavenExecutionContextView = MavenExecutionContextView.view(executionContext);
if (Files.exists(mavenSettingsFile)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ void shouldParseMavenConfigProject() {
void parseCheckstyle() {
Path baseDir = getMavenProject("checkstyle");
ParserParityTestHelper.scanProjectDir(baseDir)
.parseSequentially()
.verifyParity((comparingParsingResult, testedParsingResult) -> {
assertThat(comparingParsingResult.sourceFiles().stream().map(sf -> sf.getSourcePath().toString()).toList()).contains("checkstyle/rules.xml");
assertThat(comparingParsingResult.sourceFiles().stream().map(sf -> sf.getSourcePath().toString()).toList()).contains("checkstyle/suppressions.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

Expand Down Expand Up @@ -79,6 +80,8 @@ public void projectFailed(ExecutionEvent event) {
}
};
MavenExecutionRequest request = requestFactory.createMavenExecutionRequest(plexusContainer, baseDir);
ArrayList<String> givenGoals = new ArrayList<>(goals);
givenGoals.add("-B");
request.setGoals(goals);
request.setExecutionListener(executionListener);
execute(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ public ParallelParsingResult parseParallel(Path baseDir, ParserProperties parser

ExecutorService threadPool = Executors.newFixedThreadPool(2);

AtomicReference<RewriteProjectParsingResult> testedParsingResultRef = new AtomicReference<>();
AtomicReference<RewriteProjectParsingResult> comparingParsingResultRef = new AtomicReference<>();
AtomicReference<RewriteProjectParsingResult> actualParsingResultRef = new AtomicReference<>();
AtomicReference<RewriteProjectParsingResult> expectedParsingResultRef = new AtomicReference<>();

threadPool.submit(() -> {
RewriteProjectParsingResult parsingResult = parseWithRewriteProjectParser(baseDir, parserProperties);;
testedParsingResultRef.set(parsingResult);
actualParsingResultRef.set(parsingResult);
latch.countDown();
});

threadPool.submit(() -> {
RewriteProjectParsingResult parsingResult = parseWithComparingParser(baseDir, parserProperties, executionContext);
comparingParsingResultRef.set(parsingResult);
expectedParsingResultRef.set(parsingResult);
latch.countDown();
});
latch.await(60, TimeUnit.SECONDS);
return new ParallelParsingResult(comparingParsingResultRef.get(), testedParsingResultRef.get());
latch.await();
return new ParallelParsingResult(expectedParsingResultRef.get(), actualParsingResultRef.get());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down
Loading

0 comments on commit 76d5220

Please sign in to comment.