< prev index next >

test/javax/xml/jaxp/functional/javax/xml/xpath/ptests/XPathExpressionTest.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 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 
  24 package javax.xml.xpath.ptests;
  25 
  26 import java.io.FilePermission;







  27 import java.io.InputStream;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;

  31 import javax.xml.XMLConstants;
  32 import javax.xml.namespace.QName;
  33 import javax.xml.parsers.DocumentBuilderFactory;
  34 import javax.xml.xpath.XPath;
  35 import static javax.xml.xpath.XPathConstants.BOOLEAN;
  36 import static javax.xml.xpath.XPathConstants.NODE;
  37 import static javax.xml.xpath.XPathConstants.NODESET;
  38 import static javax.xml.xpath.XPathConstants.NUMBER;
  39 import static javax.xml.xpath.XPathConstants.STRING;
  40 import javax.xml.xpath.XPathExpressionException;
  41 import javax.xml.xpath.XPathFactory;
  42 import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR;
  43 import jaxp.library.JAXPFileReadOnlyBaseTest;
  44 import static org.testng.Assert.assertEquals;
  45 import org.testng.annotations.BeforeTest;

  46 import org.testng.annotations.Test;
  47 import org.w3c.dom.Attr;
  48 import org.w3c.dom.Document;
  49 import org.w3c.dom.NodeList;
  50 import org.xml.sax.InputSource;
  51 
  52 /**
  53  * Class containing the test cases for XPathExpression API.
  54  */
  55 public class XPathExpressionTest extends JAXPFileReadOnlyBaseTest {

  56     /**
  57      * Document object for testing XML file.
  58      */
  59     private Document document;
  60 
  61     /**
  62      * A XPath for evaluation environment and expressions.
  63      */
  64     private XPath xpath;
  65 
  66     /**
  67      * A QName using default name space.
  68      */
  69     private static final QName TEST_QNAME = new QName(XMLConstants.XML_NS_URI, "");
  70 
  71     /**
  72      * XML File Path.
  73      */
  74     private static final Path XML_PATH = Paths.get(XML_DIR + "widgets.xml");
  75 
  76     /**
  77      * An expression name which locate at "/widgets/widget[@name='a']/@quantity"
  78      */
  79     private static final String EXPRESSION_NAME_A = "/widgets/widget[@name='a']/@quantity";
  80 
  81     /**
  82      * An expression name which locate at "/widgets/widget[@name='b']/@quantity"
  83      */
  84     private static final String EXPRESSION_NAME_B = "/widgets/widget[@name='b']/@quantity";
  85 
  86     /**
  87      * Create Document object and XPath object for every time
  88      * @throws Exception If any errors occur.
  89      */
  90     @BeforeTest
  91     public void setup() throws Exception {
  92         setPermissions(new FilePermission(XML_PATH.toFile().toString(), "read"));
  93         document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile());
  94         xpath = XPathFactory.newInstance().newXPath();
  95     }
  96 
  97     /**
  98      * Test for evaluate(java.lang.Object item,QName returnType)throws
  99      * XPathExpressionException.
 100      *
 101      * @throws XPathExpressionException If the expression cannot be evaluated.
 102      */
 103     @Test
 104     public void testCheckXPathExpression01() throws XPathExpressionException {
 105         assertEquals(xpath.compile(EXPRESSION_NAME_A).
 106                 evaluate(document, STRING), "6");
 107     }
 108 
 109     /**
 110      * evaluate(java.lang.Object item,QName returnType) throws NPE if input
 111      * source is null.
 112      *


 247     public void testCheckXPathExpression13() throws XPathExpressionException {
 248         assertEquals(xpath.compile("1+1").evaluate(document), "2");
 249     }
 250 
 251     /**
 252      * evaluate(java.lang.Object item) throws NPE if document is null.
 253      *
 254      * @throws XPathExpressionException If the expression cannot be evaluated.
 255      */
 256     @Test(expectedExceptions = NullPointerException.class)
 257     public void testCheckXPathExpression14() throws XPathExpressionException {
 258         xpath.compile(EXPRESSION_NAME_A).evaluate(null);
 259     }
 260 
 261     /**
 262      * valuate(InputSource source) return a string value if return type is
 263      * String.
 264      *
 265      * @throws Exception If any errors occur.
 266      */
 267     @Test (groups = {"readLocalFiles"})
 268     public void testCheckXPathExpression15() throws Exception {
 269         try (InputStream is = Files.newInputStream(XML_PATH)) {
 270             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 271                     evaluate(new InputSource(is)), "6");
 272         }
 273     }
 274 
 275     /**
 276      * evaluate(InputSource source) throws NPE if input source is null.
 277      *
 278      * @throws XPathExpressionException If the expression cannot be evaluated.
 279      */
 280     @Test(expectedExceptions = NullPointerException.class)
 281     public void testCheckXPathExpression16() throws XPathExpressionException {
 282         xpath.compile(EXPRESSION_NAME_A).evaluate(null);
 283     }
 284 
 285     /**
 286      * evaluate(InputSource source) throws NPE if expression is null.
 287      *
 288      * @throws Exception If any errors occur.
 289      */
 290     @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
 291     public void testCheckXPathExpression17() throws Exception {
 292         try (InputStream is = Files.newInputStream(XML_PATH)) {
 293             xpath.compile(null).evaluate(new InputSource(is));
 294         }
 295     }
 296 
 297     /**
 298      * evaluate(InputSource source) throws XPathExpressionException if
 299      * returnType is String junk characters.
 300      *
 301      * @throws Exception If any errors occur.
 302      */
 303     @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
 304     public void testCheckXPathExpression18() throws Exception {
 305         try (InputStream is = Files.newInputStream(XML_PATH)) {
 306             xpath.compile("-*&").evaluate(new InputSource(is));
 307         }
 308     }
 309 
 310     /**
 311      * evaluate(InputSource source) throws XPathExpressionException if
 312      * expression is a blank string " ".
 313      *
 314      * @throws Exception If any errors occur.
 315      */
 316     @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
 317     public void testCheckXPathExpression19() throws Exception {
 318         try (InputStream is = Files.newInputStream(XML_PATH)) {
 319             xpath.compile(" ").evaluate(new InputSource(is));
 320         }
 321     }
 322 
 323     /**
 324      * Test for evaluate(InputSource source,QName returnType) returns a string
 325      * value if returnType is String.
 326      *
 327      * @throws Exception If any errors occur.
 328      */
 329     @Test(groups = {"readLocalFiles"})
 330     public void testCheckXPathExpression20() throws Exception {
 331         try (InputStream is = Files.newInputStream(XML_PATH)) {
 332             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 333                 evaluate(new InputSource(is), STRING), "6");
 334         }
 335     }
 336 
 337     /**
 338      * evaluate(InputSource source,QName returnType) throws NPE if source is
 339      * null.
 340      *
 341      * @throws XPathExpressionException If the expression cannot be evaluated.
 342      */
 343     @Test(expectedExceptions = NullPointerException.class)
 344     public void testCheckXPathExpression21() throws XPathExpressionException {
 345         xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING);
 346     }
 347 
 348     /**
 349      * evaluate(InputSource source,QName returnType) throws NPE if expression is
 350      * null.
 351      *
 352      * @throws Exception If any errors occur.
 353      */
 354     @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
 355     public void testCheckXPathExpression22() throws Exception {
 356         try (InputStream is = Files.newInputStream(XML_PATH)) {
 357             xpath.compile(null).evaluate(new InputSource(is), STRING);
 358         }
 359     }
 360 
 361     /**
 362      * evaluate(InputSource source,QName returnType) throws NPE if returnType is
 363      * null.
 364      *
 365      * @throws Exception If any errors occur.
 366      */
 367     @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
 368     public void testCheckXPathExpression23() throws Exception {
 369         try (InputStream is = Files.newInputStream(XML_PATH)) {
 370             xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), null);
 371         }
 372     }
 373 
 374     /**
 375      * evaluate(InputSource source,QName returnType) throws
 376      * XPathExpressionException if expression is junk characters.
 377      *
 378      * @throws Exception If any errors occur.
 379      */
 380     @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
 381     public void testCheckXPathExpression24() throws Exception {
 382         try (InputStream is = Files.newInputStream(XML_PATH)) {
 383             xpath.compile("-*&").evaluate(new InputSource(is), STRING);
 384         }
 385     }
 386 
 387     /**
 388      * evaluate(InputSource source,QName returnType) throws
 389      * XPathExpressionException if expression is blank " ".
 390      *
 391      * @throws Exception If any errors occur.
 392      */
 393     @Test(groups = {"readLocalFiles"}, expectedExceptions = XPathExpressionException.class)
 394     public void testCheckXPathExpression25() throws Exception {
 395         try (InputStream is = Files.newInputStream(XML_PATH)) {
 396             xpath.compile(" ").evaluate(new InputSource(is), STRING);
 397         }
 398     }
 399 
 400     /**
 401      * evaluate(InputSource source,QName returnType) throws
 402      * IllegalArgumentException if returnType is not one of the types defined
 403      * in XPathConstants.
 404      *
 405      * @throws Exception If any errors occur.
 406      */
 407     @Test(groups = {"readLocalFiles"}, expectedExceptions = IllegalArgumentException.class)
 408     public void testCheckXPathExpression26() throws Exception {
 409         try (InputStream is = Files.newInputStream(XML_PATH)) {
 410             xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), TEST_QNAME);
 411         }
 412     }
 413 
 414     /**
 415      * evaluate(InputSource source,QName returnType) return a correct boolean
 416      * value if returnType is Boolean.
 417      *
 418      * @throws Exception If any errors occur.
 419      */
 420     @Test(groups = {"readLocalFiles"})
 421     public void testCheckXPathExpression27() throws Exception {
 422         try (InputStream is = Files.newInputStream(XML_PATH)) {
 423             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 424                 evaluate(new InputSource(is), BOOLEAN), true);
 425         }
 426     }
 427 
 428     /**
 429      * evaluate(InputSource source,QName returnType) return a correct boolean
 430      * value if returnType is Boolean.
 431      *
 432      * @throws Exception If any errors occur.
 433      */
 434     @Test(groups = {"readLocalFiles"})
 435     public void testCheckXPathExpression28() throws Exception {
 436         try (InputStream is = Files.newInputStream(XML_PATH)) {
 437             assertEquals(xpath.compile(EXPRESSION_NAME_B).
 438                 evaluate(new InputSource(is), BOOLEAN), false);
 439         }
 440     }
 441 
 442     /**
 443      * evaluate(InputSource source,QName returnType) return a correct number
 444      * value if returnType is Number.
 445      *
 446      * @throws Exception If any errors occur.
 447      */
 448     @Test(groups = {"readLocalFiles"})
 449     public void testCheckXPathExpression29() throws Exception {
 450         try (InputStream is = Files.newInputStream(XML_PATH)) {
 451             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 452                 evaluate(new InputSource(is), NUMBER), 6d);
 453         }
 454     }
 455 
 456     /**
 457      * Test for evaluate(InputSource source,QName returnType) returns a node if
 458      * returnType is Node.
 459      *
 460      * @throws Exception If any errors occur.
 461      */
 462     @Test(groups = {"readLocalFiles"})
 463     public void testCheckXPathExpression30() throws Exception {
 464         try (InputStream is = Files.newInputStream(XML_PATH)) {
 465             Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A).
 466                 evaluate(new InputSource(is), NODE);
 467             assertEquals(attr.getValue(), "6");
 468         }
 469     }
 470 
 471     /**
 472      * Test for evaluate(InputSource source,QName returnType) return a node list
 473      * if returnType is NodeList.
 474      *
 475      * @throws Exception If any errors occur.
 476      */
 477     @Test(groups = {"readLocalFiles"})
 478     public void testCheckXPathExpression31() throws Exception {
 479         try (InputStream is = Files.newInputStream(XML_PATH)) {
 480             NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A).
 481                 evaluate(new InputSource(is), NODESET);
 482             assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
 483         }
 484     }
 485 }
   1 /*
   2  * Copyright (c) 2003, 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 
  24 package javax.xml.xpath.ptests;
  25 
  26 import static javax.xml.xpath.XPathConstants.BOOLEAN;
  27 import static javax.xml.xpath.XPathConstants.NODE;
  28 import static javax.xml.xpath.XPathConstants.NODESET;
  29 import static javax.xml.xpath.XPathConstants.NUMBER;
  30 import static javax.xml.xpath.XPathConstants.STRING;
  31 import static javax.xml.xpath.ptests.XPathTestConst.XML_DIR;
  32 import static org.testng.Assert.assertEquals;
  33 
  34 import java.io.InputStream;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 
  39 import javax.xml.XMLConstants;
  40 import javax.xml.namespace.QName;
  41 import javax.xml.parsers.DocumentBuilderFactory;
  42 import javax.xml.xpath.XPath;





  43 import javax.xml.xpath.XPathExpressionException;
  44 import javax.xml.xpath.XPathFactory;
  45 


  46 import org.testng.annotations.BeforeTest;
  47 import org.testng.annotations.Listeners;
  48 import org.testng.annotations.Test;
  49 import org.w3c.dom.Attr;
  50 import org.w3c.dom.Document;
  51 import org.w3c.dom.NodeList;
  52 import org.xml.sax.InputSource;
  53 
  54 /**
  55  * Class containing the test cases for XPathExpression API.
  56  */
  57 @Listeners({jaxp.library.FilePolicy.class})
  58 public class XPathExpressionTest {
  59     /**
  60      * Document object for testing XML file.
  61      */
  62     private Document document;
  63 
  64     /**
  65      * A XPath for evaluation environment and expressions.
  66      */
  67     private XPath xpath;
  68 
  69     /**
  70      * A QName using default name space.
  71      */
  72     private static final QName TEST_QNAME = new QName(XMLConstants.XML_NS_URI, "");
  73 
  74     /**
  75      * XML File Path.
  76      */
  77     private static final Path XML_PATH = Paths.get(XML_DIR + "widgets.xml");
  78 
  79     /**
  80      * An expression name which locate at "/widgets/widget[@name='a']/@quantity"
  81      */
  82     private static final String EXPRESSION_NAME_A = "/widgets/widget[@name='a']/@quantity";
  83 
  84     /**
  85      * An expression name which locate at "/widgets/widget[@name='b']/@quantity"
  86      */
  87     private static final String EXPRESSION_NAME_B = "/widgets/widget[@name='b']/@quantity";
  88 
  89     /**
  90      * Create Document object and XPath object for every time
  91      * @throws Exception If any errors occur.
  92      */
  93     @BeforeTest
  94     public void setup() throws Exception {

  95         document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile());
  96         xpath = XPathFactory.newInstance().newXPath();
  97     }
  98 
  99     /**
 100      * Test for evaluate(java.lang.Object item,QName returnType)throws
 101      * XPathExpressionException.
 102      *
 103      * @throws XPathExpressionException If the expression cannot be evaluated.
 104      */
 105     @Test
 106     public void testCheckXPathExpression01() throws XPathExpressionException {
 107         assertEquals(xpath.compile(EXPRESSION_NAME_A).
 108                 evaluate(document, STRING), "6");
 109     }
 110 
 111     /**
 112      * evaluate(java.lang.Object item,QName returnType) throws NPE if input
 113      * source is null.
 114      *


 249     public void testCheckXPathExpression13() throws XPathExpressionException {
 250         assertEquals(xpath.compile("1+1").evaluate(document), "2");
 251     }
 252 
 253     /**
 254      * evaluate(java.lang.Object item) throws NPE if document is null.
 255      *
 256      * @throws XPathExpressionException If the expression cannot be evaluated.
 257      */
 258     @Test(expectedExceptions = NullPointerException.class)
 259     public void testCheckXPathExpression14() throws XPathExpressionException {
 260         xpath.compile(EXPRESSION_NAME_A).evaluate(null);
 261     }
 262 
 263     /**
 264      * valuate(InputSource source) return a string value if return type is
 265      * String.
 266      *
 267      * @throws Exception If any errors occur.
 268      */
 269     @Test 
 270     public void testCheckXPathExpression15() throws Exception {
 271         try (InputStream is = Files.newInputStream(XML_PATH)) {
 272             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 273                     evaluate(new InputSource(is)), "6");
 274         }
 275     }
 276 
 277     /**
 278      * evaluate(InputSource source) throws NPE if input source is null.
 279      *
 280      * @throws XPathExpressionException If the expression cannot be evaluated.
 281      */
 282     @Test(expectedExceptions = NullPointerException.class)
 283     public void testCheckXPathExpression16() throws XPathExpressionException {
 284         xpath.compile(EXPRESSION_NAME_A).evaluate(null);
 285     }
 286 
 287     /**
 288      * evaluate(InputSource source) throws NPE if expression is null.
 289      *
 290      * @throws Exception If any errors occur.
 291      */
 292     @Test(expectedExceptions = NullPointerException.class)
 293     public void testCheckXPathExpression17() throws Exception {
 294         try (InputStream is = Files.newInputStream(XML_PATH)) {
 295             xpath.compile(null).evaluate(new InputSource(is));
 296         }
 297     }
 298 
 299     /**
 300      * evaluate(InputSource source) throws XPathExpressionException if
 301      * returnType is String junk characters.
 302      *
 303      * @throws Exception If any errors occur.
 304      */
 305     @Test(expectedExceptions = XPathExpressionException.class)
 306     public void testCheckXPathExpression18() throws Exception {
 307         try (InputStream is = Files.newInputStream(XML_PATH)) {
 308             xpath.compile("-*&").evaluate(new InputSource(is));
 309         }
 310     }
 311 
 312     /**
 313      * evaluate(InputSource source) throws XPathExpressionException if
 314      * expression is a blank string " ".
 315      *
 316      * @throws Exception If any errors occur.
 317      */
 318     @Test(expectedExceptions = XPathExpressionException.class)
 319     public void testCheckXPathExpression19() throws Exception {
 320         try (InputStream is = Files.newInputStream(XML_PATH)) {
 321             xpath.compile(" ").evaluate(new InputSource(is));
 322         }
 323     }
 324 
 325     /**
 326      * Test for evaluate(InputSource source,QName returnType) returns a string
 327      * value if returnType is String.
 328      *
 329      * @throws Exception If any errors occur.
 330      */
 331     @Test
 332     public void testCheckXPathExpression20() throws Exception {
 333         try (InputStream is = Files.newInputStream(XML_PATH)) {
 334             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 335                 evaluate(new InputSource(is), STRING), "6");
 336         }
 337     }
 338 
 339     /**
 340      * evaluate(InputSource source,QName returnType) throws NPE if source is
 341      * null.
 342      *
 343      * @throws XPathExpressionException If the expression cannot be evaluated.
 344      */
 345     @Test(expectedExceptions = NullPointerException.class)
 346     public void testCheckXPathExpression21() throws XPathExpressionException {
 347         xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING);
 348     }
 349 
 350     /**
 351      * evaluate(InputSource source,QName returnType) throws NPE if expression is
 352      * null.
 353      *
 354      * @throws Exception If any errors occur.
 355      */
 356     @Test(expectedExceptions = NullPointerException.class)
 357     public void testCheckXPathExpression22() throws Exception {
 358         try (InputStream is = Files.newInputStream(XML_PATH)) {
 359             xpath.compile(null).evaluate(new InputSource(is), STRING);
 360         }
 361     }
 362 
 363     /**
 364      * evaluate(InputSource source,QName returnType) throws NPE if returnType is
 365      * null.
 366      *
 367      * @throws Exception If any errors occur.
 368      */
 369     @Test(expectedExceptions = NullPointerException.class)
 370     public void testCheckXPathExpression23() throws Exception {
 371         try (InputStream is = Files.newInputStream(XML_PATH)) {
 372             xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), null);
 373         }
 374     }
 375 
 376     /**
 377      * evaluate(InputSource source,QName returnType) throws
 378      * XPathExpressionException if expression is junk characters.
 379      *
 380      * @throws Exception If any errors occur.
 381      */
 382     @Test(expectedExceptions = XPathExpressionException.class)
 383     public void testCheckXPathExpression24() throws Exception {
 384         try (InputStream is = Files.newInputStream(XML_PATH)) {
 385             xpath.compile("-*&").evaluate(new InputSource(is), STRING);
 386         }
 387     }
 388 
 389     /**
 390      * evaluate(InputSource source,QName returnType) throws
 391      * XPathExpressionException if expression is blank " ".
 392      *
 393      * @throws Exception If any errors occur.
 394      */
 395     @Test(expectedExceptions = XPathExpressionException.class)
 396     public void testCheckXPathExpression25() throws Exception {
 397         try (InputStream is = Files.newInputStream(XML_PATH)) {
 398             xpath.compile(" ").evaluate(new InputSource(is), STRING);
 399         }
 400     }
 401 
 402     /**
 403      * evaluate(InputSource source,QName returnType) throws
 404      * IllegalArgumentException if returnType is not one of the types defined
 405      * in XPathConstants.
 406      *
 407      * @throws Exception If any errors occur.
 408      */
 409     @Test(expectedExceptions = IllegalArgumentException.class)
 410     public void testCheckXPathExpression26() throws Exception {
 411         try (InputStream is = Files.newInputStream(XML_PATH)) {
 412             xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), TEST_QNAME);
 413         }
 414     }
 415 
 416     /**
 417      * evaluate(InputSource source,QName returnType) return a correct boolean
 418      * value if returnType is Boolean.
 419      *
 420      * @throws Exception If any errors occur.
 421      */
 422     @Test
 423     public void testCheckXPathExpression27() throws Exception {
 424         try (InputStream is = Files.newInputStream(XML_PATH)) {
 425             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 426                 evaluate(new InputSource(is), BOOLEAN), true);
 427         }
 428     }
 429 
 430     /**
 431      * evaluate(InputSource source,QName returnType) return a correct boolean
 432      * value if returnType is Boolean.
 433      *
 434      * @throws Exception If any errors occur.
 435      */
 436     @Test
 437     public void testCheckXPathExpression28() throws Exception {
 438         try (InputStream is = Files.newInputStream(XML_PATH)) {
 439             assertEquals(xpath.compile(EXPRESSION_NAME_B).
 440                 evaluate(new InputSource(is), BOOLEAN), false);
 441         }
 442     }
 443 
 444     /**
 445      * evaluate(InputSource source,QName returnType) return a correct number
 446      * value if returnType is Number.
 447      *
 448      * @throws Exception If any errors occur.
 449      */
 450     @Test
 451     public void testCheckXPathExpression29() throws Exception {
 452         try (InputStream is = Files.newInputStream(XML_PATH)) {
 453             assertEquals(xpath.compile(EXPRESSION_NAME_A).
 454                 evaluate(new InputSource(is), NUMBER), 6d);
 455         }
 456     }
 457 
 458     /**
 459      * Test for evaluate(InputSource source,QName returnType) returns a node if
 460      * returnType is Node.
 461      *
 462      * @throws Exception If any errors occur.
 463      */
 464     @Test
 465     public void testCheckXPathExpression30() throws Exception {
 466         try (InputStream is = Files.newInputStream(XML_PATH)) {
 467             Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A).
 468                 evaluate(new InputSource(is), NODE);
 469             assertEquals(attr.getValue(), "6");
 470         }
 471     }
 472 
 473     /**
 474      * Test for evaluate(InputSource source,QName returnType) return a node list
 475      * if returnType is NodeList.
 476      *
 477      * @throws Exception If any errors occur.
 478      */
 479     @Test
 480     public void testCheckXPathExpression31() throws Exception {
 481         try (InputStream is = Files.newInputStream(XML_PATH)) {
 482             NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A).
 483                 evaluate(new InputSource(is), NODESET);
 484             assertEquals(((Attr) nodeList.item(0)).getValue(), "6");
 485         }
 486     }
 487 }
< prev index next >