|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | + |
| 21 | +import com.google.cloud.tools.opensource.classpath.ClassPathBuilder; |
| 22 | +import com.google.cloud.tools.opensource.classpath.ClassPathEntry; |
| 23 | +import com.google.cloud.tools.opensource.classpath.ClassPathResult; |
| 24 | +import com.google.cloud.tools.opensource.classpath.DependencyMediation; |
| 25 | +import com.google.cloud.tools.opensource.dependencies.Bom; |
| 26 | +import com.google.cloud.tools.opensource.dependencies.DependencyPath; |
| 27 | +import com.google.common.base.Joiner; |
| 28 | +import com.google.common.collect.ImmutableList; |
| 29 | +import com.google.common.collect.ImmutableSet; |
| 30 | +import java.nio.file.Paths; |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.Set; |
| 33 | +import org.eclipse.aether.artifact.Artifact; |
| 34 | +import org.junit.Test; |
| 35 | + |
| 36 | +/** |
| 37 | + * Test to ensure that certain artifacts in the dependency tree of each entry of the shared |
| 38 | + * dependencies BOM have the same version. |
| 39 | + */ |
| 40 | +public class DependencyConvergenceTest { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testGrpcConvergence() throws Exception { |
| 44 | + // There were cases where the gRPC version set in the shared dependencies BOM and the gRPC |
| 45 | + // version declared in gax-grpc's dependency broke the build of downstream projects. Two |
| 46 | + // artifacts were using version range to specify exact gRPC versions. |
| 47 | + // https://github.com/googleapis/java-shared-dependencies/pull/595 |
| 48 | + Bom bom = Bom.readBom(Paths.get("../pom.xml")); |
| 49 | + assertConvergence( |
| 50 | + bom, |
| 51 | + "io.grpc", |
| 52 | + "grpc-netty-shaded", |
| 53 | + ImmutableSet.of( |
| 54 | + // Because OpenCensus's gRPC version does not use version range notation, it does not |
| 55 | + // break downstream build |
| 56 | + "opencensus-exporter-trace-stackdriver", |
| 57 | + "opencensus-exporter-stats-stackdriver", |
| 58 | + // Because grpc-gcp's gRPC version does not use version range notation, it does not |
| 59 | + // break downstream build |
| 60 | + "grpc-gcp")); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Asserts the artifact specified at {@code groupId} and {@code artifactId} have the same version |
| 65 | + * across the dependency trees of the managed dependencies of {@code bom}. |
| 66 | + * |
| 67 | + * <p>Use {@code excludingArtifactIds} to ignore certain artifacts. |
| 68 | + */ |
| 69 | + private void assertConvergence( |
| 70 | + Bom bom, String groupId, String artifactId, Set<String> excludingArtifactIds) |
| 71 | + throws Exception { |
| 72 | + ImmutableList<Artifact> managedDependencies = bom.getManagedDependencies(); |
| 73 | + |
| 74 | + Set<DependencyPath> foundPaths = new HashSet<>(); |
| 75 | + Set<String> foundVersions = new HashSet<>(); |
| 76 | + for (Artifact managedDependency : managedDependencies) { |
| 77 | + if (excludingArtifactIds.contains(managedDependency.getArtifactId())) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + ClassPathBuilder classPathBuilder = new ClassPathBuilder(); |
| 82 | + ClassPathResult result = |
| 83 | + classPathBuilder.resolve( |
| 84 | + ImmutableList.of(managedDependency), false, DependencyMediation.MAVEN); |
| 85 | + ImmutableList<ClassPathEntry> classPath = result.getClassPath(); |
| 86 | + for (ClassPathEntry entry : classPath) { |
| 87 | + Artifact artifact = entry.getArtifact(); |
| 88 | + if (artifactId.equals(artifact.getArtifactId()) && groupId.equals(artifact.getGroupId())) { |
| 89 | + ImmutableList<DependencyPath> dependencyPaths = result.getDependencyPaths(entry); |
| 90 | + foundPaths.add(dependencyPaths.get(0)); |
| 91 | + foundVersions.add(artifact.getVersion()); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + assertTrue( |
| 97 | + "There should be at least one version in the graph but empty; " |
| 98 | + + "please check the assertion is using correct groupID and artifactID.", |
| 99 | + foundVersions.size() >= 1); |
| 100 | + |
| 101 | + // Duplicate versions found in the dependency trees |
| 102 | + assertTrue( |
| 103 | + "The version for " |
| 104 | + + groupId |
| 105 | + + ":" |
| 106 | + + artifactId |
| 107 | + + " should be one but there are multiple of them: " |
| 108 | + + Joiner.on(", ").join(foundPaths), |
| 109 | + foundVersions.size() <= 1); |
| 110 | + } |
| 111 | +} |
0 commit comments