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 /* 58 * @test 59 * @library /javax/xml/jaxp/libs 60 * @run testng/othervm -DrunSecMngr=true javax.xml.xpath.ptests.XPathExpressionTest 61 * @run testng/othervm javax.xml.xpath.ptests.XPathExpressionTest 62 */ 63 @Listeners({jaxp.library.FilePolicy.class}) 64 public class XPathExpressionTest { 65 /** 66 * Document object for testing XML file. 67 */ 68 private Document document; 69 70 /** 71 * A XPath for evaluation environment and expressions. 72 */ 73 private XPath xpath; 74 75 /** 76 * A QName using default name space. 77 */ 78 private static final QName TEST_QNAME = new QName(XMLConstants.XML_NS_URI, ""); 79 80 /** 81 * XML File Path. 82 */ 83 private static final Path XML_PATH = Paths.get(XML_DIR + "widgets.xml"); 84 85 /** 86 * An expression name which locate at "/widgets/widget[@name='a']/@quantity" 87 */ 88 private static final String EXPRESSION_NAME_A = "/widgets/widget[@name='a']/@quantity"; 89 90 /** 91 * An expression name which locate at "/widgets/widget[@name='b']/@quantity" 92 */ 93 private static final String EXPRESSION_NAME_B = "/widgets/widget[@name='b']/@quantity"; 94 95 /** 96 * Create Document object and XPath object for every time 97 * @throws Exception If any errors occur. 98 */ 99 @BeforeTest 100 public void setup() throws Exception { 101 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile()); 102 xpath = XPathFactory.newInstance().newXPath(); 103 } 104 105 /** 106 * Test for evaluate(java.lang.Object item,QName returnType)throws 107 * XPathExpressionException. 108 * 109 * @throws XPathExpressionException If the expression cannot be evaluated. 110 */ 111 @Test 112 public void testCheckXPathExpression01() throws XPathExpressionException { 113 assertEquals(xpath.compile(EXPRESSION_NAME_A). 114 evaluate(document, STRING), "6"); 115 } 116 117 /** 118 * evaluate(java.lang.Object item,QName returnType) throws NPE if input 119 * source is null. 120 * 121 * @throws XPathExpressionException If the expression cannot be evaluated. 122 */ 123 @Test(expectedExceptions = NullPointerException.class) 124 public void testCheckXPathExpression02() throws XPathExpressionException { 125 xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING); 126 } 127 128 /** 129 * evaluate(java.lang.Object item,QName returnType) throws NPE if returnType 130 * is null. 131 * 132 * @throws XPathExpressionException If the expression cannot be evaluated. 133 */ 134 @Test(expectedExceptions = NullPointerException.class) 135 public void testCheckXPathExpression03() throws XPathExpressionException { 136 xpath.compile(EXPRESSION_NAME_A).evaluate(document, null); 137 } 138 139 /** 140 * Test for method evaluate(java.lang.Object item,QName returnType).If a 141 * request is made to evaluate the expression in the absence of a context 142 * item, simple expressions, such as "1+1", can be evaluated. 143 * 144 * @throws XPathExpressionException If the expression cannot be evaluated. 145 */ 146 @Test 147 public void testCheckXPathExpression04() throws XPathExpressionException { 148 assertEquals(xpath.compile("1+1").evaluate(document, STRING), "2"); 149 } 150 151 /** 152 * evaluate(java.lang.Object item,QName returnType) throws IAE If returnType 153 * is not one of the types defined in XPathConstants. 154 * 155 * @throws XPathExpressionException If the expression cannot be evaluated. 156 */ 157 @Test(expectedExceptions = IllegalArgumentException.class) 158 public void testCheckXPathExpression05() throws XPathExpressionException { 159 xpath.compile(EXPRESSION_NAME_A).evaluate(document, TEST_QNAME); 160 } 161 162 /** 163 * evaluate(java.lang.Object item,QName returnType) return correct boolean 164 * value if returnType is Boolean. 165 * 166 * @throws XPathExpressionException If the expression cannot be evaluated. 167 */ 168 @Test 169 public void testCheckXPathExpression06() throws XPathExpressionException { 170 assertEquals(xpath.compile(EXPRESSION_NAME_A). 171 evaluate(document, BOOLEAN), true); 172 } 173 174 /** 175 * evaluate(java.lang.Object item,QName returnType) return correct boolean 176 * value if returnType is Boolean. 177 * 178 * @throws XPathExpressionException If the expression cannot be evaluated. 179 */ 180 @Test 181 public void testCheckXPathExpression07() throws XPathExpressionException { 182 assertEquals(xpath.compile(EXPRESSION_NAME_B). 183 evaluate(document, BOOLEAN), false); 184 } 185 186 /** 187 * evaluate(java.lang.Object item,QName returnType) return correct number 188 * value when return type is Double. 189 * 190 * @throws XPathExpressionException If the expression cannot be evaluated. 191 */ 192 @Test 193 public void testCheckXPathExpression08() throws XPathExpressionException { 194 assertEquals(xpath.compile(EXPRESSION_NAME_A). 195 evaluate(document, NUMBER), 6d); 196 } 197 198 /** 199 * evaluate(java.lang.Object item,QName returnType) evaluate an attribute 200 * value which returnType is Node. 201 * 202 * @throws XPathExpressionException If the expression cannot be evaluated. 203 */ 204 @Test 205 public void testCheckXPathExpression09() throws XPathExpressionException { 206 Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A). 207 evaluate(document, NODE); 208 assertEquals(attr.getValue(), "6"); 209 } 210 211 /** 212 * evaluate(java.lang.Object item,QName returnType) evaluate an attribute 213 * value which returnType is NodeList. 214 * 215 * @throws XPathExpressionException If the expression cannot be evaluated. 216 */ 217 @Test 218 public void testCheckXPathExpression10() throws XPathExpressionException { 219 NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A). 220 evaluate(document, NODESET); 221 Attr attr = (Attr) nodeList.item(0); 222 assertEquals(attr.getValue(), "6"); 223 } 224 225 /** 226 * Test for evaluate(java.lang.Object item) when returnType is left off of 227 * the XPath.evaluate method, all expressions are evaluated to a String 228 * value. 229 * 230 * @throws XPathExpressionException If the expression cannot be evaluated. 231 */ 232 @Test 233 public void testCheckXPathExpression11() throws XPathExpressionException { 234 assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document), "6"); 235 } 236 237 /** 238 * evaluate(java.lang.Object item) throws NPE if expression is null. 239 * 240 * @throws XPathExpressionException If the expression cannot be evaluated. 241 */ 242 @Test(expectedExceptions = NullPointerException.class) 243 public void testCheckXPathExpression12() throws XPathExpressionException { 244 xpath.compile(null).evaluate(document); 245 } 246 247 /** 248 * evaluate(java.lang.Object item) when a request is made to evaluate the 249 * expression in the absence of a context item, simple expressions, such as 250 * "1+1", can be evaluated. 251 * 252 * @throws XPathExpressionException If the expression cannot be evaluated. 253 */ 254 @Test 255 public void testCheckXPathExpression13() throws XPathExpressionException { 256 assertEquals(xpath.compile("1+1").evaluate(document), "2"); 257 } 258 259 /** 260 * evaluate(java.lang.Object item) throws NPE if document is null. 261 * 262 * @throws XPathExpressionException If the expression cannot be evaluated. 263 */ 264 @Test(expectedExceptions = NullPointerException.class) 265 public void testCheckXPathExpression14() throws XPathExpressionException { 266 xpath.compile(EXPRESSION_NAME_A).evaluate(null); 267 } 268 269 /** 270 * valuate(InputSource source) return a string value if return type is 271 * String. 272 * 273 * @throws Exception If any errors occur. 274 */ 275 @Test 276 public void testCheckXPathExpression15() throws Exception { 277 try (InputStream is = Files.newInputStream(XML_PATH)) { 278 assertEquals(xpath.compile(EXPRESSION_NAME_A). 279 evaluate(new InputSource(is)), "6"); 280 } 281 } 282 283 /** 284 * evaluate(InputSource source) throws NPE if input source is null. 285 * 286 * @throws XPathExpressionException If the expression cannot be evaluated. 287 */ 288 @Test(expectedExceptions = NullPointerException.class) 289 public void testCheckXPathExpression16() throws XPathExpressionException { 290 xpath.compile(EXPRESSION_NAME_A).evaluate(null); 291 } 292 293 /** 294 * evaluate(InputSource source) throws NPE if expression is null. 295 * 296 * @throws Exception If any errors occur. 297 */ 298 @Test(expectedExceptions = NullPointerException.class) 299 public void testCheckXPathExpression17() throws Exception { 300 try (InputStream is = Files.newInputStream(XML_PATH)) { 301 xpath.compile(null).evaluate(new InputSource(is)); 302 } 303 } 304 305 /** 306 * evaluate(InputSource source) throws XPathExpressionException if 307 * returnType is String junk characters. 308 * 309 * @throws Exception If any errors occur. 310 */ 311 @Test(expectedExceptions = XPathExpressionException.class) 312 public void testCheckXPathExpression18() throws Exception { 313 try (InputStream is = Files.newInputStream(XML_PATH)) { 314 xpath.compile("-*&").evaluate(new InputSource(is)); 315 } 316 } 317 318 /** 319 * evaluate(InputSource source) throws XPathExpressionException if 320 * expression is a blank string " ". 321 * 322 * @throws Exception If any errors occur. 323 */ 324 @Test(expectedExceptions = XPathExpressionException.class) 325 public void testCheckXPathExpression19() throws Exception { 326 try (InputStream is = Files.newInputStream(XML_PATH)) { 327 xpath.compile(" ").evaluate(new InputSource(is)); 328 } 329 } 330 331 /** 332 * Test for evaluate(InputSource source,QName returnType) returns a string 333 * value if returnType is String. 334 * 335 * @throws Exception If any errors occur. 336 */ 337 @Test 338 public void testCheckXPathExpression20() throws Exception { 339 try (InputStream is = Files.newInputStream(XML_PATH)) { 340 assertEquals(xpath.compile(EXPRESSION_NAME_A). 341 evaluate(new InputSource(is), STRING), "6"); 342 } 343 } 344 345 /** 346 * evaluate(InputSource source,QName returnType) throws NPE if source is 347 * null. 348 * 349 * @throws XPathExpressionException If the expression cannot be evaluated. 350 */ 351 @Test(expectedExceptions = NullPointerException.class) 352 public void testCheckXPathExpression21() throws XPathExpressionException { 353 xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING); 354 } 355 356 /** 357 * evaluate(InputSource source,QName returnType) throws NPE if expression is 358 * null. 359 * 360 * @throws Exception If any errors occur. 361 */ 362 @Test(expectedExceptions = NullPointerException.class) 363 public void testCheckXPathExpression22() throws Exception { 364 try (InputStream is = Files.newInputStream(XML_PATH)) { 365 xpath.compile(null).evaluate(new InputSource(is), STRING); 366 } 367 } 368 369 /** 370 * evaluate(InputSource source,QName returnType) throws NPE if returnType is 371 * null. 372 * 373 * @throws Exception If any errors occur. 374 */ 375 @Test(expectedExceptions = NullPointerException.class) 376 public void testCheckXPathExpression23() throws Exception { 377 try (InputStream is = Files.newInputStream(XML_PATH)) { 378 xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), null); 379 } 380 } 381 382 /** 383 * evaluate(InputSource source,QName returnType) throws 384 * XPathExpressionException if expression is junk characters. 385 * 386 * @throws Exception If any errors occur. 387 */ 388 @Test(expectedExceptions = XPathExpressionException.class) 389 public void testCheckXPathExpression24() throws Exception { 390 try (InputStream is = Files.newInputStream(XML_PATH)) { 391 xpath.compile("-*&").evaluate(new InputSource(is), STRING); 392 } 393 } 394 395 /** 396 * evaluate(InputSource source,QName returnType) throws 397 * XPathExpressionException if expression is blank " ". 398 * 399 * @throws Exception If any errors occur. 400 */ 401 @Test(expectedExceptions = XPathExpressionException.class) 402 public void testCheckXPathExpression25() throws Exception { 403 try (InputStream is = Files.newInputStream(XML_PATH)) { 404 xpath.compile(" ").evaluate(new InputSource(is), STRING); 405 } 406 } 407 408 /** 409 * evaluate(InputSource source,QName returnType) throws 410 * IllegalArgumentException if returnType is not one of the types defined 411 * in XPathConstants. 412 * 413 * @throws Exception If any errors occur. 414 */ 415 @Test(expectedExceptions = IllegalArgumentException.class) 416 public void testCheckXPathExpression26() throws Exception { 417 try (InputStream is = Files.newInputStream(XML_PATH)) { 418 xpath.compile(EXPRESSION_NAME_A).evaluate(new InputSource(is), TEST_QNAME); 419 } 420 } 421 422 /** 423 * evaluate(InputSource source,QName returnType) return a correct boolean 424 * value if returnType is Boolean. 425 * 426 * @throws Exception If any errors occur. 427 */ 428 @Test 429 public void testCheckXPathExpression27() throws Exception { 430 try (InputStream is = Files.newInputStream(XML_PATH)) { 431 assertEquals(xpath.compile(EXPRESSION_NAME_A). 432 evaluate(new InputSource(is), BOOLEAN), true); 433 } 434 } 435 436 /** 437 * evaluate(InputSource source,QName returnType) return a correct boolean 438 * value if returnType is Boolean. 439 * 440 * @throws Exception If any errors occur. 441 */ 442 @Test 443 public void testCheckXPathExpression28() throws Exception { 444 try (InputStream is = Files.newInputStream(XML_PATH)) { 445 assertEquals(xpath.compile(EXPRESSION_NAME_B). 446 evaluate(new InputSource(is), BOOLEAN), false); 447 } 448 } 449 450 /** 451 * evaluate(InputSource source,QName returnType) return a correct number 452 * value if returnType is Number. 453 * 454 * @throws Exception If any errors occur. 455 */ 456 @Test 457 public void testCheckXPathExpression29() throws Exception { 458 try (InputStream is = Files.newInputStream(XML_PATH)) { 459 assertEquals(xpath.compile(EXPRESSION_NAME_A). 460 evaluate(new InputSource(is), NUMBER), 6d); 461 } 462 } 463 464 /** 465 * Test for evaluate(InputSource source,QName returnType) returns a node if 466 * returnType is Node. 467 * 468 * @throws Exception If any errors occur. 469 */ 470 @Test 471 public void testCheckXPathExpression30() throws Exception { 472 try (InputStream is = Files.newInputStream(XML_PATH)) { 473 Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A). 474 evaluate(new InputSource(is), NODE); 475 assertEquals(attr.getValue(), "6"); 476 } 477 } 478 479 /** 480 * Test for evaluate(InputSource source,QName returnType) return a node list 481 * if returnType is NodeList. 482 * 483 * @throws Exception If any errors occur. 484 */ 485 @Test 486 public void testCheckXPathExpression31() throws Exception { 487 try (InputStream is = Files.newInputStream(XML_PATH)) { 488 NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A). 489 evaluate(new InputSource(is), NODESET); 490 assertEquals(((Attr) nodeList.item(0)).getValue(), "6"); 491 } 492 } 493 } 494 495