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

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

XMLWordPrintable

    • b44
    • generic
    • generic

      According to spec , XPath.evaluate(java.lang.String expression, InputSource source, QName returnType) should throw NPE if expression is null.
      ------------------------------------------------------------------------------
      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b37)
      Java HotSpot(TM) Client VM (build 1.5.0-beta2-b37, mixed mode)

      javac Test4.java
      java Test4
       checkXPath24() - failed , expected NPE - but XPathExpressionException thrown
       checkXPath29() - failed , expected NPE - but XPathExpressionException thrown
      ------------------------------------------------------------------------------

      Testcase - Test4.java & widgets.xml attached
      ********************************************
      --------------------------------------------------------------------------------
      <?xml version="1.0" standalone="yes"?>
      <widgets>
      <widget name="a" style="red" quantity="6"/>
      <widget name="b" style="blue"/>
      <widget name="c">
      <style>green</style>
      </widget>
      </widgets>
      ------------------------------------------------------------------------------
      //checkXPath24()
      //checkXPath29()
      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 Test4 {
         
         String xmlPath = "";
         FileInputStream fis = null;
         InputSource iSource = null;
         Test4 () {
           xmlPath = "./";
         }
          
          // main()
          public static void main(String[] argv) {
              Test4 test4 = new Test4();
              test4.init();
              test4.checkXPath24();
              test4.checkXPath29();
          
          }// end main()
          
          
          // initializing
          private void init(){
              try {
                  fis = new FileInputStream(xmlPath+"widgets.xml");
                  iSource = new InputSource(fis);
              }catch (Exception e) {
                System.out.println(" Exception thrown in XPath01/init() - Test failed");
              }
          }
          
          // test for XPath.evaluate(java.lang.String expression, InputSource source) - default returnType is String
          // expression is null , should throw NPE
          private void checkXPath24() {
           try{
           XPathFactory xpathFactory = XPathFactory.newInstance();
           if( xpathFactory instanceof XPathFactory ){
           XPath xpath = xpathFactory.newXPath();
      if ( xpath instanceof XPath ){
      String expression = null ;
      String quantity = (String) xpath.evaluate(expression, iSource);
      System.out.println("checkXPath24() : failed - expected NPE not thrown ");
      }
           else{
           System.out.println("checkXPath24() failed - xpath not instance of XPath");
           }
           }else{
           System.out.println(" checkXPath24() failed - creating instance of XPathFactory ");
           }
           }catch( NullPointerException npe ){
           System.out.println(" checkXPath24() - passed , expected NPE thrown ");
           }catch(XPathFactoryConfigurationException xpfce ){
           System.out.println(" checkXPath24() - failed , Default object model not available ");
           }catch(XPathExpressionException xpee ){
           System.out.println(" checkXPath24() - failed , expected NPE - but XPathExpressionException thrown " );
           }catch(Exception e){
           System.out.println(" Exception thrown - checkXPath24() failed ");
           }
          }
          
          // test for XPath.evaluate(java.lang.String expression, InputSource source, QName returnType)
          // expression is null , should throw NPE
          private void checkXPath29() {
           try{
           XPathFactory xpathFactory = XPathFactory.newInstance();
           if( xpathFactory instanceof XPathFactory ){
           XPath xpath = xpathFactory.newXPath();
      if ( xpath instanceof XPath ){
      String expression = null ;
      String quantity = (String) xpath.evaluate(expression, iSource, XPathConstants.STRING);
      System.out.println("checkXPath29() : failed - expected NPE not thrown ");
      }
           else{
           System.out.println("checkXPath29() failed - xpath not instance of XPath");
           }
           }else{
           System.out.println(" checkXPath29() failed - creating instance of XPathFactory ");
           }
           }catch( NullPointerException npe ){
           System.out.println(" checkXPath29() - passed , expected NPE thrown ");
           }catch(XPathFactoryConfigurationException xpfce ){
           System.out.println(" checkXPath29() - failed , Default object model not available ");
           }catch(XPathExpressionException xpee ){
           System.out.println(" checkXPath29() - failed , expected NPE - but XPathExpressionException thrown " );
           }catch(Exception e){
           System.out.println(" Exception thrown - checkXPath29() 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: