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());
}
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");
}
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());
}
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();
}
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();
}
Aggregations