Duplicate :
|
XPathExpression.evaluate(java.lang.Object item,QName returnType)should throw XPathExpressionException when item is null and expression refers to the context According to spec : A null value for item indicates that the expression should be evaluated without a context . In the absence of a context item, simple expressions, such as "1+1", can be evaluated . Any expression that refers to the context will throw an XPathExpressionException ------------------------------------ java -version java version "1.5.0-beta2" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b38) Java HotSpot(TM) Client VM (build 1.5.0-beta2-b38, mixed mode) javac Test12.java java Test12 checkXPathExpression02() : failed - expected XPathExpressionException not thrown :: checkXPathExpression14() : failed - expected XPathExpressionException not thrown :: ------------------------------------ Testcase - Test12.java *********************** ------------------------------------------------------------------------------- import javax.xml.xpath.XPath; import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactoryConfigurationException; import javax.xml.xpath.XPathExpressionException; import javax.xml.namespace.QName; import org.w3c.dom.Document; import org.xml.sax.InputSource; public class Test12 { Document document1 = null; Test12 () {} // main() public static void main(String[] argv) { Test12 xpathExpression01 = new Test12(); xpathExpression01.checkXPathExpression02(); xpathExpression01.checkXPathExpression14(); }// end main() // test for evaluate(java.lang.Object item,QName returnType)throws XPathExpressionException // item is null , A null value for item indicates that the expression should be evaluated without a context. // In the absence of a context item, simple expressions, such as "1+1", can be evaluated. // Any expression that refers to the context will throw an XPathExpressionException private void checkXPathExpression02() { try{ XPathFactory xpathFactory = XPathFactory.newInstance(); if( xpathFactory instanceof XPathFactory ){ XPath xpath = xpathFactory.newXPath(); if ( xpath instanceof XPath ){ String expression = "/widgets/widget[@name='a']/@quantity" ; XPathExpression xpathExpression = xpath.compile(expression); String quantity = (String)xpathExpression.evaluate(document1, XPathConstants.STRING); System.out.println("checkXPathExpression02() : failed - expected XPathExpressionException not thrown :"+quantity+":"); } else{ System.out.println("checkXPathExpression02() failed - xpath not instance of XPath"); } }else{ System.out.println(" checkXPathExpression02() failed - creating instance of XPathFactory "); } }catch(NullPointerException npe ){ System.out.println(" checkXPathExpression02() - failed , NPE thrown "); }catch(XPathFactoryConfigurationException xpfce ){ System.out.println(" checkXPathExpression02() - failed , Default object model not available "); }catch(XPathExpressionException xpee ){ System.out.println(" checkXPathExpression02() - passed , expected XPathExpressionException thrown "); }catch(Exception e){ System.out.println(" Exception thrown - checkXPathExpression02() failed "); } } // test for evaluate(java.lang.Object item)throws XPathExpressionException // when returnType is left off of the XPath.evaluate method, all expressions are evaluated to a String value. // item is null , A null value for item indicates that the expression should be evaluated without a context. // In the absence of a context item, simple expressions, such as "1+1", can be evaluated. // Any expression that refers to the context will throw an XPathExpressionException private void checkXPathExpression14() { try{ XPathFactory xpathFactory = XPathFactory.newInstance(); if( xpathFactory instanceof XPathFactory ){ XPath xpath = xpathFactory.newXPath(); if ( xpath instanceof XPath ){ String expression = "/widgets/widget[@name='a']/@quantity" ; XPathExpression xpathExpression = xpath.compile(expression); String quantity = (String)xpathExpression.evaluate(document1 ); System.out.println("checkXPathExpression14() : failed - expected XPathExpressionException not thrown :"+quantity+":"); } else{ System.out.println("checkXPathExpression14() failed - xpath not instance of XPath"); } }else{ System.out.println(" checkXPathExpression14() failed - creating instance of XPathFactory "); } }catch(NullPointerException npe ){ System.out.println(" checkXPathExpression14() - failed , NPE thrown "); }catch(XPathFactoryConfigurationException xpfce ){ System.out.println(" checkXPathExpression14() - failed , Default object model not available "); }catch(XPathExpressionException xpee ){ System.out.println(" checkXPathExpression14() - passed , expected XPathExpressionException thrown "); }catch(Exception e){ System.out.println(" Exception thrown - checkXPathExpression14() failed "); } } } -------------------------------------------------------------------------------