Search in sources :

Example 1 with Bom

use of com.google.cloud.tools.opensource.dependencies.Bom in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageCheckerTest method testBomResolutionWithMissingTransitiveDependency.

@Test
public void testBomResolutionWithMissingTransitiveDependency() throws Exception {
    DefaultArtifact cglib = new DefaultArtifact("cglib:cglib-nodep:jar:2.2");
    // cglib-nodep has 2 transitive dependencies, which do not exist in Maven Central
    // cglib:cglib-nodep:jar:2.2
    // - ant:ant:jar:1.6.2 (compile?)
    // - xml-apis:xml-apis:jar:2.6.2 (compile?)
    // - xerces:xerces-impl:jar:2.6.2 (compile?)
    // https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/2097
    Bom bomWithMissingTransitiveDependency = new Bom("com.google.cloud:bom-with-missing-transitive-dependency:0.1", ImmutableList.of(cglib));
    try {
        LinkageChecker.create(bomWithMissingTransitiveDependency);
        fail("Linkage Checker should throw IOException");
    } catch (IOException expected) {
        assertEquals("Could not resolve 2 dependencies. See the message above for details.", expected.getMessage());
    }
}
Also used : Bom(com.google.cloud.tools.opensource.dependencies.Bom) IOException(java.io.IOException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Example 2 with Bom

use of com.google.cloud.tools.opensource.dependencies.Bom in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageMonitor method copyWithSnapshot.

/**
 * Returns a copy of {@code bom} replacing its managed dependencies that have locally-installed
 * snapshot versions.
 */
@VisibleForTesting
static Bom copyWithSnapshot(RepositorySystem repositorySystem, RepositorySystemSession session, Bom bom, Map<String, String> localArtifacts) throws ModelBuildingException, ArtifactResolutionException {
    ImmutableList.Builder<Artifact> managedDependencies = ImmutableList.builder();
    Model model = buildModelWithSnapshotBom(repositorySystem, session, bom.getCoordinates(), localArtifacts);
    ArtifactTypeRegistry registry = session.getArtifactTypeRegistry();
    ImmutableList<Artifact> newManagedDependencies = model.getDependencyManagement().getDependencies().stream().map(dependency -> RepositoryUtils.toDependency(dependency, registry)).map(Dependency::getArtifact).collect(toImmutableList());
    for (Artifact managedDependency : newManagedDependencies) {
        if (Bom.shouldSkipBomMember(managedDependency)) {
            continue;
        }
        String version = localArtifacts.getOrDefault(managedDependency.getGroupId() + ":" + managedDependency.getArtifactId(), managedDependency.getVersion());
        managedDependencies.add(managedDependency.setVersion(version));
    }
    // "-SNAPSHOT" suffix for coordinate to distinguish easily.
    return new Bom(bom.getCoordinates() + "-SNAPSHOT", managedDependencies.build());
}
Also used : Bom(com.google.cloud.tools.opensource.dependencies.Bom) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Model(org.apache.maven.model.Model) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with Bom

use of com.google.cloud.tools.opensource.dependencies.Bom in project cloud-opensource-java by GoogleCloudPlatform.

the class MaximumLinkageErrorsTest method testForNewLinkageErrors.

@Test
public void testForNewLinkageErrors() throws IOException, MavenRepositoryException, RepositoryException {
    // Not using RepositoryUtility.findLatestCoordinates, which may return a snapshot version
    String version = findLatestNonSnapshotVersion();
    String baselineCoordinates = "com.google.cloud:libraries-bom:" + version;
    Bom baseline = Bom.readBom(baselineCoordinates);
    Path bomFile = Paths.get("../cloud-oss-bom/pom.xml");
    Bom bom = Bom.readBom(bomFile);
    ImmutableSet<LinkageProblem> oldProblems = LinkageChecker.create(baseline).findLinkageProblems();
    LinkageChecker checker = LinkageChecker.create(bom);
    ImmutableSet<LinkageProblem> currentProblems = checker.findLinkageProblems();
    // This only tests for newly missing methods, not new invocations of
    // previously missing methods.
    Set<LinkageProblem> newProblems = Sets.difference(currentProblems, oldProblems);
    // Appengine-api-1.0-sdk is known to contain linkage errors because it shades dependencies
    // https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/441
    newProblems = newProblems.stream().filter(problem -> !hasLinkageProblemFromArtifactId(problem, "appengine-api-1.0-sdk")).collect(Collectors.toSet());
    // Check that no new linkage errors have been introduced since the baseline
    StringBuilder message = new StringBuilder("Baseline BOM: " + baselineCoordinates + "\n");
    if (!newProblems.isEmpty()) {
        message.append("Newly introduced problems:\n");
        message.append(LinkageProblem.formatLinkageProblems(newProblems, null));
        Assert.fail(message.toString());
    }
}
Also used : Path(java.nio.file.Path) Bom(com.google.cloud.tools.opensource.dependencies.Bom) LinkageProblem(com.google.cloud.tools.opensource.classpath.LinkageProblem) LinkageChecker(com.google.cloud.tools.opensource.classpath.LinkageChecker) Test(org.junit.Test)

Example 4 with Bom

use of com.google.cloud.tools.opensource.dependencies.Bom in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageMonitor method run.

/**
 * Returns new problems in the BOM specified by {@code groupId} and {@code artifactId}. This
 * method compares the latest release of the BOM and its snapshot version which uses artifacts in
 * {@link #localArtifacts}.
 */
private ImmutableSet<LinkageProblem> run(String groupId, String artifactId) throws RepositoryException, IOException, MavenRepositoryException, ModelBuildingException {
    String latestBomCoordinates = RepositoryUtility.findLatestCoordinates(repositorySystem, groupId, artifactId);
    logger.info("BOM Coordinates: " + latestBomCoordinates);
    Bom baseline = Bom.readBom(latestBomCoordinates);
    ImmutableSet<LinkageProblem> problemsInBaseline = LinkageChecker.create(baseline, null).findLinkageProblems();
    Bom snapshot = copyWithSnapshot(repositorySystem, session, baseline, localArtifacts);
    // Comparing coordinates because DefaultArtifact does not override equals
    ImmutableList<String> baselineCoordinates = coordinatesList(baseline.getManagedDependencies());
    ImmutableList<String> snapshotCoordinates = coordinatesList(snapshot.getManagedDependencies());
    if (baselineCoordinates.equals(snapshotCoordinates)) {
        logger.info("Snapshot is same as baseline. Not running comparison.");
        logger.info("Baseline coordinates: " + Joiner.on(";").join(baselineCoordinates));
        return ImmutableSet.of();
    }
    ImmutableList<Artifact> snapshotManagedDependencies = snapshot.getManagedDependencies();
    ClassPathResult classPathResult = (new ClassPathBuilder()).resolve(snapshotManagedDependencies, true, DependencyMediation.MAVEN);
    ImmutableList<ClassPathEntry> classpath = classPathResult.getClassPath();
    List<ClassPathEntry> entryPointJars = classpath.subList(0, snapshotManagedDependencies.size());
    ImmutableSet<LinkageProblem> problemsInSnapshot = LinkageChecker.create(classpath, ImmutableSet.copyOf(entryPointJars), null).findLinkageProblems();
    if (problemsInBaseline.equals(problemsInSnapshot)) {
        logger.info("Snapshot versions have the same " + problemsInBaseline.size() + " errors as baseline");
        return ImmutableSet.of();
    }
    Set<LinkageProblem> fixedProblems = Sets.difference(problemsInBaseline, problemsInSnapshot);
    if (!fixedProblems.isEmpty()) {
        logger.info(messageForFixedErrors(fixedProblems));
    }
    Set<LinkageProblem> newProblems = Sets.difference(problemsInSnapshot, problemsInBaseline);
    if (!newProblems.isEmpty()) {
        logger.severe(messageForNewErrors(problemsInSnapshot, problemsInBaseline, classPathResult));
    }
    return ImmutableSet.copyOf(newProblems);
}
Also used : Bom(com.google.cloud.tools.opensource.dependencies.Bom) LinkageProblem(com.google.cloud.tools.opensource.classpath.LinkageProblem) ClassPathResult(com.google.cloud.tools.opensource.classpath.ClassPathResult) ClassPathBuilder(com.google.cloud.tools.opensource.classpath.ClassPathBuilder) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ClassPathEntry(com.google.cloud.tools.opensource.classpath.ClassPathEntry)

Example 5 with Bom

use of com.google.cloud.tools.opensource.dependencies.Bom in project cloud-opensource-java by GoogleCloudPlatform.

the class LinkageMonitorTest method testBomSnapshot.

@Test
public void testBomSnapshot() throws ModelBuildingException, ArtifactResolutionException, ArtifactDescriptorException {
    Bom bom = Bom.readBom("com.google.cloud:libraries-bom:1.2.0");
    Bom snapshotBom = LinkageMonitor.copyWithSnapshot(system, session, bom, ImmutableMap.of("com.google.protobuf:protobuf-java", "3.8.0-SNAPSHOT"));
    assertWithMessage("The first element of the SNAPSHOT BOM should be the same as the original BOM").that(toCoordinates(snapshotBom.getManagedDependencies().get(0))).isEqualTo("com.google.protobuf:protobuf-java:3.8.0-SNAPSHOT");
    assertWithMessage("Artifacts other than protobuf-java should have the original version").that(skip(snapshotBom.getManagedDependencies(), 1)).comparingElementsUsing(transforming(Artifacts::toCoordinates, Artifacts::toCoordinates, "has the same Maven coordinates as")).containsExactlyElementsIn(skip(bom.getManagedDependencies(), 1)).inOrder();
}
Also used : Bom(com.google.cloud.tools.opensource.dependencies.Bom) Test(org.junit.Test)

Aggregations

Bom (com.google.cloud.tools.opensource.dependencies.Bom)14 Test (org.junit.Test)8 Artifact (org.eclipse.aether.artifact.Artifact)7 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)5 Path (java.nio.file.Path)4 ClassPathResult (com.google.cloud.tools.opensource.classpath.ClassPathResult)3 LinkageProblem (com.google.cloud.tools.opensource.classpath.LinkageProblem)3 AnnotatedClassPath (com.google.cloud.tools.opensource.classpath.AnnotatedClassPath)2 ClassPathEntry (com.google.cloud.tools.opensource.classpath.ClassPathEntry)2 DependencyGraph (com.google.cloud.tools.opensource.dependencies.DependencyGraph)2 DependencyPath (com.google.cloud.tools.opensource.dependencies.DependencyPath)2 ImmutableList (com.google.common.collect.ImmutableList)2 File (java.io.File)2 IOException (java.io.IOException)2 ClassFile (com.google.cloud.tools.opensource.classpath.ClassFile)1 ClassNotFoundProblem (com.google.cloud.tools.opensource.classpath.ClassNotFoundProblem)1 ClassPathBuilder (com.google.cloud.tools.opensource.classpath.ClassPathBuilder)1 ClassSymbol (com.google.cloud.tools.opensource.classpath.ClassSymbol)1 LinkageChecker (com.google.cloud.tools.opensource.classpath.LinkageChecker)1 MethodSymbol (com.google.cloud.tools.opensource.classpath.MethodSymbol)1