alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons JXPath example source code file (ContainerModelTest.java)

This example Commons JXPath source code file (ContainerModelTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons JXPath tags/keywords

arraycontainer, arraycontainer, bean, checking, container, jxpathcontext, list, list, listcontainer, listcontainer, object, string, string, unsupportedoperationexception, util

The Commons JXPath ContainerModelTest.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.container;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.jxpath.Container;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathTestCase;

/**
 * Tests JXPath with containers as root or value of a variable, property, etc.
 *
 * @author Dmitri Plotnikov
 * @version $Revision: 652845 $ $Date: 2008-05-02 12:46:46 -0500 (Fri, 02 May 2008) $
 */

public class ContainerModelTest extends JXPathTestCase {

    private class ArrayContainer implements Container
    {
        private String[] array = new String[]{"foo", "bar"};
        public Object getValue() {
            return array;
        }

        public void setValue(Object value) {
            throw new UnsupportedOperationException();
        }
    };

    public class ListContainer implements Container
    {
        private List list;

        public ListContainer() {
            list = new ArrayList();
            list.add("foo");
            list.add("bar");
        }

        public Object getValue() {
            return list;
        }

        public void setValue(Object value) {
            throw new UnsupportedOperationException();
        }
    }

    public class Bean
    {
        private ListContainer container = new ListContainer();

        public ListContainer getContainer() {
            return container;
        }
    }
        
    public void testContainerVariableWithCollection() {
        ArrayContainer container = new ArrayContainer();
        String[] array = (String[]) container.getValue();
        
        JXPathContext context = JXPathContext.newContext(null);
        context.getVariables().declareVariable("list", container);
        
        assertXPathValueAndPointer(context, "$list", array, "$list");
        assertXPathValueAndPointer(context, "$list[1]", "foo", "$list[1]");
        assertXPathValueAndPointer(context, "$list[2]", "bar", "$list[2]");
        
        assertXPathSetValue(context, "$list[1]", "baz");
        assertEquals("Checking setValue(index)", "baz", array[0]);
    }
    
    public void testContainerPropertyWithCollection() {
        Bean bean = new Bean();
        List list = (List) bean.getContainer().getValue();
        
        JXPathContext context = JXPathContext.newContext(bean);
        
        assertXPathValueAndPointer(context, "/container", 
                list, "/container");
        assertXPathValueAndPointer(context, "/container[1]",
                list.get(0), "/container[1]");
        assertXPathValueAndPointer(context, "/container[2]",
                list.get(1), "/container[2]");
        
        assertXPathSetValue(context, "/container[1]", "baz");
        assertEquals("Checking setValue(index)", "baz", list.get(0));
    }
    
    public void testContainerMapWithCollection() {
        ListContainer container = new ListContainer();
        List list = (List) container.getValue();
                
        Map map = new HashMap();
        map.put("container", container);
        
        JXPathContext context = JXPathContext.newContext(map);
        
        assertXPathValueAndPointer(context, "/container", 
                list, "/.[@name='container']");
        assertXPathValueAndPointer(context, "/container[1]",
                list.get(0), "/.[@name='container'][1]");
        assertXPathValueAndPointer(context, "/container[2]",
                list.get(1), "/.[@name='container'][2]");
        
        assertXPathSetValue(context, "/container[1]", "baz");
        assertEquals("Checking setValue(index)", "baz", list.get(0));
    }
    
    public void testContainerRootWithCollection() {
        ArrayContainer container = new ArrayContainer();
        String[] array = (String[]) container.getValue();
        
        JXPathContext context = JXPathContext.newContext(container);
        context.getVariables().declareVariable("list", container);
        
        assertXPathValueAndPointer(context, "/", array, "/");
        assertXPathValueAndPointer(context, "/.[1]", "foo", "/.[1]");
        assertXPathValueAndPointer(context, "/.[2]", "bar", "/.[2]");
        
        assertXPathSetValue(context, "/.[1]", "baz");
        assertEquals("Checking setValue(index)", "baz", array[0]);    }
    
}

Other Commons JXPath examples (source code examples)

Here is a short list of links related to this Commons JXPath ContainerModelTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.