1 /*
   2  * Copyright (c) 2002, 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 test.astro;
  24 
  25 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
  26 import static javax.xml.xpath.XPathConstants.DOM_OBJECT_MODEL;
  27 import static javax.xml.xpath.XPathConstants.NODESET;
  28 import static jaxp.library.JAXPTestUtilities.filenameToURL;
  29 import static org.testng.Assert.assertEquals;
  30 import static test.astro.AstroConstants.ASTROCAT;
  31 import static test.astro.AstroConstants.JAXP_SCHEMA_LANGUAGE;
  32 import static test.astro.AstroConstants.JAXP_SCHEMA_SOURCE;
  33 
  34 import java.net.MalformedURLException;
  35 import java.util.ArrayList;
  36 import java.util.Iterator;
  37 
  38 import javax.xml.namespace.NamespaceContext;
  39 import javax.xml.namespace.QName;
  40 import javax.xml.parsers.DocumentBuilder;
  41 import javax.xml.parsers.DocumentBuilderFactory;
  42 import javax.xml.xpath.XPath;
  43 import javax.xml.xpath.XPathExpression;
  44 import javax.xml.xpath.XPathExpressionException;
  45 import javax.xml.xpath.XPathFactory;
  46 import javax.xml.xpath.XPathVariableResolver;
  47 
  48 import org.testng.annotations.BeforeClass;
  49 import org.testng.annotations.DataProvider;
  50 import org.testng.annotations.Listeners;
  51 import org.testng.annotations.Test;
  52 import org.w3c.dom.Document;
  53 import org.w3c.dom.NodeList;
  54 import org.xml.sax.InputSource;
  55 
  56 /*
  57  * @test
  58  * @library /javax/xml/jaxp/libs
  59  * @run testng/othervm -DrunSecMngr=true test.astro.XPathAPITest
  60  * @run testng/othervm test.astro.XPathAPITest
  61  * @summary test XPath API
  62  */
  63 @Test(singleThreaded = true)
  64 @Listeners({jaxp.library.FilePolicy.class})
  65 public class XPathAPITest {
  66     private static final String STARDB_STAR_3_CONSTELLATION = "//astro:stardb/astro:star[3]/astro:constellation";
  67     private static final String STARDB_STAR = "//astro:stardb/astro:star";
  68     private Document doc;
  69     private XPathFactory xpf;
  70     private NamespaceContext nsContext;
  71 
  72     @BeforeClass
  73     public void setup() throws Exception {
  74         DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
  75         df.setNamespaceAware(true);
  76         df.setValidating(true);
  77         df.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
  78         df.setAttribute(JAXP_SCHEMA_SOURCE, "catalog.xsd");
  79         DocumentBuilder bldr = df.newDocumentBuilder();
  80         doc = bldr.parse(ASTROCAT);
  81 
  82         xpf = XPathFactory.newInstance(DOM_OBJECT_MODEL);
  83 
  84         nsContext = new MyNamespaceContext();
  85     }
  86 
  87     @DataProvider(name = "nodelist-evaluator")
  88     public Object[][] getNodeListEvaluator() throws MalformedURLException {
  89         return new Object[][] { { (XPathEvaluator) expression -> getXPath().evaluate(expression, doc.getDocumentElement(), NODESET) },
  90                 { (XPathEvaluator) expression -> getXPath().evaluate(expression, createXMLInputSource(), NODESET) },
  91                 { (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(doc.getDocumentElement(), NODESET) },
  92                 { (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(createXMLInputSource(), NODESET) } };
  93     }
  94 
  95     /*
  96      * Test xpath expression evaluations method that returns type indicated by
  97      * QName
  98      */
  99     @Test(dataProvider = "nodelist-evaluator")
 100     public void testEvaluateNodeList(XPathEvaluator evaluator) throws Exception {
 101         NodeList o = (NodeList) evaluator.evaluate(STARDB_STAR);
 102         assertEquals(o.getLength(), 10);
 103     }
 104 
 105     @DataProvider(name = "string-evaluator")
 106     public Object[][] getStringEvaluator() throws MalformedURLException {
 107         return new Object[][] { { (XPathEvaluator) expression -> getXPath().evaluate(expression, doc.getDocumentElement()) },
 108                 { (XPathEvaluator) expression -> getXPath().evaluate(expression, createXMLInputSource()) },
 109                 { (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(doc.getDocumentElement()) },
 110                 { (XPathEvaluator) expression -> getXPathExpression(expression).evaluate(createXMLInputSource()) } };
 111     }
 112 
 113     /*
 114      * Test xpath expression evaluations method that returns String
 115      */
 116     @Test(dataProvider = "string-evaluator")
 117     public void testEvaluateString(XPathEvaluator evaluator) throws Exception {
 118         assertEquals(evaluator.evaluate(STARDB_STAR_3_CONSTELLATION), "Psc");
 119     }
 120 
 121     @Test
 122     public void testXPathVariableResolver() throws Exception {
 123         XPath xpath = getXPath();
 124         xpath.setXPathVariableResolver(new MyXPathVariableResolver());
 125         assertEquals(xpath.evaluate("//astro:stardb/astro:star[astro:hr=$id]/astro:constellation", doc.getDocumentElement()), "Peg");
 126 
 127     }
 128 
 129     private static class MyXPathVariableResolver implements XPathVariableResolver {
 130         public Object resolveVariable(QName vname) {
 131             return "4"; // resolve $id as 4, xpath will locate to star[hr=4]
 132         }
 133     }
 134 
 135     /*
 136      * Implementation of a NamespaceContext interface for the Xpath api tests.
 137      * Used in xpath.setNamespaceContext(...)
 138      */
 139     private static class MyNamespaceContext implements NamespaceContext {
 140         public String getNamespaceURI(String prefix) {
 141             return "astro".equals(prefix) ? "http://www.astro.com/astro" : "";
 142         }
 143 
 144         public String getPrefix(String nsURI) {
 145             return "http://www.astro.com/astro".equals(nsURI) ? "astro" : "";
 146         }
 147 
 148         public Iterator getPrefixes(String nsURI) {
 149             ArrayList list = new ArrayList();
 150             list.add("astro");
 151             return list.iterator();
 152         }
 153     }
 154 
 155     @FunctionalInterface
 156     private interface XPathEvaluator {
 157         Object evaluate(String expression) throws XPathExpressionException;
 158     }
 159 
 160     private XPath getXPath() {
 161         XPath xpath = xpf.newXPath();
 162         xpath.setNamespaceContext(nsContext);
 163         return xpath;
 164     }
 165 
 166     private XPathExpression getXPathExpression(String expression) throws XPathExpressionException {
 167         return getXPath().compile(expression);
 168     }
 169 
 170     private InputSource createXMLInputSource() {
 171         return new InputSource(filenameToURL(ASTROCAT));
 172     }
 173 }