Search in sources :

Example 11 with ClassPathResult

use of com.google.cloud.tools.opensource.classpath.ClassPathResult 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 12 with ClassPathResult

use of com.google.cloud.tools.opensource.classpath.ClassPathResult 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)

Example 13 with ClassPathResult

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

the class BomContentTest method assertUniqueClasses.

/**
 * Asserts that the BOM only provides JARs which contains unique class names to the classpath.
 */
private static void assertUniqueClasses(List<Artifact> allArtifacts) throws InvalidVersionSpecificationException, IOException {
    StringBuilder errorMessageBuilder = new StringBuilder();
    ClassPathBuilder classPathBuilder = new ClassPathBuilder();
    ClassPathResult result = classPathBuilder.resolve(allArtifacts, false, DependencyMediation.MAVEN);
    // A Map of every class name to its artifact ID.
    HashMap<String, String> fullClasspathMap = new HashMap<>();
    for (ClassPathEntry classPathEntry : result.getClassPath()) {
        Artifact currentArtifact = classPathEntry.getArtifact();
        if (!currentArtifact.getGroupId().contains("google") || currentArtifact.getGroupId().contains("com.google.android") || currentArtifact.getGroupId().contains("com.google.cloud.bigtable") || currentArtifact.getArtifactId().startsWith("proto-") || currentArtifact.getArtifactId().equals("protobuf-javalite") || currentArtifact.getArtifactId().equals("appengine-testing")) {
            // See: https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/2226
            continue;
        }
        String artifactCoordinates = Artifacts.toCoordinates(currentArtifact);
        for (String className : classPathEntry.getFileNames()) {
            if (className.contains("javax.annotation") || className.contains("$") || className.equals("com.google.cloud.location.LocationsGrpc") || className.endsWith("package-info")) {
                // Ignore LocationsGrpc classes which are duplicated in generated grpc libraries.
                continue;
            }
            String previousArtifact = fullClasspathMap.get(className);
            if (previousArtifact != null) {
                String msg = String.format("Duplicate class %s found in classpath. Found in artifacts %s and %s.\n", className, previousArtifact, artifactCoordinates);
                errorMessageBuilder.append(msg);
            } else {
                fullClasspathMap.put(className, artifactCoordinates);
            }
        }
    }
    String error = errorMessageBuilder.toString();
    Assert.assertTrue("Failing test due to duplicate classes found on classpath:\n" + error, error.isEmpty());
}
Also used : HashMap(java.util.HashMap) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) Artifact(org.eclipse.aether.artifact.Artifact)

Example 14 with ClassPathResult

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

the class BomContentTest method findNoDowngradeViolation.

/**
 * Returns messages describing the violation of the no-downgrade rule by {@code artifact} against
 * the BOM containing {@code bomArtifacts}. An empty list if there is no violations.
 */
private static ImmutableList<String> findNoDowngradeViolation(Map<String, Artifact> bomArtifacts, Artifact artifact) throws InvalidVersionSpecificationException {
    ImmutableList.Builder<String> violations = ImmutableList.builder();
    ClassPathBuilder classPathBuilder = new ClassPathBuilder();
    ClassPathResult result = classPathBuilder.resolve(ImmutableList.of(artifact), false, DependencyMediation.MAVEN);
    for (ClassPathEntry entry : result.getClassPath()) {
        Artifact transitiveDependency = entry.getArtifact();
        String key = Artifacts.makeKey(transitiveDependency);
        Artifact bomArtifact = bomArtifacts.get(key);
        if (bomArtifact == null) {
            // transitiveDependency is not part of the BOM
            continue;
        }
        Version versionInBom = versionScheme.parseVersion(bomArtifact.getVersion());
        Version versionInTransitiveDependency = versionScheme.parseVersion(transitiveDependency.getVersion());
        if (versionInTransitiveDependency.compareTo(versionInBom) <= 0) {
            // the no-downgrade rule.
            continue;
        }
        // A violation of the no-downgrade rule is found.
        violations.add(artifact + " has a transitive dependency " + transitiveDependency + ". This is higher version than " + bomArtifact + " in the BOM");
    }
    return violations.build();
}
Also used : Version(org.eclipse.aether.version.Version) ImmutableList(com.google.common.collect.ImmutableList) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) Artifact(org.eclipse.aether.artifact.Artifact)

Example 15 with ClassPathResult

use of com.google.cloud.tools.opensource.classpath.ClassPathResult in project java-cloud-bom by googleapis.

the class BomContentTest method findNoDowngradeViolation.

/**
 * Returns messages describing the violation of the no-downgrade rule by {@code artifact} against
 * the BOM containing {@code bomArtifacts}. An empty list if there is no violations.
 */
private static ImmutableList<String> findNoDowngradeViolation(Map<String, Artifact> bomArtifacts, Artifact artifact) throws InvalidVersionSpecificationException {
    ImmutableList.Builder<String> violations = ImmutableList.builder();
    ClassPathBuilder classPathBuilder = new ClassPathBuilder();
    ClassPathResult result = classPathBuilder.resolve(ImmutableList.of(artifact), false, DependencyMediation.MAVEN);
    for (ClassPathEntry entry : result.getClassPath()) {
        Artifact transitiveDependency = entry.getArtifact();
        String key = Artifacts.makeKey(transitiveDependency);
        Artifact bomArtifact = bomArtifacts.get(key);
        if (bomArtifact == null) {
            // transitiveDependency is not part of the BOM
            continue;
        }
        Version versionInBom = versionScheme.parseVersion(bomArtifact.getVersion());
        Version versionInTransitiveDependency = versionScheme.parseVersion(transitiveDependency.getVersion());
        if (versionInTransitiveDependency.compareTo(versionInBom) <= 0) {
            // the no-downgrade rule.
            continue;
        }
        // Filter by scopes that are invisible to library users
        ImmutableList<DependencyPath> dependencyPaths = result.getDependencyPaths(entry);
        Verify.verify(!dependencyPaths.isEmpty(), "The class path entry should have at least one dependency path from the root");
        boolean dependencyVisibleToUsers = false;
        for (DependencyPath dependencyPath : dependencyPaths) {
            int length = dependencyPath.size();
            // As the root element is an empty node, the last element is at "length - 2".
            Dependency dependency = dependencyPath.getDependency(length - 2);
            if (dependencyScopesVisibleToUsers.contains(dependency.getScope())) {
                dependencyVisibleToUsers = true;
                break;
            }
        }
        if (!dependencyVisibleToUsers) {
            // provided scope.
            continue;
        }
        // A violation of the no-downgrade rule is found.
        violations.add(artifact + " has a transitive dependency " + transitiveDependency + ". This is higher version than " + bomArtifact + " in the BOM. Example dependency path: " + dependencyPaths.get(0));
    }
    return violations.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DependencyPath(com.google.cloud.tools.opensource.dependencies.DependencyPath) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) Dependency(org.eclipse.aether.graph.Dependency) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry) Artifact(org.eclipse.aether.artifact.Artifact) Version(org.eclipse.aether.version.Version)

Aggregations

ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)15 Artifact (org.eclipse.aether.artifact.Artifact)12 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)11 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)8 ClassPathBuilder (com.google.cloud.tools.opensource.classpath.ClassPathBuilder)8 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)8 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)8 LinkageProblem (com.google.cloud.tools.opensource.classpath.LinkageProblem)6 Path (java.nio.file.Path)6 DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)5 ImmutableList (com.google.common.collect.ImmutableList)5 LinkageChecker (com.google.cloud.tools.opensource.classpath.LinkageChecker)4 Dependency (org.eclipse.aether.graph.Dependency)4 Bom (com.google.cloud.tools.opensource.dependencies.Bom)3 DependencyGraphBuilder (com.google.cloud.tools.opensource.dependencies.DependencyGraphBuilder)3 Configuration (freemarker.template.Configuration)3 HashMap (java.util.HashMap)3 EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)3 Test (org.junit.Test)3 ClassReferenceGraph (com.google.cloud.tools.opensource.classpath.ClassReferenceGraph)2