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.dynamic;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.apache.commons.jxpath.JXPathContext;
024: import org.apache.commons.jxpath.JXPathTestCase;
025: import org.apache.commons.jxpath.TestBean;
026:
027: /**
028: * @todo more iterator testing with maps
029: *
030: * @author Dmitri Plotnikov
031: * @version $Revision: 1.8 $ $Date: 2004/02/29 14:17:45 $
032: */
033:
034: public class DynamicPropertiesModelTest extends JXPathTestCase {
035: private static boolean enabled = true;
036: private JXPathContext context;
037:
038: /**
039: * Construct a new instance of this test case.
040: *
041: * @param name Name of the test case
042: */
043: public DynamicPropertiesModelTest(String name) {
044: super (name);
045: }
046:
047: public void setUp() {
048: if (context == null) {
049: context = JXPathContext.newContext(new TestBean());
050: context.setFactory(new TestDynamicPropertyFactory());
051: }
052: }
053:
054: public void testAxisChild() {
055: assertXPathValue(context, "map/Key1", "Value 1");
056:
057: assertXPathPointer(context, "map/Key1", "/map[@name='Key1']");
058:
059: assertXPathValue(context, "map/Key2/name", "Name 6");
060:
061: assertXPathPointer(context, "map/Key2/name",
062: "/map[@name='Key2']/name");
063: }
064:
065: public void testAxisDescendant() {
066: assertXPathValue(context, "//Key1", "Value 1");
067: }
068:
069: /**
070: * Testing the pseudo-attribute "name" that dynamic property
071: * objects appear to have.
072: */
073: public void testAttributeName() {
074: assertXPathValue(context, "map[@name = 'Key1']", "Value 1");
075:
076: assertXPathPointer(context, "map[@name = 'Key1']",
077: "/map[@name='Key1']");
078:
079: assertXPathPointerLenient(context,
080: "map[@name = 'Key"'"'1']",
081: "/map[@name='Key"'"'1']");
082:
083: assertXPathValue(context, "/.[@name='map']/Key2/name", "Name 6");
084:
085: assertXPathPointer(context, "/.[@name='map']/Key2/name",
086: "/map[@name='Key2']/name");
087:
088: // Bean in a map
089: assertXPathValue(context, "/map[@name='Key2'][@name='name']",
090: "Name 6");
091:
092: assertXPathPointer(context, "/map[@name='Key2'][@name='name']",
093: "/map[@name='Key2']/name");
094:
095: // Map in a bean in a map
096: assertXPathValue(context,
097: "/.[@name='map'][@name='Key2'][@name='name']", "Name 6");
098:
099: assertXPathPointer(context,
100: "/.[@name='map'][@name='Key2'][@name='name']",
101: "/map[@name='Key2']/name");
102:
103: ((Map) context.getValue("map")).put("Key:3", "value3");
104:
105: assertXPathValueAndPointer(context, "/map[@name='Key:3']",
106: "value3", "/map[@name='Key:3']");
107:
108: assertXPathValueAndPointer(context, "/map[@name='Key:4:5']",
109: null, "/map[@name='Key:4:5']");
110: }
111:
112: public void testSetPrimitiveValue() {
113: assertXPathSetValue(context, "map/Key1", new Integer(6));
114: }
115:
116: public void testSetCollection() {
117: // See if we can assign a whole collection
118: context.setValue("map/Key1", new Integer[] { new Integer(7),
119: new Integer(8) });
120:
121: // And then an element in that collection
122: assertXPathSetValue(context, "map/Key1[1]", new Integer(9));
123: }
124:
125: /**
126: * The key does not exist, but the assignment should succeed anyway,
127: * because you should always be able to store anything in a Map.
128: */
129: public void testSetNewKey() {
130: // Using a "simple" path
131: assertXPathSetValue(context, "map/Key4", new Integer(7));
132:
133: // Using a "non-simple" path
134: assertXPathPointerLenient(context, "//map/Key5", "/map/Key5");
135:
136: assertXPathSetValue(context, "//map/Key5", new Integer(8));
137: }
138:
139: public void testCreatePath() {
140: TestBean bean = (TestBean) context.getContextBean();
141: bean.setMap(null);
142:
143: // Calls factory.createObject(..., testBean, "map"), then
144: // sets the value
145: assertXPathCreatePath(context, "/map[@name='TestKey1']", "",
146: "/map[@name='TestKey1']");
147: }
148:
149: public void testCreatePathAndSetValue() {
150: TestBean bean = (TestBean) context.getContextBean();
151: bean.setMap(null);
152:
153: // Calls factory.createObject(..., testBean, "map"), then
154: // sets the value
155: assertXPathCreatePathAndSetValue(context,
156: "/map[@name='TestKey1']", "Test",
157: "/map[@name='TestKey1']");
158: }
159:
160: public void testCreatePathCreateBean() {
161: TestBean bean = (TestBean) context.getContextBean();
162: bean.setMap(null);
163:
164: // Calls factory.createObject(..., testBean, "map"), then
165: // then factory.createObject(..., map, "TestKey2"), then
166: // sets the value
167: assertXPathCreatePath(context, "/map[@name='TestKey2']/int",
168: new Integer(1), "/map[@name='TestKey2']/int");
169: }
170:
171: public void testCreatePathAndSetValueCreateBean() {
172: TestBean bean = (TestBean) context.getContextBean();
173: bean.setMap(null);
174:
175: // Calls factory.createObject(..., testBean, "map"), then
176: // then factory.createObject(..., map, "TestKey2"), then
177: // sets the value
178: assertXPathCreatePathAndSetValue(context,
179: "/map[@name='TestKey2']/int", new Integer(4),
180: "/map[@name='TestKey2']/int");
181: }
182:
183: public void testCreatePathCollectionElement() {
184: TestBean bean = (TestBean) context.getContextBean();
185: bean.setMap(null);
186:
187: assertXPathCreatePath(context, "/map/TestKey3[2]", null,
188: "/map[@name='TestKey3'][2]");
189:
190: // Should be the same as the one before
191: assertXPathCreatePath(context, "/map[@name='TestKey3'][3]",
192: null, "/map[@name='TestKey3'][3]");
193: }
194:
195: public void testCreatePathAndSetValueCollectionElement() {
196: TestBean bean = (TestBean) context.getContextBean();
197: bean.setMap(null);
198:
199: assertXPathCreatePathAndSetValue(context, "/map/TestKey3[2]",
200: "Test1", "/map[@name='TestKey3'][2]");
201:
202: // Should be the same as the one before
203: assertXPathCreatePathAndSetValue(context,
204: "/map[@name='TestKey3'][3]", "Test2",
205: "/map[@name='TestKey3'][3]");
206: }
207:
208: public void testCreatePathNewCollectionElement() {
209: TestBean bean = (TestBean) context.getContextBean();
210: bean.setMap(null);
211:
212: // Create an element of a dynamic map element, which is a collection
213: assertXPathCreatePath(context, "/map/TestKey4[1]/int",
214: new Integer(1), "/map[@name='TestKey4'][1]/int");
215:
216: bean.getMap().remove("TestKey4");
217:
218: // Should be the same as the one before
219: assertXPathCreatePath(context, "/map/TestKey4[1]/int",
220: new Integer(1), "/map[@name='TestKey4'][1]/int");
221: }
222:
223: public void testCreatePathAndSetValueNewCollectionElement() {
224: TestBean bean = (TestBean) context.getContextBean();
225: bean.setMap(null);
226:
227: // Create an element of a dynamic map element, which is a collection
228: assertXPathCreatePathAndSetValue(context,
229: "/map/TestKey4[1]/int", new Integer(2),
230: "/map[@name='TestKey4'][1]/int");
231:
232: bean.getMap().remove("TestKey4");
233:
234: // Should be the same as the one before
235: assertXPathCreatePathAndSetValue(context,
236: "/map/TestKey4[1]/int", new Integer(3),
237: "/map[@name='TestKey4'][1]/int");
238: }
239:
240: public void testRemovePath() {
241: TestBean bean = (TestBean) context.getContextBean();
242: bean.getMap().put("TestKey1", "test");
243:
244: // Remove dynamic property
245: context.removePath("map[@name = 'TestKey1']");
246: assertEquals("Remove dynamic property value", null, context
247: .getValue("map[@name = 'TestKey1']"));
248: }
249:
250: public void testRemovePathArrayElement() {
251: TestBean bean = (TestBean) context.getContextBean();
252:
253: bean.getMap()
254: .put("TestKey2", new String[] { "temp1", "temp2" });
255: context.removePath("map[@name = 'TestKey2'][1]");
256: assertEquals("Remove dynamic property collection element",
257: "temp2", context.getValue("map[@name = 'TestKey2'][1]"));
258: }
259:
260: public void testCollectionOfMaps() {
261: TestBean bean = (TestBean) context.getContextBean();
262: List list = new ArrayList();
263:
264: bean.getMap().put("stuff", list);
265:
266: Map m = new HashMap();
267: m.put("fruit", "apple");
268: list.add(m);
269:
270: m = new HashMap();
271: m.put("berry", "watermelon");
272: list.add(m);
273:
274: m = new HashMap();
275: m.put("fruit", "banana");
276: list.add(m);
277:
278: assertXPathValueIterator(context, "/map/stuff/fruit", list(
279: "apple", "banana"));
280:
281: assertXPathValueIterator(context, "/map/stuff[@name='fruit']",
282: list("apple", "banana"));
283: }
284:
285: public void testMapOfMaps() {
286: TestBean bean = (TestBean) context.getContextBean();
287:
288: Map fruit = new HashMap();
289: fruit.put("apple", "green");
290: fruit.put("orange", "red");
291:
292: Map meat = new HashMap();
293: meat.put("pork", "pig");
294: meat.put("beef", "cow");
295:
296: bean.getMap().put("fruit", fruit);
297: bean.getMap().put("meat", meat);
298:
299: assertXPathPointer(context, "//beef",
300: "/map[@name='meat'][@name='beef']");
301:
302: assertXPathPointer(context, "map//apple",
303: "/map[@name='fruit'][@name='apple']");
304:
305: // Ambiguous search - will return nothing
306: assertXPathPointerLenient(context, "map//banana", "null()");
307:
308: // Unambiguous, even though the particular key is missing
309: assertXPathPointerLenient(context, "//fruit/pear",
310: "/map[@name='fruit']/pear");
311: }
312: }
|