Skip to content

Commit

Permalink
Use EclipseWorkspaceManager.getWorkspace(URI, Mojo) in ApiAnalysisMojo
Browse files Browse the repository at this point in the history
(cherry picked from commit 36324dc)
  • Loading branch information
laeubi authored and eclipse-tycho-bot committed Apr 27, 2024
1 parent 01aace7 commit 2c8cd5c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 53 deletions.
Expand Up @@ -49,8 +49,8 @@
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.EcJLogFileEnhancer;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.EcJLogFileEnhancer.Source;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.model.project.EclipseProject;
import org.eclipse.tycho.osgi.framework.EclipseApplication;
Expand Down Expand Up @@ -161,25 +161,20 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}
long start = System.currentTimeMillis();
Collection<Path> baselineBundles;
try {
baselineBundles = getBaselineBundles();
} catch (DependencyResolutionException e) {
if (failOnResolutionError) {
throw new MojoFailureException("Can't resolve API baseline!", e);
} else {
log.warn("Can't resolve API baseline, API baseline check is skipped!");
return;
}
Collection<Path> baselineBundles = getBaselineBundles();
if (baselineBundles.isEmpty()) {
log.info("Skipped because no bundles in the baseline!");
return;
}
Collection<Path> dependencyBundles;
try {
dependencyBundles = projectManager.getProjectDependencies(project);
} catch (Exception e) {
throw new MojoFailureException("Can't fetch dependencies!", e);
}
EclipseWorkspace<ApiAppKey> workspace = getWorkspace();
EclipseApplication apiApplication = applicationResolver.getApiApplication(workspace.getKey().repository);
MavenRepositoryLocation repository = getRepository();
EclipseWorkspace<?> workspace = workspaceManager.getWorkspace(repository.getURL(), this);
EclipseApplication apiApplication = applicationResolver.getApiApplication(repository);
EclipseFramework eclipseFramework;
try {
eclipseFramework = apiApplication.startFramework(workspace, List.of());
Expand Down Expand Up @@ -336,10 +331,6 @@ private Path getFullPath(IApiProblem problem) {
return project.getBasedir().toPath().resolve(path);
}

private EclipseWorkspace<ApiAppKey> getWorkspace() {
return workspaceManager.getWorkspace(new ApiAppKey(getRepository()));
}

private boolean wasReplaced() {
if (DefaultReactorProject.adapt(project)
.getContextValue(TychoConstants.KEY_BASELINE_REPLACE_ARTIFACT_MAIN) instanceof Boolean replaced) {
Expand All @@ -357,22 +348,29 @@ private MavenRepositoryLocation getRepository() {

private Collection<Path> getBaselineBundles() throws MojoFailureException {
long start = System.currentTimeMillis();
Collection<Path> baselineBundles;
try {
Collection<TargetEnvironment> targetEnvironments = getBaselineEnvironments();
Optional<ArtifactKey> artifactKey = projectManager.getArtifactKey(project);
getLog().info("Resolve API baseline for " + project.getId() + " with "
+ targetEnvironments.stream().map(String::valueOf).collect(Collectors.joining(", ")));
baselineBundles = applicationResolver.getApiBaselineBundles(
Collection<Path> baselineBundles = applicationResolver.getApiBaselineBundles(
baselines.stream().filter(repo -> repo.getUrl() != null)
.map(repo -> new MavenRepositoryLocation(repo.getId(), URI.create(repo.getUrl()))).toList(),
artifactKey.get(), targetEnvironments);
getLog().debug("API baseline contains " + baselineBundles.size() + " bundles (resolve takes " + time(start)
+ ").");
return baselineBundles;
} catch (IllegalArtifactReferenceException e) {
throw new MojoFailureException("Project specify an invalid artifact key", e);
} catch (DependencyResolutionException e) {
if (failOnResolutionError) {
throw new MojoFailureException("Can't resolve API baseline!", e);
} else {
getLog().warn(
"Can't resolve API baseline: " + Objects.requireNonNullElse(e.getMessage(), e.toString()));
return List.of();
}
}
return baselineBundles;
}

/**
Expand Down Expand Up @@ -403,35 +401,6 @@ private String time(long start) {
return sec + " s";
}

private static final class ApiAppKey {

private URI key;
private MavenRepositoryLocation repository;

public ApiAppKey(MavenRepositoryLocation repository) {
this.repository = repository;
key = Objects.requireNonNull(repository.getURL()).normalize();
}

@Override
public int hashCode() {
return Objects.hash(key);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ApiAppKey other = (ApiAppKey) obj;
return Objects.equals(key, other.key);
}

}

private static Path stringToPath(String file) {
if (file == null) {
return null;
Expand Down
Expand Up @@ -156,7 +156,8 @@ public void testInvalidRepo() throws Exception {
File repo = ResourceUtil.resolveTestResource("repositories/api-tools-broken");
verifier.addCliOption("-DbaselineRepo=" + repo.toURI());

assertThrows(VerificationException.class, () -> verifier.executeGoals(List.of("clean", "verify")), "Did not error on missing repo");
assertThrows(VerificationException.class, () -> verifier.executeGoals(List.of("clean", "verify")),
"Did not error on missing repo");
}

@Test
Expand All @@ -166,7 +167,8 @@ public void testBaselineResolutonFailure_Error() throws Exception {
verifier.addCliOption("-DbaselineRepo=" + repo.toURI());
verifier.addCliOption("-DfailResolutionError=true");

assertThrows(VerificationException.class, () -> verifier.executeGoals(List.of("clean", "verify")), "Did not error on resolution failure");
assertThrows(VerificationException.class, () -> verifier.executeGoals(List.of("clean", "verify")),
"Did not error on resolution failure");
verifier.verifyTextInLog("Can't resolve API baseline!");
}

Expand All @@ -177,7 +179,7 @@ public void testBaselineResolutonFailure_Warn() throws Exception {
verifier.addCliOption("-DbaselineRepo=" + repo.toURI());

verifier.executeGoals(List.of("clean", "verify"));
verifier.verifyTextInLog("Can't resolve API baseline, API baseline check is skipped!");
verifier.verifyTextInLog("Can't resolve API baseline");
}

@Test
Expand All @@ -187,6 +189,6 @@ public void testBaselineResolutonFailure_Default() throws Exception {
verifier.addCliOption("-DbaselineRepo=" + repo.toURI());

verifier.executeGoals(List.of("clean", "verify"));
verifier.verifyTextInLog("Can't resolve API baseline, API baseline check is skipped!");
verifier.verifyTextInLog("Can't resolve API baseline");
}
}

0 comments on commit 2c8cd5c

Please sign in to comment.