Search in sources :

Example 1 with AnnotatedClassPath

use of com.google.cloud.tools.opensource.classpath.AnnotatedClassPath 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");
}
Also used : AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) Path(java.nio.file.Path) Configuration(freemarker.template.Configuration) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) Element(nu.xom.Element) ArrayList(java.util.ArrayList) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Document(nu.xom.Document) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Nodes(nu.xom.Nodes) Test(org.junit.Test)

Example 2 with AnnotatedClassPath

use of com.google.cloud.tools.opensource.classpath.AnnotatedClassPath 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());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) Dependency(org.eclipse.aether.graph.Dependency) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Artifact(org.eclipse.aether.artifact.Artifact) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) DependencyNode(org.eclipse.aether.graph.DependencyNode) UnresolvableArtifactProblem(com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem) File(java.io.File)

Example 3 with AnnotatedClassPath

use of com.google.cloud.tools.opensource.classpath.AnnotatedClassPath 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());
}
Also used : AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry)

Example 4 with AnnotatedClassPath

use of com.google.cloud.tools.opensource.classpath.AnnotatedClassPath in project cloud-opensource-java by GoogleCloudPlatform.

the class DashboardUnavailableArtifactTest method testDashboardForRepositoryException.

@Test
public void testDashboardForRepositoryException() throws TemplateException {
    Configuration configuration = DashboardMain.configureFreemarker();
    Artifact validArtifact = new DefaultArtifact("io.grpc:grpc-context:1.15.0");
    Artifact nonExistentArtifact = new DefaultArtifact("io.grpc:nonexistent:jar:1.15.0");
    DependencyGraphBuilder graphBuilder = new DependencyGraphBuilder();
    Map<Artifact, ArtifactInfo> map = new LinkedHashMap<>();
    DependencyGraph graph1 = graphBuilder.buildMavenDependencyGraph(new Dependency(validArtifact, "compile"));
    DependencyGraph graph2 = graphBuilder.buildMavenDependencyGraph(new Dependency(nonExistentArtifact, "compile"));
    map.put(validArtifact, new ArtifactInfo(graph1, graph2));
    map.put(nonExistentArtifact, new ArtifactInfo(new RepositoryException("foo")));
    ArtifactCache cache = new ArtifactCache();
    cache.setInfoMap(map);
    List<ArtifactResults> artifactResults = DashboardMain.generateReports(configuration, outputDirectory, cache, ImmutableMap.of(), new ClassPathResult(new AnnotatedClassPath(), ImmutableList.of()), bom);
    Assert.assertEquals("The length of the ArtifactResults should match the length of artifacts", 2, artifactResults.size());
    Assert.assertEquals("The first artifact result should be valid", true, artifactResults.get(0).getResult(DashboardMain.TEST_NAME_UPPER_BOUND));
    ArtifactResults errorArtifactResult = artifactResults.get(1);
    Assert.assertNull("The second artifact result should be null", errorArtifactResult.getResult(DashboardMain.TEST_NAME_UPPER_BOUND));
    Assert.assertEquals("The error artifact result should contain error message", "foo", errorArtifactResult.getExceptionMessage());
}
Also used : Configuration(freemarker.template.Configuration) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) RepositoryException(org.eclipse.aether.RepositoryException) Dependency(org.eclipse.aether.graph.Dependency) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) LinkedHashMap(java.util.LinkedHashMap) DependencyGraphBuilder(com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Example 5 with AnnotatedClassPath

use of com.google.cloud.tools.opensource.classpath.AnnotatedClassPath in project cloud-opensource-java by GoogleCloudPlatform.

the class FreemarkerTest method testCountFailures.

@Test
public void testCountFailures() throws IOException, TemplateException, ParsingException {
    Configuration configuration = DashboardMain.configureFreemarker();
    Artifact artifact1 = new DefaultArtifact("io.grpc:grpc-context:1.15.0");
    ArtifactResults results1 = new ArtifactResults(artifact1);
    results1.addResult("Linkage Errors", 56);
    Artifact artifact2 = new DefaultArtifact("grpc:grpc:1.15.0");
    ArtifactResults results2 = new ArtifactResults(artifact2);
    results2.addResult("Linkage Errors", 0);
    List<ArtifactResults> table = ImmutableList.of(results1, results2);
    List<DependencyGraph> globalDependencies = ImmutableList.of();
    DashboardMain.generateDashboard(configuration, outputDirectory, table, globalDependencies, symbolProblemTable, new ClassPathResult(new AnnotatedClassPath(), ImmutableList.of()), new Bom("mock:artifact:1.6.7", null));
    Path dashboardHtml = outputDirectory.resolve("index.html");
    Assert.assertTrue(Files.isRegularFile(dashboardHtml));
    Document document = builder.build(dashboardHtml.toFile());
    // xom's query cannot specify partial class field, e.g., 'statistic-item'
    Nodes counts = document.query("//div[@class='container']/div/h2");
    Assert.assertTrue(counts.size() > 0);
    for (int i = 0; i < counts.size(); i++) {
        Integer.parseInt(counts.get(i).getValue().trim());
    }
    // Linkage Errors
    Truth.assertThat(counts.get(1).getValue().trim()).isEqualTo("1");
}
Also used : AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) Path(java.nio.file.Path) Bom(com.google.cloud.tools.opensource.dependencies.Bom) Configuration(freemarker.template.Configuration) AnnotatedClassPath(com.google.cloud.tools.opensource.classpath.AnnotatedClassPath) DependencyGraph(com.google.cloud.tools.opensource.dependencies.DependencyGraph) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Document(nu.xom.Document) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) Nodes(nu.xom.Nodes) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Aggregations

AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)5 ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)5 Artifact (org.eclipse.aether.artifact.Artifact)5 DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)4 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)4 Configuration (freemarker.template.Configuration)3 Test (org.junit.Test)3 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)2 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)2 Path (java.nio.file.Path)2 Document (nu.xom.Document)2 Nodes (nu.xom.Nodes)2 Dependency (org.eclipse.aether.graph.Dependency)2 Bom (com.google.cloud.tools.opensource.dependencies.Bom)1 DependencyGraphBuilder (com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder)1 UnresolvableArtifactProblem (com.google.cloud.tools.opensource.dependencies.UnresolvableArtifactProblem)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1