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/10/20 05:48:23 UTC

cvs commit: jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/util BasicTypeConverterTest.java

dmitri      2002/10/19 20:48:23

  Modified:    jxpath/src/test/org/apache/commons/jxpath TestBean.java
                        Vendor.xml
               jxpath/src/test/org/apache/commons/jxpath/ri/axes
                        SimplePathInterpreterTest.java
               jxpath/src/test/org/apache/commons/jxpath/ri/model
                        XMLModelTestCase.java
               jxpath/src/test/org/apache/commons/jxpath/ri/model/dom
                        DOMModelTest.java
               jxpath/src/test/org/apache/commons/jxpath/ri/model/jdom
                        JDOMModelTest.java
  Added:       jxpath/src/test/org/apache/commons/jxpath
                        JXPathTestCase.java JXPathTestSuite.java
                        TestMixedModelBean.java
               jxpath/src/test/org/apache/commons/jxpath/ri/axes
                        TestBeanWithNode.java
               jxpath/src/test/org/apache/commons/jxpath/ri/compiler
                        ContextDependencyTest.java CoreFunctionTest.java
                        CoreOperationTest.java ExtensionFunctionTest.java
                        TestFunctions.java TestFunctions2.java
                        VariableFactory.java VariableTest.java
               jxpath/src/test/org/apache/commons/jxpath/ri/model
                        BeanModelTestCase.java MixedModelTest.java
                        TestDynamicPropertyFactory.java
                        TestMixedModelFactory.java
               jxpath/src/test/org/apache/commons/jxpath/ri/model/beans
                        BeanModelTest.java DynamicPropertiesModelTest.java
                        TestBeanFactory.java
               jxpath/src/test/org/apache/commons/jxpath/ri/model/dom
                        TestDOMFactory.java
               jxpath/src/test/org/apache/commons/jxpath/ri/model/dynabeans
                        DynaBeanModelTest.java TestDynaBeanFactory.java
               jxpath/src/test/org/apache/commons/jxpath/ri/model/jdom
                        TestJDOMFactory.java
               jxpath/src/test/org/apache/commons/jxpath/util
                        BasicTypeConverterTest.java
  Removed:     jxpath/src/test/org/apache/commons/jxpath JXPathTest.java
                        TestBeanWithNode.java TestFactory.java
                        TestFunctions.java TestFunctions2.java
  Log:
  Complete overhaul of the test suites
  
  Revision  Changes    Path
  1.3       +7 -6      jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/TestBean.java
  
  Index: TestBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/TestBean.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestBean.java	10 Apr 2002 03:40:21 -0000	1.2
  +++ TestBean.java	20 Oct 2002 03:48:21 -0000	1.3
  @@ -83,6 +83,7 @@
           beans = new NestedTestBean[2];
           beans[0] = new NestedTestBean("Name 1");
           beans[1] = new NestedTestBean("Name 2");
  +        beans[1].setInt(3);
       }
   
       public NestedTestBean[] getBeans(){
  @@ -161,8 +162,8 @@
           return map;
       }
   
  -    public void setMap(HashMap map){
  -        this.map = map;
  +    public void setMap(Map map){
  +        this.map = (HashMap)map;
       }
   
       /**
  
  
  
  1.5       +2 -0      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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Vendor.xml	10 Aug 2002 01:50:39 -0000	1.4
  +++ Vendor.xml	20 Oct 2002 03:48:21 -0000	1.5
  @@ -25,10 +25,12 @@
     </location>
   
     <location id="101">
  +    Text before address.
       <address>
         <street>Tangerine Drive</street>
         <city>Fruit Market</city>
       </address>
  +    Text after address.
     </location>
   
     <product xmlns="productNS" xmlns:price="priceNS" xmlns:value="priceNS">
  
  
  
  1.29      +237 -1379 jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/JXPathTestCase.java
  
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/JXPathTestSuite.java
  
  Index: JXPathTestSuite.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/JXPathTestSuite.java,v 1.1 2002/10/20 03:48:21 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:21 $
   *
   * ====================================================================
   * 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 junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.jxpath.ri.axes.SimplePathInterpreterTest;
  import org.apache.commons.jxpath.ri.compiler.ContextDependencyTest;
  import org.apache.commons.jxpath.ri.compiler.CoreFunctionTest;
  import org.apache.commons.jxpath.ri.compiler.CoreOperationTest;
  import org.apache.commons.jxpath.ri.compiler.ExtensionFunctionTest;
  import org.apache.commons.jxpath.ri.compiler.VariableTest;
  import org.apache.commons.jxpath.ri.model.MixedModelTest;
  import org.apache.commons.jxpath.ri.model.beans.BeanModelTest;
  import org.apache.commons.jxpath.ri.model.beans.DynamicPropertiesModelTest;
  import org.apache.commons.jxpath.ri.model.dom.DOMModelTest;
  import org.apache.commons.jxpath.ri.model.dynabeans.DynaBeanModelTest;
  import org.apache.commons.jxpath.ri.model.jdom.JDOMModelTest;
  import org.apache.commons.jxpath.util.BasicTypeConverterTest;
  
  /**
   * <p>
   *  Test Suite for the JXPath class.  The majority of these tests use
   *  instances of the TestBean class, so be sure to update the tests if you
   *  change the characteristics of that class.
   * </p>
   *
   * <p>
   *   Note that the tests are dependent upon the static aspects
   *   (such as array sizes...) of the TestBean.java class, so ensure
   *   that all changes to TestBean are reflected here and in other JXPath tests.
   * </p>
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:21 $
   */
  
  public class JXPathTestSuite extends TestCase
  {
      private static boolean enabled = true;
  
      /**
       * Exercise the whole suite
       */
      public static void main(String args[]) {
          TestRunner.run(suite());
      }
  
      public JXPathTestSuite(String name){
          super(name);
      }
  
      /**
       * Return the tests included in this test suite.
       */
      public static Test suite()
      {
          TestSuite suite = new TestSuite();
          suite.addTestSuite(SimplePathInterpreterTest.class);
          suite.addTestSuite(ContextDependencyTest.class);
          suite.addTestSuite(CoreFunctionTest.class);
          suite.addTestSuite(CoreOperationTest.class);
          suite.addTestSuite(ExtensionFunctionTest.class);
          suite.addTestSuite(VariableTest.class);
          suite.addTestSuite(BeanModelTest.class);
          suite.addTestSuite(DynamicPropertiesModelTest.class);
          suite.addTestSuite(DOMModelTest.class);
          suite.addTestSuite(DynaBeanModelTest.class);
          suite.addTestSuite(JDOMModelTest.class);
          suite.addTestSuite(MixedModelTest.class);
          suite.addTestSuite(BasicTypeConverterTest.class);
          return suite;
      }
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/TestMixedModelBean.java
  
  Index: TestMixedModelBean.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/TestMixedModelBean.java,v 1.1 2002/10/20 03:48:21 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:21 $
   *
   * ====================================================================
   * 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 java.util.*;
  
  import org.apache.commons.jxpath.xml.DocumentContainer;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  
  /**
   * Mixed model test bean: Java, collections, map, DOM, Container.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:21 $
   */
  public class TestMixedModelBean {
      private String string;
      private TestBean bean;
      private Container container;
      private Document document;
      private Element element;
  
      private Map map;
  
      private List list;
  
      public TestMixedModelBean(){
          string = "string";
          bean = new TestBean();
          map = new HashMap();
          list = new ArrayList();
  
          container = new DocumentContainer(getClass().getResource("Vendor.xml"));
          document = (Document)container.getValue();
          element = document.getDocumentElement();
  
          map.put("string", string);
          map.put("bean", bean);
          map.put("map", map);
          map.put("list", list);
          map.put("document", document);
          map.put("element", element);
          map.put("container", container);
  
          list.add(string);
          list.add(bean);
          list.add(map);
          list.add(Collections.singletonList("string2"));
          list.add(document);
          list.add(element);
          list.add(container);
      }
  
      public String getString(){
          return string;
      }
      
      public TestBean getBean() {
          return bean;
      }
      
      public Map getMap(){
          return map;
      }
  
      public List getList() {
          return list;
      }
          
      public Document getDocument() {
          return document;
      }
      
      public Element getElement() {
          return element;
      }
      
      public Container getContainer(){
          return container;
      }
  }
  
  
  
  1.2       +1 -3      jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/axes/SimplePathInterpreterTest.java
  
  Index: SimplePathInterpreterTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/axes/SimplePathInterpreterTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SimplePathInterpreterTest.java	10 Aug 2002 01:50:39 -0000	1.1
  +++ SimplePathInterpreterTest.java	20 Oct 2002 03:48:22 -0000	1.2
  @@ -4,9 +4,7 @@
   
   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.*;
  @@ -52,7 +50,7 @@
           bean.getMap().put("Key6", new Object[0]);
           context = JXPathContext.newContext(null, bean);
           context.setLenient(true);
  -        context.setFactory(new TestFactory());
  +        context.setFactory(new TestBeanFactory());
       }
   
       public void test_doStep_noPredicates_propertyOwner(){
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/axes/TestBeanWithNode.java
  
  Index: TestBeanWithNode.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/axes/TestBeanWithNode.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.axes;
  
  import org.apache.commons.jxpath.JXPathTestCase;
  import org.apache.commons.jxpath.TestBean;
  import org.apache.commons.jxpath.xml.DocumentContainer;
  import org.w3c.dom.Document;
  
  /**
   * Test bean for mixed model JUnit tests.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  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(
                  JXPathTestCase.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/compiler/ContextDependencyTest.java
  
  Index: ContextDependencyTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/ContextDependencyTest.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.compiler;
  
  import org.apache.commons.jxpath.JXPathTestCase;
  import org.apache.commons.jxpath.ri.Parser;
  
  /**
   * Tests the determination of whether an expression is context dependent.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  
  public class ContextDependencyTest extends JXPathTestCase
  {
      public ContextDependencyTest(String name){
          super(name);
      }
  
      public void testContextDependency(){
          testContextDependency("1", false);
          testContextDependency("$x", false);
          testContextDependency("/foo", false);
          testContextDependency("foo", true);
          testContextDependency("/foo[3]", false);
          testContextDependency("/foo[$x]", false);
          testContextDependency("/foo[bar]", true);
          testContextDependency("3 + 5", false);
          testContextDependency("test:func(3, 5)", true);
          testContextDependency("test:func(3, foo)", true);
      }
  
      public void testContextDependency(String xpath, boolean expected){
          Expression expr = 
              (Expression)Parser.parseExpression(xpath, new TreeCompiler());
          
          assertEquals("Context dependency <" + xpath + ">", 
                  expected, 
                  expr.isContextDependent());
      }   
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/CoreFunctionTest.java
  
  Index: CoreFunctionTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/CoreFunctionTest.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.compiler;
  
  import org.apache.commons.jxpath.IdentityManager;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.JXPathTestCase;
  import org.apache.commons.jxpath.KeyManager;
  import org.apache.commons.jxpath.Pointer;
  import org.apache.commons.jxpath.TestMixedModelBean;
  import org.apache.commons.jxpath.Variables;
  import org.apache.commons.jxpath.ri.model.NodePointer;
  
  /**
   * Test basic functionality of JXPath - core functions.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  
  public class CoreFunctionTest extends JXPathTestCase
  {
      private JXPathContext context;
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public CoreFunctionTest(String name){
          super(name);
      }
  
      public void setUp(){
          if (context == null){
              context = JXPathContext.newContext(new TestMixedModelBean());
              Variables vars = context.getVariables();
              vars.declareVariable("nan", new Double(Double.NaN));
          }
      }
  
      public void testCoreFunctions(){
          assertXPathValue(context,"string(2)", "2");
          assertXPathValue(context,"string($nan)", "NaN");
          assertXPathValue(context,"string(-$nan)", "NaN");
          assertXPathValue(context,"string(-2 div 0)", "-Infinity");
          assertXPathValue(context,"string(2 div 0)", "Infinity");
          assertXPathValue(context,"concat('a', 'b', 'c')", "abc");
          assertXPathValue(context,"starts-with('abc', 'ab')", Boolean.TRUE);
          assertXPathValue(context,"starts-with('xabc', 'ab')", Boolean.FALSE);
          assertXPathValue(context,"contains('xabc', 'ab')", Boolean.TRUE);
          assertXPathValue(context,"contains('xabc', 'ba')", Boolean.FALSE);
          assertXPathValue(context,"substring-before('1999/04/01', '/')", "1999");
          assertXPathValue(context,"substring-after('1999/04/01', '/')", "04/01");
          assertXPathValue(context,"substring('12345', 2, 3)", "234");
          assertXPathValue(context,"substring('12345', 2)", "2345");
          assertXPathValue(context,"substring('12345', 1.5, 2.6)", "234");
          assertXPathValue(context,"substring('12345', 0, 3)", "12");
          assertXPathValue(context,"substring('12345', 0 div 0, 3)", "");
          assertXPathValue(context,"substring('12345', 1, 0 div 0)", "");
          assertXPathValue(context,"substring('12345', -42, 1 div 0)", "12345");
          assertXPathValue(context,"substring('12345', -1 div 0, 1 div 0)", "");
          assertXPathValue(context,"string-length('12345')", new Double(5));
          assertXPathValue(context,"normalize-space(' abc  def  ')", "abc def");
          assertXPathValue(context,"normalize-space('abc def')", "abc def");
          assertXPathValue(context,"normalize-space('   ')", "");
          assertXPathValue(context,"translate('--aaa--', 'abc-', 'ABC')", "AAA");
          assertXPathValue(context,"boolean(1)", Boolean.TRUE);
          assertXPathValue(context,"boolean(0)", Boolean.FALSE);
          assertXPathValue(context,"boolean('x')", Boolean.TRUE);
          assertXPathValue(context,"boolean('')", Boolean.FALSE);
          
          assertXPathValue(context,"true()", Boolean.TRUE);
          assertXPathValue(context,"false()", Boolean.FALSE);
          assertXPathValue(context,"not(false())", Boolean.TRUE);
          assertXPathValue(context,"not(true())", Boolean.FALSE);
          assertXPathValue(context,"number('1')", new Double(1));
          assertXPathValue(context,"floor(1.5)", new Double(1));
          assertXPathValue(context,"floor(-1.5)", new Double(-2));
          assertXPathValue(context,"ceiling(1.5)", new Double(2));
          assertXPathValue(context,"ceiling(-1.5)", new Double(-1));
          assertXPathValue(context,"round(1.5)", new Double(2));
          assertXPathValue(context,"round(-1.5)", new Double(-1));
          assertXPathValue(context,"null()", null);
      }
  
      public void testIDFunction(){
          context.setIdentityManager(new IdentityManager(){
              public Pointer getPointerByID(JXPathContext context, String id){
                  NodePointer ptr = (NodePointer)context.getPointer("/document");
                  return ptr.getPointerByID(context, id);
              }
          });
  
          assertXPathValueAndPointer(context,
                  "id(101)//street",
                  "Tangerine Drive",
                  "id('101')/address[1]/street[1]");
  
          assertXPathPointerLenient(context,
                  "id(105)/address/street",
                  "id(105)/address/street");
      }
      
      public void testKeyFunction(){
          context.setKeyManager(new KeyManager(){
              public Pointer getPointerByKey(JXPathContext context,
                                              String key, String value){
                  return NodePointer.newNodePointer(null, "42", null);
              }
          });
          
          assertEquals("Test key", "42",
              context.getValue("key('a', 'b')"));
      }
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java
  
  Index: CoreOperationTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.compiler;
  
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.JXPathTestCase;
  import org.apache.commons.jxpath.Variables;
  
  /**
   * Test basic functionality of JXPath - infoset types,
   * operations.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  
  public class CoreOperationTest extends JXPathTestCase
  {
      private JXPathContext context;
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public CoreOperationTest(String name){
          super(name);
      }
  
      public void setUp(){
          if (context == null){
              context = JXPathContext.newContext(null);
              Variables vars = context.getVariables();
              vars.declareVariable("integer", new Integer(1));
          }
      }
  
      public void testInfoSetTypes(){
  
          // Numbers
          assertXPathValue(context, "1",  new Double(1.0));
          assertXPathPointer(context, "1", "1");
          assertXPathValueIterator(context,"1", list(new Double(1.0)));
  
          assertXPathPointerIterator(context, "1", list("1"));
  
          assertXPathValue(context,"-1", new Double(-1.0));
          assertXPathValue(context,"2 + 2", new Double(4.0));
          assertXPathValue(context,"3 - 2", new Double(1.0));
          assertXPathValue(context,"1 + 2 + 3 - 4 + 5", new Double(7.0));
          assertXPathValue(context,"3 * 2", new Double(3.0*2.0));
          assertXPathValue(context,"3 div 2", new Double(3.0/2.0));
          assertXPathValue(context,"5 mod 2", new Double(1.0));
  
          // This test produces a different result with Xalan?
          assertXPathValue(context,"5.9 mod 2.1", new Double(1.0));
  
          assertXPathValue(context,"5 mod -2", new Double(1.0));
          assertXPathValue(context,"-5 mod 2", new Double(-1.0));
          assertXPathValue(context,"-5 mod -2", new Double(-1.0));
          assertXPathValue(context,"1 < 2", Boolean.TRUE);
          assertXPathValue(context,"1 > 2", Boolean.FALSE);
          assertXPathValue(context,"1 <= 1", Boolean.TRUE);
          assertXPathValue(context,"1 >= 2", Boolean.FALSE);
          assertXPathValue(context,"3 > 2 > 1", Boolean.FALSE);
          assertXPathValue(context,"3 > 2 and 2 > 1", Boolean.TRUE);
          assertXPathValue(context,"3 > 2 and 2 < 1", Boolean.FALSE);
          assertXPathValue(context,"3 < 2 or 2 > 1", Boolean.TRUE);
          assertXPathValue(context,"3 < 2 or 2 < 1", Boolean.FALSE);
          assertXPathValue(context,"1 = 1", Boolean.TRUE);
          assertXPathValue(context,"1 = '1'", Boolean.TRUE);
          assertXPathValue(context,"1 > 2 = 2 > 3", Boolean.TRUE);
          assertXPathValue(context,"1 > 2 = 0", Boolean.TRUE);
          assertXPathValue(context,"1 = 2", Boolean.FALSE);
  
          assertXPathValue(context, 
                  "$integer",
                  new Double(1), 
                  Double.class);
                  
          assertXPathValue(context, 
                  "2 + 3",
                  "5.0",
                  String.class);
                  
          assertXPathValue(context, 
                  "2 + 3",
                  Boolean.TRUE, 
                  boolean.class);
                  
          assertXPathValue(context, 
                  "'true'",
                  Boolean.TRUE, 
                  Boolean.class);
      }
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java
  
  Index: ExtensionFunctionTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.compiler;
  
  import java.util.List;
  import java.util.Locale;
  
  import org.apache.commons.jxpath.ClassFunctions;
  import org.apache.commons.jxpath.ExpressionContext;
  import org.apache.commons.jxpath.Function;
  import org.apache.commons.jxpath.FunctionLibrary;
  import org.apache.commons.jxpath.Functions;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.JXPathTestCase;
  import org.apache.commons.jxpath.PackageFunctions;
  import org.apache.commons.jxpath.Pointer;
  import org.apache.commons.jxpath.TestBean;
  import org.apache.commons.jxpath.Variables;
  import org.apache.commons.jxpath.ri.model.NodePointer;
  
  /**
   * Test extension functions.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  
  public class ExtensionFunctionTest extends JXPathTestCase
  {
      private Functions functions;
      private JXPathContext context;
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public ExtensionFunctionTest(String name){
          super(name);
      }
  
      public void setUp(){
          if (context == null){
              context = JXPathContext.newContext(new TestBean());
              Variables vars = context.getVariables();
              vars.declareVariable("test", new TestFunctions(4, "test"));
             
              FunctionLibrary lib = new FunctionLibrary();
              lib.addFunctions(new ClassFunctions(TestFunctions.class, "test"));
              lib.addFunctions(new ClassFunctions(TestFunctions2.class, "test"));
              lib.addFunctions(new PackageFunctions("", "call"));
              lib.addFunctions(
                      new PackageFunctions("org.apache.commons.jxpath.", 
                              "jxpathtest"));
              context.setFunctions(lib);
          }
          functions = new ClassFunctions(TestFunctions.class, "test");
      }
      
      public void testConstructorLookup(){
          Object[] args = new Object[]{new Integer(1), "x"};
          Function func = functions.getFunction("test", "new", args);
          
          assertEquals("test:new(1, x)", 
              func.invoke(new Context(null), args).toString(), 
              "foo=1; bar=x");
      }
      
      public void testConstructorLookupWithExpressionContext(){
          Object[] args = new Object[]{"baz"};
          Function func =functions.getFunction("test", "new", args);
          assertEquals("test:new('baz')", 
                  func.invoke(new Context(new Integer(1)), args).toString(), 
                  "foo=1; bar=baz");
      }
      
      public void testStaticMethodLookup(){
          Object[] args = new Object[]{new Integer(1), "x"};
          Function func =functions.getFunction("test", "build", args);
          assertEquals("test:build(1, x)", 
                  func.invoke(new Context(null), args).toString(), 
                  "foo=1; bar=x");
      }
      
      public void testStaticMethodLookupWithConversion(){
          Object[] args = new Object[]{"7", new Integer(1)};
          Function func =functions.getFunction("test", "build", args);
          assertEquals("test:build('7', 1)", 
                  func.invoke(new Context(null), args).toString(), 
                  "foo=7; bar=1");
      }
      
      public void testMethodLookup(){
          Object[] args = new Object[]{new TestFunctions()};
          Function func =functions.getFunction("test", "getFoo", args);
          assertEquals("test:getFoo($test, 1, x)", 
                  func.invoke(new Context(null), args).toString(), 
                  "0");
      }
      
      public void testStaticMethodLookupWithExpressionContext(){
          Object[] args = new Object[0];
          Function func =functions.getFunction("test", "path", args);
          assertEquals("test:path()", 
                  func.invoke(new Context(new Integer(1)), args), 
                  "1");
      }
      
      public void testMethodLookupWithExpressionContext(){
          Object[] args = new Object[]{new TestFunctions()};
          Function func =functions.getFunction("test", "instancePath", args);
          assertEquals("test:instancePath()", 
                  func.invoke(new Context(new Integer(1)), args), 
                  "1");
      }
      
      public void testMethodLookupWithExpressionContextAndArgument(){
          Object[] args = new Object[]{new TestFunctions(), "*"};
          Function func =functions.getFunction("test", "pathWithSuffix", args);
          assertEquals("test:pathWithSuffix('*')", 
                  func.invoke(new Context(new Integer(1)), args), 
                  "1*");
      }    
  
      public void testAllocation(){ 
          
          // Allocate new object using the default constructor
          assertXPathValue(context, 
                  "string(test:new())", 
                  "foo=0; bar=null");
                  
          // Allocate new object using PackageFunctions and class name
          assertXPathValue(context, 
                  "string(jxpathtest:TestFunctions.new())", 
                  "foo=0; bar=null");
                  
          // Allocate new object using a fully qualified class name
          assertXPathValue(context, 
                  "string(" + TestFunctions.class.getName() + ".new())", 
                  "foo=0; bar=null");
                  
          // Allocate new object using a custom constructor
          assertXPathValue(context, 
                  "string(test:new(3, 'baz'))", 
                  "foo=3; bar=baz");
                  
          // Allocate new object using a custom constructor - type conversion
          assertXPathValue(context, 
                  "string(test:new('3', 4))", 
                  "foo=3; bar=4.0");                
      }
  
      public void testMethodCall(){ 
          assertXPathValue(context, 
                  "length('foo')", 
                  new Integer(3));
                  
          // We are just calling a method - prefix is ignored
          assertXPathValue(context, 
                  "call:substring('foo', 1, 2)", 
                  "o");
          
          // Invoke a function implemented as a regular method
          assertXPathValue(context, 
                  "string(test:getFoo($test))", 
                  "4");
          
          // Note that the prefix is ignored anyway, we are just calling a method
          assertXPathValue(context, 
                  "string(call:getFoo($test))", 
                  "4");
          
          // We don't really need to supply a prefix in this case
          assertXPathValue(context, 
                  "string(getFoo($test))", 
                  "4");                
  
          // Method with two arguments
          assertXPathValue(context, 
                  "string(test:setFooAndBar($test, 7, 'biz'))", 
                  "foo=7; bar=biz");
      }
  
      public void testStaticMethodCall(){ 
                                  
          assertXPathValue(context, 
                  "string(test:build(8, 'goober'))", 
                  "foo=8; bar=goober");
                  
          // Call a static method using PackageFunctions and class name
          assertXPathValue(context, 
                  "string(jxpathtest:TestFunctions.build(8, 'goober'))", 
                  "foo=8; bar=goober");
          
          // Call a static method with a fully qualified class name
          assertXPathValue(context, 
                  "string(" + TestFunctions.class.getName() + 
                          ".build(8, 'goober'))", 
                  "foo=8; bar=goober");
                  
          // Two ClassFunctions are sharing the same prefix.
          // This is TestFunctions2
          assertXPathValue(context, 
                  "string(test:increment(8))", 
                  "9");
                  
      }
  
      public void testExpressionContext(){ 
          // Execute an extension function for each node while searching
          // The function uses ExpressionContext to get to the current
          // node.
          assertXPathValue(context, 
                  "//.[test:isMap()]/Key1", 
                  "Value 1");
                  
          // The function uses ExpressionContext to get to all
          // nodes in the context that is passed to it.
          assertXPathValue(context, 
                  "count(//.[test:count(strings) = 3])", 
                  new Double(7));
  
          // The function uses ExpressionContext to get to the current
          // pointer and returns its path.
          assertXPathValue(context, 
                  "/beans[contains(test:path(), '[2]')]/name", 
                  "Name 2");
      }
      
      private static class Context implements ExpressionContext {
          private Object object;
  
          public Context(Object object){
              this.object = object;
          }
  
          public Pointer getContextNodePointer(){
              return NodePointer.newNodePointer(null, object, Locale.getDefault());
          }
  
          public List getContextNodeList(){
              return null;
          }
  
          public JXPathContext getJXPathContext(){
              return null;
          }
  
          public int getPosition(){
              return 0;
          }
      }
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions.java
  
  Index: TestFunctions.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.compiler;
  
  import java.util.Collection;
  import java.util.Map;
  
  import org.apache.commons.jxpath.ExpressionContext;
  import org.apache.commons.jxpath.Pointer;
  
  /**
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  public class TestFunctions {
  
      private int foo;
      private String bar;
  
      public TestFunctions(){
      }
  
      public TestFunctions(int foo, String bar){
          this.foo = foo;
          this.bar = bar;
      }
  
      public TestFunctions(ExpressionContext context, String bar){
          this.foo = ((Number)context.getContextNodePointer().getValue()).
          		intValue();
          this.bar = bar;
      }
  
      public int getFoo(){
          return foo;
      }
  
      public String getBar(){
          return bar;
      }
  
      public void doit(){
      }
  
      public TestFunctions setFooAndBar(int foo, String bar){
          this.foo = foo;
          this.bar = bar;
          return this;
      }
  
      public static TestFunctions build(int foo, String bar){
          return new TestFunctions(foo, bar);
      }
  
      public String toString(){
          return "foo=" + foo + "; bar=" + bar;
      }
  
      public static String path(ExpressionContext context){
          return context.getContextNodePointer().asPath();
      }
  
      public String instancePath(ExpressionContext context){
          return context.getContextNodePointer().asPath();
      }
  
      public String pathWithSuffix(ExpressionContext context, String suffix){
          return context.getContextNodePointer().asPath() + suffix;
      }
  
      public String className(ExpressionContext context, ExpressionContext child){
          return context.getContextNodePointer().asPath();
      }
  
      /**
       * Returns true if the current node in the current context is a map
       */
      public static boolean isMap(ExpressionContext context){
          Pointer ptr = context.getContextNodePointer();
          return ptr == null ? false : (ptr.getValue() instanceof Map);
      }
  
      /**
       * Returns the number of nodes in the context that is passed as
       * the first argument.
       */
      public static int count(ExpressionContext context, Collection col){
          return col.size();
      }
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions2.java
  
  Index: TestFunctions2.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions2.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.compiler;
  
  import java.util.*;
  
  /**
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  public class TestFunctions2 {
  
      public static int increment(int i){
          return i + 1;
      }
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/VariableFactory.java
  
  Index: VariableFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/VariableFactory.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.compiler;
  
  import org.apache.commons.jxpath.AbstractFactory;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.Pointer;
  import org.apache.commons.jxpath.TestBean;
  
  /**
   * Test AbstractFactory.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  public class VariableFactory extends AbstractFactory {
  
      /**
       */
      public boolean createObject(JXPathContext context, Pointer pointer, Object parent, String name, int index){
          if (name.equals("testArray")){
              ((TestBean[])parent)[index] = new TestBean();
              return true;
          }
          else if (name.equals("stringArray")){
              ((String[])parent)[index] = "";
              return true;
          }
          else if (name.equals("array")){
              ((String[])parent)[index] = "";
              return true;
          }
          return false;
      }
      
      /**
       * Create a new object and set it on the specified variable
       */
      public boolean declareVariable(JXPathContext context, String name){
          if (name.equals("test")){
              context.getVariables().declareVariable(name, new TestBean());
              return true;
          }
          else if (name.equals("testArray")){
              context.getVariables().declareVariable(name, new TestBean[0]);
              return true;
          }
          else if (name.equals("stringArray")){
              context.getVariables().declareVariable(name, new String[]{"Value1"});
              return true;
          }
          context.getVariables().declareVariable(name, null);
          return true;
      }
  }
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/VariableTest.java
  
  Index: VariableTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/VariableTest.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.compiler;
  
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.JXPathTestCase;
  import org.apache.commons.jxpath.Variables;
  
  /**
   * Test basic functionality of JXPath - infoset types,
   * operations.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  
  public class VariableTest extends JXPathTestCase
  {
      private JXPathContext context;
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public VariableTest(String name){
          super(name);
      }
  
      public void setUp(){
          if (context == null){
              context = JXPathContext.newContext(null);
              context.setFactory(new VariableFactory());
          
              Variables vars = context.getVariables();
              vars.declareVariable("a", new Double(1));
              vars.declareVariable("b", new Double(1));
              vars.declareVariable("c", null);            
              vars.declareVariable("d", new String[]{"a", "b"});
              vars.declareVariable("integer", new Integer(1));
              vars.declareVariable("nan", new Double(Double.NaN));
              vars.declareVariable("x", null);
          }
      }
  
      public void testVariables(){
          // Variables
          assertXPathValueAndPointer(context,
                  "$a", 
                  new Double(1),
                  "$a");
      }
      
      public void testVariablesInExpressions(){
          assertXPathValue(context,
                  "$a = $b", 
                  Boolean.TRUE);
                  
          assertXPathValue(context,
                  "$a = $nan", 
                  Boolean.FALSE);
  
          assertXPathValue(context,
                  "$a + 1", 
                  new Double(2));
  
          assertXPathValue(context,
                  "$c", 
                  null);
                  
          assertXPathValue(context, 
                  "$d[2]",  
                  "b");
      }
      
      public void testInvalidVariableName(){                
          boolean exception = false;
          try {
              context.getValue("$none");
          }
          catch (Exception ex){
              exception = true;
          }
          assertTrue("Evaluating '$none', expected exception - did not get it", 
                  exception);
  
          exception = false;
          try {
              context.setValue("$none", new Integer(1));
          }
          catch (Exception ex){
              exception = true;
          }
          assertTrue("Setting '$none = 1', expected exception - did not get it", 
              exception);
      }
      
      public void testNestedContext(){
          JXPathContext nestedContext = 
                  JXPathContext.newContext(context, null);
  
          assertXPathValue(nestedContext, 
                  "$a",  
                  new Double(1));
      }    
      
      public void testSetValue(){        
          assertXPathSetValue(context, 
                  "$x",
                  new Integer(1));
      }
      
      public void testCreatePathDeclareVariable(){
          // Calls factory.declareVariable("string")
          assertXPathCreatePath(context, 
                  "$string", 
                  null,
                  "$string");
      }
      
      public void testCreatePathAndSetValueDeclareVariable(){
          // Calls factory.declareVariable("string")
          assertXPathCreatePathAndSetValue(context, 
                  "$string", 
                  "Value",
                  "$string");
      }
      
      public void testCreatePathDeclareVariableSetCollectionElement(){
          // Calls factory.declareVariable("stringArray"). 
          // The factory needs to create a collection
          assertXPathCreatePath(context, 
                  "$stringArray[2]", 
                  "",
                  "$stringArray[2]");
          
          // See if the factory populated the first element as well
          assertEquals("Created <" + "$stringArray[1]" + ">", 
                  "Value1", 
                  context.getValue("$stringArray[1]"));
      }
  
      public void testCreateAndSetValuePathDeclareVariableSetCollectionElement(){
          // Calls factory.declareVariable("stringArray"). 
          // The factory needs to create a collection
          assertXPathCreatePathAndSetValue(context, 
                  "$stringArray[2]", 
                  "Value2",
                  "$stringArray[2]");
          
          // See if the factory populated the first element as well
          assertEquals("Created <" + "$stringArray[1]" + ">", 
                  "Value1", 
                  context.getValue("$stringArray[1]"));
      }
  
      public void testCreatePathExpandCollection(){
          context.getVariables().
              declareVariable("array", new String[]{"Value1"});
  
          // Does not involve factory at all - just expands the collection
          assertXPathCreatePath(context, 
                  "$array[2]", 
                  "", 
                  "$array[2]");
  
          // Make sure it is still the same array
          assertEquals("Created <" + "$array[1]" + ">", 
                  "Value1", 
                  context.getValue("$array[1]"));
      }
  
      public void testCreatePathAndSetValueExpandCollection(){
          context.getVariables().
              declareVariable("array", new String[]{"Value1"});
  
          // Does not involve factory at all - just expands the collection
          assertXPathCreatePathAndSetValue(context, 
                  "$array[2]", 
                  "Value2", 
                  "$array[2]");
  
          // Make sure it is still the same array
          assertEquals("Created <" + "$array[1]" + ">", 
                  "Value1", 
                  context.getValue("$array[1]"));
      }
  
      public void testCreatePathDeclareVariableSetProperty(){
          // Calls factory.declareVariable("test"). 
          // The factory should create a TestBean
          assertXPathCreatePath(context, 
                  "$test/boolean", 
                  Boolean.FALSE, 
                  "$test/boolean");
  
      }
  
      public void testCreatePathAndSetValueDeclareVariableSetProperty(){
          // Calls factory.declareVariable("test"). 
          // The factory should create a TestBean
          assertXPathCreatePathAndSetValue(context, 
                  "$test/boolean", 
                  Boolean.TRUE, 
                  "$test/boolean");
  
      }
  
      public void testCreatePathDeclareVariableSetCollectionElementProperty(){
          // Calls factory.declareVariable("testArray").
          // The factory should create a collection of TestBeans.
          // Then calls factory.createObject(..., collection, "testArray", 1).
          // That one should produce an instance of TestBean and 
          // put it in the collection at index 1.
          assertXPathCreatePath(context, 
                  "$testArray[2]/boolean", 
                  Boolean.FALSE, 
                  "$testArray[2]/boolean");
      }
      
      public void testCreatePathAndSetValueDeclVarSetCollectionElementProperty(){
          // Calls factory.declareVariable("testArray").
          // The factory should create a collection of TestBeans.
          // Then calls factory.createObject(..., collection, "testArray", 1).
          // That one should produce an instance of TestBean and 
          // put it in the collection at index 1.
          assertXPathCreatePathAndSetValue(context, 
                  "$testArray[2]/boolean", 
                  Boolean.TRUE, 
                  "$testArray[2]/boolean");
      }
      
      public void testRemovePathUndeclareVariable(){
          // Undeclare variable
          context.getVariables().declareVariable("temp", "temp");
          context.removePath("$temp");
          assertTrue("Undeclare variable",
                  !context.getVariables().isDeclaredVariable("temp"));
  
      }
      
      public void testRemovePathArrayElement(){
          // Remove array element - reassigns the new array to the var
          context.getVariables().
                  declareVariable("temp", new String[]{"temp1", "temp2"});
          context.removePath("$temp[1]");
          assertEquals("Remove array element", "temp2",
                      context.getValue("$temp[1]"));
      }
      
      public void testRemovePathCollectionElement(){
          // Remove list element - does not create a new list
          context.getVariables().
                  declareVariable("temp", list("temp1", "temp2"));
          context.removePath("$temp[1]");
          assertEquals("Remove collection element", "temp2",
                      context.getValue("$temp[1]"));
      }
  }
  
  
  1.2       +511 -336  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/XMLModelTestCase.java
  
  Index: XMLModelTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/XMLModelTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLModelTestCase.java	13 Oct 2002 03:01:03 -0000	1.1
  +++ XMLModelTestCase.java	20 Oct 2002 03:48:22 -0000	1.2
  @@ -62,26 +62,13 @@
   
   package org.apache.commons.jxpath.ri.model;
   
  -import java.lang.reflect.InvocationTargetException;
  -
  -import junit.framework.Test;
  -import junit.framework.TestCase;
  -import junit.framework.TestSuite;
  -
  -import org.w3c.dom.*;
  -import java.util.*;
  -import java.lang.reflect.*;
  -import org.apache.commons.jxpath.*;
  -import org.apache.commons.jxpath.util.*;
  -import org.apache.commons.jxpath.ri.*;
  -import org.apache.commons.jxpath.ri.parser.*;
  -import org.apache.commons.jxpath.ri.model.*;
  -import org.apache.commons.jxpath.ri.model.beans.*;
  -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.*;
  +import org.apache.commons.jxpath.AbstractFactory;
  +import org.apache.commons.jxpath.IdentityManager;
  +import org.apache.commons.jxpath.JXPathContext;
  +import org.apache.commons.jxpath.JXPathTestCase;
  +import org.apache.commons.jxpath.Pointer;
  +import org.apache.commons.jxpath.Variables;
  +import org.apache.commons.jxpath.xml.DocumentContainer;
   
   /**
    * Abstract superclass for pure XPath 1.0.  Subclasses
  @@ -92,107 +79,128 @@
    * @version $Revision$ $Date$
    */
   
  -public abstract class XMLModelTestCase extends TestCase
  +public abstract class XMLModelTestCase extends JXPathTestCase
   {
  -    private boolean enabled = true;
  +    private JXPathContext context;
   
       /**
        * Construct a new instance of this test case.
        *
        * @param name Name of the test case
        */
  -    public XMLModelTestCase(String name)
  -    {
  +    public XMLModelTestCase(String name){
           super(name);
       }
   
  +    public void setUp(){
  +        if (context == null){
  +            DocumentContainer docCtr = createDocumentContainer();
  +            context = createContext();
  +            Variables vars = context.getVariables();
  +            vars.declareVariable("document", docCtr.getValue());
  +            vars.declareVariable("container", docCtr);
  +        }
  +    }
   
       protected abstract String getModel();
   
       protected DocumentContainer createDocumentContainer(){
           return new DocumentContainer(
  -                getClass().getClassLoader().
  -                        getResource("org/apache/commons/jxpath/Vendor.xml"),
  +                JXPathTestCase.class.getResource("Vendor.xml"),
                   getModel());
       }
  +    
  +    protected abstract AbstractFactory getAbstractFactory();
   
       protected JXPathContext createContext(){
           JXPathContext context =
                   JXPathContext.newContext(createDocumentContainer());
  -        context.setFactory(new TestFactory());
  +        context.setFactory(getAbstractFactory());
           return context;
       }
   
  +	/**
  +	 * An XML signature is used to determine if we have the right result
  +	 * after a modification of XML by JXPath.  It is basically a piece
  +	 * of simplified XML.
  +	 */    
  +    protected abstract String getXMLSignature(Object node, 
  +    		boolean elements, boolean attributes, boolean text, boolean pi);
  +
  +	protected void assertXMLSignature(JXPathContext context,
  +			String path, String signature,
  +    		boolean elements, boolean attributes, boolean text, boolean pi)
  +	{
  +		Object node = context.getPointer(path).getNode();
  +		String sig = getXMLSignature(node, elements, attributes, text, pi);
  +		assertEquals("XML Signature mismatch: ", signature, sig);
  +	}
  +	
       // ------------------------------------------------ Individual Test Methods
   
       public void testDocumentOrder(){
  -        if (!enabled){
  -            return;
  -        }
  -
  -        JXPathContext context = createContext();
  -        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);
  -    }
  -
  -    private void testDocumentOrder(JXPathContext context, String path1, String path2, int expected){
  -        NodePointer np1 = (NodePointer)context.getPointer(path1);
  -        NodePointer np2 = (NodePointer)context.getPointer(path2);
  -        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);
  -        }
  -        catch (Exception ex){
  -            System.err.println("Comparing paths '" + path1 + "' and '" + path2 + "'");
  -            ex.printStackTrace();
  -        }
  -    }
  -
  +        assertDocumentOrder(context, 
  +                "vendor/location", 
  +                "vendor/location/address/street", 
  +                -1);
  +                
  +        assertDocumentOrder(context, 
  +                "vendor/location[@id = '100']", 
  +                "vendor/location[@id = '101']", 
  +                -1);
  +                
  +        assertDocumentOrder(context, 
  +                "vendor//price:amount", 
  +                "vendor/location", 
  +                1);
  +    }
  +
  +	public void testSetValue(){
  +		assertXPathSetValue(context,
  +				"vendor/location[@id = '100']",
  +				"New Text");
  +		
  +		assertXMLSignature(context,
  +				"vendor/location[@id = '100']",
  +				"<E>New Text</E>",
  +				false, false, true, false);
  +				
  +		assertXPathSetValue(context,
  +				"vendor/location[@id = '101']",
  +				"Replacement Text");
  +
  +		assertXMLSignature(context,
  +				"vendor/location[@id = '101']",
  +				"<E>Replacement Text</E>",
  +				false, false, true, false);				
  +	}
  +	
       /**
        * Test JXPathContext.createPath() with various arguments
        */
       public void testCreatePath(){
  -        if (!enabled){
  -            return;
  -        }
  -
  -        JXPathContext context = createContext();
  -
           // Create a DOM element
  -        testCreatePath(context, "/vendor[1]/location[3]", "");
  +        assertXPathCreatePath(context, 
  +                "/vendor[1]/location[3]", 
  +                "",
  +                "/vendor[1]/location[3]");
   
           // Create a DOM element with contents
  -        testCreatePath(context, "/vendor[1]/location[3]/address/street", "",
  +        assertXPathCreatePath(context, 
  +                "/vendor[1]/location[3]/address/street", 
  +                "",
                   "/vendor[1]/location[3]/address[1]/street[1]");
   
           // Create a DOM attribute
  -        testCreatePath(context, "/vendor[1]/location[2]/@manager", "");
  -        testCreatePath(context, "/vendor[1]/location[1]/@name", "local");
  -    }
  -
  -    private void testCreatePath(JXPathContext context, String path, Object value){
  -        testCreatePath(context, path, value, path);
  -    }
  -
  -    private void testCreatePath(JXPathContext context, String path,
  -                Object value, String expectedPath){
  -        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());
  +        assertXPathCreatePath(context, 
  +                "/vendor[1]/location[2]/@manager", 
  +                "",
  +                "/vendor[1]/location[2]/@manager");
  +
  +        assertXPathCreatePath(context, 
  +                "/vendor[1]/location[1]/@name", 
  +                "local",
  +                "/vendor[1]/location[1]/@name");
       }
   
   
  @@ -200,40 +208,39 @@
        * Test JXPath.createPathAndSetValue() with various arguments
        */
       public void testCreatePathAndSetValue(){
  -        if (!enabled){
  -            return;
  -        }
  -        JXPathContext context = createContext();
  -
           // Create a XML element
  -        testCreatePathAndSetValue(context, "vendor/location[3]", "");
  +        assertXPathCreatePathAndSetValue(context, 
  +                "vendor/location[3]", 
  +                "",
  +                "/vendor[1]/location[3]");
   
           // Create a DOM element with contents
  -        testCreatePathAndSetValue(context, "vendor/location[3]/address/street", "Lemon Circle");
  +        assertXPathCreatePathAndSetValue(context, 
  +                "vendor/location[3]/address/street", 
  +                "Lemon Circle",
  +                "/vendor[1]/location[3]/address[1]/street[1]");
   
           // Create an attribute
  -        testCreatePathAndSetValue(context, "vendor/location[2]/@manager", "John Doe");
  -        testCreatePathAndSetValue(context, "vendor/location[1]/@manager", "John Doe");
  -
  -        testCreatePathAndSetValue(context, "vendor/product/name/attribute::price:language", "English");
  -    }
  -
  -    private void testCreatePathAndSetValue(JXPathContext context, String path, Object value){
  -        Pointer ptr = context.createPathAndSetValue(path, value);
  -        assertTrue("Pointer <" + path + ">", ptr != null);
  -        assertEquals("Created <" + path + ">", value, context.getValue(path));
  -        assertEquals("Pointer value <" + path + ">", value, ptr.getValue());
  +        assertXPathCreatePathAndSetValue(context, 
  +                "vendor/location[2]/@manager", 
  +                "John Doe",
  +                "/vendor[1]/location[2]/@manager");
  +                
  +        assertXPathCreatePathAndSetValue(context,
  +                "vendor/location[1]/@manager", 
  +                "John Doe",
  +                "/vendor[1]/location[1]/@manager");
  +
  +        assertXPathCreatePathAndSetValue(context, 
  +                "vendor/product/name/attribute::price:language", 
  +                "English",
  +                "/vendor[1]/product[1]/name[1]/@price:language");
       }
   
       /**
        * Test JXPathContext.removePath() with various arguments
        */
       public void testRemovePath(){
  -        if (!enabled){
  -            return;
  -        }
  -        JXPathContext context = createContext();
  -
           // Remove XML nodes
           context.removePath("vendor/location[@id = '101']//street/text()");
           assertEquals("Remove DOM text", "",
  @@ -249,10 +256,6 @@
       }
   
       public void testID(){
  -        if (!enabled){
  -            return;
  -        }
  -        JXPathContext context = createContext();
           context.setIdentityManager(new IdentityManager(){
               public Pointer getPointerByID(JXPathContext context, String id){
                   NodePointer ptr = (NodePointer)context.getPointer("/");
  @@ -260,228 +263,400 @@
                   return ptr.getPointerByID(context, id);
               }
           });
  -        context.setKeyManager(new KeyManager(){
  -            public Pointer getPointerByKey(JXPathContext context,
  -                                            String key, String value){
  -                return NodePointer.newNodePointer(null, "42", null);
  -            }
  -        });
  -        assertEquals("Test ID", "Tangerine Drive",
  -            context.getValue("id(101)//street"));
  -        assertEquals("Test ID Path", "id('101')/address[1]/street[1]",
  -            context.getPointer("id(101)//street").asPath());
  -
  -        context.setLenient(true);
  -        assertEquals("Test ID Path Null", "id(105)/address/street",
  -            context.getPointer("id(105)/address/street").asPath());
  -    }
  -
  -    public void testModel() throws Exception {
  -        if (!enabled){
  -            return;
  -        }
  -
  -        DocumentContainer docCtr = createDocumentContainer();
  -        JXPathContext context = createContext();
  -        context.getVariables().declareVariable("document", docCtr.getValue());
  -        context.getVariables().declareVariable("container", docCtr);
  -        testXPaths(context, dom_tests, false);
  -    }
  -
  -    private void testXPaths(JXPathContext ctx, XP xpath_tests[], boolean ignorePath) throws Exception{
  -        Exception exception = null;
  -        for  (int i=0; i < xpath_tests.length; i++) {
  -            try {
  -                Object actual;
  -                // System.err.println("XPATH: " + xpath_tests[i].xpath);
  -                if (xpath_tests[i].path){
  -                    if (ignorePath){
  -                        actual = xpath_tests[i].expected;
  -                    }
  -                    else {
  -                        if (xpath_tests[i].eval){
  -                            Iterator it = ctx.iteratePointers(xpath_tests[i].xpath);
  -                            List paths = new ArrayList();
  -                            while (it.hasNext()){
  -                                paths.add(((Pointer)it.next()).asPath());
  -                            }
  -                            actual = paths;
  -                        }
  -                        else {
  -                            ctx.setLenient(xpath_tests[i].lenient);
  -                            actual = ctx.getPointer(xpath_tests[i].xpath).asPath();
  -                        }
  -                    }
  -                }
  -                else {
  -                    if (xpath_tests[i].eval){
  -                        ArrayList list = new ArrayList();
  -                        Iterator it = ctx.iterate(xpath_tests[i].xpath);
  -                        while (it.hasNext()){
  -                            list.add(it.next());
  -                        }
  -                        actual = list;
  -                    }
  -                    else {
  -                        ctx.setLenient(xpath_tests[i].lenient);
  -                        actual = ctx.getValue(xpath_tests[i].xpath);
  -                        ctx.setLenient(false);
  -                    }
  -                }
  -                assertEquals("Evaluating <" + xpath_tests[i].xpath + ">", xpath_tests[i].expected, actual);
  -            }
  -            catch (Exception ex){
  -                System.err.println("Exception during <" + xpath_tests[i].xpath + ">");
  -                ex.printStackTrace();
  -                exception = ex;
  -            }
  -            if (exception != null){
  -                throw exception;
  -            }
  -        }
   
  -        // Make sure that location paths are properly constructed
  -        for (int i=0; i < xpath_tests.length; i++) {
  -            try {
  -                if (!xpath_tests[i].path && !xpath_tests[i].eval){
  -                    Pointer ptr = ctx.getPointer(xpath_tests[i].xpath);
  -                    Pointer test = ctx.getPointer(ptr.asPath());
  -                    assertEquals("Testing pointer for <" + xpath_tests[i].xpath + ">", ptr.asPath(), test.asPath());
  -                }
  -            }
  -            catch (Exception ex){
  -                System.err.println("Exception during pointer test <" + xpath_tests[i].xpath + ">");
  -                ex.printStackTrace();
  -            }
  -        }
  +        assertXPathValueAndPointer(context,
  +                "id(101)//street",
  +                "Tangerine Drive",
  +                "id('101')/address[1]/street[1]");
  +
  +        assertXPathPointerLenient(context,
  +                "id(105)/address/street",
  +                "id(105)/address/street");
  +    }
  +
  +    public void testAxisChild() {
  +        assertXPathValue(context,
  +                "vendor/location/address/street",
  +                "Orchard Road");
  +
  +        // child:: - first child does not match, need to search
  +        assertXPathValue(context,
  +                "vendor/location/address/city",
  +                "Fruit Market");
  +        // local-name(qualified)
  +        assertXPathValue(context,
  +                "local-name(vendor/product/price:amount)",
  +                "amount");
  +
  +        // local-name(non-qualified)
  +        assertXPathValue(context,
  +                "local-name(vendor/location)",
  +                "location");
  +
  +        // name (qualified)
  +        assertXPathValue(context,
  +                "name(vendor/product/price:amount)",
  +                "priceNS:amount");
  +
  +        // name (non-qualified)
  +        assertXPathValue(context,
  +                "name(vendor/location)",
  +                "location");
  +
  +        // namespace-uri (qualified)
  +        assertXPathValue(context,
  +                "namespace-uri(vendor/product/price:amount)",
  +                "priceNS");
  +
  +        // default namespace does not affect search
  +        assertXPathValue(context,
  +                "vendor/product/prix",
  +                "934.99");
  +
  +        // child:: with a wildcard
  +        assertXPathValue(context,
  +                "count(vendor/product/price:*)",
  +                new Double(2));
  +
  +        // child:: with a namespace and wildcard
  +        assertXPathValue(context,
  +                "count(vendor/product/value:*)",
  +                new Double(2));
  +
  +        // child:: with the default namespace
  +        assertXPathValue(context,
  +                "count(vendor/product/*)",
  +                new Double(2));
  +
  +        // child:: with a qualified name
  +        assertXPathValue(context,
  +                "vendor/product/price:amount",
  +                "45.95");
  +    }
  +
  +    public void testAxisChildIndexPredicate() {
  +        assertXPathValue(context,
  +                "vendor/location[2]/address/street",
  +                "Tangerine Drive");
  +    }
  +
  +    public void testAxisDescendant() {
  +        // descendant::
  +        assertXPathValue(context,
  +                "//street",
  +                "Orchard Road");
  +
  +        // descendent:: with a namespace and wildcard
  +        assertXPathValue(context,
  +                "count(//price:*)",
  +                new Double(2));
  +    }
  +
  +    public void testAxisParent() {
  +        // parent::
  +        assertXPathPointer(context,
  +                "//street/..",
  +                "/vendor[1]/location[1]/address[1]");
  +
  +        // parent:: (note reverse document order)
  +        assertXPathPointerIterator(context,
  +                "//street/..",
  +                list("/vendor[1]/location[2]/address[1]",
  +                     "/vendor[1]/location[1]/address[1]"));
  +
  +        // parent:: with a namespace and wildcard
  +        assertXPathValue(context,
  +                "vendor/product/price:sale/saleEnds/parent::price:*" +
  +                        "/saleEnds",
  +                "never");
  +    }
  +
  +    public void testAxisFollowingSibling() {
  +        // following-sibling::
  +        assertXPathValue(context,
  +                "vendor/location[.//employeeCount = 10]/" +
  +                        "following-sibling::location//street",
  +                "Tangerine Drive");
  +
  +        // following-sibling:: produces the correct pointer
  +        assertXPathPointer(context,
  +                "vendor/location[.//employeeCount = 10]/" +
  +                        "following-sibling::location//street",
  +                "/vendor[1]/location[2]/address[1]/street[1]");
  +    }
  +
  +    public void testAxisPrecedingSibling() {
  +        // preceding-sibling:: produces the correct pointer
  +        assertXPathPointer(context,
  +                "//location[2]/preceding-sibling::location//street",
  +                "/vendor[1]/location[1]/address[1]/street[1]");
  +    }
  +
  +    public void testAxisAttribute() {
  +        // attribute::
  +        assertXPathValue(context,
  +                "vendor/location/@id",
  +                "100");
  +
  +        // attribute:: produces the correct pointer
  +        assertXPathPointer(context,
  +                "vendor/location/@id",
  +                "/vendor[1]/location[1]/@id");
  +
  +        // iterate over attributes
  +        assertXPathValueIterator(context,
  +                "vendor/location/@id",
  +                list("100", "101"));
  +
  +        // Using different prefixes for the same namespace
  +        assertXPathValue(context,
  +                "vendor/product/price:amount/@price:discount",
  +                "10%");
  +        assertXPathValue(context,
  +                "vendor/product/value:amount/@value:discount",
  +                "10%");
  +
  +        // namespace uri for an attribute
  +        assertXPathValue(context,
  +                "namespace-uri(vendor/product/price:amount/@price:discount)",
  +                "priceNS");
  +
  +        // local name of an attribute
  +        assertXPathValue(context,
  +                "local-name(vendor/product/price:amount/@price:discount)",
  +                "discount");
  +
  +        // name for an attribute
  +        assertXPathValue(context,
  +                "name(vendor/product/price:amount/@price:discount)",
  +                "priceNS:discount");
  +
  +        // attribute:: with the default namespace
  +        assertXPathValue(context,
  +                "vendor/product/price:amount/@discount",
  +                "20%");
  +
  +        // namespace uri of an attribute with the default namespace
  +        assertXPathValue(context,
  +                "namespace-uri(vendor/product/price:amount/@discount)",
  +                "");
  +
  +        // local name of an attribute with the default namespace
  +        assertXPathValue(context,
  +                "local-name(vendor/product/price:amount/@discount)",
  +                "discount");
  +
  +        // name of an attribute with the default namespace
  +        assertXPathValue(context,
  +                "name(vendor/product/price:amount/@discount)",
  +                "discount");
  +
  +        // attribute:: with a namespace and wildcard
  +        assertXPathValueIterator(context,
  +                "vendor/product/price:amount/@price:*",
  +                list("10%"));
  +
  +        // attribute:: with a wildcard
  +        assertXPathValueIterator(context,
  +                "vendor/location[1]/@*",
  +                set("100", "", "local"));
  +
  +        // attribute:: with default namespace and wildcard
  +        assertXPathValueIterator(context,
  +                "vendor/product/price:amount/@*",
  +                list("20%"));
  +
  +
  +        // Empty attribute
  +        assertXPathValue(context,
  +                "vendor/location/@manager",
  +                "");
  +
  +        // Missing attribute
  +        assertXPathValueLenient(context,
  +                "vendor/location/@missing",
  +                null);
  +
  +        // Using attribute in a predicate
  +        assertXPathValue(context,
  +                "vendor/location[@id='101']//street",
  +                "Tangerine Drive");
  +    }
  +
  +    public void testAxisNamespace() {
  +        // namespace::
  +        assertXPathValue(context,
  +                "vendor/product/prix/namespace::price",
  +                "priceNS");
  +
  +        // namespace:: pointer
  +        assertXPathPointer(context,
  +                "vendor/product/prix/namespace::price",
  +                "/vendor[1]/product[1]/prix[1]/namespace::price");
  +
  +        // namespace::*
  +        assertXPathValue(context,
  +                "count(vendor/product/namespace::*)",
  +                new Double(3));
  +
  +        // name of namespace
  +        assertXPathValue(context,
  +                "name(vendor/product/prix/namespace::price)",
  +                "priceNS:price");
  +
  +        // local name of namespace
  +        assertXPathValue(context,
  +                "local-name(vendor/product/prix/namespace::price)",
  +                "price");
  +    }
  +
  +    public void testAxisAncestor() {
  +        // ancestor::
  +        assertXPathValue(context,
  +                "vendor/product/price:sale/saleEnds/" +
  +                        "ancestor::price:sale/saleEnds",
  +                "never");
  +
  +        // ancestor:: with a wildcard
  +        assertXPathValue(context,
  +                "vendor/product/price:sale/saleEnds/ancestor::price:*" +
  +                        "/saleEnds",
  +                "never");
  +    }
  +
  +    public void testAxisAncestorOrSelf() {
  +        // ancestor-or-self::
  +        assertXPathValue(context,
  +                "vendor/product/price:sale/" +
  +                        "ancestor-or-self::price:sale/saleEnds",
  +                "never");
  +    }
  +
  +    public void testAxisFollowing() {
  +        assertXPathValueIterator(context,
  +                "vendor/contact/following::location//street",
  +                list("Orchard Road", "Tangerine Drive"));
  +
  +        // following:: with a namespace
  +        assertXPathValue(context,
  +                "//location/following::price:sale/saleEnds",
  +                "never");
  +    }
  +
  +    public void testAxisSelf() {
  +        // self:: with a namespace
  +        assertXPathValue(context,
  +                "//price:sale/self::price:sale/saleEnds",
  +                "never");
  +
  +        // self:: with an unmatching name
  +        assertXPathValueLenient(context,
  +                "//price:sale/self::x/saleEnds",
  +                null);
  +    }
  +
  +    public void testNodeTypeComment() {
  +        // comment()
  +        assertXPathValue(context,
  +                "//product/comment()",
  +                "We are not buying this product, ever");
  +    }
  +
  +    public void testNodeTypeText() {
  +        // text()
  +        assertXPathValue(context,
  +                "//product/text()[. != '']",
  +                "We love this product.");
  +
  +        // text() pointer
  +        assertXPathPointer(context,
  +                "//product/text()",
  +                "/vendor[1]/product[1]/text()[1]");
  +
  +    }
  +
  +    public void testNodeTypeProcessingInstruction() {
  +        // processing-instruction() without an argument
  +        assertXPathValue(context,
  +                "//product/processing-instruction()",
  +                "do not show anybody");
  +
  +        // processing-instruction() with an argument
  +        assertXPathValue(context,
  +                "//product/processing-instruction('report')",
  +                "average only");
  +
  +        // processing-instruction() pointer without an argument
  +        assertXPathPointer(context,
  +                "//product/processing-instruction('report')",
  +                "/vendor[1]/product[1]/processing-instruction('report')[1]");
  +
  +        // processing-instruction name
  +        assertXPathValue(context,
  +                "name(//product/processing-instruction()[1])",
  +                "security");
  +    }
  +
  +    public void testLang() {
  +        // xml:lang built-in attribute
  +        assertXPathValue(context,
  +                "//product/prix/@xml:lang",
  +                "fr");
  +
  +        // lang() used the built-in xml:lang attribute
  +        assertXPathValue(context,
  +                "//product/prix[lang('fr')]",
  +                "934.99");
  +
  +        // Default language
  +        assertXPathValue(context,
  +                "//product/price:sale[lang('en')]/saleEnds",
  +                "never");
  +    }
  +
  +    public void testDocument() {
  +        assertXPathValue(context,
  +                "$document/vendor/location[1]//street",
  +                "Orchard Road");
  +
  +        assertXPathPointer(context,
  +                "$document/vendor/location[1]//street",
  +                "$document/vendor[1]/location[1]/address[1]/street[1]");
  +
  +        assertXPathValue(context,
  +                "$document/vendor//street",
  +                "Orchard Road");
  +
  +    }
  +
  +    public void testContainer() {
  +        assertXPathValue(context,
  +                "$container/vendor//street",
  +                "Orchard Road");
  +
  +        assertXPathValue(context,
  +                "$container//street",
  +                "Orchard Road");
  +
  +        assertXPathPointer(context,
  +                "$container//street",
  +                "$container/vendor[1]/location[1]/address[1]/street[1]");
  +
  +        // Conversion to number
  +        assertXPathValue(context,
  +                "number(vendor/location/employeeCount)",
  +                new Double(10));
  +    }
  +
  +    public void testTypeConversions() {
  +        // Implicit conversion to number
  +        assertXPathValue(context,
  +                "vendor/location/employeeCount + 1",
  +                new Double(11));
  +
  +        // Implicit conversion to boolean
  +        assertXPathValue(context,
  +                "vendor/location/employeeCount and true()",
  +                Boolean.TRUE);
       }
  -
  -    private static class XP {
  -        public String xpath;
  -        public Object expected;
  -        public boolean eval;
  -        public boolean path;
  -        public boolean lenient;
  -
  -        public XP(String xpath,  Object expected, boolean eval, boolean path, boolean lenient){
  -            this.xpath = xpath;
  -            this.expected = expected;
  -            this.eval = eval;
  -            this.path = path;
  -            this.lenient = lenient;
  -        }
  -    }
  -
  -    private static XP test(String xpath, Object expected){
  -        return new XP(xpath, expected, false, false, false);
  -    }
  -
  -    private static XP testLenient(String xpath, Object expected){
  -        return new XP(xpath, expected, false, false, true);
  -    }
  -
  -    private static XP testEval(String xpath, Object expected){
  -        return new XP(xpath, expected, true, false, false);
  -    }
  -
  -    private static XP testPath(String xpath, Object expected){
  -        return new XP(xpath, expected, false, true, true);
  -    }
  -
  -    private static XP testEvalPath(String xpath, Object expected){
  -        return new XP(xpath, expected, true, true, false);
  -    }
  -
  -    private static List list(Object o1){
  -        List list = new ArrayList();
  -        list.add(o1);
  -        return list;
  -    }
  -
  -    private static List list(Object o1, Object o2){
  -        List list = new ArrayList();
  -        list.add(o1);
  -        list.add(o2);
  -        return list;
  -    }
  -
  -
  -    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)),
  -        test("vendor/location/employeeCount + 1", new Double(11)),
  -        test("vendor/location/employeeCount and true()", Boolean.TRUE),
  -        test("vendor/location[.//employeeCount = 10]/following-sibling::location//street", "Tangerine Drive"),
  -        testPath("vendor/location[.//employeeCount = 10]/following-sibling::location//street",
  -                "/vendor[1]/location[2]/address[1]/street[1]"),
  -        testPath("//location[2]/preceding-sibling::location//street",
  -                "/vendor[1]/location[1]/address[1]/street[1]"),
  -        test("vendor/location/@id", "100"),
  -        testPath("vendor/location/@id", "/vendor[1]/location[1]/@id"),
  -        testEval("vendor/location/@id", list("100", "101")),
  -        test("vendor/product/price:amount", "45.95"),
  -        test("namespace-uri(vendor/product/price:amount)", "priceNS"),
  -        test("local-name(vendor/product/price:amount)", "amount"),
  -        test("name(vendor/product/price:amount)", "priceNS:amount"),
  -        test("vendor/product/prix", "934.99"),
  -        test("vendor/product/prix/namespace::price", "priceNS"),
  -        testPath("vendor/product/prix/namespace::price", "/vendor[1]/product[1]/prix[1]/namespace::price"),
  -        test("count(vendor/product/namespace::*)", new Double(3)),
  -        test("name(vendor/product/prix/namespace::price)", "priceNS:price"),
  -        test("local-name(vendor/product/prix/namespace::price)", "price"),
  -        test("vendor/product/price:amount/@price:discount", "10%"),
  -        test("vendor/product/value:amount/@value:discount", "10%"),
  -        test("namespace-uri(vendor/product/price:amount/@price:discount)", "priceNS"),
  -        test("local-name(vendor/product/price:amount/@price:discount)", "discount"),
  -        test("name(vendor/product/price:amount/@price:discount)", "priceNS:discount"),
  -        test("vendor/product/price:amount/@discount", "20%"),
  -        test("namespace-uri(vendor/product/price:amount/@discount)", ""),
  -        test("local-name(vendor/product/price:amount/@discount)", "discount"),
  -        test("name(vendor/product/price:amount/@discount)", "discount"),
  -        test("vendor/product/price:sale/saleEnds/ancestor::price:sale/saleEnds", "never"),
  -        test("vendor/product/price:sale/ancestor-or-self::price:sale/saleEnds", "never"),
  -        test("vendor/product/price:sale/saleEnds/ancestor::price:*" + "/saleEnds", "never"),
  -        test("count(vendor/product/price:*)", new Double(2)),
  -        test("count(vendor/product/value:*)", new Double(2)),
  -        test("count(vendor/product/*)", new Double(2)),
  -        testEval("vendor/product/price:amount/@price:*", list("10%")),
  -        testEval("vendor/product/price:amount/@*", list("20%")),
  -        test("count(//price:*)", new Double(2)),
  -        test("vendor/product/price:sale/saleEnds/parent::price:*" + "/saleEnds", "never"),
  -        test("//location/following::price:sale/saleEnds", "never"),
  -        test("//price:sale/self::price:sale/saleEnds", "never"),
  -        testLenient("//price:sale/self::x/saleEnds", null),
  -
  -        test("//product/comment()", "We are not buying this product, ever"),
  -        test("//product/text()[. != '']", "We love this product."),
  -        testPath("//product/text()", "/vendor[1]/product[1]/text()[1]"),
  -        test("//product/processing-instruction()", "do not show anybody"),
  -        test("//product/processing-instruction('report')", "average only"),
  -        testPath("//product/processing-instruction('report')",
  -            "/vendor[1]/product[1]/processing-instruction('report')[1]"),
  -        test("name(//product/processing-instruction()[1])", "security"),
  -
  -        test("//product/prix/@xml:lang", "fr"),
  -        test("//product/prix[lang('fr')]", "934.99"),
  -        test("//product/price:sale[lang('en')]/saleEnds", "never"),
  -        test("vendor/location/@manager", ""),
  -        testLenient("vendor/location/@missing", null),
  -        test("count(vendor/location[1]/@*)", new Double(3)),
  -        test("vendor/location[@id='101']//street", "Tangerine Drive"),
  -
  -        test("$document/vendor/location[1]//street", "Orchard Road"),
  -        testPath("$document/vendor/location[1]//street",
  -            "$document/vendor[1]/location[1]/address[1]/street[1]"),
  -        test("$document/vendor//street", "Orchard Road"),
  -        test("$container/vendor//street", "Orchard Road"),
  -        test("$container//street", "Orchard Road"),
  -        testPath("$container//street", "$container/vendor[1]/location[1]/address[1]/street[1]"),
  -
  -        testEval("vendor/contact/following::location//street",
  -            list("Orchard Road", "Tangerine Drive")),
  -   };
   }
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/BeanModelTestCase.java
  
  Index: BeanModelTestCase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/BeanModelTestCase.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Locale;
  
  import junit.framework.TestSuite;
  
  import org.apache.commons.jxpath.AbstractFactory;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.JXPathTestCase;
  import org.apache.commons.jxpath.NestedTestBean;
  import org.apache.commons.jxpath.Pointer;
  import org.apache.commons.jxpath.ri.QName;
  import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
  import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
  import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
  
  /**
   * Abstract superclass for Bean access with JXPath.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  
  public abstract class BeanModelTestCase extends JXPathTestCase
  {
      private static boolean enabled = true;
      private JXPathContext context;
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public BeanModelTestCase(String name){
          super(name);
      }
  
      public void setUp(){
          if (context == null){
              context = JXPathContext.newContext(createContextBean());
              context.setLocale(Locale.US);
              context.setFactory(getAbstractFactory());
          }
      }
  
      protected abstract Object createContextBean();
      protected abstract AbstractFactory getAbstractFactory();
  
      /**
       * Test property iterators, the core of the graph traversal engine
       */
      public void testIndividualIterators(){
          if (!enabled){
              return;
          }
  //        testIndividual(0, 0, true, false, 3);
          testIndividual(+1, 0, true, false, 0);
          testIndividual(-1, 0, true, false, 4);
  
          testIndividual(0, -1, true, true, 4);
          testIndividual(+1, -1, true, true, 4);
          testIndividual(-1, -1, true, true, 0);
  
          testIndividual(0, 1, true, false, 2);
          testIndividual(0, 1, true, true, 1);
  
          testIndividual(0, 0, false, false, 4);
          testIndividual(0, 0, false, true, 4);
      }
  
      private void testIndividual(int relativePropertyIndex, int offset,
                  boolean useStartLocation, boolean reverse, int expected)
      {
          PropertyOwnerPointer root =
              (PropertyOwnerPointer)NodePointer.newNodePointer(
                      new QName(null, "root"), createContextBean(),
                      Locale.getDefault());
  
          NodeIterator it;
  
          PropertyPointer start = null;
  
          if (useStartLocation){
              start = root.getPropertyPointer();
              start.setPropertyIndex(relativeProperty(start, relativePropertyIndex));
              start.setIndex(offset);
          }
          it = root.childIterator(
                  new NodeNameTest(new QName(null, "integers")),
                  reverse, start);
  
          int size = 0;
          while(it.setPosition(it.getPosition() + 1)){
              size++;
          }
          assertEquals("ITERATIONS: Individual, relativePropertyIndex=" +
              relativePropertyIndex +
              ", offset=" + offset + ", useStartLocation=" + useStartLocation +
              ", reverse=" + reverse, expected, size);
      }
  
      /**
       * Test property iterators with multiple properties returned
       */
      public void testMultipleIterators(){
          if (!enabled){
              return;
          }
          testMultiple(0, 0, true, false, 20);
  
          testMultiple(3, 0, true, false, 16);
          testMultiple(3, -1, true, true, 8);
          testMultiple(3, 0, true, true, 4);
          testMultiple(0, 0, false, false, 21);
          testMultiple(0, 0, false, true, 21);
  
          testMultiple(3, 1, true, false, 15);
          testMultiple(3, 3, true, false, 13);
      }
  
      private void testMultiple(int propertyIndex, int offset,
                  boolean useStartLocation, boolean reverse, int expected)
      {
          PropertyOwnerPointer root =
                  (PropertyOwnerPointer)NodePointer.newNodePointer(
                          new QName(null, "root"), createContextBean(),
                          Locale.getDefault());
          NodeIterator it;
  
          PropertyPointer start = null;
  
          if (useStartLocation){
              start = root.getPropertyPointer();
              start.setPropertyIndex(propertyIndex);
              start.setIndex(offset);
          }
          it = root.childIterator(null, reverse, start);
  
          int size = 0;
          while(it.setPosition(it.getPosition() + 1)){
  //            System.err.println("LOC: " + it.getCurrentNodePointer());
              size++;
          }
          assertEquals("ITERATIONS: Multiple, propertyIndex=" + propertyIndex +
              ", offset=" + offset + ", useStartLocation=" + useStartLocation +
              ", reverse=" + reverse, expected, size);
      }
  
      private int relativeProperty(PropertyPointer holder, int offset){
          String[] names = holder.getPropertyNames();
          for (int i = 0; i < names.length; i++){
              if (names[i].equals("integers")){
                  return i + offset;
              }
          }
          return -1;
      }
  
      public void testIteratePropertyArrayWithHasNext(){
          if (!enabled){
              return;
          }
          JXPathContext context = JXPathContext.newContext(createContextBean());
          Iterator it = context.iteratePointers("/integers");
          List actual = new ArrayList();
          while(it.hasNext()){
              actual.add(((Pointer)it.next()).asPath());
          }
          assertEquals("Iterating 'hasNext'/'next'<" + "/integers" + ">",
              list("/integers[1]", "/integers[2]", "/integers[3]", "/integers[4]"),
              actual);
      }
  
      public void testIteratePropertyArrayWithoutHasNext(){
          if (!enabled){
              return;
          }
          JXPathContext context = JXPathContext.newContext(createContextBean());
          Iterator it = context.iteratePointers("/integers");
          List actual = new ArrayList();
          for (int i = 0; i < 4; i++){
              actual.add(it.next().toString());
          }
          assertEquals("Iterating 'next'<" + "/integers" + ">",
              list("/integers[1]", "/integers[2]", "/integers[3]", "/integers[4]"),
              actual);
      }
  
      public void testIterateAndSet(){
          if (!enabled){
              return;
          }
  
          JXPathContext context = JXPathContext.newContext(createContextBean());
  
          Iterator it = context.iteratePointers("beans/int");
          int i = 5;
          while (it.hasNext()){
              NodePointer pointer = (NodePointer)it.next();
              pointer.setValue(new Integer(i++));
          }
  
          it = context.iteratePointers("beans/int");
          List actual = new ArrayList();
          while (it.hasNext()){
              actual.add(((Pointer)it.next()).getValue());
          }
          assertEquals("Iterating <" + "beans/int" + ">",
                  list(new Integer(5), new Integer(6)), actual);
      }
  
      /**
       * Test contributed by Kate Dvortsova
       */
      public void testIteratePointerSetValue() {
          JXPathContext context = JXPathContext.newContext(createContextBean());
  
          assertXPathValue(context, "/beans[1]/name", "Name 1");
          assertXPathValue(context, "/beans[2]/name", "Name 2");
  
          // Test setting via context
          context.setValue("/beans[2]/name", "Name 2 set");
          assertXPathValue(context, "/beans[2]/name", "Name 2 set");
  
          // Restore original value
          context.setValue("/beans[2]/name", "Name 2");
          assertXPathValue(context, "/beans[2]/name", "Name 2");
  
          int iter_count = 0;
          Iterator iter = context.iteratePointers("/beans/name");
          while (iter.hasNext()) {
              iter_count++;
              Pointer pointer = (Pointer) iter.next();
              String s = (String) pointer.getValue();
              s = s + "suffix";
              pointer.setValue(s);
              assertEquals("pointer.getValue", s, pointer.getValue());
              // fails right here, the value isn't getting set in the bean.
              assertEquals("context.getValue", s, context.getValue(pointer.asPath()));
          }
          assertEquals("Iteration count", 2, iter_count);
  
          assertXPathValue(context, "/beans[1]/name", "Name 1suffix");
          assertXPathValue(context, "/beans[2]/name", "Name 2suffix");
      }
  
      public void testRoot(){
          assertXPathValueAndPointer(context,
                  "/",
                  context.getContextBean(),
                  "/");
      }
  
      public void testAxisAncestor(){
          // ancestor::
          assertXPathValue(context,
                  "int/ancestor::root = /",
                  Boolean.TRUE);
  
          assertXPathValue(context,
                  "count(beans/name/ancestor-or-self::node())",
                  new Double(5));
  
          assertXPathValue(context,
                  "beans/name/ancestor-or-self::node()[3] = /",
                  Boolean.TRUE);
      }
  
      public void testAxisChild(){
          assertXPathValue(context,
                  "boolean",
                  Boolean.FALSE);
  
          assertXPathPointer(context,
                  "boolean",
                  "/boolean");
  
          assertXPathPointerIterator(context,
                  "boolean",
                  list("/boolean"));
  
          // Count elements in a child collection
          assertXPathValue(context,
                  "count(set)",
                  new Double(3));
  
  //        assertXPathValue(context,"boolean/class/name", "java.lang.Boolean");
  
          // Child with namespace - should not find any
          assertXPathValueIterator(context,
                  "foo:boolean",
                  list());
  
          // Count all children with a wildcard
          assertXPathValue(context,
                  "count(*)",
                  new Double(21));
  
          // Same, constrained by node type = node()
          assertXPathValue(context,
                  "count(child::node())",
                  new Double(21));
      }
  
      public void testAxisChildNestedBean(){
          // Nested bean
          assertXPathValue(context,
                  "nestedBean/name",
                  "Name 0");
  
          assertXPathPointer(context,
                  "nestedBean/name",
                  "/nestedBean/name");
  
          assertXPathPointerIterator(context,
                  "nestedBean/name",
                  list("/nestedBean/name"));
      }
  
      public void testAxisChildNestedCollection(){
          assertXPathValueIterator(context,
                  "integers",
                  list(new Integer(1), new Integer(2),
                       new Integer(3), new Integer(4)));
  
          assertXPathPointer(context,
                  "integers",
                  "/integers");
  
          assertXPathPointerIterator(context,
                  "integers",
                  list("/integers[1]", "/integers[2]",
                       "/integers[3]", "/integers[4]"));
      }
  
      public void testIndexPredicate(){
          assertXPathValue(context,
                  "integers[2]",
                  new Integer(2));
  
          assertXPathPointer(context,
                  "integers[2]",
                  "/integers[2]");
  
          assertXPathPointerIterator(context,
                  "integers[2]",
                  list("/integers[2]"));
  
          assertXPathValue(context,
                  "beans[1]/name",
                  "Name 1");
  
          assertXPathPointer(context,
                  "beans[1]/name",
                  "/beans[1]/name");
  
          assertXPathValueIterator(context,
                  "beans[1]/strings",
                  list("String 1", "String 2", "String 3"));
  
          assertXPathValueIterator(context,
                  "beans/strings[2]",
                  list("String 2", "String 2"));
  
          // Find the first match
          assertXPathValue(context,
                  "beans/strings[2]",
                  "String 2");
  
          // Indexing in a set collected from a UnionContext
          assertXPathValue(context,
                  "(beans/strings[2])[1]",
                  "String 2");
      }
  
      public void testAxisDescendant(){
         // descendant::
          assertXPathValue(context,
                  "count(descendant::node())",
                  new Double(65));
  
          // Should not find any descendants with name root
          assertXPathValue(context,
                  "count(descendant::root)",
                  new Double(0));
  
          assertXPathValue(context,
                  "count(descendant::name)",
                  new Double(7));
      }
  
      public void testAxisDescendantOrSelf(){
          // descendant-or-self::
          assertXPathValueIterator(context,
                  "descendant-or-self::name",
                  set("Name 1", "Name 2", "Name 3", "Name 6",
                       "Name 0", "Name 5", "Name 4"));
  
          // Same - abbreviated syntax
          assertXPathValueIterator(context,
                  "//name",
                  set("Name 1", "Name 2", "Name 3", "Name 6",
                       "Name 0", "Name 5", "Name 4"));
  
          // See that it actually finds self
          assertXPathValue(context,
                  "count(descendant-or-self::root)",
                  new Double(1));
  
          // Combine descendant-or-self:: and and self::
          assertXPathValue(context,
                  "count(nestedBean//.)",
                  new Double(7));
  
          // Combine descendant-or-self:: and and self::name
          assertXPathValue(context,
                  "count(//self::beans)",
                  new Double(2));
  
          // Count all nodes in the tree
          assertXPathValue(context,
                  "count(descendant-or-self::node())",
                  new Double(66));
  
      }
  
      public void testAxisFollowing(){
          // following::
          assertXPathValue(context,
                  "count(nestedBean/strings[2]/following::node())",
                  new Double(21));
  
          assertXPathValue(context,
                  "count(nestedBean/strings[2]/following::strings)",
                  new Double(7));
      }
  
      public void testAxisFollowingSibling(){
          // following-sibling::
          assertXPathValue(context,
                  "count(/nestedBean/following-sibling::node())",
                  new Double(8));
  
          assertXPathValue(context,
                  "count(/nestedBean/following-sibling::object)",
                  new Double(1));
  
          // Combine parent:: and following-sibling::
          assertXPathValue(context,
                  "count(/nestedBean/boolean/../following-sibling::node())",
                  new Double(8));
  
          assertXPathValue(context,
                  "count(/nestedBean/boolean/../following-sibling::object)",
                  new Double(1));
  
          // Combine descendant:: and following-sibling::
          assertXPathValue(context,
                  "count(/descendant::boolean/following-sibling::node())",
                  new Double(53));
  
          assertXPathValue(context,
                  "count(/descendant::boolean/following-sibling::name)",
                  new Double(7));
      }
  
      public void testAxisParent(){
          // parent::
          assertXPathValue(context,
                  "count(/beans/..)",
                  new Double(1));
  
          assertXPathValue(context,
                  "count(//..)",
                  new Double(9));
  
          assertXPathValue(context,
                  "count(//../..)",
                  new Double(2));
  
          assertXPathValueIterator(context,
                  "//parent::beans/name",
                  list("Name 1", "Name 2"));
      }
  
      public void testAxisPreceding(){
          // preceding::
          assertXPathValue(context,
                  "count(beans[2]/int/preceding::node())",
                  new Double(8));
  
          assertXPathValue(context,
                  "count(beans[2]/int/preceding::boolean)",
                  new Double(2));
      }
  
      public void testAxisPrecedingSibling(){
          // preceding-sibling::
          assertXPathValue(context,
                  "count(/boolean/preceding-sibling::node())",
                  new Double(2));
  
          assertXPathValue(context,
                  "count(/nestedBean/int/../preceding-sibling::node())",
                  new Double(12));
  
          assertXPathValue(context,
                  "count(/descendant::int/preceding-sibling::node())",
                  new Double(10));
      }
  
      public void testAxisSelf(){
          // self::
          assertXPathValue(context,
                  "self::node() = /",
                  Boolean.TRUE);
  
          assertXPathValue(context,
                  "self::root = /",
                  Boolean.TRUE);
      }
  
      public void testUnion(){
          // Union - note corrected document order
          assertXPathValueIterator(context,
                  "integers | beans[1]/strings",
                  list("String 1", "String 2", "String 3",
                       new Integer(1), new Integer(2),
                       new Integer(3), new Integer(4)));
  
          assertXPathValue(context,
                  "count((integers | beans[1]/strings)[contains(., '1')])",
                  new Double(2));
  
          assertXPathValue(context,
                  "count((integers | beans[1]/strings)[name(.) = 'strings'])",
                  new Double(3));
  
          // Note that the following is different from "integer[2]" -
          // it is a filter expression
          assertXPathValue(context,
                  "(integers)[2]",
                  new Integer(2));
       }
  
      public void testAxisAttribute(){
          // Attributes are just like children to beans
          assertXPathValue(context,
                  "count(@*)",
                  new Double(21.0));
  
          // Unknown attribute
          assertXPathValueLenient(context,
                  "@foo",
                  null);
      }
  
      /**
       * Testing the pseudo-attribute "name" that java beans
       * objects appear to have.
       */
      public void testAttributeName(){
          assertXPathValue(context,
                  "nestedBean[@name = 'int']",
                  new Integer(1));
  
          assertXPathPointer(context,
                  "nestedBean[@name = 'int']",
                  "/nestedBean/int");
      }
  
      public void testAttributeLang(){
  
          assertXPathValue(context,
                  "@xml:lang",
                  "en-US");
  
          assertXPathValue(context,
                  "count(@xml:*)",
                  new Double(1));
  
          assertXPathValue(context,"lang('en')", Boolean.TRUE);
          assertXPathValue(context,"lang('fr')", Boolean.FALSE);
          assertXPathValueIterator(context, "beans[1]/strings[string-length() = 8]", list("String 1", "String 2", "String 3"));
          assertXPathValue(context,"boolean(boolean)", Boolean.FALSE);
          assertXPathValue(context,"boolean(integers[position() < 3])", Boolean.TRUE);
          assertXPathValue(context,"boolean(integers[position() > 4])", Boolean.FALSE);
          assertXPathValue(context,"sum(integers)", new Double(10));
      }
  
      public void testBooleanPredicate(){
          // use child axis
          assertXPathValue(context,
                  "beans[int > 2]/name",
                  "Name 2");
  
          // use some fancy axis and the child axis in the predicate
          assertXPathValueIterator(context,
                  "//self::node()[name = 'Name 0']/name",
                  list("Name 0"));
  
          // use context-dependent function in the predicate
          assertXPathValue(context,
                  "beans/strings[name(.)='strings'][2]",
                  "String 2");
  
          // use context-independent function in the predicate
          assertXPathValueIterator(context,
                  "//self::node()[name(.) = concat('n', 'a', 'm', 'e')]",
                  list("Name 1", "Name 2", "Name 3", "Name 6",
                       "Name 0", "Name 5", "Name 4"));
  
          assertXPathValueIterator(context,
                  "integers[position()<3]",
                  list(new Integer(1), new Integer(2)));
      }
  
      public void testDocumentOrder(){
          assertDocumentOrder(context,
                  "boolean", "int", -1);
  
          assertDocumentOrder(context,
                  "integers[1]", "integers[2]", -1);
  
          assertDocumentOrder(context,
                  "integers[1]", "integers[1]", 0);
  
          assertDocumentOrder(context,
                  "nestedBean/int", "nestedBean", 1);
  
          assertDocumentOrder(context,
                  "nestedBean/int", "nestedBean/strings", -1);
  
          assertDocumentOrder(context,
                  "nestedBean/int", "object/int", -1);
      }
  
      public void testSetPropertyValue(){
          // Simple property
          assertXPathSetValue(context,
                  "int",
                  new Integer(2));
  
          // Simple property with conversion from string
          assertXPathSetValue(context,
                  "int",
                  "3",
                  new Integer(3));
  
          // Simple property with conversion from array
          assertXPathSetValue(context,
                  "int",
                  new int[]{4},
                  new Integer(4));
  
          // Attribute (which is the same as a child for beans
          assertXPathSetValue(context,
                  "@int",
                  new Integer(10));
      }
  
      public void testSetCollectionElement(){
          // Collection element
          assertXPathSetValue(context,
                  "integers[2]",
                  new Integer(5));
  
          // Collection element with conversion
          assertXPathSetValue(context,
                  "integers[2]",
                  new int[]{6},
                  new Integer(6));
      }
  
      public void testSetContextDependentNode(){
          // Find node without using SimplePathInterpreter
          assertXPathSetValue(context,
                  "integers[position() = 1]",
                  new Integer(8));
  
          // Find node without using SimplePathInterpreter and set its property
          assertXPathSetValue(context,
                  "beans[name = 'Name 1']/int",
                  new Integer(9));
  
      }
  
      public void testSetNonPrimitiveValue(){
          // First, let's see if we can set a collection element to null
          assertXPathSetValue(context,
                  "beans[2]",
                  null);
  
          // Now, assign it a whole bean
          context.setValue("beans[2]", new NestedTestBean("Name 9"));
  
          assertEquals("Modified <" + "beans[2]/name" + ">",
              "Name 9", context.getValue("beans[2]/name"));
      }
  
      public void testCreatePath(){
          context.setValue("nestedBean", null);
  
          // Calls factory.createObject(..., TestBean, "nestedBean")
          assertXPathCreatePath(context,
                  "/nestedBean/int",
                  new Integer(1),
                  "/nestedBean/int");
      }
  
      public void testCreatePathAndSetValue(){
          context.setValue("nestedBean", null);
  
          // Calls factory.createObject(..., TestBean, "nestedBean")
          assertXPathCreatePathAndSetValue(context,
                  "/nestedBean/int",
                  new Integer(2),
                  "/nestedBean/int");
      }
  
      public void testCreatePathExpandNewCollection(){
          context.setValue("beans", null);
  
          // Calls factory.expandCollection(..., testBean, "beans", 2), then
          // factory.createObject(..., testBean, "beans", 2)
          assertXPathCreatePath(context,
                  "/beans[2]/int",
                  new Integer(1),
                  "/beans[2]/int");
      }
  
      public void testCreatePathAndSetValueExpandNewCollection(){
          context.setValue("beans", null);
  
          // Calls factory.expandCollection(..., testBean, "beans", 2), then
          // factory.createObject(..., testBean, "beans", 2)
          assertXPathCreatePathAndSetValue(context,
                  "/beans[2]/int",
                  new Integer(2),
                  "/beans[2]/int");
      }
  
      public void testCreatePathExpandExistingCollection(){
          // Another, but the collection already exists
          assertXPathCreatePath(context,
                  "/beans[3]/int",
                  new Integer(1),
                  "/beans[3]/int");
      }
  
      public void testCreatePathAndSetValueExpandExistingCollection(){
          // Another, but the collection already exists
          assertXPathCreatePathAndSetValue(context,
                  "/beans[3]/int",
                  new Integer(2),
                  "/beans[3]/int");
      }
  
      public void testCreatePathCreateBeanExpandCollection(){
          context.setValue("nestedBean", null);
  
          // Calls factory.createObject(..., TestBean, "nestedBean")
          // Calls factory.expandCollection(..., nestedBean, "strings", 2)
          assertXPathCreatePath(context,
                  "/nestedBean/strings[2]",
                  "String 2",
                  "/nestedBean/strings[2]");
      }
  
      public void testCreatePathAndSetValueCreateBeanExpandCollection(){
          context.setValue("nestedBean", null);
  
          // Calls factory.createObject(..., TestBean, "nestedBean")
          // Calls factory.expandCollection(..., nestedBean, "strings", 2)
          assertXPathCreatePathAndSetValue(context,
                  "/nestedBean/strings[2]",
                  "Test",
                  "/nestedBean/strings[2]");
      }
  
      public void testRemovePathPropertyValue(){
          // Remove property value
          context.removePath("nestedBean/int");
          assertEquals("Remove property value", new Integer(0),
                      context.getValue("nestedBean/int"));
      }
  
      public void testRemovePathArrayElement(){
          // Assigns a new array to the property
          context.removePath("nestedBean/strings[1]");
          assertEquals("Remove array element", "String 2",
                      context.getValue("nestedBean/strings[1]"));
      }
  
      public void testRemovePathBeanValue(){
          context.removePath("nestedBean");
          assertEquals("Remove collection element", null,
                      context.getValue("nestedBean"));
      }
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/MixedModelTest.java
  
  Index: MixedModelTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/MixedModelTest.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Locale;
  import java.util.Map;
  import java.util.Vector;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.JXPathTestCase;
  import org.apache.commons.jxpath.Pointer;
  import org.apache.commons.jxpath.TestBean;
  import org.apache.commons.jxpath.TestMixedModelBean;
  import org.apache.commons.jxpath.TestNull;
  import org.apache.commons.jxpath.Variables;
  
  /**
   * Tests JXPath with mixed model: beans, maps, DOM etc.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  
  public class MixedModelTest extends JXPathTestCase
  {
      private static final boolean enabled = true;
  
      private JXPathContext context;
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public MixedModelTest(String name){
          super(name);
      }
  
      /**
       * Return the tests included in this test suite.
       */
      public static Test suite(){
          return (new TestSuite(MixedModelTest.class));
      }
  
      public void setUp(){
          TestMixedModelBean bean = new TestMixedModelBean();
          context = JXPathContext.newContext(bean);
          context.setFactory(new TestMixedModelFactory());
          context.setLocale(Locale.US);
          Variables vars = context.getVariables();
          vars.declareVariable("string", bean.getString());
          vars.declareVariable("bean", bean.getBean());
          vars.declareVariable("map", bean.getMap());
          vars.declareVariable("list", bean.getList());
          vars.declareVariable("document", bean.getDocument());
          vars.declareVariable("element", bean.getElement());
          vars.declareVariable("container", bean.getContainer());
          
          vars.declareVariable("testnull", new TestNull());
      }
  
      public void testVarPrimitive(){
          assertXPathValueAndPointer(context,
                  "$string",
                  "string",
                  "$string");
      }
  
      public void testVarBean(){
          assertXPathValueAndPointer(context,
                  "$bean/int",
                  new Integer(1),
                  "$bean/int");
      }
  
      public void testVarMap(){
          assertXPathValueAndPointer(context,
                  "$map/string",
                  "string",
                  "$map[@name='string']");
      }
  
      public void testVarList(){
          assertXPathValueAndPointer(context,
                  "$list[1]",
                  "string",
                  "$list[1]");
      }
  
      public void testVarDocument(){
          assertXPathValueAndPointer(context,
                  "$document/vendor/location/address/city",
                  "Fruit Market",
                  "$document/vendor[1]/location[2]/address[1]/city[1]");
      }
  
      public void testVarElement(){
          assertXPathValueAndPointer(context,
                  "$element/location/address/city",
                  "Fruit Market",
                  "$element/location[2]/address[1]/city[1]");
      }
  
      public void testVarContainer(){
          assertXPathValueAndPointer(context,
                  "$container/vendor/location/address/city",
                  "Fruit Market",
                  "$container/vendor[1]/location[2]/address[1]/city[1]");
      }
  
  
      // ----------------------------------------------------------------------
  
      public void testBeanPrimitive(){
          assertXPathValueAndPointer(context,
                  "string",
                  "string",
                  "/string");
      }
  
      public void testBeanBean(){
          assertXPathValueAndPointer(context,
                  "bean/int",
                  new Integer(1),
                  "/bean/int");
      }
  
      public void testBeanMap(){
          assertXPathValueAndPointer(context,
                  "map/string",
                  "string",
                  "/map[@name='string']");
      }
  
      public void testBeanList(){
          assertXPathValueAndPointer(context,
                  "list[1]",
                  "string",
                  "/list[1]");
      }
  
      public void testBeanDocument(){
          assertXPathValueAndPointer(context,
                  "document/vendor/location/address/city",
                  "Fruit Market",
                  "/document/vendor[1]/location[2]/address[1]/city[1]");
      }
  
      public void testBeanElement(){
          assertXPathValueAndPointer(context,
                  "element/location/address/city",
                  "Fruit Market",
                  "/element/location[2]/address[1]/city[1]");
      }
  
      public void testBeanContainer(){
          assertXPathValueAndPointer(context,
                  "container/vendor/location/address/city",
                  "Fruit Market",
                  "/container/vendor[1]/location[2]/address[1]/city[1]");
      }
  
      // ----------------------------------------------------------------------
  
      public void testMapPrimitive(){
          assertXPathValueAndPointer(context,
                  "map/string",
                  "string",
                  "/map[@name='string']");
      }
  
      public void testMapBean(){
          assertXPathValueAndPointer(context,
                  "map/bean/int",
                  new Integer(1),
                  "/map[@name='bean']/int");
      }
  
      public void testMapMap(){
          assertXPathValueAndPointer(context,
                  "map/map/string",
                  "string",
                  "/map[@name='map'][@name='string']");
      }
  
      public void testMapList(){
          assertXPathValueAndPointer(context,
                  "map/list[1]",
                  "string",
                  "/map[@name='list'][1]");
      }
  
      public void testMapDocument(){
          assertXPathValueAndPointer(context,
                  "map/document/vendor/location/address/city",
                  "Fruit Market",
                  "/map[@name='document']" +
                          "/vendor[1]/location[2]/address[1]/city[1]");
      }
  
      public void testMapElement(){
          assertXPathValueAndPointer(context,
                  "map/element/location/address/city",
                  "Fruit Market",
                  "/map[@name='element']/location[2]/address[1]/city[1]");
      }
  
      public void testMapContainer(){
          assertXPathValueAndPointer(context,
                  "map/container/vendor/location/address/city",
                  "Fruit Market",
                  "/map[@name='container']" +
                          "/vendor[1]/location[2]/address[1]/city[1]");
      }
  
      // ----------------------------------------------------------------------
  
      public void testListPrimitive(){
          assertXPathValueAndPointer(context,
                  "list[1]",
                  "string",
                  "/list[1]");
      }
  
      public void testListBean(){
          assertXPathValueAndPointer(context,
                  "list[2]/int",
                  new Integer(1),
                  "/list[2]/int");
      }
  
      public void testListMap(){
          assertXPathValueAndPointer(context,
                  "list[3]/string",
                  "string",
                  "/list[3][@name='string']");
      }
  
      public void testListList(){
          /** @todo: what is this supposed to do? Should we stick to XPath,
           *  in which case [1] is simply ignored, or Java, in which case
           *  it is supposed to extract the first element from the list?
           */
  //        assertXPathValueAndPointer(context,
  //                "list[4][1]",
  //                "string2",
  //                "/list[4][1]");
  
          assertXPathValueAndPointer(context,
                  "list[4]/.[1]",
                  "string2",
                  "/list[4]/.[1]");
      }
  
      public void testListDocument(){
          assertXPathValueAndPointer(context,
                  "list[5]/vendor/location/address/city",
                  "Fruit Market",
                  "/list[5]/vendor[1]/location[2]/address[1]/city[1]");
      }
  
      public void testListElement(){
          assertXPathValueAndPointer(context,
                  "list[6]/location/address/city",
                  "Fruit Market",
                  "/list[6]/location[2]/address[1]/city[1]");
      }
  
      public void testListContainer(){
          assertXPathValueAndPointer(context,
                  "list[7]/vendor/location/address/city",
                  "Fruit Market",
                  "/list[7]/vendor[1]/location[2]/address[1]/city[1]");
      }
  
      public void testNull(){
  
          assertXPathPointerLenient(context, 
                  "$null", 
                  "$null");
                  
          assertXPathPointerLenient(context, 
                  "$null[3]", 
                  "$null[3]");
                  
          assertXPathPointerLenient(context, 
                  "$testnull/nothing", 
                  "$testnull/nothing");
                  
          assertXPathPointerLenient(context, 
                  "$testnull/nothing[2]", 
                  "$testnull/nothing[2]");
          
          assertXPathPointerLenient(context, 
                  "beans[8]/int", 
                  "/beans[8]/int");
          
          assertXPathValueIterator(context, 
                  "$testnull/nothing[1]", 
                  Collections.EMPTY_LIST);
                  
          JXPathContext ctx = JXPathContext.newContext(new TestNull());
          assertXPathValue(ctx, 
                  "nothing", 
                  null);
                  
          assertXPathValue(ctx, 
                  "child/nothing", 
                  null);
                  
          assertXPathValue(ctx, 
                  "array[2]", 
                  null);
                  
          assertXPathValueLenient(ctx, 
                  "nothing/something", 
                  null);
                  
          assertXPathValueLenient(ctx, 
                  "array[2]/something", 
                  null);
      }
      
      public void testRootAsCollection(){
          assertXPathValue(context, 
                  ".[1]/string",
                  "string");
      }
      
      public void testCreatePath(){
          context = JXPathContext.newContext(new TestBean());
          context.setFactory(new TestMixedModelFactory());
          
          TestBean bean = (TestBean)context.getContextBean();
          bean.setMap(null);
  
          assertXPathCreatePath(context, 
                  "/map[@name='TestKey5']/nestedBean/int", 
                  new Integer(1),
                  "/map[@name='TestKey5']/nestedBean/int");
                  
          bean.setMap(null);
          assertXPathCreatePath(context, 
                  "/map[@name='TestKey5']/beans[2]/int", 
                  new Integer(1),
                  "/map[@name='TestKey5']/beans[2]/int");
      }
      
      /**
       * Test JXPath.iterate() with map containing an array
       */
      public void testIterateArray(){
          if (!enabled){
              return;
          }
          Map map = new HashMap();
          map.put("foo", new String[]{"a", "b", "c"});
  
          JXPathContext context = JXPathContext.newContext(map);
  
          assertXPathValueIterator(context,
              "foo",
              list("a", "b", "c"));
      }
  
      public void testIteratePointersArray(){
          if (!enabled){
              return;
          }
          Map map = new HashMap();
          map.put("foo", new String[]{"a", "b", "c"});
  
          JXPathContext context = JXPathContext.newContext(map);
  
          Iterator it = context.iteratePointers("foo");
          List actual = new ArrayList();
          while (it.hasNext()){
              Pointer ptr = (Pointer)it.next();
              actual.add(context.getValue(ptr.asPath()));
          }
          assertEquals("Iterating pointers <" + "foo" + ">",
                  list("a", "b", "c"), actual);
      }
  
      public void testIteratePointersArrayElementWithVariable(){
          if (!enabled){
              return;
          }
          Map map = new HashMap();
          map.put("foo", new String[]{"a", "b", "c"});
  
          JXPathContext context = JXPathContext.newContext(map);
          context.getVariables().declareVariable("x", new Integer(2));
          Iterator it = context.iteratePointers("foo[$x]");
          List actual = new ArrayList();
          while (it.hasNext()){
              Pointer ptr = (Pointer)it.next();
              actual.add(context.getValue(ptr.asPath()));
          }
          assertEquals("Iterating pointers <" + "foo" + ">", list("b"), actual);
      }
  
      public void testIterateVector(){
          if (!enabled){
              return;
          }
          Map map = new HashMap();
          Vector vec = new Vector();
          vec.add(new HashMap());
          vec.add(new HashMap());
  
          map.put("vec", vec);
          JXPathContext context = JXPathContext.newContext(map);
          assertXPathPointerIterator(context,
              "/vec",
              list("/.[@name='vec'][1]", "/.[@name='vec'][2]"));
      }
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/TestDynamicPropertyFactory.java
  
  Index: TestDynamicPropertyFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/TestDynamicPropertyFactory.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Vector;
  
  import org.apache.commons.jxpath.AbstractFactory;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.NestedTestBean;
  import org.apache.commons.jxpath.Pointer;
  import org.apache.commons.jxpath.TestBean;
  import org.jdom.Element;
  import org.w3c.dom.Node;
  
  /**
   * Test AbstractFactory.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  public class TestDynamicPropertyFactory extends AbstractFactory {
  
      /**
       * Create a new instance and put it in the collection on the parent object.
       * Return <b>false</b> if this factory cannot create the requested object.
       */
      public boolean createObject(JXPathContext context, Pointer pointer, Object parent, String name, int index){
          if (name.equals("map")){
              ((TestBean)parent).setMap(new HashMap());
              return true;
          }
          else if (name.equals("TestKey1")){
              ((Map)parent).put(name, "");
              return true;
          }
          else if (name.equals("TestKey2")){
              ((Map)parent).put(name, new NestedTestBean("newName"));
              return true;
          }
          else if (name.equals("TestKey3")){
              Vector v = new Vector();
              for (int i = 0; i <= index; i++){
                  v.add(null);
              }
              ((Map)parent).put(name, v);
              return true;
          }
          else if (name.equals("TestKey4")){
              ((Map)parent).put(name, new Object[]{new TestBean()});
              return true;
          }
          return false;
      }
  
      public boolean declareVariable(JXPathContext context, String name){
          return false;
      }
  }
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/TestMixedModelFactory.java
  
  Index: TestMixedModelFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/TestMixedModelFactory.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Vector;
  
  import org.apache.commons.jxpath.AbstractFactory;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.NestedTestBean;
  import org.apache.commons.jxpath.Pointer;
  import org.apache.commons.jxpath.TestBean;
  import org.jdom.Element;
  import org.w3c.dom.Node;
  
  /**
   * Test AbstractFactory.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  public class TestMixedModelFactory extends AbstractFactory {
  
      /**
       * Create a new instance and put it in the collection on the parent object.
       * Return <b>false</b> if this factory cannot create the requested object.
       */
      public boolean createObject(JXPathContext context, Pointer pointer, Object parent, String name, int index){
          if (name.equals("nestedBean")){
              ((TestBean)parent).setNestedBean(new NestedTestBean("newName"));
              return true;
          }
          else if (name.equals("beans")){
              TestBean bean = (TestBean)parent;
              if (bean.getBeans() == null || index >= bean.getBeans().length){
                  bean.setBeans(new NestedTestBean[index + 1]);
              }
              ((TestBean)parent).getBeans()[index] = new NestedTestBean("newName");
              return true;
          }
          else if (name.equals("map")){
              ((TestBean)parent).setMap(new HashMap());
              return true;
          }
          else if (name.equals("TestKey5")){
              TestBean tb = new TestBean();
              tb.setNestedBean(null);
              tb.setBeans(null);
              ((Map)parent).put(name, tb);
              return true;
          }
          return false;
      }
  
      public boolean declareVariable(JXPathContext context, String name){
          return false;
      }
  }
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/beans/BeanModelTest.java
  
  Index: BeanModelTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/beans/BeanModelTest.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model.beans;
  
  import java.lang.reflect.InvocationTargetException;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import org.w3c.dom.*;
  import java.util.*;
  import java.lang.reflect.*;
  import org.apache.commons.jxpath.ri.model.XMLModelTestCase;
  import org.apache.commons.jxpath.*;
  import org.apache.commons.jxpath.util.*;
  import org.apache.commons.jxpath.ri.*;
  import org.apache.commons.jxpath.ri.parser.*;
  import org.apache.commons.jxpath.ri.model.*;
  import org.apache.commons.jxpath.ri.model.beans.*;
  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.*;
  
  /**
   * Tests JXPath with JavaBeans
  *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  
  public class BeanModelTest extends BeanModelTestCase
  {
      private static final boolean enabled = true;
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public BeanModelTest(String name){
          super(name);
      }
  
      /**
       * Return the tests included in this test suite.
       */
      public static TestSuite suite(){
  //        return (new TestSuite(BeanModelTest.class));
          TestSuite s = new TestSuite();
          s.addTest(new BeanModelTest("testAxisParent"));
          return s;
      }
  
      protected Object createContextBean(){
          return new TestBean();
      }
  
      protected AbstractFactory getAbstractFactory(){
          return new TestBeanFactory();
      }
  
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/beans/DynamicPropertiesModelTest.java
  
  Index: DynamicPropertiesModelTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/beans/DynamicPropertiesModelTest.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model.beans;
  
  import java.lang.reflect.InvocationTargetException;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import org.w3c.dom.*;
  import java.util.*;
  import java.lang.reflect.*;
  import org.apache.commons.jxpath.*;
  import org.apache.commons.jxpath.util.*;
  import org.apache.commons.jxpath.ri.*;
  import org.apache.commons.jxpath.ri.parser.*;
  import org.apache.commons.jxpath.ri.model.*;
  import org.apache.commons.jxpath.ri.model.beans.*;
  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.*;
  
  /**
   * @todo more iterator testing with maps
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  
  public class DynamicPropertiesModelTest extends JXPathTestCase
  {
      private static boolean enabled = true;
      private JXPathContext context;
  
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public DynamicPropertiesModelTest(String name){
          super(name);
      }
  
      public void setUp(){
          if (context == null){
              context = JXPathContext.newContext(new TestBean());
              context.setFactory(new TestDynamicPropertyFactory());
          }
      }
  
      public void testAxisChild(){
          assertXPathValue(context,
                  "map/Key1",
                  "Value 1");
  
          assertXPathPointer(context,
                  "map/Key1",
                  "/map[@name='Key1']");
  
          assertXPathValue(context,
                  "map/Key2/name",
                  "Name 6");
  
          assertXPathPointer(context,
                  "map/Key2/name",
                  "/map[@name='Key2']/name");
      }
  
      public void testAxisDescendant(){
          assertXPathValue(context,
                  "//Key1",
                  "Value 1");
      }
  
      /**
       * Testing the pseudo-attribute "name" that dynamic property
       * objects appear to have.
       */
      public void testAttributeName(){
          assertXPathValue(context,
                  "map[@name = 'Key1']",
                  "Value 1");
  
          assertXPathPointer(context,
                  "map[@name = 'Key1']",
                  "/map[@name='Key1']");
  
          assertXPathPointerLenient(context,
                  "map[@name = 'Key&quot;&apos;&quot;&apos;1']",
                  "/map[@name='Key&quot;&apos;&quot;&apos;1']");
  
          assertXPathValue(context,
                  "/.[@name='map']/Key2/name",
                  "Name 6");
  
          assertXPathPointer(context,
                  "/.[@name='map']/Key2/name",
                  "/map[@name='Key2']/name");
  
          // Bean in a map
          assertXPathValue(context,
                  "/map[@name='Key2'][@name='name']",
                  "Name 6");
  
          assertXPathPointer(context,
                  "/map[@name='Key2'][@name='name']",
                  "/map[@name='Key2']/name");
  
          // Map in a bean in a map
          assertXPathValue(context,
                  "/.[@name='map'][@name='Key2'][@name='name']",
                  "Name 6");
  
          assertXPathPointer(context,
                  "/.[@name='map'][@name='Key2'][@name='name']",
                  "/map[@name='Key2']/name");
      }
      
      public void testSetPrimitiveValue(){
          assertXPathSetValue(context, 
                  "map/Key1",
                  new Integer(6));
      }
      
      public void testSetCollection(){
          // See if we can assign a whole collection        
          context.setValue(
                  "map/Key1", 
                  new Integer[]{new Integer(7), new Integer(8)});
          
          // And then an element in that collection
          assertXPathSetValue(context,
                  "map/Key1[1]", 
                  new Integer(9));
      }
      
      public void testSetNewKey(){
          assertXPathSetValue(context,
                  "map/Key4", 
                  new Integer(7));
      }
      
      public void testCreatePath(){
          TestBean bean = (TestBean)context.getContextBean();
          bean.setMap(null);
          
          // Calls factory.createObject(..., testBean, "map"), then
          // sets the value
          assertXPathCreatePath(context, 
                  "/map[@name='TestKey1']", 
                  "", 
                  "/map[@name='TestKey1']");
      }
      
      public void testCreatePathAndSetValue(){
          TestBean bean = (TestBean)context.getContextBean();
          bean.setMap(null);
          
          // Calls factory.createObject(..., testBean, "map"), then
          // sets the value
          assertXPathCreatePathAndSetValue(context, 
                  "/map[@name='TestKey1']", 
                  "Test", 
                  "/map[@name='TestKey1']");
      }
      
      public void testCreatePathCreateBean(){
          TestBean bean = (TestBean)context.getContextBean();
          bean.setMap(null);
          
          // Calls factory.createObject(..., testBean, "map"), then
          // then factory.createObject(..., map, "TestKey2"), then
          // sets the value
          assertXPathCreatePath(context, 
                  "/map[@name='TestKey2']/int", 
                  new Integer(1),
                  "/map[@name='TestKey2']/int");
      }
      
      public void testCreatePathAndSetValueCreateBean(){
          TestBean bean = (TestBean)context.getContextBean();
          bean.setMap(null);
          
          // Calls factory.createObject(..., testBean, "map"), then
          // then factory.createObject(..., map, "TestKey2"), then
          // sets the value
          assertXPathCreatePathAndSetValue(context, 
                  "/map[@name='TestKey2']/int", 
                  new Integer(4),
                  "/map[@name='TestKey2']/int");
      }
      
      public void testCreatePathCollectionElement(){
          TestBean bean = (TestBean)context.getContextBean();
          bean.setMap(null);
          
  
          assertXPathCreatePath(context, 
                  "/map/TestKey3[2]", 
                  null,
                  "/map[@name='TestKey3'][2]");
  
          // Should be the same as the one before
          assertXPathCreatePath(context, 
                  "/map[@name='TestKey3'][3]", 
                  null,
                  "/map[@name='TestKey3'][3]");
      }
      
      public void testCreatePathAndSetValueCollectionElement(){
          TestBean bean = (TestBean)context.getContextBean();
          bean.setMap(null);
          
  
          assertXPathCreatePathAndSetValue(context, 
                  "/map/TestKey3[2]", 
                  "Test1",
                  "/map[@name='TestKey3'][2]");
  
          // Should be the same as the one before
          assertXPathCreatePathAndSetValue(context, 
                  "/map[@name='TestKey3'][3]", 
                  "Test2",
                  "/map[@name='TestKey3'][3]");
      }
      
      public void testCreatePathNewCollectionElement(){
          TestBean bean = (TestBean)context.getContextBean();
          bean.setMap(null);
          
          // Create an element of a dynamic map element, which is a collection
          assertXPathCreatePath(context, 
                  "/map/TestKey4[1]/int", 
                  new Integer(1),
                  "/map[@name='TestKey4'][1]/int");
  
          bean.getMap().remove("TestKey4");
  
          // Should be the same as the one before
          assertXPathCreatePath(context, 
                  "/map/TestKey4[1]/int", 
                  new Integer(1),
                  "/map[@name='TestKey4'][1]/int");
      }
      
      public void testCreatePathAndSetValueNewCollectionElement(){
          TestBean bean = (TestBean)context.getContextBean();
          bean.setMap(null);
          
          // Create an element of a dynamic map element, which is a collection
          assertXPathCreatePathAndSetValue(context, 
                  "/map/TestKey4[1]/int", 
                  new Integer(2),
                  "/map[@name='TestKey4'][1]/int");
  
          bean.getMap().remove("TestKey4");
  
          // Should be the same as the one before
          assertXPathCreatePathAndSetValue(context, 
                  "/map/TestKey4[1]/int", 
                  new Integer(3),
                  "/map[@name='TestKey4'][1]/int");
      }
      
      public void testRemovePath(){
          TestBean bean = (TestBean)context.getContextBean();
          bean.getMap().put("TestKey1", "test");
  
          // Remove dynamic property
          context.removePath("map[@name = 'TestKey1']");
          assertEquals("Remove dynamic property value", null,
                      context.getValue("map[@name = 'TestKey1']"));
      }
      
      public void testRemovePathArrayElement(){
          TestBean bean = (TestBean)context.getContextBean();
  
          bean.getMap().put("TestKey2", new String[]{"temp1", "temp2"});
          context.removePath("map[@name = 'TestKey2'][1]");
          assertEquals("Remove dynamic property collection element", "temp2",
                      context.getValue("map[@name = 'TestKey2'][1]"));
      }
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/beans/TestBeanFactory.java
  
  Index: TestBeanFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/beans/TestBeanFactory.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model.beans;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Vector;
  
  import org.apache.commons.jxpath.AbstractFactory;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.NestedTestBean;
  import org.apache.commons.jxpath.Pointer;
  import org.apache.commons.jxpath.TestBean;
  import org.jdom.Element;
  import org.w3c.dom.Node;
  
  /**
   * Test AbstractFactory.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  public class TestBeanFactory extends AbstractFactory {
  
      /**
       * Return <b>false</b> if this factory cannot create the requested object.
       */
      public boolean createObject(JXPathContext context, 
                  Pointer pointer, Object parent, String name, int index)
      {
          if (name.equals("nestedBean")){
              ((TestBean)parent).setNestedBean(new NestedTestBean("newName"));
              return true;
          }
          else if (name.equals("beans")){
              TestBean bean = (TestBean)parent;
              if (bean.getBeans() == null || index >= bean.getBeans().length){
                  bean.setBeans(new NestedTestBean[index + 1]);
              }
              ((TestBean)parent).getBeans()[index] = new NestedTestBean("newName");
              return true;
          }
          return false;
      }
  
      /**
       * Create a new object and set it on the specified variable
       */
      public boolean declareVariable(JXPathContext context, String name){
          return false;
      }
  }
  
  
  
  1.3       +64 -31    jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/dom/DOMModelTest.java
  
  Index: DOMModelTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/dom/DOMModelTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DOMModelTest.java	13 Oct 2002 03:01:03 -0000	1.2
  +++ DOMModelTest.java	20 Oct 2002 03:48:22 -0000	1.3
  @@ -62,32 +62,17 @@
   
   package org.apache.commons.jxpath.ri.model.dom;
   
  -import java.lang.reflect.InvocationTargetException;
  -
   import junit.framework.Test;
  -import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
  -import org.w3c.dom.*;
  -import java.util.*;
  -import java.lang.reflect.*;
  +import org.apache.commons.jxpath.AbstractFactory;
   import org.apache.commons.jxpath.ri.model.XMLModelTestCase;
  -import org.apache.commons.jxpath.*;
  -import org.apache.commons.jxpath.util.*;
  -import org.apache.commons.jxpath.ri.*;
  -import org.apache.commons.jxpath.ri.parser.*;
  -import org.apache.commons.jxpath.ri.model.*;
  -import org.apache.commons.jxpath.ri.model.beans.*;
  -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.*;
  +import org.apache.commons.jxpath.xml.DocumentContainer;
  +
  +import org.w3c.dom.*;
   
   /**
  - * Abstract superclass for pure XPath 1.0.  Subclasses
  - * apply the same XPaths to contexts using different models:
  - * DOM, JDOM etc.
  + * Tests JXPath with DOM
    *
    * @author Dmitri Plotnikov
    * @version $Revision$ $Date$
  @@ -96,13 +81,6 @@
   public class DOMModelTest extends XMLModelTestCase
   {
       /**
  -     * Exercises this test case only
  -     */
  -    public static void main(String args[]) {
  -        junit.textui.TestRunner.run(suite());
  -    }
  -
  -    /**
        * Construct a new instance of this test case.
        *
        * @param name Name of the test case
  @@ -121,4 +99,59 @@
       protected String getModel(){
           return DocumentContainer.MODEL_DOM;
       }
  +    
  +    protected AbstractFactory getAbstractFactory(){
  +        return new TestDOMFactory();
  +    }
  +    
  +    protected String getXMLSignature(Object node, 
  +    		boolean elements, boolean attributes, boolean text, boolean pi){
  +    	StringBuffer buffer = new StringBuffer();
  +    	appendXMLSignature(buffer, node, elements, attributes, text, pi);
  +    	return buffer.toString();
  +    }
  +    
  +    private void appendXMLSignature(StringBuffer buffer, Object object, 
  +    		boolean elements, boolean attributes, boolean text, boolean pi){
  +    	Node node = (Node)object;
  +    	int type = node.getNodeType();
  +    	switch (type){
  +    		case Node.DOCUMENT_NODE:
  +    			buffer.append("<D>");
  +    			appendXMLSignature(buffer, node.getChildNodes(), 
  +    					elements, attributes, text, pi);
  +    			buffer.append("</D");
  +    			break;
  +    			
  +    		case Node.ELEMENT_NODE:
  +    			String tag = elements ? ((Element)node).getTagName() : "E";
  +				buffer.append("<");
  +				buffer.append(tag);
  +				buffer.append(">");
  +    			appendXMLSignature(buffer, node.getChildNodes(), 
  +    					elements, attributes, text, pi);
  +				buffer.append("</");
  +				buffer.append(tag);
  +				buffer.append(">");    				
  +				break;
  +				
  +    		case Node.TEXT_NODE:
  +    		case Node.CDATA_SECTION_NODE:
  +    			if (text){
  +    				String string = node.getNodeValue();
  +    				string = string.replace('\n', '=');
  +    				buffer.append(string);
  +    			}
  +				break;
  +    	}
  +	}
  +	
  +    private void appendXMLSignature(StringBuffer buffer, NodeList children, 
  +    		boolean elements, boolean attributes, boolean text, boolean pi)
  +    {
  +    	for (int i = 0; i < children.getLength(); i++){
  +			appendXMLSignature(buffer, children.item(i), 
  +					elements, attributes, text, pi);
  +    	}
  +	}
   }
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/dom/TestDOMFactory.java
  
  Index: TestDOMFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/dom/TestDOMFactory.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model.dom;
  
  import org.apache.commons.jxpath.AbstractFactory;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.Pointer;
  import org.w3c.dom.Node;
  
  /**
   * Test AbstractFactory.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  public class TestDOMFactory extends AbstractFactory {
  
      /**
       * Return <b>false</b> if this factory cannot create the requested object.
       */
      public boolean createObject(JXPathContext context, 
                  Pointer pointer, Object parent, String name, int index)
      {
          if (name.equals("location") || 
                  name.equals("address") || 
                  name.equals("street")){
              addDOMElement((Node)parent, index, name);
              return true;
          }
          return false;
      }
  
      private void addDOMElement(Node parent, int index, String tag){
          Node child = parent.getFirstChild();
          int count = 0;
          while (child != null){
              if (child.getNodeName().equals(tag)){
                  count++;
              }
              child = child.getNextSibling();
          }
  
          // Keep inserting new elements until we have index + 1 of them
          while (count <= index){
              Node newElement = parent.getOwnerDocument().createElement(tag);
              parent.appendChild(newElement);
              count++;
          }
      }
  
      public boolean declareVariable(JXPathContext context, String name){
          return false;
      }
  }
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/dynabeans/DynaBeanModelTest.java
  
  Index: DynaBeanModelTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/dynabeans/DynaBeanModelTest.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model.dynabeans;
  
  import org.apache.commons.beanutils.WrapDynaBean;
  import org.apache.commons.jxpath.AbstractFactory;
  import org.apache.commons.jxpath.TestBean;
  import org.apache.commons.jxpath.ri.model.BeanModelTestCase;
  
  import junit.framework.*;
  
  /**
   * Test for support of DynaBeans (see BeanUtils)
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  
  public class DynaBeanModelTest extends BeanModelTestCase
  {
      public DynaBeanModelTest(String name)
      {
          super(name);
      }
  
      public static TestSuite suite(){
  //        return (new TestSuite(BeanModelTest.class));
          TestSuite s = new TestSuite();
          s.addTest(new DynaBeanModelTest("testAxisParent"));
          return s;
      }
  
      protected Object createContextBean(){
          return new WrapDynaBean(new TestBean());
      }
  
      protected AbstractFactory getAbstractFactory(){
          return new TestDynaBeanFactory();
      }
  }
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/dynabeans/TestDynaBeanFactory.java
  
  Index: TestDynaBeanFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/dynabeans/TestDynaBeanFactory.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model.dynabeans;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Vector;
  
  import org.apache.commons.beanutils.DynaBean;
  import org.apache.commons.jxpath.AbstractFactory;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.NestedTestBean;
  import org.apache.commons.jxpath.Pointer;
  import org.apache.commons.jxpath.TestBean;
  
  /**
   * Test AbstractFactory.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  public class TestDynaBeanFactory extends AbstractFactory {
  
      /**
       */
      public boolean createObject(JXPathContext context, Pointer pointer, Object parent, String name, int index){
          if (name.equals("nestedBean")){
              ((DynaBean)parent).set("nestedBean", new NestedTestBean("newName"));
              return true;
          }
          else if (name.equals("beans")){
              DynaBean bean = (DynaBean)parent;
              Object beans[] = (Object[])bean.get("beans");
              if (beans == null || index >= beans.length){
                  beans = new NestedTestBean[index + 1];
                  bean.set("beans", beans);
              }
              beans[index] = new NestedTestBean("newName");
              return true;
          }
          return false;
      }
  
  
      /**
       */
      public boolean declareVariable(JXPathContext context, String name){
          context.getVariables().declareVariable(name, null);
          return true;
      }
  }
  
  
  
  1.3       +55 -15    jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/jdom/JDOMModelTest.java
  
  Index: JDOMModelTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/jdom/JDOMModelTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JDOMModelTest.java	13 Oct 2002 03:01:03 -0000	1.2
  +++ JDOMModelTest.java	20 Oct 2002 03:48:22 -0000	1.3
  @@ -68,7 +68,6 @@
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
  -import org.w3c.dom.*;
   import java.util.*;
   import java.lang.reflect.*;
   import org.apache.commons.jxpath.ri.model.XMLModelTestCase;
  @@ -82,12 +81,12 @@
   import org.apache.commons.jxpath.ri.compiler.*;
   import org.apache.commons.jxpath.ri.compiler.Expression;
   import org.apache.commons.jxpath.xml.*;
  +import org.jdom.*;
  +
   import java.beans.*;
   
   /**
  - * Abstract superclass for pure XPath 1.0.  Subclasses
  - * apply the same XPaths to contexts using different models:
  - * DOM, JDOM etc.
  + * Tests JXPath with JDOM
    *
    * @author Dmitri Plotnikov
    * @version $Revision$ $Date$
  @@ -96,13 +95,6 @@
   public class JDOMModelTest extends XMLModelTestCase
   {
       /**
  -     * Exercises this test case only
  -     */
  -    public static void main(String args[]) {
  -        junit.textui.TestRunner.run(suite());
  -    }
  -
  -    /**
        * Construct a new instance of this test case.
        *
        * @param name Name of the test case
  @@ -125,4 +117,52 @@
       public void testID(){
           // id() is not supported by JDOM
       }
  +
  +    protected AbstractFactory getAbstractFactory(){
  +        return new TestJDOMFactory();
  +    }
  +    
  +    protected String getXMLSignature(Object node, 
  +    		boolean elements, boolean attributes, boolean text, boolean pi){
  +    	StringBuffer buffer = new StringBuffer();
  +    	appendXMLSignature(buffer, node, elements, attributes, text, pi);
  +    	return buffer.toString();
  +    }
  +    
  +    private void appendXMLSignature(StringBuffer buffer, Object object, 
  +    		boolean elements, boolean attributes, boolean text, boolean pi){
  +    	if (object instanceof Document){
  +			buffer.append("<D>");
  +			appendXMLSignature(buffer, ((Document)object).getContent(), 
  +					elements, attributes, text, pi);
  +			buffer.append("</D");
  +    	}
  +    	else if (object instanceof Element){
  +			String tag = elements ? ((Element)object).getName() : "E";
  +			buffer.append("<");
  +			buffer.append(tag);
  +			buffer.append(">");
  +			appendXMLSignature(buffer, ((Element)object).getContent(), 
  +					elements, attributes, text, pi);
  +			buffer.append("</");
  +			buffer.append(tag);
  +			buffer.append(">");    				
  +    	}
  +    	else if (object instanceof Text || object instanceof CDATA){
  +			if (text){
  +				String string = ((Text)object).getText();
  +				string = string.replace('\n', '=');
  +				buffer.append(string);
  +			}
  +    	}
  +	}
  +	
  +    private void appendXMLSignature(StringBuffer buffer, List children, 
  +    		boolean elements, boolean attributes, boolean text, boolean pi)
  +    {
  +    	for (int i = 0; i < children.size(); i++){
  +			appendXMLSignature(buffer, children.get(i), 
  +					elements, attributes, text, pi);
  +    	}
  +	}  
   }
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/jdom/TestJDOMFactory.java
  
  Index: TestJDOMFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/jdom/TestJDOMFactory.java,v 1.1 2002/10/20 03:48:22 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:22 $
   *
   * ====================================================================
   * 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.ri.model.jdom;
  
  import java.util.List;
  
  import org.apache.commons.jxpath.AbstractFactory;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.Pointer;
  import org.jdom.Element;
  
  /**
   * Test AbstractFactory.
   *
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:22 $
   */
  public class TestJDOMFactory extends AbstractFactory {
  
      /**
       * Create a new instance and put it in the collection on the parent object.
       * Return <b>false</b> if this factory cannot create the requested object.
       */
      public boolean createObject(JXPathContext context, 
                  Pointer pointer, Object parent, String name, int index)
      {
          if (name.equals("location") || 
                  name.equals("address") || name.equals("street")){
              addJDOMElement((Element)parent, index, name);
              return true;
          }
          return false;
      }
  
      private void addJDOMElement(Element parent, int index, String tag){
          List children = parent.getContent();
          int count = 0;
          for (int i = 0; i < children.size(); i++){
              Object child = children.get(i);
              if (child instanceof Element &&
                      ((Element)child).getQualifiedName().equals(tag)){
                  count++;
              }
          }
  
          // Keep inserting new elements until we have index + 1 of them
          while (count <= index){
              // In a real factory we would need to do the right thing with
              // the namespace prefix.
              Element newElement = new Element(tag);
              parent.addContent(newElement);
              count++;
          }
      }
  
      public boolean declareVariable(JXPathContext context, String name){
          return false;
      }
  }
  
  
  
  1.1                  jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/util/BasicTypeConverterTest.java
  
  Index: BasicTypeConverterTest.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/util/BasicTypeConverterTest.java,v 1.1 2002/10/20 03:48:23 dmitri Exp $
   * $Revision: 1.1 $
   * $Date: 2002/10/20 03:48:23 $
   *
   * ====================================================================
   * 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.util;
  
  import java.lang.reflect.Array;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Date;
  import java.util.List;
  
  import junit.framework.TestCase;
  
  /**
   * Tests BasicTypeConverter
   * 
   * @author Dmitri Plotnikov
   * @version $Revision: 1.1 $ $Date: 2002/10/20 03:48:23 $
   */
  
  public class BasicTypeConverterTest extends TestCase
  {
      /**
       * Construct a new instance of this test case.
       *
       * @param name Name of the test case
       */
      public BasicTypeConverterTest(String name)
      {
          super(name);
      }
      
      public void testPrimitiveToString(){
          assertConversion(
                  new Integer(1), 
                  String.class, 
                  "1");
      }
      
      public void testArrayToList(){
          assertConversion(
                  new int[]{1, 2}, 
                  List.class,
                  Arrays.asList(new Object[]{new Integer(1), new Integer(2)}));
      }
      
      public void testArrayToArray(){
          assertConversion(
                  new int[]{1, 2}, 
                  String[].class,
                  Arrays.asList(new String[]{"1", "2"}));
      }
      
      public void testListToArray(){
          assertConversion(
                  Arrays.asList(new Integer[]{new Integer(1), new Integer(2)}),
                  String[].class,
                  Arrays.asList(new String[]{"1", "2"}));
                  
          assertConversion(
                  Arrays.asList(new String[]{"1", "2"}),
                  int[].class,
                  Arrays.asList(new Integer[]{new Integer(1), new Integer(2)}));
      }
      
      public void testInvalidConversion(){
          boolean exception = false;
          try {
              TypeUtils.convert(
                      "'foo'",
                      Date.class);
          }
          catch (Throwable ex){
              exception = true;
          }
          assertTrue("Type conversion exception", exception);
      }
      
      public void assertConversion(Object from, Class toType, Object expected) {
          boolean can = TypeUtils.canConvert(from, toType);
          assertTrue("Can convert: " + from.getClass() + 
                  " to " + toType, can);
          Object result = TypeUtils.convert(from, toType);
          if (result.getClass().isArray()){
              ArrayList list = new ArrayList();
              for (int j = 0; j < Array.getLength(result); j++){
                  list.add(Array.get(result, j));
              }
              result = list;
          }
          assertEquals("Convert: " + from.getClass() + 
                  " to " + toType, expected, result);
      }
  }
  
  

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