Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4992788

XPath.evaluate(expression,source,returnType) should throw NPE if source is null

XMLWordPrintable

    • b41
    • generic
    • generic

      According to the spec , if source is null
      XPath.evaluate(java.lang.String expression, InputSource source)
      and
      XPath.evaluate(java.lang.String expression, InputSource source, QName returnType)
      should throw NPE

      -----------------------------------------------------------------------------
      java -version
      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b37)

      javac Test3.java
      java Test3
      checkXPath23() - failed , expected NPE - but XPathExpressionException thrown
      checkXPath28() - failed , expected NPE - but XPathExpressionException thrown

      Testcase : Test3.java - also attached
      -----------------------------------------------------------------------------

      // checkXPath23()
      // checkXPath28()
      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 org.xml.sax.InputSource;

      import java.io.FileInputStream;


      public class Test3 {

         FileInputStream fis = null;
         InputSource iSource1 = null;
         Test3 () {}
          
          // main()
          public static void main(String[] argv) {
              Test3 test3 = new Test3();
              test3.checkXPath23();
              test3.checkXPath28();
          }
         
          // test for XPath.evaluate(java.lang.String expression, InputSource source) - default returnType is String
          // source is null , should throw NPE
          private void checkXPath23() {
           try{
           XPathFactory xpathFactory = XPathFactory.newInstance();
           if( xpathFactory instanceof XPathFactory ){
           XPath xpath = xpathFactory.newXPath();
      if ( xpath instanceof XPath ){
      String expression = "/widgets/widget[@name='a']/@quantity";
      String quantity = (String) xpath.evaluate(expression, iSource1);
      System.out.println("checkXPath23() : failed - expected NPE not thrown ");
      }
           else{
           System.out.println("checkXPath23() failed - xpath not instance of XPath");
           }
           }else{
           System.out.println(" checkXPath23() failed - creating instance of XPathFactory ");
           }
           }catch( NullPointerException npe ){
           System.out.println(" checkXPath23() - passed , expected NPE thrown ");
           }catch(XPathFactoryConfigurationException xpfce ){
           System.out.println(" checkXPath23() - failed , Default object model not available ");
           }catch(XPathExpressionException xpee ){
           System.out.println(" checkXPath23() - failed , expected NPE - but XPathExpressionException thrown "+xpee.getMessage());
           }catch(Exception e){
           System.out.println(" Exception thrown - checkXPath23() failed ");
           }
          }

          // test for XPath.evaluate(java.lang.String expression, InputSource source, QName returnType)
          // source is null , should throw NPE
          private void checkXPath28() {
           try{
           XPathFactory xpathFactory = XPathFactory.newInstance();
           if( xpathFactory instanceof XPathFactory ){
           XPath xpath = xpathFactory.newXPath();
      if ( xpath instanceof XPath ){
      String expression = "/widgets/widget[@name='a']/@quantity";
      String quantity = (String) xpath.evaluate(expression, iSource1, XPathConstants.STRING);
      System.out.println("checkXPath28() : failed - expected NPE not thrown ");
      }
           else{
           System.out.println("checkXPath28() failed - xpath not instance of XPath");
           }
           }else{
           System.out.println(" checkXPath28() failed - creating instance of XPathFactory ");
           }
           }catch( NullPointerException npe ){
           System.out.println(" checkXPath28() - passed , expected NPE thrown ");
           }catch(XPathFactoryConfigurationException xpfce ){
           System.out.println(" checkXPath28() - failed , Default object model not available ");
           }catch(XPathExpressionException xpee ){
           System.out.println(" checkXPath28() - failed ,expected NPE - but XPathExpressionException thrown "+xpee.getMessage());
           }catch(Exception e){
           System.out.println(" Exception thrown - checkXPath28() failed ");
           }
          }

      }

      -----------------------------------------------------------------------------

            kkawagucsunw Kohsuke Kawaguchi (Inactive)
            saksunw Sreejith Ak (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: