Java Code Examples for nu.xom.Document#query()

The following examples show how to use nu.xom.Document#query() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FreemarkerTest.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testVersionIndex()
    throws IOException, TemplateException, URISyntaxException, ParsingException {
  Path output =
      DashboardMain.generateVersionIndex(
          "com.google.cloud",
          "libraries-bom",
          ImmutableList.of("1.0.0", "2.0.0", "2.1.0-SNAPSHOT"));
  Assert.assertTrue(
      output.endsWith(Paths.get("target", "com.google.cloud", "libraries-bom", "index.html")));
  Assert.assertTrue(Files.isRegularFile(output));

  Document document = builder.build(output.toFile());
  Nodes links = document.query("//a/@href");
  Assert.assertEquals(3, links.size());
  Node snapshotLink = links.get(2);
  // 2.1.0-SNAPSHOT has directory 'snapshot'
  Assert.assertEquals("snapshot/index.html", snapshotLink.getValue());
}
 
Example 2
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testComponent_linkageCheckResult() throws IOException, ParsingException {
  Document document = parseOutputFile(
      "com.google.http-client_google-http-client-appengine_1.29.1.html");
  Nodes reports = document.query("//p[@class='jar-linkage-report']");
  Assert.assertEquals(1, reports.size());
  Truth.assertThat(trimAndCollapseWhiteSpace(reports.get(0).getValue()))
      .isEqualTo("91 target classes causing linkage errors referenced from 501 source classes.");

  Nodes causes = document.query("//p[@class='jar-linkage-report-cause']");
  Truth.assertWithMessage(
          "google-http-client-appengine should show linkage errors for RpcStubDescriptor")
      .that(causes)
      .comparingElementsUsing(NODE_VALUES)
      .contains(
          "Class com.google.net.rpc3.client.RpcStubDescriptor is not found,"
              + " referenced from 21 classes ▶"); // '▶' is the toggle button
}
 
Example 3
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testComponent_failure() throws IOException, ParsingException {
  Document document = parseOutputFile(
      "com.google.api.grpc_grpc-google-common-protos_1.14.0.html");

  // com.google.api.grpc:grpc-google-common-protos:1.14.0 has no green section
  Nodes greens = document.query("//h3[@style='color: green']");
  Assert.assertEquals(0, greens.size());

  // "Global Upper Bounds Fixes", "Upper Bounds Fixes", and "Suggested Dependency Updates" are red
  Nodes reds = document.query("//h3[@style='color: red']");
  Assert.assertEquals(3, reds.size());
  Nodes presDependencyMediation =
      document.query("//pre[@class='suggested-dependency-mediation']");
  Assert.assertTrue(
      "For failed component, suggested dependency should be shown",
      presDependencyMediation.size() >= 1);
  Nodes dependencyTree = document.query("//p[@class='dependency-tree-node']");
  Assert.assertTrue(
      "Dependency Tree should be shown in dashboard even when FAILED",
      dependencyTree.size() > 0);
}
 
Example 4
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGlobalUpperBoundUpgradeMessage() throws IOException, ParsingException {
  // Case 1: BOM needs to be updated
  Document document = parseOutputFile("com.google.protobuf_protobuf-java-util_3.6.1.html");
  Nodes globalUpperBoundBomUpgradeNodes =
      document.query("//li[@class='global-upper-bound-bom-upgrade']");
  Truth.assertThat(globalUpperBoundBomUpgradeNodes.size()).isEqualTo(1);
  String bomUpgradeMessage = globalUpperBoundBomUpgradeNodes.get(0).getValue();
  Truth.assertThat(bomUpgradeMessage).contains(
      "Upgrade com.google.protobuf:protobuf-java-util:jar:3.6.1 in the BOM to version \"3.7.1\"");

  // Case 2: Dependency needs to be updated
  Nodes globalUpperBoundDependencyUpgradeNodes =
      document.query("//li[@class='global-upper-bound-dependency-upgrade']");

  // The artifact report should contain the following 6 global upper bound dependency upgrades:
  //   Upgrade com.google.guava:guava:jar:19.0 to version "27.1-android"
  //   Upgrade com.google.protobuf:protobuf-java:jar:3.6.1 to version "3.7.1"
  Truth.assertThat(globalUpperBoundDependencyUpgradeNodes.size()).isEqualTo(2);
  String dependencyUpgradeMessage = globalUpperBoundDependencyUpgradeNodes.get(0).getValue();
  Truth.assertThat(dependencyUpgradeMessage).contains(
      "Upgrade com.google.guava:guava:jar:19.0 to version \"27.1-android\"");
}
 
Example 5
Source File: XmlEntity.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Use the CacheXmlGenerator to create XML from the entity associated with
 * the current cache.
 * 
 * @param element Name of the XML element to search for.
 * @param attributes
 *          Attributes of the element that should match, for example "name" or
 *          "id" and the value they should equal.
 * 
 * @return XML string representation of the entity.
 */
private final String loadXmlDefinition(final String element, final Map<String, String> attributes) {
  final Cache cache = CacheFactory.getAnyInstance();
  Document document = null;

  try {
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    CacheXmlGenerator.generate(cache, printWriter, false, false, false);
    printWriter.close();

    // Remove the DOCTYPE line when getting the string to prevent
    // errors when trying to locate the DTD.
    String xml = stringWriter.toString().replaceFirst("<!DOCTYPE.*>", "");
    document = new Builder(false).build(xml, null);

  } catch (ValidityException vex) {
    cache.getLogger().error("Could not parse XML when creating XMLEntity", vex);

  } catch (ParsingException pex) {
    cache.getLogger().error("Could not parse XML when creating XMLEntity", pex);
    
  } catch (IOException ioex) {
    cache.getLogger().error("Could not parse XML when creating XMLEntity", ioex);
  }
  
  Nodes nodes = document.query(createQueryString(element, attributes));
  if (nodes.size() != 0) {
    return nodes.get(0).toXML();
  }
  
  cache.getLogger().warning("No XML definition could be found with name=" + element + " and attributes=" + attributes);
  return null;
}
 
Example 6
Source File: XmlUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static Node getNode(Document doc, String entityType, String entityName) {
  String query = createQueryStringByName(entityType, entityName);
  Nodes nodes = doc.query(query);
  if (nodes != null && nodes.size() > 0) {
    return nodes.get(0);
  } 
  return null;
}
 
Example 7
Source File: XmlEntity.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Use the CacheXmlGenerator to create XML from the entity associated with
 * the current cache.
 * 
 * @param element Name of the XML element to search for.
 * @param attributes
 *          Attributes of the element that should match, for example "name" or
 *          "id" and the value they should equal.
 * 
 * @return XML string representation of the entity.
 */
private final String loadXmlDefinition(final String element, final Map<String, String> attributes) {
  final Cache cache = CacheFactory.getAnyInstance();
  Document document = null;

  try {
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    CacheXmlGenerator.generate(cache, printWriter, false, false, false);
    printWriter.close();

    // Remove the DOCTYPE line when getting the string to prevent
    // errors when trying to locate the DTD.
    String xml = stringWriter.toString().replaceFirst("<!DOCTYPE.*>", "");
    document = new Builder(false).build(xml, null);

  } catch (ValidityException vex) {
    cache.getLogger().error("Could not parse XML when creating XMLEntity", vex);

  } catch (ParsingException pex) {
    cache.getLogger().error("Could not parse XML when creating XMLEntity", pex);
    
  } catch (IOException ioex) {
    cache.getLogger().error("Could not parse XML when creating XMLEntity", ioex);
  }
  
  Nodes nodes = document.query(createQueryString(element, attributes));
  if (nodes.size() != 0) {
    return nodes.get(0).toXML();
  }
  
  cache.getLogger().warning("No XML definition could be found with name=" + element + " and attributes=" + attributes);
  return null;
}
 
Example 8
Source File: XmlUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static Node getNode(Document doc, String entityType, String entityName) {
  String query = createQueryStringByName(entityType, entityName);
  Nodes nodes = doc.query(query);
  if (nodes != null && nodes.size() > 0) {
    return nodes.get(0);
  } 
  return null;
}
 
Example 9
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDependencyTrees() throws IOException, ParsingException {
  Document document = parseOutputFile("dependency_trees.html");
  Nodes dependencyTreeParagraph = document.query("//p[@class='dependency-tree-node']");

  Assert.assertEquals(53144, dependencyTreeParagraph.size());
  Assert.assertEquals(
      "com.google.protobuf:protobuf-java:jar:3.6.1", dependencyTreeParagraph.get(0).getValue());
}
 
Example 10
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBomCoordinatesInUnstableArtifacts() throws IOException, ParsingException {
  Document document = parseOutputFile("unstable_artifacts.html");
  Nodes bomCoordinatesNodes = document.query("//p[@class='bom-coordinates']");
  Assert.assertEquals(1, bomCoordinatesNodes.size());
  Assert.assertEquals(
      "BOM: com.google.cloud:libraries-bom:1.0.0", bomCoordinatesNodes.get(0).getValue());
}
 
Example 11
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBomCoordinatesInArtifactDetails() throws IOException, ParsingException {
  Document document = parseOutputFile("artifact_details.html");
  Nodes bomCoordinatesNodes = document.query("//p[@class='bom-coordinates']");
  Assert.assertEquals(1, bomCoordinatesNodes.size());
  Assert.assertEquals(
      "BOM: com.google.cloud:libraries-bom:1.0.0", bomCoordinatesNodes.get(0).getValue());
}
 
Example 12
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBomCoordinatesInComponent() throws IOException, ParsingException {
  Document document = parseOutputFile("com.google.protobuf_protobuf-java-util_3.6.1.html");
  Nodes bomCoordinatesNodes = document.query("//p[@class='bom-coordinates']");
  Assert.assertEquals(1, bomCoordinatesNodes.size());
  Assert.assertEquals(
      "BOM: com.google.cloud:libraries-bom:1.0.0", bomCoordinatesNodes.get(0).getValue());
}
 
Example 13
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testZeroLinkageErrorShowsZero() throws IOException, ParsingException {
  // grpc-auth does not have a linkage error, and it should show zero in the section
  Document document = parseOutputFile("io.grpc_grpc-auth_1.20.0.html");
  Nodes linkageErrorsTotal = document.query("//p[@id='linkage-errors-total']");
  Truth.assertThat(linkageErrorsTotal.size()).isEqualTo(1);
  Truth.assertThat(linkageErrorsTotal.get(0).getValue())
      .contains("0 linkage error(s)");
}
 
Example 14
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testLinkageErrors_ensureNoDuplicateSymbols() throws IOException, ParsingException {
  Document document =
      parseOutputFile("com.google.http-client_google-http-client-appengine_1.29.1.html");
  Nodes linkageCheckMessages = document.query("//p[@class='jar-linkage-report-cause']");
  Truth.assertThat(linkageCheckMessages.size()).isGreaterThan(0);

  List<String> messages = new ArrayList<>();
  for (int i = 0; i < linkageCheckMessages.size(); ++i) {
    messages.add(linkageCheckMessages.get(i).getValue());
  }

  // When uniqueness of SymbolProblem and Symbol classes are incorrect, dashboard has duplicates.
  Truth.assertThat(messages).containsNoDuplicates();
}
 
Example 15
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testLinkageErrorsInProvidedDependency() throws IOException, ParsingException {
  // google-http-client-appengine has provided dependency to (problematic) appengine-api-1.0-sdk
  Document document = parseOutputFile(
      "com.google.http-client_google-http-client-appengine_1.29.1.html");
  Nodes linkageCheckMessages = document.query("//ul[@class='jar-linkage-report-cause']/li");
  Truth.assertThat(linkageCheckMessages.size()).isGreaterThan(0);
  Truth.assertThat(linkageCheckMessages.get(0).getValue())
      .contains("com.google.appengine.api.appidentity.AppIdentityServicePb");
}
 
Example 16
Source File: DashboardTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testComponent_success() throws IOException, ParsingException {
  Document document = parseOutputFile(
      "com.google.api.grpc_proto-google-common-protos_1.14.0.html");
  Nodes greens = document.query("//h3[@style='color: green']");
  Assert.assertTrue(greens.size() >= 2);
  Nodes presDependencyMediation =
      document.query("//pre[@class='suggested-dependency-mediation']");
  // There's a pre tag for dependency
  Assert.assertEquals(1, presDependencyMediation.size());

  Nodes presDependencyTree = document.query("//p[@class='dependency-tree-node']");
  Assert.assertTrue(
      "Dependency Tree should be shown in dashboard", presDependencyTree.size() > 0);
}
 
Example 17
Source File: FreemarkerTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@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(LinkedListMultimap.create(), 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");
}
 
Example 18
Source File: DashboardUnavailableArtifactTest.java    From cloud-opensource-java with Apache License 2.0 4 votes vote down vote up
@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(LinkedListMultimap.create(), 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");
}