XPathExpressionTest.java revision 687:e7736286abe1
198766Smarkm/* 298766Smarkm * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. 398766Smarkm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 498766Smarkm * 598766Smarkm * This code is free software; you can redistribute it and/or modify it 698766Smarkm * under the terms of the GNU General Public License version 2 only, as 798766Smarkm * published by the Free Software Foundation. 898766Smarkm * 998766Smarkm * This code is distributed in the hope that it will be useful, but WITHOUT 1098766Smarkm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1198766Smarkm * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1298766Smarkm * version 2 for more details (a copy is included in the LICENSE file that 1398766Smarkm * accompanied this code). 1498766Smarkm * 1598766Smarkm * You should have received a copy of the GNU General Public License version 1698766Smarkm * 2 along with this work; if not, write to the Free Software Foundation, 1798766Smarkm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1898766Smarkm * 1998766Smarkm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2098766Smarkm * or visit www.oracle.com if you need additional information or have any 2198766Smarkm * questions. 2298766Smarkm */ 2398766Smarkm 24179433Sflzpackage javax.xml.xpath.ptests; 25179433Sflz 26179433Sflzimport java.io.FilePermission; 27179433Sflzimport java.io.InputStream; 28222035Sflzimport java.nio.file.Files; 2998766Smarkmimport java.nio.file.Path; 3098766Smarkmimport java.nio.file.Paths; 3198766Smarkmimport javax.xml.XMLConstants; 3298766Smarkmimport javax.xml.namespace.QName; 3398766Smarkmimport javax.xml.parsers.DocumentBuilderFactory; 34148435Skrionimport javax.xml.xpath.XPath; 35131275Seikimport static javax.xml.xpath.XPathConstants.BOOLEAN; 36146559Scpercivaimport static javax.xml.xpath.XPathConstants.NODE; 37148435Skrionimport static javax.xml.xpath.XPathConstants.NODESET; 3898766Smarkmimport static javax.xml.xpath.XPathConstants.NUMBER; 39173412Skevloimport static javax.xml.xpath.XPathConstants.STRING; 4098766Smarkmimport javax.xml.xpath.XPathExpressionException; 41179433Sflzimport javax.xml.xpath.XPathFactory; 42179433Sflzimport static javax.xml.xpath.ptests.XPathTestConst.XML_DIR; 43179433Sflzimport jaxp.library.JAXPFileReadOnlyBaseTest; 44179433Sflzimport static org.testng.Assert.assertEquals; 45179433Sflzimport org.testng.annotations.BeforeTest; 46179433Sflzimport org.testng.annotations.Test; 47179433Sflzimport org.w3c.dom.Attr; 48179433Sflzimport org.w3c.dom.Document; 49179433Sflzimport org.w3c.dom.NodeList; 50179433Sflzimport org.xml.sax.InputSource; 51179433Sflz 52179433Sflz/** 53179433Sflz * Class containing the test cases for XPathExpression API. 54179433Sflz */ 55179433Sflzpublic class XPathExpressionTest extends JAXPFileReadOnlyBaseTest { 5698766Smarkm /** 5798766Smarkm * Document object for testing XML file. 5898766Smarkm */ 5998766Smarkm private Document document; 6098766Smarkm 6198766Smarkm /** 6298766Smarkm * A XPath for evaluation environment and expressions. 6398766Smarkm */ 6498766Smarkm private XPath xpath; 6598766Smarkm 66131275Seik /** 67131275Seik * A QName using default name space. 68131275Seik */ 69131275Seik private static final QName TEST_QNAME = new QName(XMLConstants.XML_NS_URI, ""); 70179433Sflz 7198766Smarkm /** 7298766Smarkm * XML File Path. 73159554Sobrien */ 7498766Smarkm private static final Path XML_PATH = Paths.get(XML_DIR + "widgets.xml"); 7598766Smarkm 76146559Scperciva /** 77146559Scperciva * An expression name which locate at "/widgets/widget[@name='a']/@quantity" 78146559Scperciva */ 79146559Scperciva private static final String EXPRESSION_NAME_A = "/widgets/widget[@name='a']/@quantity"; 8098766Smarkm 8198766Smarkm /** 8298766Smarkm * An expression name which locate at "/widgets/widget[@name='b']/@quantity" 8398766Smarkm */ 8498766Smarkm private static final String EXPRESSION_NAME_B = "/widgets/widget[@name='b']/@quantity"; 8598766Smarkm 8698766Smarkm /** 8798766Smarkm * Create Document object and XPath object for every time 88136643Sobrien * @throws Exception If any errors occur. 89136643Sobrien */ 90136643Sobrien @BeforeTest 91136643Sobrien public void setup() throws Exception { 9298766Smarkm setPermissions(new FilePermission(XML_PATH.toFile().toString(), "read")); 9398766Smarkm document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(XML_PATH.toFile()); 9498766Smarkm xpath = XPathFactory.newInstance().newXPath(); 9598766Smarkm } 96148435Skrion 97148435Skrion /** 98148435Skrion * Test for evaluate(java.lang.Object item,QName returnType)throws 99148435Skrion * XPathExpressionException. 100148435Skrion * 101148435Skrion * @throws XPathExpressionException If the expression cannot be evaluated. 102148435Skrion */ 103148435Skrion @Test 10498766Smarkm public void testCheckXPathExpression01() throws XPathExpressionException { 10598766Smarkm assertEquals(xpath.compile(EXPRESSION_NAME_A). 10698766Smarkm evaluate(document, STRING), "6"); 10798766Smarkm } 108131275Seik 109131275Seik /** 110131275Seik * evaluate(java.lang.Object item,QName returnType) throws NPE if input 111131275Seik * source is null. 112131275Seik * 113131275Seik * @throws XPathExpressionException If the expression cannot be evaluated. 114131275Seik */ 115131275Seik @Test(expectedExceptions = NullPointerException.class) 11698766Smarkm public void testCheckXPathExpression02() throws XPathExpressionException { 11798766Smarkm xpath.compile(EXPRESSION_NAME_A).evaluate(null, STRING); 11898766Smarkm } 11998766Smarkm 12098766Smarkm /** 12198766Smarkm * evaluate(java.lang.Object item,QName returnType) throws NPE if returnType 12298766Smarkm * is null. 12398766Smarkm * 12498766Smarkm * @throws XPathExpressionException If the expression cannot be evaluated. 12598766Smarkm */ 12698766Smarkm @Test(expectedExceptions = NullPointerException.class) 12798766Smarkm public void testCheckXPathExpression03() throws XPathExpressionException { 12898766Smarkm xpath.compile(EXPRESSION_NAME_A).evaluate(document, null); 12998766Smarkm } 130201226Sed 13198766Smarkm /** 132131275Seik * Test for method evaluate(java.lang.Object item,QName returnType).If a 133148435Skrion * request is made to evaluate the expression in the absence of a context 134131275Seik * item, simple expressions, such as "1+1", can be evaluated. 135131275Seik * 13698766Smarkm * @throws XPathExpressionException If the expression cannot be evaluated. 13798766Smarkm */ 138 @Test 139 public void testCheckXPathExpression04() throws XPathExpressionException { 140 assertEquals(xpath.compile("1+1").evaluate(document, STRING), "2"); 141 } 142 143 /** 144 * evaluate(java.lang.Object item,QName returnType) throws IAE If returnType 145 * is not one of the types defined in XPathConstants. 146 * 147 * @throws XPathExpressionException If the expression cannot be evaluated. 148 */ 149 @Test(expectedExceptions = IllegalArgumentException.class) 150 public void testCheckXPathExpression05() throws XPathExpressionException { 151 xpath.compile(EXPRESSION_NAME_A).evaluate(document, TEST_QNAME); 152 } 153 154 /** 155 * evaluate(java.lang.Object item,QName returnType) return correct boolean 156 * value if returnType is Boolean. 157 * 158 * @throws XPathExpressionException If the expression cannot be evaluated. 159 */ 160 @Test 161 public void testCheckXPathExpression06() throws XPathExpressionException { 162 assertEquals(xpath.compile(EXPRESSION_NAME_A). 163 evaluate(document, BOOLEAN), true); 164 } 165 166 /** 167 * evaluate(java.lang.Object item,QName returnType) return correct boolean 168 * value if returnType is Boolean. 169 * 170 * @throws XPathExpressionException If the expression cannot be evaluated. 171 */ 172 @Test 173 public void testCheckXPathExpression07() throws XPathExpressionException { 174 assertEquals(xpath.compile(EXPRESSION_NAME_B). 175 evaluate(document, BOOLEAN), false); 176 } 177 178 /** 179 * evaluate(java.lang.Object item,QName returnType) return correct number 180 * value when return type is Double. 181 * 182 * @throws XPathExpressionException If the expression cannot be evaluated. 183 */ 184 @Test 185 public void testCheckXPathExpression08() throws XPathExpressionException { 186 assertEquals(xpath.compile(EXPRESSION_NAME_A). 187 evaluate(document, NUMBER), 6d); 188 } 189 190 /** 191 * evaluate(java.lang.Object item,QName returnType) evaluate an attribute 192 * value which returnType is Node. 193 * 194 * @throws XPathExpressionException If the expression cannot be evaluated. 195 */ 196 @Test 197 public void testCheckXPathExpression09() throws XPathExpressionException { 198 Attr attr = (Attr) xpath.compile(EXPRESSION_NAME_A). 199 evaluate(document, NODE); 200 assertEquals(attr.getValue(), "6"); 201 } 202 203 /** 204 * evaluate(java.lang.Object item,QName returnType) evaluate an attribute 205 * value which returnType is NodeList. 206 * 207 * @throws XPathExpressionException If the expression cannot be evaluated. 208 */ 209 @Test 210 public void testCheckXPathExpression10() throws XPathExpressionException { 211 NodeList nodeList = (NodeList) xpath.compile(EXPRESSION_NAME_A). 212 evaluate(document, NODESET); 213 Attr attr = (Attr) nodeList.item(0); 214 assertEquals(attr.getValue(), "6"); 215 } 216 217 /** 218 * Test for evaluate(java.lang.Object item) when returnType is left off of 219 * the XPath.evaluate method, all expressions are evaluated to a String 220 * value. 221 * 222 * @throws XPathExpressionException If the expression cannot be evaluated. 223 */ 224 @Test 225 public void testCheckXPathExpression11() throws XPathExpressionException { 226 assertEquals(xpath.compile(EXPRESSION_NAME_A).evaluate(document), "6"); 227 } 228 229 /** 230 * evaluate(java.lang.Object item) throws NPE if expression is null. 231 * 232 * @throws XPathExpressionException If the expression cannot be evaluated. 233 */ 234 @Test(expectedExceptions = NullPointerException.class) 235 public void testCheckXPathExpression12() throws XPathExpressionException { 236 xpath.compile(null).evaluate(document); 237 } 238 239 /** 240 * evaluate(java.lang.Object item) when a request is made to evaluate the 241 * expression in the absence of a context item, simple expressions, such as 242 * "1+1", can be evaluated. 243 * 244 * @throws XPathExpressionException If the expression cannot be evaluated. 245 */ 246 @Test 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} 486