1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package javax.xml.xpath;
  24 
  25 import java.io.ByteArrayInputStream;
  26 import javax.xml.parsers.DocumentBuilderFactory;
  27 import static org.testng.Assert.assertEquals;
  28 import static org.testng.Assert.assertFalse;
  29 import static org.testng.Assert.assertTrue;
  30 import org.testng.annotations.DataProvider;
  31 import org.w3c.dom.Document;
  32 import org.w3c.dom.Node;
  33 
  34 /*
  35  * Base class for XPath test
  36  */
  37 class XPathTestBase {
  38 
  39     static final String rawXML
  40             = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
  41             + "<Customers>"
  42             + "    <Customer id=\"1\">"
  43             + "        <Name>name1</Name>"
  44             + "        <Phone>1111111111</Phone>"
  45             + "        <Email>123@xyz.com</Email>"
  46             + "        <Address>"
  47             + "            <Street>1111 111st ave</Street>"
  48             + "            <City>The City</City>"
  49             + "            <State>The State</State>"
  50             + "        </Address>"
  51             + "    </Customer>"
  52             + "    <Customer id=\"2\">"
  53             + "        <Name>name1</Name>"
  54             + "        <Phone>2222222222</Phone>"
  55             + "        <Email>123@xyz.com</Email>"
  56             + "        <Address>"
  57             + "            <Street>2222 222nd ave</Street>"
  58             + "            <City>The City</City>"
  59             + "            <State>The State</State>"
  60             + "        </Address>"
  61             + "    </Customer>"
  62             + "    <Customer id=\"3\">"
  63             + "        <Name>name1</Name>"
  64             + "        <Phone>3333333333</Phone>"
  65             + "        <Email>123@xyz.com</Email>"
  66             + "        <Address>"
  67             + "            <Street>3333 333rd ave</Street>"
  68             + "            <City>The City</City>"
  69             + "            <State>The State</State>"
  70             + "        </Address>"
  71             + "    </Customer>"
  72             + "</Customers>";
  73 
  74     void verifyResult(XPathEvaluationResult<?> result, Object expected) {
  75         switch (result.type()) {
  76             case BOOLEAN:
  77                 assertTrue(((Boolean) result.value()).equals(expected));
  78                 return;
  79             case NUMBER:
  80                 assertTrue(((Double) result.value()).equals(expected));
  81                 return;
  82             case STRING:
  83                 assertTrue(((String) result.value()).equals(expected));
  84                 return;
  85             case NODESET:
  86                 XPathNodes nodes = (XPathNodes) result.value();
  87                 for (Node n : nodes) {
  88                     assertEquals(n.getLocalName(), expected);
  89                 }
  90                 return;
  91             case NODE:
  92                 assertTrue(((Node) result.value()).getLocalName().equals(expected));
  93                 return;
  94         }
  95         assertFalse(true, "Unsupported type");
  96     }
  97 
  98     /*
  99      * DataProvider: XPath object
 100      */
 101     @DataProvider(name = "xpath")
 102     Object[][] getXPath() {
 103         return new Object[][]{{XPathFactory.newInstance().newXPath()}};
 104     }
 105 
 106     /*
 107      * DataProvider: XPath and Document objects
 108      */
 109     @DataProvider(name = "document")
 110     Object[][] getDocument() throws Exception {
 111         DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
 112         dBF.setValidating(false);
 113         dBF.setNamespaceAware(true);
 114         Document doc = dBF.newDocumentBuilder().parse(
 115                 new ByteArrayInputStream(rawXML.getBytes("UTF-8")));
 116 
 117         return new Object[][]{{XPathFactory.newInstance().newXPath(), doc}};
 118     }
 119 }