001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri.model.container;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025: import junit.textui.TestRunner;
026:
027: import org.apache.commons.jxpath.Container;
028: import org.apache.commons.jxpath.JXPathContext;
029: import org.apache.commons.jxpath.JXPathTestCase;
030:
031: /**
032: * Tests JXPath with containers as root or value of a variable, property, etc.
033: *
034: * @author Dmitri Plotnikov
035: * @version $Revision: 1.1 $ $Date: 2004/04/04 22:06:35 $
036: */
037:
038: public class ContainerModelTest extends JXPathTestCase {
039: private JXPathContext context;
040:
041: /**
042: * Construct a new instance of this test case.
043: *
044: * @param name Name of the test case
045: */
046: public ContainerModelTest(String name) {
047: super (name);
048: }
049:
050: public static void main(String[] args) {
051: TestRunner.run(suite());
052: }
053:
054: /**
055: * Return the tests included in this test suite.
056: */
057: public static Test suite() {
058: return (new TestSuite(ContainerModelTest.class));
059: }
060:
061: private class ArrayContainer implements Container {
062: private String[] array = new String[] { "foo", "bar" };
063:
064: public Object getValue() {
065: return array;
066: }
067:
068: public void setValue(Object value) {
069: throw new UnsupportedOperationException();
070: }
071: };
072:
073: public class ListContainer implements Container {
074: private List list;
075:
076: public ListContainer() {
077: list = new ArrayList();
078: list.add("foo");
079: list.add("bar");
080: }
081:
082: public Object getValue() {
083: return list;
084: }
085:
086: public void setValue(Object value) {
087: throw new UnsupportedOperationException();
088: }
089: }
090:
091: public class Bean {
092: private ListContainer container = new ListContainer();
093:
094: public ListContainer getContainer() {
095: return container;
096: }
097: }
098:
099: public void testContainerVariableWithCollection() {
100: ArrayContainer container = new ArrayContainer();
101: String[] array = (String[]) container.getValue();
102:
103: JXPathContext context = JXPathContext.newContext(null);
104: context.getVariables().declareVariable("list", container);
105:
106: assertXPathValueAndPointer(context, "$list", array, "$list");
107: assertXPathValueAndPointer(context, "$list[1]", "foo",
108: "$list[1]");
109: assertXPathValueAndPointer(context, "$list[2]", "bar",
110: "$list[2]");
111:
112: assertXPathSetValue(context, "$list[1]", "baz");
113: assertEquals("Checking setValue(index)", "baz", array[0]);
114: }
115:
116: public void testContainerPropertyWithCollection() {
117: Bean bean = new Bean();
118: List list = (List) bean.getContainer().getValue();
119:
120: JXPathContext context = JXPathContext.newContext(bean);
121:
122: assertXPathValueAndPointer(context, "/container", list,
123: "/container");
124: assertXPathValueAndPointer(context, "/container[1]", list
125: .get(0), "/container[1]");
126: assertXPathValueAndPointer(context, "/container[2]", list
127: .get(1), "/container[2]");
128:
129: assertXPathSetValue(context, "/container[1]", "baz");
130: assertEquals("Checking setValue(index)", "baz", list.get(0));
131: }
132:
133: public void testContainerMapWithCollection() {
134: ListContainer container = new ListContainer();
135: List list = (List) container.getValue();
136:
137: Map map = new HashMap();
138: map.put("container", container);
139:
140: JXPathContext context = JXPathContext.newContext(map);
141:
142: assertXPathValueAndPointer(context, "/container", list,
143: "/.[@name='container']");
144: assertXPathValueAndPointer(context, "/container[1]", list
145: .get(0), "/.[@name='container'][1]");
146: assertXPathValueAndPointer(context, "/container[2]", list
147: .get(1), "/.[@name='container'][2]");
148:
149: assertXPathSetValue(context, "/container[1]", "baz");
150: assertEquals("Checking setValue(index)", "baz", list.get(0));
151: }
152:
153: public void testContainerRootWithCollection() {
154: ArrayContainer container = new ArrayContainer();
155: String[] array = (String[]) container.getValue();
156:
157: JXPathContext context = JXPathContext.newContext(container);
158: context.getVariables().declareVariable("list", container);
159:
160: assertXPathValueAndPointer(context, "/", array, "/");
161: assertXPathValueAndPointer(context, "/.[1]", "foo", "/.[1]");
162: assertXPathValueAndPointer(context, "/.[2]", "bar", "/.[2]");
163:
164: assertXPathSetValue(context, "/.[1]", "baz");
165: assertEquals("Checking setValue(index)", "baz", array[0]);
166: }
167:
168: }
|