beanmodeltestcase.java

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

JAVA
966
字号
            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);
    }

    public void testCoreFunctions() {

        assertXPathValue(context, "boolean(boolean)", Boolean.TRUE);

        assertXPathValue(context, "boolean(boolean = false())", Boolean.TRUE);

        assertXPathValue(
            context,
            "boolean(integers[position() < 3])",
            Boolean.TRUE);

        assertXPathValue(
            context,
            "boolean(integers[position() > 4])",
            Boolean.FALSE);

        assertXPathValue(context, "sum(integers)", new Double(10));        

        assertXPathValueAndPointer(
                context,
                "integers[last()]",
                new Integer(4),
                "/integers[4]");

        assertXPathValueAndPointer(
                context,
                "//strings[last()]",
                "String 3",
                "/beans[1]/strings[3]");
    }

    public void testBooleanPredicate() {
        // use child axis

        // bean[1]/int = 1
        // bean[2]/int = 3

        assertXPathValue(context, "beans[int > 2]/name", "Name 2");

        assertXPathValueIterator(
            context,
            "beans[int > 2]/name",
            list("Name 2"));

        assertXPathValueIterator(
            context,
            "beans[int >= 1]/name",
            list("Name 1", "Name 2"));

        assertXPathValueIterator(
            context,
            "beans[int < 2]/name",
            list("Name 1"));

        assertXPathValueIterator(
            context,
            "beans[int <= 3]/name",
            list("Name 1", "Name 2"));

        assertXPathValueIterator(
            context,
            "beans[1]/strings[string-length() = 8]",
            list("String 1", "String 2", "String 3"));

        // 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)));
            
        context.getVariables().declareVariable(
            "temp",
            context.getValue("beans"));
        
        assertXPathValueIterator(
            context,
            "$temp[int < 2]/int",
            list(new Integer(1)));
    }

    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");

        boolean ex = false;
        try {
            assertXPathCreatePath(
                context,
                "/nestedBean/beans[last() + 1]",
                new Integer(1),
                "/nestedBean/beans[last() + 1]");
        }
        catch (Exception e) {
            ex = true;
        }
        assertTrue("Exception thrown on invalid path for creation", ex);
        
    }

    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.createObject(..., 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.createObject(..., testBean, "beans", 2), 
        // then factory.createObject(..., testBean, "beans", 2)
        assertXPathCreatePathAndSetValue(
            context,
            "/beans[2]/int",
            new Integer(2),
            "/beans[2]/int");
    }

    public void testCreatePathExpandExistingCollection() {
        // Calls factory.createObject(..., TestBean, "integers", 5)
        // to expand collection
        assertXPathCreatePathAndSetValue(
            context,
            "/integers[5]",
            new Integer(3),
            "/integers[5]");
    }

    public void testCreatePathExpandExistingCollectionAndSetProperty() {
        // 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.createObject(..., 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.createObject(..., 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"));
    }
    
    public void testRelativeContextRelativePath() {
        JXPathContext relative =
            context.getRelativeContext(context.getPointer("nestedBean"));
        
        assertXPathValueAndPointer(relative, 
            "int", 
            new Integer(1), 
            "/nestedBean/int");
    }

    public void testRelativeContextAbsolutePath() {
        JXPathContext relative =
            context.getRelativeContext(context.getPointer("nestedBean"));
        
        assertXPathValueAndPointer(relative, 
            "/integers[2]", 
            new Integer(2), 
            "/integers[2]");
    }

    public void testRelativeContextParent() {
        JXPathContext relative =
            context.getRelativeContext(context.getPointer("nestedBean"));
        
        assertXPathValueAndPointer(relative, 
            "../integers[2]", 
            new Integer(2), 
            "/integers[2]");
    }
    
    public void testRelativeContextInheritance() {
        context.setFunctions(new ClassFunctions(TestFunctions.class, "test"));
        JXPathContext relative =
            context.getRelativeContext(context.getPointer("nestedBean"));
        
        assertXPathValue(relative, 
            "test:countPointers(strings)", 
            new Integer(3));
    }
}

⌨️ 快捷键说明

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