1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jxpath.ri.model.container;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25 import junit.textui.TestRunner;
26
27 import org.apache.commons.jxpath.Container;
28 import org.apache.commons.jxpath.JXPathContext;
29 import org.apache.commons.jxpath.JXPathTestCase;
30
31 /***
32 * Tests JXPath with containers as root or value of a variable, property, etc.
33 *
34 * @author Dmitri Plotnikov
35 * @version $Revision: 1.1 $ $Date: 2004/04/04 22:06:35 $
36 */
37
38 public class ContainerModelTest extends JXPathTestCase {
39 private JXPathContext context;
40
41 /***
42 * Construct a new instance of this test case.
43 *
44 * @param name Name of the test case
45 */
46 public ContainerModelTest(String name) {
47 super(name);
48 }
49
50 public static void main(String[] args) {
51 TestRunner.run(suite());
52 }
53
54 /***
55 * Return the tests included in this test suite.
56 */
57 public static Test suite() {
58 return (new TestSuite(ContainerModelTest.class));
59 }
60
61
62 private class ArrayContainer implements Container
63 {
64 private String[] array = new String[]{"foo", "bar"};
65 public Object getValue() {
66 return array;
67 }
68
69 public void setValue(Object value) {
70 throw new UnsupportedOperationException();
71 }
72 };
73
74 public class ListContainer implements Container
75 {
76 private List list;
77
78 public ListContainer() {
79 list = new ArrayList();
80 list.add("foo");
81 list.add("bar");
82 }
83
84 public Object getValue() {
85 return list;
86 }
87
88 public void setValue(Object value) {
89 throw new UnsupportedOperationException();
90 }
91 }
92
93 public class Bean
94 {
95 private ListContainer container = new ListContainer();
96
97 public ListContainer getContainer() {
98 return container;
99 }
100 }
101
102 public void testContainerVariableWithCollection() {
103 ArrayContainer container = new ArrayContainer();
104 String[] array = (String[]) container.getValue();
105
106 JXPathContext context = JXPathContext.newContext(null);
107 context.getVariables().declareVariable("list", container);
108
109 assertXPathValueAndPointer(context, "$list", array, "$list");
110 assertXPathValueAndPointer(context, "$list[1]", "foo", "$list[1]");
111 assertXPathValueAndPointer(context, "$list[2]", "bar", "$list[2]");
112
113 assertXPathSetValue(context, "$list[1]", "baz");
114 assertEquals("Checking setValue(index)", "baz", array[0]);
115 }
116
117 public void testContainerPropertyWithCollection() {
118 Bean bean = new Bean();
119 List list = (List) bean.getContainer().getValue();
120
121 JXPathContext context = JXPathContext.newContext(bean);
122
123 assertXPathValueAndPointer(context, "/container",
124 list, "/container");
125 assertXPathValueAndPointer(context, "/container[1]",
126 list.get(0), "/container[1]");
127 assertXPathValueAndPointer(context, "/container[2]",
128 list.get(1), "/container[2]");
129
130 assertXPathSetValue(context, "/container[1]", "baz");
131 assertEquals("Checking setValue(index)", "baz", list.get(0));
132 }
133
134 public void testContainerMapWithCollection() {
135 ListContainer container = new ListContainer();
136 List list = (List) container.getValue();
137
138 Map map = new HashMap();
139 map.put("container", container);
140
141 JXPathContext context = JXPathContext.newContext(map);
142
143 assertXPathValueAndPointer(context, "/container",
144 list, "/.[@name='container']");
145 assertXPathValueAndPointer(context, "/container[1]",
146 list.get(0), "/.[@name='container'][1]");
147 assertXPathValueAndPointer(context, "/container[2]",
148 list.get(1), "/.[@name='container'][2]");
149
150 assertXPathSetValue(context, "/container[1]", "baz");
151 assertEquals("Checking setValue(index)", "baz", list.get(0));
152 }
153
154 public void testContainerRootWithCollection() {
155 ArrayContainer container = new ArrayContainer();
156 String[] array = (String[]) container.getValue();
157
158 JXPathContext context = JXPathContext.newContext(container);
159 context.getVariables().declareVariable("list", container);
160
161 assertXPathValueAndPointer(context, "/", array, "/");
162 assertXPathValueAndPointer(context, "/.[1]", "foo", "/.[1]");
163 assertXPathValueAndPointer(context, "/.[2]", "bar", "/.[2]");
164
165 assertXPathSetValue(context, "/.[1]", "baz");
166 assertEquals("Checking setValue(index)", "baz", array[0]); }
167
168 }