beanmodeltestcase.java

来自「JXPath」· Java 代码 · 共 966 行 · 第 1/2 页

JAVA
966
字号
/*
 * Copyright 1999-2004 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.jxpath.ri.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.apache.commons.jxpath.AbstractFactory;
import org.apache.commons.jxpath.ClassFunctions;
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.compiler.TestFunctions;
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.19 $ $Date: 2004/02/29 14:17:45 $
 */

public abstract class BeanModelTestCase extends JXPathTestCase {
    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() {
        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() {
        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() {
        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() {
        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() {
        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 iterCount = 0;
        Iterator iter = context.iteratePointers("/beans/name");
        while (iter.hasNext()) {
            iterCount++;
            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, iterCount);

        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(

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?