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