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

should throw IllegalArgumentException if returnType is not one of the types defi

XMLWordPrintable

    • b41
    • generic
    • generic

      XPath.evaluate(java.lang.String expression, java.lang.Object item, QName returnType) should throw IllegalArgumentException if returnType is not one of the types defined in XPathConstants .

      According to the spec , If returnType is not one of the types defined in XPathConstants, then an IllegalArgumentException is thrown.
      ---------------------------------------------------------------
      java -version
      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 Test2.java
      java Test2
      checkXPath13() failed , expected IAE not thrown
      Quantity : null

      Test2.java & widgets.xml attached
      ------------------ widgets.xml ---------------------
      <?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>
      ---------------------------------------
      Testcase - Test2.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.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.DocumentBuilder;

      import javax.xml.namespace.QName;
      import javax.xml.XMLConstants;

      import org.w3c.dom.Document;

      import java.io.File;


      public class Test2 {
         
         DocumentBuilderFactory dbf = null;
         DocumentBuilder builder = null;
         Document document = null;
         String xmlPath = "./";
         File file1 = null;
         public static final javax.xml.namespace.QName qname = new QName( XMLConstants.XML_NS_URI , "" );

         Test2 () {}
          
          // main()
          public static void main(String[] argv) {
              Test2 test2 = new Test2();
              test2.init();
              test2.checkXPath13();
          }
          // end main()

          // initializing
          private void init(){
              try {
                  file1 = new File(xmlPath+"widgets.xml");
                  dbf = DocumentBuilderFactory.newInstance();
                  builder = dbf.newDocumentBuilder();
                  document = builder.parse(file1);
                  
              }catch (Exception e) {
                System.out.println(" Exception thrown in XPath01/init() - Test failed");
              }
          }
          
          // test for XPath.evaluate(java.lang.String expression, java.lang.Object item, QName returnType)
          // if returnType is not one of the types defined in XPathConstants , should throw IllegalArgumentException
          private void checkXPath13() {
           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,document,qname);
      System.out.println("checkXPath13() failed , expected IAE not thrown ");
      System.out.println("Quantity : "+quantity);
      }
           else{
           System.out.println("checkXPath13() failed - xpath not instance of XPath");
           }
           }else{
           System.out.println(" checkXPath13() failed - creating instance of XPathFactory ");
           }
           }catch(IllegalArgumentException iae ){
           System.out.println(" checkXPathFactory13() - passed , expected IAE thrown ");
           }catch(XPathFactoryConfigurationException xpfce ){
           System.out.println(" checkXPathFactory13() - failed , Default object model not available ");
           }catch(XPathExpressionException xpee ){
           System.out.println(" checkXPathFactory13() - failed , XPathExpressionException thrown ");
           }catch(Exception e){
           System.out.println(" Exception thrown - checkXPath13() 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: