1 /*
   2  * Copyright (c) 2016, 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 xpath;
  24 
  25 import java.io.ByteArrayInputStream;
  26 import java.math.BigDecimal;
  27 import java.math.BigInteger;
  28 import java.util.concurrent.atomic.AtomicInteger;
  29 import java.util.concurrent.atomic.AtomicLong;
  30 
  31 import javax.xml.parsers.DocumentBuilderFactory;
  32 import javax.xml.xpath.XPath;
  33 import javax.xml.xpath.XPathFactory;
  34 import static javax.xml.xpath.XPathConstants.BOOLEAN;
  35 import static javax.xml.xpath.XPathConstants.NUMBER;
  36 import static javax.xml.xpath.XPathConstants.STRING;
  37 import static javax.xml.xpath.XPathConstants.NODE;
  38 import static javax.xml.xpath.XPathConstants.NODESET;
  39 import javax.xml.xpath.XPathNodes;
  40 import javax.xml.xpath.XPathEvaluationResult;
  41 
  42 import static org.testng.Assert.assertEquals;
  43 import static org.testng.Assert.assertFalse;
  44 import static org.testng.Assert.assertTrue;
  45 
  46 import org.testng.annotations.DataProvider;
  47 import org.w3c.dom.Document;
  48 import org.w3c.dom.Node;
  49 
  50 /*
  51  * Base class for XPath test
  52  */
  53 class XPathTestBase {
  54 
  55     static final String rawXML
  56             = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
  57             + "<Customers>"
  58             + "    <Customer id=\"1\">"
  59             + "        <Name>name1</Name>"
  60             + "        <Phone>1111111111</Phone>"
  61             + "        <Email>123@xyz.com</Email>"
  62             + "        <Address>"
  63             + "            <Street>1111 111st ave</Street>"
  64             + "            <City>The City</City>"
  65             + "            <State>The State</State>"
  66             + "        </Address>"
  67             + "    </Customer>"
  68             + "    <Customer id=\"2\">"
  69             + "        <Name>name1</Name>"
  70             + "        <Phone>2222222222</Phone>"
  71             + "        <Email>123@xyz.com</Email>"
  72             + "        <Address>"
  73             + "            <Street>2222 222nd ave</Street>"
  74             + "            <City>The City</City>"
  75             + "            <State>The State</State>"
  76             + "        </Address>"
  77             + "    </Customer>"
  78             + "    <Customer id=\"3\">"
  79             + "        <Name>name1</Name>"
  80             + "        <Phone>3333333333</Phone>"
  81             + "        <Email>123@xyz.com</Email>"
  82             + "        <Address>"
  83             + "            <Street>3333 333rd ave</Street>"
  84             + "            <City>The City</City>"
  85             + "            <State>The State</State>"
  86             + "        </Address>"
  87             + "    </Customer>"
  88             + "</Customers>";
  89 
  90     void verifyResult(XPathEvaluationResult<?> result, Object expected) {
  91         switch (result.type()) {
  92             case BOOLEAN:
  93                 assertTrue(((Boolean) result.value()).equals(expected));
  94                 return;
  95             case NUMBER:
  96                 assertTrue(((Double) result.value()).equals(expected));
  97                 return;
  98             case STRING:
  99                 assertTrue(((String) result.value()).equals(expected));
 100                 return;
 101             case NODESET:
 102                 XPathNodes nodes = (XPathNodes) result.value();
 103                 for (Node n : nodes) {
 104                     assertEquals(n.getLocalName(), expected);
 105                 }
 106                 return;
 107             case NODE:
 108                 assertTrue(((Node) result.value()).getLocalName().equals(expected));
 109                 return;
 110         }
 111         assertFalse(true, "Unsupported type");
 112     }
 113 
 114     /*
 115      * DataProvider: XPath object
 116      */
 117     @DataProvider(name = "xpath")
 118     Object[][] getXPath() {
 119         return new Object[][]{{XPathFactory.newInstance().newXPath()}};
 120     }
 121 
 122     /*
 123      * DataProvider: Numeric types not supported
 124      */
 125     @DataProvider(name = "invalidNumericTypes")
 126     Object[][] getInvalidNumericTypes() {
 127         XPath xpath = XPathFactory.newInstance().newXPath();
 128         return new Object[][]{{xpath, AtomicInteger.class},
 129             {xpath, AtomicInteger.class},
 130             {xpath, AtomicLong.class},
 131             {xpath, BigDecimal.class},
 132             {xpath, BigInteger.class},
 133             {xpath, Byte.class},
 134             {xpath, Float.class},
 135             {xpath, Short.class}
 136         };
 137     }
 138 
 139     /*
 140      * DataProvider: XPath and Document objects
 141      */
 142     @DataProvider(name = "document")
 143     Object[][] getDocument() throws Exception {
 144         DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
 145         dBF.setValidating(false);
 146         dBF.setNamespaceAware(true);
 147         Document doc = dBF.newDocumentBuilder().parse(
 148                 new ByteArrayInputStream(rawXML.getBytes("UTF-8")));
 149 
 150         return new Object[][]{{XPathFactory.newInstance().newXPath(), doc}};
 151     }
 152 }