You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dm...@apache.org on 2002/08/10 03:50:39 UTC

cvs commit: jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/axes SimplePathInterpreterTest.java

dmitri      2002/08/09 18:50:39

  Modified:    jxpath/src/test/org/apache/commons/jxpath
                        JXPathTestCase.java TestFactory.java Vendor.xml
  Added:       jxpath/src/test/org/apache/commons/jxpath
                        TestBeanWithNode.java
               jxpath/src/test/org/apache/commons/jxpath/ri/axes
                        SimplePathInterpreterTest.java
  Removed:     jxpath/src/test/org/apache/commons/jxpath
                        TestBeanWithDOM.java
  Log:
  Updated test cases to the latest changes
  
  Revision  Changes    Path
  1.26      +103 -46   jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/JXPathTestCase.java
  
  Index: JXPathTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/JXPathTestCase.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- JXPathTestCase.java	3 Jul 2002 21:12:36 -0000	1.25
  +++ JXPathTestCase.java	10 Aug 2002 01:50:38 -0000	1.26
  @@ -79,6 +79,7 @@
   import org.apache.commons.jxpath.ri.axes.*;
   import org.apache.commons.jxpath.ri.compiler.*;
   import org.apache.commons.jxpath.ri.compiler.Expression;
  +import org.apache.commons.jxpath.xml.*;
   import java.beans.*;
   
   /**
  @@ -283,7 +284,7 @@
           testGetValue(context, "2 + 3",                  Boolean.TRUE, boolean.class);
           testGetValue(context, "'true'",                 Boolean.TRUE, Boolean.class);
   
  -        Map tm = new HashMap();
  +        Map tm = new Hashtable();
           tm.put("bar", "zz");
           bean.getMap().put("foo", new Map[]{tm, tm});
           bean.getMap().put("biz", tm);
  @@ -374,33 +375,34 @@
        * Test JXPath.getValue() with variables
        */
       public void testVariables(){
  -        if (enabled){
  -            JXPathContext context = JXPathContext.newContext(bean.getBeans());
  -            context.getVariables().declareVariable("x", new Double(7.0));
  -            context.getVariables().declareVariable("y", null);
  -            context.getVariables().declareVariable("z", bean);
  -            context.getVariables().declareVariable("t", new String[]{"a", "b"});
  -            context.getVariables().declareVariable("m", bean.getMap());
  -
  -            testGetValue(context, "$x + 3",  new Double(10.0));
  -            testGetValue(context, "$y",  null);
  -            testGetValue(context, "$y + 1",  new Double(1.0));
  -            boolean exception = false;
  -            try {
  -                testGetValue(context, "$none",  null);
  -            }
  -            catch (Exception ex){
  -                exception = true;
  -            }
  -            assertTrue("Evaluating '$none', expected exception - did not get it", exception);
  +        if (!enabled){
  +            return;
  +        }
  +        JXPathContext context = JXPathContext.newContext(bean.getBeans());
  +        context.getVariables().declareVariable("x", new Double(7.0));
  +        context.getVariables().declareVariable("y", null);
  +        context.getVariables().declareVariable("z", bean);
  +        context.getVariables().declareVariable("t", new String[]{"a", "b"});
  +        context.getVariables().declareVariable("m", bean.getMap());
  +
  +        testGetValue(context, "$x + 3",  new Double(10.0));
  +        testGetValue(context, "$y",  null);
  +        testGetValue(context, "$y + 1",  new Double(1.0));
  +        boolean exception = false;
  +        try {
  +            testGetValue(context, "$none",  null);
  +        }
  +        catch (Exception ex){
  +            exception = true;
  +        }
  +        assertTrue("Evaluating '$none', expected exception - did not get it", exception);
   
  -            testGetValue(context, "$z/int",  new Integer(1));
  -            testGetValue(context, "$z/integers[$x - 5]",  new Integer(2));
  -            testGetValue(context, ".",  bean.getBeans());
  +        testGetValue(context, "$z/int",  new Integer(1));
  +        testGetValue(context, "$z/integers[$x - 5]",  new Integer(2));
  +        testGetValue(context, ".",  bean.getBeans());
   //            testGetValue(context, ".[2]/name",  "Name 2");        // TBD: is this even legal?
  -            testGetValue(context, "$t[2]",  "b");
  -            testGetValue(context, "$m/Key1",  "Value 1");
  -        }
  +        testGetValue(context, "$t[2]",  "b");
  +        testGetValue(context, "$m/Key1",  "Value 1");
       }
   
       private void testGetValue(JXPathContext context, String xpath, Object expected) {
  @@ -474,7 +476,7 @@
               return;
           }
   
  -        JXPathContext context = JXPathContext.newContext(createTestBeanWithDOM());
  +        JXPathContext context = JXPathContext.newContext(bean);
   
           testDocumentOrder(context, "boolean", "int", -1);
           testDocumentOrder(context, "integers[1]", "integers[2]", -1);
  @@ -482,6 +484,8 @@
           testDocumentOrder(context, "nestedBean/int", "nestedBean", 1);
           testDocumentOrder(context, "nestedBean/int", "nestedBean/strings", -1);
           testDocumentOrder(context, "nestedBean/int", "object/int", -1);
  +
  +        context = JXPathContext.newContext(createTestBeanWithDOM());
           testDocumentOrder(context, "vendor/location", "vendor/location/address/street", -1);
           testDocumentOrder(context, "vendor/location[@id = '100']", "vendor/location[@id = '101']", -1);
           testDocumentOrder(context, "vendor//price:amount", "vendor/location", 1);
  @@ -491,14 +495,20 @@
       private void testDocumentOrder(JXPathContext context, String path1, String path2, int expected){
           NodePointer np1 = (NodePointer)context.getPointer(path1);
           NodePointer np2 = (NodePointer)context.getPointer(path2);
  -        int res = np1.compareTo(np2);
  -        if (res < 0){
  -            res = -1;
  +        try {
  +            int res = np1.compareTo(np2);
  +            if (res < 0){
  +                res = -1;
  +            }
  +            else if (res > 0){
  +                res = 1;
  +            }
  +            assertEquals("Comparing paths '" + path1 + "' and '" + path2 + "'", expected, res);
           }
  -        else if (res > 0){
  -            res = 1;
  +        catch (Exception ex){
  +            System.err.println("Comparing paths '" + path1 + "' and '" + path2 + "'");
  +            ex.printStackTrace();
           }
  -        assertEquals("Comparing paths '" + path1 + "' and '" + path2 + "'", expected, res);
       }
   
       /**
  @@ -565,7 +575,7 @@
           if (!enabled){
               return;
           }
  -        TestBeanWithDOM tBean = createTestBeanWithDOM();
  +        TestBeanWithNode tBean = createTestBeanWithDOM();
           tBean.setNestedBean(null);
           tBean.setBeans(null);
           tBean.setMap(null);
  @@ -650,6 +660,7 @@
           testCreatePath(context, "/map[@name='TestKey5']/nestedBean/int", new Integer(1));
           tBean.setMap(null);
           testCreatePath(context, "/map[@name='TestKey5']/beans[2]/int", new Integer(1));
  +
       }
   
       private void testCreatePath(JXPathContext context, String path, Object value){
  @@ -658,7 +669,14 @@
   
       private void testCreatePath(JXPathContext context, String path,
                   Object value, String expectedPath){
  -        Pointer ptr = context.createPath(path);
  +        Pointer ptr = null;
  +        try {
  +            ptr = context.createPath(path);
  +        }
  +        catch(JXPathException ex){
  +            ex.getException().printStackTrace();
  +        }
  +
           assertEquals("Pointer <" + path + ">", expectedPath, ptr.asPath());
           assertEquals("Created <" + path + ">", value, ptr.getValue());
       }
  @@ -751,6 +769,11 @@
           testCreatePathAndSetValue(context, "map[@name = 'TestKey5']/nestedBean/int", new Integer(6));
           tBean.setMap(null);
           testCreatePathAndSetValue(context, "map[@name = 'TestKey5']/beans[2]/int", new Integer(7));
  +
  +        context = JXPathContext.newContext(new HashMap());
  +        context.setFactory(new TestFactory());
  +        testCreatePathAndSetValue(context, "/testKey1/testKey2/testKey3", new Integer(8));
  +        context.setValue("/testKey1", new HashMap());
       }
   
       private void testCreatePathAndSetValue(JXPathContext context, String path, Object value){
  @@ -767,7 +790,7 @@
           if (!enabled){
               return;
           }
  -        TestBeanWithDOM tBean = createTestBeanWithDOM();
  +        TestBeanWithNode tBean = createTestBeanWithDOM();
           JXPathContext context = JXPathContext.newContext(tBean);
   
           // Undeclare variable
  @@ -851,10 +874,13 @@
               context.getValue("id(101)//street"));
           assertEquals("Test ID Path", "id('101')/address[1]/street[1]",
               context.getPointer("id(101)//street").asPath());
  -        assertEquals("Test ID Path Null", "id(105)/address[1]/street",
  -            context.getPointer("id(105)/address/street").asPath());
  +
           assertEquals("Test key", "42",
               context.getValue("key('a', 'b')"));
  +
  +        context.setLenient(true);
  +        assertEquals("Test ID Path Null", "id(105)/address/street",
  +            context.getPointer("id(105)/address/street").asPath());
       }
   
       public void testNull(){
  @@ -996,6 +1022,7 @@
                               actual = paths;
                           }
                           else {
  +                            ctx.setLenient(xpath_tests[i].lenient);
                               actual = ctx.getPointer(xpath_tests[i].xpath).asPath();
                           }
                       }
  @@ -1072,7 +1099,7 @@
       }
   
       private static XP testPath(String xpath, Object expected){
  -        return new XP(xpath, expected, false, true, false);
  +        return new XP(xpath, expected, false, true, true);
       }
   
       private static XP testEvalPath(String xpath, Object expected){
  @@ -1397,30 +1424,60 @@
           }
           System.setProperty(JXPathContextFactory.FACTORY_NAME_PROPERTY,
                   "org.apache.commons.jxpath.ri.JXPathContextFactoryReferenceImpl");
  -        XMLDocumentContainer docCtr = new XMLDocumentContainer(getClass().getResource("Vendor.xml"));
  +        DocumentContainer docCtr = new DocumentContainer(getClass().getResource("Vendor.xml"));
           Document doc = (Document)docCtr.getValue();
           JXPathContext ctx = JXPathContextFactory.newInstance().newContext(null, doc);
           ctx.setLocale(Locale.US);
           ctx.getVariables().declareVariable("dom", doc);
           ctx.getVariables().declareVariable("object", docCtr);
           ctx.getVariables().declareVariable("null", null);
  -        TestBeanWithDOM tbwdom = createTestBeanWithDOM();
  +        TestBeanWithNode tbwdom = createTestBeanWithDOM();
           ctx.getVariables().declareVariable("test", tbwdom);
           testXPaths(ctx, dom_tests, false);
       }
  -
  -    private TestBeanWithDOM createTestBeanWithDOM(){
  -        XMLDocumentContainer docCtr = new XMLDocumentContainer(getClass().getResource("Vendor.xml"));
  +/*
  +    public void testJDOM() throws Exception {
  +        if (true){
  +            return;
  +        }
  +        DocumentContainer docCtr =
  +            new DocumentContainer(getClass().getResource("Vendor.xml"),
  +                DocumentContainer.MODEL_JDOM);
  +        org.jdom.Document doc = (org.jdom.Document)docCtr.getValue();
  +        JXPathContext ctx = JXPathContextFactory.newInstance().newContext(null, doc);
  +        ctx.setLocale(Locale.US);
  +        ctx.getVariables().declareVariable("dom", doc);
  +        ctx.getVariables().declareVariable("object", docCtr);
  +        ctx.getVariables().declareVariable("null", null);
  +        TestBeanWithNode tbwdom = createTestBeanWithDOM();
  +        ctx.getVariables().declareVariable("test", tbwdom);
  +        testXPaths(ctx, dom_tests, false);
  +    }
  +*/
  +    private TestBeanWithNode createTestBeanWithDOM(){
  +        DocumentContainer docCtr = new DocumentContainer(getClass().getResource("Vendor.xml"));
           Document doc = (Document)docCtr.getValue();
  -        TestBeanWithDOM tbwdom = new TestBeanWithDOM();
  +        TestBeanWithNode tbwdom = new TestBeanWithNode();
           tbwdom.setVendor(doc.getDocumentElement());
           tbwdom.setObject(docCtr);
           return tbwdom;
       }
  -
  +/*
  +    private TestBeanWithNode createTestBeanWithJDOM(){
  +        DocumentContainer docCtr = new DocumentContainer(
  +            getClass().getResource("Vendor.xml"),
  +            DocumentContainer.MODEL_JDOM);
  +        org.jdom.Document doc = (org.jdom.Document)docCtr.getValue();
  +        TestBeanWithNode tbwdom = new TestBeanWithNode();
  +        tbwdom.setVendor(doc.getRootElement());
  +        tbwdom.setObject(docCtr);
  +        return tbwdom;
  +    }
  +*/
       static final XP[] dom_tests = new XP[]{
           test("vendor/location/address/street", "Orchard Road"),
           test("vendor/location[2]/address/street", "Tangerine Drive"),
  +        test("vendor/location/address/city", "Fruit Market"),
           test("//street", "Orchard Road"),
           test("local-name(//street/..)", "address"),
           test("number(vendor/location/employeeCount)", new Double(10)),
  
  
  
  1.4       +8 -4      jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/TestFactory.java
  
  Index: TestFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/TestFactory.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestFactory.java	8 May 2002 23:05:05 -0000	1.3
  +++ TestFactory.java	10 Aug 2002 01:50:39 -0000	1.4
  @@ -140,6 +140,10 @@
               addElement((Node)parent, index, name);
               return true;
           }
  +        else if (name.startsWith("testKey")){
  +            ((Map)parent).put(name, new HashMap());
  +            return true;
  +        }
           return false;
       }
   
  
  
  
  1.4       +5 -2      jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/Vendor.xml
  
  Index: Vendor.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/Vendor.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Vendor.xml	8 Jun 2002 22:47:24 -0000	1.3
  +++ Vendor.xml	10 Aug 2002 01:50:39 -0000	1.4
  @@ -12,9 +12,11 @@
     <!ELEMENT product ANY>
   ]>
   
  -<vendor>
  +<vendor name="fruitco">
     <contact>John</contact>
  -  <contact>Jack</contact>
  +  <contact name='jack'>Jack</contact>
  +  <contact name='jim'>Jim</contact>
  +  <contact name='jack'>Jack Black</contact>
     <location id="100" name="local" manager="">
       <address>
         <street>Orchard Road</street>
  @@ -25,6 +27,7 @@
     <location id="101">
       <address>
         <street>Tangerine Drive</street>
  +      <city>Fruit Market</city>
       </address>
     </location>
   
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/TestBeanWithNode.java
  
  Index: TestBeanWithNode.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/TestBeanWithNode.java,v 1.1 2002/08/10 01:50:39 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/08/10 01:50:39 $
   *
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 2001, Plotnix, Inc,
   * <http://www.plotnix.com/>.
   * For more information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.jxpath;
  
  import org.apache.commons.jxpath.xml.DocumentContainer;
  import org.w3c.dom.Document;
  
  /**
   * General purpose test bean for JUnit tests for the "jxpath" component.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/08/10 01:50:39 $
   */
  public class TestBeanWithNode extends TestBean {
      private Object node;
      private Object object;
  
      public Object getVendor(){
          return node;
      }
  
      public Object[] getVendors(){
          return new Object[]{node};
      }
  
      public void setVendor(Object node){
          this.node = node;
      }
  
      public Object getObject(){
          return object;
      }
  
      public void setObject(Object object){
          this.object = object;
      }
  
      public static TestBeanWithNode createTestBeanWithDOM(){
          DocumentContainer docCtr = new DocumentContainer(
                  TestBeanWithNode.class.getResource("Vendor.xml"));
          Document doc = (Document)docCtr.getValue();
          TestBeanWithNode tbwdom = new TestBeanWithNode();
          tbwdom.setVendor(doc.getDocumentElement());
          tbwdom.setObject(docCtr);
          return tbwdom;
      }
  
  }
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/axes/SimplePathInterpreterTest.java
  
  Index: SimplePathInterpreterTest.java
  ===================================================================
  package org.apache.commons.jxpath.ri.axes;
  
  import junit.framework.TestCase;
  
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.Pointer;
  import org.apache.commons.jxpath.TestBeanWithNode;
  import org.apache.commons.jxpath.TestNull;
  import org.apache.commons.jxpath.TestFactory;
  import org.apache.commons.jxpath.NestedTestBean;
  import org.apache.commons.jxpath.ri.model.*;
  import org.apache.commons.jxpath.ri.model.beans.*;
  import org.apache.commons.jxpath.ri.model.dom.*;
  import java.util.*;
  
  public class SimplePathInterpreterTest extends TestCase {
  
      private TestBeanWithNode bean;
      private JXPathContext context;
  
      /**
       * Constructor for SimplePathInterpreterTest.
       */
      public SimplePathInterpreterTest(String name) {
          super(name);
      }
  
      public static void main(String[] args) {
          junit.textui.TestRunner.run(SimplePathInterpreterTest.class);
      }
  
      /**
       * @see TestCase#setUp()
       */
      protected void setUp() throws Exception {
          bean = TestBeanWithNode.createTestBeanWithDOM();
          HashMap submap = new HashMap();
          submap.put("key", new NestedTestBean("Name 9"));
          submap.put("strings", bean.getNestedBean().getStrings());
          bean.getList().add(new int[]{1, 2});
          bean.getList().add(bean.getVendor());
          bean.getMap().put("Key3",
              new Object[]{
                  new NestedTestBean("some"),
                  new Integer(2),
                  bean.getVendor(),
                  submap
              }
          );
          bean.getMap().put("Key4", bean.getVendor());
          bean.getMap().put("Key5", submap);
          bean.getMap().put("Key6", new Object[0]);
          context = JXPathContext.newContext(null, bean);
          context.setLenient(true);
          context.setFactory(new TestFactory());
      }
  
      public void test_doStep_noPredicates_propertyOwner(){
          // Existing scalar property
          testValueAndPointer("/int",
                  new Integer(1),
                  "/int",
                  "BbB");
  
          // self::
          testValueAndPointer("/./int",
                  new Integer(1),
                  "/int",
                  "BbB");
  
          // Missing property
          testNullPointer("/foo",
                  "/foo",
                  "BnN");
  
          // existingProperty/existingScalarProperty
          testValueAndPointer("/nestedBean/int",
                  new Integer(1),
                  "/nestedBean/int",
                  "BbBbB");
  
          // existingProperty/collectionProperty
          testValueAndPointer("/nestedBean/strings",
                  bean.getNestedBean().getStrings(),
                  "/nestedBean/strings",
                  "BbBbC");
  
          // existingProperty/missingProperty
          testNullPointer("/nestedBean/foo",
                  "/nestedBean/foo",
                  "BbBnN");
  
          // map/missingProperty
          testNullPointer("/map/foo",
                  "/map[@name='foo']",
                  "BbDdN");
  
          // Existing property by search in collection
          testValueAndPointer("/list/int",
                  new Integer(1),
                  "/list[3]/int",
                  "BbBbB");
  
          // Missing property by search in collection
          testNullPointer("/list/foo",
                  "/list[1]/foo",
                  "BbBnN");
  
          // existingProperty/missingProperty/missingProperty
          testNullPointer("/nestedBean/foo/bar",
                  "/nestedBean/foo/bar",
                  "BbBnNnN");
  
          // collection/existingProperty/missingProperty
          testNullPointer("/list/int/bar",
                  "/list[3]/int/bar",
                  "BbBbBnN");
  
          // collectionProperty/missingProperty/missingProperty
          testNullPointer("/list/foo/bar",
                  "/list[1]/foo/bar",
                  "BbBnNnN");
  
          // map/missingProperty/anotherStep
          testNullPointer("/map/foo/bar",
                  "/map[@name='foo']/bar",
                  "BbDdNnN");
  
          // Existing dynamic property
          testValueAndPointer("/map/Key1",
                  "Value 1",
                  "/map[@name='Key1']",
                  "BbDdB");
  
          // collectionProperty
          testValueAndPointer("/integers",
                  bean.getIntegers(),
                  "/integers",
                  "BbC");
      }
  
      public void test_doStep_noPredicates_standard(){
          // Existing DOM node
          testValueAndPointer("/vendor/location/address/city",
                  "Fruit Market",
                  "/vendor/location[2]/address[1]/city[1]",
                  "BbMMMM");
  
          // Missing DOM node
          testNullPointer("/vendor/location/address/pity",
                  "/vendor/location[1]/address[1]/pity",
                  "BbMMMnN");
  
          // Missing DOM node inside a missing element
          testNullPointer("/vendor/location/address/itty/bitty",
                  "/vendor/location[1]/address[1]/itty/bitty",
                  "BbMMMnNnN");
  
          // Missing DOM node by search for the best match
          testNullPointer("/vendor/location/address/city/pretty",
                  "/vendor/location[2]/address[1]/city[1]/pretty",
                  "BbMMMMnN");
      }
  
      public void test_doStep_predicates_propertyOwner(){
          // missingProperty[@name=foo]
          testNullPointer("/foo[@name='foo']",
                  "/foo[@name='foo']",
                  "BnNnN");
  
          // missingProperty[index]
          testNullPointer("/foo[3]",
                  "/foo[3]",
                  "BnN");
      }
  
      public void test_doStep_predicates_standard(){
          // Looking for an actual XML attribute called "name"
          // nodeProperty/name[@name=value]
          testValueAndPointer("/vendor/contact[@name='jack']",
                  "Jack",
                  "/vendor/contact[2]",
                  "BbMM");
  
          // Indexing in XML
          testValueAndPointer("/vendor/contact[2]",
                  "Jack",
                  "/vendor/contact[2]",
                  "BbMM");
  
          // Indexing in XML, no result
          testNullPointer("/vendor/contact[5]",
                  "/vendor/contact[5]",
                  "BbMnN");
  
          // Combination of search by name and indexing in XML
          testValueAndPointer("/vendor/contact[@name='jack'][2]",
                  "Jack Black",
                  "/vendor/contact[4]",
                  "BbMM");
  
          // Combination of search by name and indexing in XML
          testValueAndPointer("/vendor/contact[@name='jack'][2]",
                  "Jack Black",
                  "/vendor/contact[4]",
                  "BbMM");
      }
  
      public void test_doPredicate_name(){
          // existingProperty[@name=existingProperty]
          testValueAndPointer("/nestedBean[@name='int']",
                  new Integer(1),
                  "/nestedBean/int",
                  "BbBbB");
  
          // /self::node()[@name=existingProperty]
          testValueAndPointer("/.[@name='int']",
                  new Integer(1),
                  "/int",
                  "BbB");
  
          // dynamicProperty[@name=existingProperty]
          testValueAndPointer("/map[@name='Key1']",
                  "Value 1",
                  "/map[@name='Key1']",
                  "BbDdB");
  
          // existingProperty[@name=collectionProperty]
          testValueAndPointer("/nestedBean[@name='strings']",
                  bean.getNestedBean().getStrings(),
                  "/nestedBean/strings",
                  "BbBbC");
  
          // existingProperty[@name=missingProperty]
          testNullPointer("/nestedBean[@name='foo']",
                  "/nestedBean[@name='foo']",
                  "BbBnN");
  
          // map[@name=collectionProperty]
          testValueAndPointer("/map[@name='Key3']",
                  bean.getMap().get("Key3"),
                  "/map[@name='Key3']",
                  "BbDdC");
  
          // map[@name=missingProperty]
          testNullPointer("/map[@name='foo']",
                  "/map[@name='foo']",
                  "BbDdN");
  
          // collectionProperty[@name=...] (find node)
          testValueAndPointer("/list[@name='fruitco']",
                  context.getValue("/vendor"),
                  "/list[5]",
                  "BbCM");
  
          // collectionProperty[@name=...] (find map entry)
          testValueAndPointer("/map/Key3[@name='key']/name",
                  "Name 9",
                  "/map[@name='Key3'][4][@name='key']/name",
                  "BbDdCDdBbB");
  
          // map/collectionProperty[@name...]
          testValueAndPointer("map/Key3[@name='fruitco']",
                  context.getValue("/vendor"),
                  "/map[@name='Key3'][3]",
                  "BbDdCM");
  
          // Bean property -> DOM Node, name match
          testValueAndPointer("/vendor[@name='fruitco']",
                  context.getValue("/vendor"),
                  "/vendor",
                  "BbM");
  
          // Bean property -> DOM Node, name mismatch
          testNullPointer("/vendor[@name='foo']",
                  "/vendor[@name='foo']",
                  "BbMnN");
  
          testNullPointer("/vendor[@name='foo'][3]",
                  "/vendor[@name='foo'][3]",
                  "BbMnN");
  
          // existingProperty(bean)[@name=missingProperty]/anotherStep
          testNullPointer("/nestedBean[@name='foo']/bar",
                  "/nestedBean[@name='foo']/bar",
                  "BbBnNnN");
  
          // map[@name=missingProperty]/anotherStep
          testNullPointer("/map[@name='foo']/bar",
                  "/map[@name='foo']/bar",
                  "BbDdNnN");
  
          // existingProperty(node)[@name=missingProperty]/anotherStep
          testNullPointer("/vendor[@name='foo']/bar",
                  "/vendor[@name='foo']/bar",
                  "BbMnNnN");
  
          // existingProperty(node)[@name=missingProperty][index]/anotherStep
          testNullPointer("/vendor[@name='foo'][3]/bar",
                  "/vendor[@name='foo'][3]/bar",
                  "BbMnNnN");
  
          // Existing dynamic property + existing property
          testValueAndPointer("/map[@name='Key2'][@name='name']",
                  "Name 6",
                  "/map[@name='Key2']/name",
                  "BbDdBbB");
  
          // Existing dynamic property + existing property + index
          testValueAndPointer("/map[@name='Key2'][@name='strings'][2]",
                  "String 2",
                  "/map[@name='Key2']/strings[2]",
                  "BbDdBbB");
  
          // bean/map/map/property
          testValueAndPointer("map[@name='Key5'][@name='key']/name",
                  "Name 9",
                  "/map[@name='Key5'][@name='key']/name",
                  "BbDdDdBbB");
  
          testNullPointer("map[@name='Key2'][@name='foo']",
                  "/map[@name='Key2'][@name='foo']",
                  "BbDdBnN");
  
          testNullPointer("map[@name='Key2'][@name='foo'][@name='bar']",
                  "/map[@name='Key2'][@name='foo'][@name='bar']",
                  "BbDdBnNnN");
  
          // bean/map/node
          testValueAndPointer("map[@name='Key4'][@name='fruitco']",
                  context.getValue("/vendor"),
                  "/map[@name='Key4']",
                  "BbDdM");
      }
  
      public void test_doPredicates_standard(){
          // bean/map/collection/node
          testValueAndPointer("map[@name='Key3'][@name='fruitco']",
                  context.getValue("/vendor"),
                  "/map[@name='Key3'][3]",
                  "BbDdCM");
  
          // bean/map/collection/missingNode
          testNullPointer("map[@name='Key3'][@name='foo']",
                  "/map[@name='Key3'][4][@name='foo']",
                  "BbDdCDdN");
  
          // bean/map/node
          testValueAndPointer("map[@name='Key4'][@name='fruitco']",
                  context.getValue("/vendor"),
                  "/map[@name='Key4']",
                  "BbDdM");
  
          // bean/map/emptyCollection[@name=foo]
          testNullPointer("map[@name='Key6'][@name='fruitco']",
                  "/map[@name='Key6'][@name='fruitco']",
                  "BbDdCnN");
  
          // bean/node[@name=foo][index]
          testValueAndPointer("/vendor/contact[@name='jack'][2]",
                  "Jack Black",
                  "/vendor/contact[4]",
                  "BbMM");
  
          // bean/node[@name=foo][missingIndex]
          testNullPointer("/vendor/contact[@name='jack'][5]",
                  "/vendor/contact[@name='jack'][5]",
                  "BbMnNnN");
  
          // bean/node/.[@name=foo][index]
          testValueAndPointer("/vendor/contact/.[@name='jack']",
                  "Jack",
                  "/vendor/contact[2]",
                  "BbMM");
      }
  
      public void test_doPredicate_index(){
          // Existing dynamic property + existing property + index
          testValueAndPointer("/map[@name='Key2'][@name='strings'][2]",
                  "String 2",
                  "/map[@name='Key2']/strings[2]",
                  "BbDdBbB");
  
          // existingProperty[@name=collectionProperty][index]
          testValueAndPointer("/nestedBean[@name='strings'][2]",
                  bean.getNestedBean().getStrings()[1],
                  "/nestedBean/strings[2]",
                  "BbBbB");
  
          // existingProperty[@name=missingProperty][index]
          testNullPointer("/nestedBean[@name='foo'][3]",
                  "/nestedBean[@name='foo'][3]",
                  "BbBnN");
  
          // existingProperty[@name=collectionProperty][missingIndex]
          testNullPointer("/nestedBean[@name='strings'][5]",
                  "/nestedBean/strings[5]",
                  "BbBbEN");
  
          // map[@name=collectionProperty][index]
          testValueAndPointer("/map[@name='Key3'][2]",
                  new Integer(2),
                  "/map[@name='Key3'][2]",
                  "BbDdB");
  
          // map[@name=collectionProperty][missingIndex]
          testNullPointer("/map[@name='Key3'][5]",
                  "/map[@name='Key3'][5]",
                  "BbDdEN");
  
          // map[@name=collectionProperty][missingIndex]/property
          testNullPointer("/map[@name='Key3'][5]/foo",
                  "/map[@name='Key3'][5]/foo",
                  "BbDdENnN");
  
          // map[@name=map][@name=collection][index]
          testValueAndPointer("/map[@name='Key5'][@name='strings'][2]",
                  "String 2",
                  "/map[@name='Key5'][@name='strings'][2]",
                  "BbDdDdB");
  
          // map[@name=map][@name=collection][missingIndex]
          testNullPointer("/map[@name='Key5'][@name='strings'][5]",
                  "/map[@name='Key5'][@name='strings'][5]",
                  "BbDdDdEN");
  
          // Existing dynamic property + indexing
          testValueAndPointer("/map[@name='Key3'][2]",
                  new Integer(2),
                  "/map[@name='Key3'][2]",
                  "BbDdB");
  
          // Existing dynamic property + indexing
          testValueAndPointer("/map[@name='Key3'][1]/name",
                  "some",
                  "/map[@name='Key3'][1]/name",
                  "BbDdBbB");
  
          // map[@name=missingProperty][index]
          testNullPointer("/map[@name='foo'][3]",
                  "/map[@name='foo'][3]",
                  "BbDdEN");
  
          // collectionProperty[index]
          testValueAndPointer("/integers[2]",
                  new Integer(2),
                  "/integers[2]",
                  "BbB");
  
          // existingProperty/collectionProperty[index]
          testValueAndPointer("/nestedBean/strings[2]",
                  bean.getNestedBean().getStrings()[1],
                  "/nestedBean/strings[2]",
                  "BbBbB");
  
          // existingProperty[index]/existingProperty
          testValueAndPointer("/list[3]/int",
                  new Integer(1),
                  "/list[3]/int",
                  "BbBbB");
  
          // existingProperty[missingIndex]
          testNullPointer("/list[6]",
                  "/list[6]",
                  "BbEN");
  
          // existingProperty/missingProperty[index]
          testNullPointer("/nestedBean/foo[3]",
                  "/nestedBean/foo[3]",
                  "BbBnN");
  
          // map[@name=missingProperty][index]
          testNullPointer("/map/foo[3]",
                  "/map[@name='foo'][3]",
                  "BbDdEN");
  
          // existingProperty/collectionProperty[missingIndex]
          testNullPointer("/nestedBean/strings[5]",
                  "/nestedBean/strings[5]",
                  "BbBbEN");
  
          // map/collectionProperty[missingIndex]/property
          testNullPointer("/map/Key3[5]/foo",
                  "/map[@name='Key3'][5]/foo",
                  "BbDdENnN");
  
          // map[@name=map]/collection[index]
          testValueAndPointer("/map[@name='Key5']/strings[2]",
                  "String 2",
                  "/map[@name='Key5'][@name='strings'][2]",
                  "BbDdDdB");
  
          // map[@name=map]/collection[missingIndex]
          testNullPointer("/map[@name='Key5']/strings[5]",
                  "/map[@name='Key5'][@name='strings'][5]",
                  "BbDdDdEN");
  
          // scalarPropertyAsCollection[index]
          testValueAndPointer("/int[1]",
                  new Integer(1),
                  "/int",
                  "BbB");
  
          // scalarPropertyAsCollection[index]
          testValueAndPointer(".[1]/int",
                  new Integer(1),
                  "/int",
                  "BbB");
      }
  
      public void testInterpretExpressionPath(){
          context.getVariables().declareVariable("array", new String[]{"Value1"});
          context.getVariables().declareVariable("testnull", new TestNull());
  
          testNullPointer("$testnull/nothing[2]",
                  "$testnull/nothing[2]",
                  "VBbEN");
      }
  
      private void testValueAndPointer(
              String path, Object expectedValue, String expectedPath,
              String expectedSignature)
      {
          Object value = context.getValue(path);
          assertEquals("Checking value: " + path, expectedValue, value);
  
          Pointer pointer = context.getPointer(path);
          assertEquals("Checking pointer: " + path,
                  expectedPath, pointer.toString());
  
          assertEquals("Checking signature: " + path,
                  expectedSignature, pointerSignature(pointer));
      }
  
  
      private void testNullPointer(String path, String expectedPath,
              String expectedSignature)
      {
          Pointer pointer = context.getPointer(path);
          assertNotNull("Null path exists: " + path,
                      pointer);
          assertTrue("Null path is null: " + path,
                      !((NodePointer)pointer).isActual());
          assertEquals("Null path as path: " + path,
                      expectedPath, pointer.asPath());
          assertEquals("Checking Signature: " + path,
                  expectedSignature, pointerSignature(pointer));
      }
  
      /**
       * Since we need to test the internal Signature of a pointer,
       * we will get a signature which will contain a single character
       * per pointer in the chain, representing that pointer's type.
       */
      private String pointerSignature(Pointer pointer){
          if (pointer == null){
              return "";
          }
  
          char type = '?';
          if (pointer instanceof NullPointer){                 type = 'N'; }
          else if (pointer instanceof NullPropertyPointer){    type = 'n'; }
          else if (pointer instanceof NullElementPointer){     type = 'E'; }
          else if (pointer instanceof VariablePointer){        type = 'V'; }
          else if (pointer instanceof CollectionPointer){      type = 'C'; }
          else if (pointer instanceof BeanPointer){            type = 'B'; }
          else if (pointer instanceof BeanPropertyPointer){    type = 'b'; }
          else if (pointer instanceof DynamicPointer){         type = 'D'; }
          else if (pointer instanceof DynamicPropertyPointer){ type = 'd'; }
          else if (pointer instanceof DOMNodePointer){         type = 'M'; }
          else {
              System.err.println("UNKNOWN TYPE: " + pointer.getClass());
          }
          return pointerSignature(((NodePointer)pointer).getParent()) + type;
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>