/**
  * Test method for {@link
  * org.jenkinsci.plugins.neoload_integration.supporting.XMLUtilities#getMap(org.w3c.dom.NamedNodeMap)}.
  *
  * @throws XPathExpressionException
  */
 @Test
 public void testGetMap() throws XPathExpressionException {
   final Map<String, String> map =
       XMLUtilities.getMap(
           XMLUtilities.findFirstByExpression("/bookstore/book/title", d).getAttributes());
   assertTrue(map.size() > 0);
 }
 /**
  * Test method for {@link
  * org.jenkinsci.plugins.neoload_integration.supporting.XMLUtilities#findByExpression(java.lang.String,
  * org.w3c.dom.Node)}.
  *
  * @throws XPathExpressionException
  * @throws IOException
  * @throws SAXException
  * @throws ParserConfigurationException
  */
 @Test
 public void testFindByExpression() throws XPathExpressionException {
   assertTrue(XMLUtilities.findByExpression("/bookstore/book/title", d).size() == 4);
   assertTrue(
       "Everyday Italian"
           .equals(
               XMLUtilities.findByExpression("/bookstore/book[1]/title", d)
                   .get(0)
                   .getTextContent()));
 }
 /**
  * Test method for {@link
  * org.jenkinsci.plugins.neoload_integration.supporting.XMLUtilities#findFirstByExpression(java.lang.String,
  * org.w3c.dom.Node)}.
  *
  * @throws XPathExpressionException
  * @throws DOMException
  * @throws IOException
  * @throws SAXException
  * @throws ParserConfigurationException
  */
 @Test
 public void testFindFirstByExpression()
     throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
   assertTrue(
       "Everyday Italian"
           .equals(
               XMLUtilities.findFirstByExpression("/bookstore/book/title", d).getTextContent()));
   assertTrue(
       XMLUtilities.findFirstByExpression(
               "/bookstore/book/title", XMLUtilities.createNodeFromText("<empty></empty>"))
           == null);
 }
 /* (non-Javadoc)
  * @see junit.framework.TestCase#setUp()
  */
 @Override
 @Before
 protected void setUp() throws Exception {
   super.setUp();
   url = this.getClass().getResource("books.xml");
   d = XMLUtilities.readXmlFile(url.getFile());
 }
  /**
   * It is expected that a Docbook 5 book or article will define the namespaces at the root element.
   * But for validation against a single topic, we need to add these namespaces in in order for the
   * dtd to validate.
   *
   * @param xml The source xml
   * @return the xml with the docbook 5 namespaces added
   */
  public static String addDocBook50Namespaces(@NotNull final String xml) {
    final String rootEleName = XMLUtilities.getRootElementName(xml);
    final String fixedRootEleName = rootEleName == null ? "section" : rootEleName;
    final MatchResult result =
        RegExp.compile("^([\\s\\S]*?)<\\s*" + fixedRootEleName + "(\\s*.*?)>").exec(xml);
    if (result != null) {
      final StringBuilder retValue = new StringBuilder(result.getGroup(1));
      retValue.append("<" + fixedRootEleName);

      // Clean out the normal attributes
      String fixedAttributes = result.getGroup(2);
      // Remove any current namespace declaration
      fixedAttributes = fixedAttributes.replaceFirst(" xmlns\\s*=\\s*('|\").*?('|\")", "");
      // Remove any current version declaration
      fixedAttributes = fixedAttributes.replaceFirst(" version\\s*=\\s*('|\").*?('|\")", "");
      // Remove any current xlink namespace declaration
      fixedAttributes = fixedAttributes.replaceFirst(" xmlns:xlink\\s*=\\s*('|\").*?('|\")", "");

      // Add the generic attributes
      retValue.append(" xmlns=\"http://docbook.org/ns/docbook\"");
      retValue.append(" version=\"5.0\"");
      if (!rootEleName.equalsIgnoreCase("info")) {
        // Info is not allowed to have xlink declared for the DocBook 5.0 DTD
        retValue.append(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"");
      }

      retValue.append(fixedAttributes + ">");
      return xml.replace(result.getGroup(0), retValue.toString());
    }

    return xml;
  }
  /**
   * Test method for {@link
   * org.jenkinsci.plugins.neoload_integration.supporting.XMLUtilities#createNodeFromText(java.lang.String)}.
   *
   * @throws IOException
   * @throws SAXException
   * @throws ParserConfigurationException
   */
  @Test
  public void testCreateNodeFromText()
      throws ParserConfigurationException, SAXException, IOException {
    final String content = "weeeeeeeeeee";
    final Node n = XMLUtilities.createNodeFromText("<test>" + content + "</test>");

    assertTrue(content.equals(n.getTextContent()));
  }
  /**
   * Check to ensure that a docbook row has the required number of columns for a table.
   *
   * @param row The DOM row element to be checked.
   * @param numColumns The number of entry elements that should exist in the row.
   * @return True if the row has the required number of entries, otherwise false.
   */
  public static boolean validateTableRow(final Node row, final int numColumns) {
    assert row != null;
    assert row.getNodeName().equals("row") || row.getNodeName().equals("tr");

    if (row.getNodeName().equals("row")) {
      final List<Node> entries = XMLUtilities.getDirectChildNodes(row, "entry");
      final List<Node> entryTbls = XMLUtilities.getDirectChildNodes(row, "entrytbl");

      if ((entries.size() + entryTbls.size()) <= numColumns) {
        for (final Node entryTbl : entryTbls) {
          if (!validateEntryTbl((Element) entryTbl)) return false;
        }
        return true;
      } else {
        return false;
      }
    } else {
      final List<Node> nodes = XMLUtilities.getDirectChildNodes(row, "td", "th");

      return nodes.size() <= numColumns;
    }
  }
  /**
   * Check to ensure that a Docbook tgroup isn't missing an row entries, using number of cols
   * defined for the tgroup.
   *
   * @param tgroup The DOM tgroup element to be checked.
   * @return True if the tgroup has the required number of entries, otherwise false.
   */
  public static boolean validateTableGroup(final Element tgroup) {
    assert tgroup != null;
    assert tgroup.getNodeName().equals("tgroup");

    final Integer numColumns = Integer.parseInt(tgroup.getAttribute("cols"));

    // Check that all the thead, tbody and tfoot elements have the correct number of entries.
    final List<Node> nodes = XMLUtilities.getDirectChildNodes(tgroup, "thead", "tbody", "tfoot");
    for (final Node ele : nodes) {
      // Find all child nodes that are a row
      final NodeList children = ele.getChildNodes();
      for (int i = 0; i < children.getLength(); i++) {
        final Node node = children.item(i);
        if (node.getNodeName().equals("row") || node.getNodeName().equals("tr")) {
          if (!validateTableRow(node, numColumns)) return false;
        }
      }
    }

    return true;
  }
 /**
  * Test method for {@link
  * org.jenkinsci.plugins.neoload_integration.supporting.XMLUtilities#toList(org.w3c.dom.NodeList)}.
  *
  * @throws XPathExpressionException
  */
 @Test
 public void testToList() throws XPathExpressionException {
   final List<Node> list = XMLUtilities.findByExpression("/bookstore/book/title", d);
   assertTrue(list.size() > 0);
 }