use of com.google.cloud.tools.opensource.classpath.ClassPathResult in project cloud-opensource-java by GoogleCloudPlatform.
the class DashboardUnavailableArtifactTest method testDashboardWithRepositoryException.
@Test
public void testDashboardWithRepositoryException() throws IOException, TemplateException, ParsingException {
Configuration configuration = DashboardMain.configureFreemarker();
Artifact validArtifact = new DefaultArtifact("io.grpc:grpc-context:1.15.0");
ArtifactResults validArtifactResult = new ArtifactResults(validArtifact);
validArtifactResult.addResult(DashboardMain.TEST_NAME_UPPER_BOUND, 0);
validArtifactResult.addResult(DashboardMain.TEST_NAME_DEPENDENCY_CONVERGENCE, 0);
validArtifactResult.addResult(DashboardMain.TEST_NAME_GLOBAL_UPPER_BOUND, 0);
Artifact invalidArtifact = new DefaultArtifact("io.grpc:nonexistent:jar:1.15.0");
ArtifactResults errorArtifactResult = new ArtifactResults(invalidArtifact);
errorArtifactResult.setExceptionMessage("Could not find artifact io.grpc:nonexistent:jar:1.15.0 in central" + " (https://repo1.maven.org/maven2/)");
List<ArtifactResults> table = new ArrayList<>();
table.add(validArtifactResult);
table.add(errorArtifactResult);
DashboardMain.generateDashboard(configuration, outputDirectory, table, ImmutableList.of(), ImmutableMap.of(), new ClassPathResult(new AnnotatedClassPath(), ImmutableList.of()), bom);
Path generatedHtml = outputDirectory.resolve("artifact_details.html");
Assert.assertTrue(Files.isRegularFile(generatedHtml));
Document document = builder.build(generatedHtml.toFile());
Assert.assertEquals("en-US", document.getRootElement().getAttribute("lang").getValue());
Nodes tr = document.query("//tr");
Assert.assertEquals("The size of rows in table should match the number of artifacts + 1 (header)", tr.size(), table.size() + 1);
Nodes tdForValidArtifact = tr.get(1).query("td");
Assert.assertEquals(Artifacts.toCoordinates(validArtifact), tdForValidArtifact.get(0).getValue());
Element firstResult = (Element) (tdForValidArtifact.get(2));
Truth.assertThat(firstResult.getValue().trim()).isEqualTo("PASS");
Truth.assertThat(firstResult.getAttributeValue("class")).isEqualTo("pass");
Nodes tdForErrorArtifact = tr.get(2).query("td");
Assert.assertEquals(Artifacts.toCoordinates(invalidArtifact), tdForErrorArtifact.get(0).getValue());
Element secondResult = (Element) (tdForErrorArtifact.get(2));
Truth.assertThat(secondResult.getValue().trim()).isEqualTo("UNAVAILABLE");
Truth.assertThat(secondResult.getAttributeValue("class")).isEqualTo("unavailable");
}
use of com.google.cloud.tools.opensource.classpath.ClassPathResult in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckerRule method findBomClasspath.
/**
* Builds a class path for {@code bomProject}.
*/
private ClassPathResult findBomClasspath(MavenProject bomProject, RepositorySystemSession repositorySystemSession) throws EnforcerRuleException {
ArtifactTypeRegistry artifactTypeRegistry = repositorySystemSession.getArtifactTypeRegistry();
List<org.apache.maven.model.Dependency> managedDependencies = bomProject.getDependencyManagement().getDependencies();
ImmutableList<Artifact> artifacts = managedDependencies.stream().map(dependency -> RepositoryUtils.toDependency(dependency, artifactTypeRegistry)).map(Dependency::getArtifact).filter(artifact -> !Bom.shouldSkipBomMember(artifact)).collect(toImmutableList());
try {
ClassPathResult result = classPathBuilder.resolve(artifacts, false, DependencyMediation.MAVEN);
ImmutableList<UnresolvableArtifactProblem> artifactProblems = result.getArtifactProblems();
if (!artifactProblems.isEmpty()) {
throw new EnforcerRuleException("Failed to collect dependency: " + artifactProblems);
}
return result;
} catch (InvalidVersionSpecificationException ex) {
throw new EnforcerRuleException("Dependency mediation failed due to invalid version", ex);
}
}
use of com.google.cloud.tools.opensource.classpath.ClassPathResult in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckerRule method buildClassPathResult.
private static ClassPathResult buildClassPathResult(DependencyResolutionResult result) throws EnforcerRuleException {
// The root node must have the project's JAR file
DependencyNode root = result.getDependencyGraph();
File rootFile = root.getArtifact().getFile();
if (rootFile == null) {
throw new EnforcerRuleException("The root project artifact is not associated with a file.");
}
List<Dependency> unresolvedDependencies = result.getUnresolvedDependencies();
Set<Artifact> unresolvedArtifacts = unresolvedDependencies.stream().map(Dependency::getArtifact).collect(toImmutableSet());
DependencyGraph dependencyGraph = DependencyGraph.from(root);
AnnotatedClassPath annotatedClassPath = new AnnotatedClassPath();
ImmutableList.Builder<UnresolvableArtifactProblem> problems = ImmutableList.builder();
for (DependencyPath path : dependencyGraph.list()) {
Artifact artifact = path.getLeaf();
if (unresolvedArtifacts.contains(artifact)) {
problems.add(new UnresolvableArtifactProblem(artifact));
} else {
annotatedClassPath.put(new ClassPathEntry(artifact), path);
}
}
return new ClassPathResult(annotatedClassPath, problems.build());
}
use of com.google.cloud.tools.opensource.classpath.ClassPathResult in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckTask method createClassPathResult.
private ClassPathResult createClassPathResult(ResolvedConfiguration configuration) {
DependencyGraph dependencyGraph = createDependencyGraph(configuration);
AnnotatedClassPath annotatedClassPath = new AnnotatedClassPath();
for (DependencyPath path : dependencyGraph.list()) {
Artifact artifact = path.getLeaf();
annotatedClassPath.put(new ClassPathEntry(artifact), path);
}
return new ClassPathResult(annotatedClassPath, ImmutableList.of());
}
use of com.google.cloud.tools.opensource.classpath.ClassPathResult in project cloud-opensource-java by GoogleCloudPlatform.
the class LinkageCheckTask method findLinkageErrors.
/**
* Returns true iff {@code configuration}'s artifacts contain linkage errors.
*/
private boolean findLinkageErrors(Configuration configuration) throws IOException {
ClassPathResult classPathResult = createClassPathResult(configuration.getResolvedConfiguration());
ImmutableList.Builder<ClassPathEntry> classPathEntriesBuilder = ImmutableList.builder();
for (ResolvedArtifact resolvedArtifact : configuration.getResolvedConfiguration().getResolvedArtifacts()) {
ModuleVersionIdentifier moduleVersionId = resolvedArtifact.getModuleVersion().getId();
DefaultArtifact artifact = new DefaultArtifact(moduleVersionId.getGroup(), moduleVersionId.getName(), resolvedArtifact.getClassifier(), resolvedArtifact.getExtension(), moduleVersionId.getVersion(), null, resolvedArtifact.getFile());
classPathEntriesBuilder.add(new ClassPathEntry(artifact));
}
ImmutableList<ClassPathEntry> classPath = classPathEntriesBuilder.build();
if (!classPath.isEmpty()) {
String exclusionFileName = extension.getExclusionFile();
Path exclusionFile = exclusionFileName == null ? null : Paths.get(exclusionFileName);
if (exclusionFile != null && !exclusionFile.isAbsolute()) {
// Relative path from the project root
Path projectRoot = getProject().getRootDir().toPath();
exclusionFile = projectRoot.resolve(exclusionFile).toAbsolutePath();
}
// TODO(suztomo): Specify correct entry points if reportOnlyReachable is true.
LinkageChecker linkageChecker = LinkageChecker.create(classPath, classPath, exclusionFile);
ImmutableSet<LinkageProblem> linkageProblems = linkageChecker.findLinkageProblems();
ClassPathBuilder classPathBuilder = new ClassPathBuilder();
LinkageProblemCauseAnnotator.annotate(classPathBuilder, classPathResult, linkageProblems);
int errorCount = linkageProblems.size();
// TODO(suztomo): Show the dependency paths to the problematic artifacts.
if (errorCount > 0) {
getLogger().error("Linkage Checker rule found {} error{}:\n{}", errorCount, errorCount > 1 ? "s" : "", LinkageProblem.formatLinkageProblems(linkageProblems, classPathResult));
ResolutionResult result = configuration.getIncoming().getResolutionResult();
ResolvedComponentResult root = result.getRoot();
String dependencyPaths = dependencyPathsOfProblematicJars(root, linkageProblems);
getLogger().error(dependencyPaths);
getLogger().info("For the details of the linkage errors, see " + "https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/Linkage-Checker-Messages");
}
return errorCount > 0;
}
// When the configuration does not have any artifacts, there's no linkage error.
return false;
}
Aggregations