001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.beanutils;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Date;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030:
031: /**
032: * <p>Test Case for the <code>DynaBeanMapDecorator</code> implementation class.</p>
033: *
034: * @author Niall Pemberton
035: */
036: public class DynaBeanMapDecoratorTestCase extends TestCase {
037:
038: private static final DynaProperty stringProp = new DynaProperty(
039: "stringProp", String.class);
040: private static final DynaProperty nullProp = new DynaProperty(
041: "nullProp", String.class);
042: private static final DynaProperty intProp = new DynaProperty(
043: "intProp", Integer.class);
044: private static final DynaProperty dateProp = new DynaProperty(
045: "dateProp", Date.class);
046: private static final DynaProperty mapProp = new DynaProperty(
047: "mapProp", Map.class);
048: private static final DynaProperty[] properties = new DynaProperty[] {
049: stringProp, nullProp, intProp, dateProp, mapProp };
050: private static final DynaClass dynaClass = new BasicDynaClass(
051: "testDynaClass", BasicDynaBean.class, properties);
052:
053: private static String stringVal = "somevalue";
054: private static Integer intVal = new Integer(5);
055: private static Date dateVal = new Date();
056: private Map mapVal = new HashMap();
057:
058: private Object[] values = new Object[] { stringVal, null, intVal,
059: dateVal, mapVal };
060:
061: private BasicDynaBean dynaBean;
062: private Map decoratedMap;
063: private Map modifiableMap;
064: private static final Map emptyMap = new DynaBeanMapDecorator(
065: new BasicDynaBean(new BasicDynaClass()));
066:
067: // ---------------------------------------------------------- Constructors
068:
069: /**
070: * Construct a new instance of this test case.
071: *
072: * @param name Name of the test case
073: */
074: public DynaBeanMapDecoratorTestCase(String name) {
075: super (name);
076: }
077:
078: // -------------------------------------------------- Overall Test Methods
079:
080: /**
081: * Run thus Test
082: */
083: public static void main(String[] args) {
084: junit.textui.TestRunner.run(suite());
085: }
086:
087: /**
088: * Return the tests included in this test suite.
089: */
090: public static Test suite() {
091: return (new TestSuite(DynaBeanMapDecoratorTestCase.class));
092: }
093:
094: /**
095: * Set up instance variables required by this test case.
096: */
097: public void setUp() throws Exception {
098:
099: mapVal.clear();
100: mapVal.put("key1", "key1Value");
101: mapVal.put("key2", "key2Value");
102:
103: // Initialize DynaBean and properties
104: dynaBean = new BasicDynaBean(dynaClass);
105: for (int i = 0; i < properties.length; i++) {
106: dynaBean.set(properties[i].getName(), values[i]);
107: }
108:
109: // Create decorated Maps
110: decoratedMap = new DynaBeanMapDecorator(dynaBean);
111: modifiableMap = new DynaBeanMapDecorator(dynaBean, false);
112:
113: }
114:
115: /**
116: * Tear down instance variables required by this test case.
117: */
118: public void tearDown() {
119: dynaBean = null;
120: decoratedMap = null;
121: modifiableMap = null;
122: }
123:
124: // ------------------------------------------------ Individual Test Methods
125:
126: /**
127: * Test isReadOnly() method
128: */
129: public void testIsReadOnly() {
130: assertTrue("decoratedMap true",
131: ((DynaBeanMapDecorator) decoratedMap).isReadOnly());
132: assertFalse("modifiableMap false",
133: ((DynaBeanMapDecorator) modifiableMap).isReadOnly());
134: }
135:
136: /**
137: * Test clear() method
138: */
139: public void testClear() {
140: try {
141: decoratedMap.clear();
142: fail("decoratedMap.clear()");
143: } catch (UnsupportedOperationException ignore) {
144: // expected result
145: }
146: try {
147: modifiableMap.clear();
148: fail("modifiableMap.clear()");
149: } catch (UnsupportedOperationException ignore) {
150: // expected result
151: }
152: }
153:
154: /**
155: * Test containsKey() method
156: */
157: public void testContainsKey() {
158: assertTrue("decoratedMap true", decoratedMap
159: .containsKey(stringProp.getName()));
160: assertFalse("decoratedMap false", decoratedMap
161: .containsKey("xyz"));
162: }
163:
164: /**
165: * Test containsValue() method
166: */
167: public void testContainsValue() {
168: assertTrue("decoratedMap true", decoratedMap
169: .containsValue(stringVal));
170: assertFalse("decoratedMap false", decoratedMap
171: .containsValue("xyz"));
172: }
173:
174: /**
175: * Test entrySet() method
176: */
177: public void testEntrySet() {
178: Set set = modifiableMap.entrySet();
179:
180: // Check the Set can't be modified
181: checkUnmodifiable("entrySet()", set);
182:
183: assertEquals("entrySet size", properties.length, set.size());
184:
185: Iterator iterator = set.iterator();
186: List namesList = new ArrayList();
187: int i = 0;
188: while (iterator.hasNext()) {
189: Map.Entry entry = (Map.Entry) iterator.next();
190: String name = (String) entry.getKey();
191: namesList.add(name);
192: Object expectValue = decoratedMap.get(name);
193: assertEquals("entrySet(" + i + ") val", expectValue, entry
194: .getValue());
195: i++;
196: }
197: for (int j = 0; j < properties.length; j++) {
198: String name = properties[j].getName();
199: assertTrue("Check property[" + j + "]", namesList
200: .contains(name));
201: }
202: }
203:
204: /**
205: * Test get() method
206: */
207: public void testGet() {
208:
209: // valid property name
210: assertEquals("decoratedMap valid", stringVal, decoratedMap
211: .get(stringProp.getName()));
212:
213: // invalid property name
214: try {
215: decoratedMap.get("xyz");
216: fail("decoratedMap invalid");
217: } catch (IllegalArgumentException ignore) {
218: // expected result
219: }
220: }
221:
222: /**
223: * Test isEmpty() method
224: */
225: public void testIsEmpty() {
226: assertTrue("Empty", emptyMap.isEmpty());
227: assertFalse("Not Empty", decoratedMap.isEmpty());
228: }
229:
230: /**
231: * Test keySet() method
232: */
233: public void testKeySet() {
234: Set set = modifiableMap.keySet();
235:
236: // Check the Set can't be modified
237: checkUnmodifiable("keySet()", set);
238:
239: assertEquals("keySet size", properties.length, set.size());
240:
241: for (int i = 0; i < properties.length; i++) {
242: String name = properties[i].getName();
243: assertTrue("Check property[" + i + "]", set.contains(name));
244: }
245: }
246:
247: /**
248: * Test put() method
249: */
250: public void testPut() {
251:
252: String newValue = "ABC";
253:
254: // Test read only
255: try {
256: decoratedMap.put(stringProp.getName(), newValue);
257: fail("Not read only");
258: } catch (UnsupportedOperationException ignore) {
259: // expected result
260: }
261:
262: // Test Writable
263: assertEquals("modifiableMap put", stringVal, modifiableMap.put(
264: stringProp.getName(), newValue));
265: assertEquals("dynaBean get", newValue, dynaBean.get(stringProp
266: .getName()));
267: assertEquals("modifiableMap get", newValue, modifiableMap
268: .get(stringProp.getName()));
269: }
270:
271: /**
272: * Test putAll() method
273: */
274: public void testPutAll() {
275:
276: String newValue = "ABC";
277: Map newMap = new HashMap();
278: newMap.put(stringProp.getName(), newValue);
279:
280: // Test read only
281: try {
282: decoratedMap.putAll(newMap);
283: fail("Not read only");
284: } catch (UnsupportedOperationException ignore) {
285: // expected result
286: }
287:
288: // Test Writable
289: assertEquals("before putAll", stringVal, dynaBean
290: .get(stringProp.getName()));
291: modifiableMap.putAll(newMap);
292: assertEquals("after putAll", newValue, dynaBean.get(stringProp
293: .getName()));
294: }
295:
296: /**
297: * Test remove() method
298: */
299: public void testRemove() {
300: try {
301: decoratedMap.remove(stringProp.getName());
302: fail("decoratedMap.remove()");
303: } catch (UnsupportedOperationException ignore) {
304: // expected result
305: }
306: try {
307: modifiableMap.remove(stringProp.getName());
308: fail("modifiableMap.remove()");
309: } catch (UnsupportedOperationException ignore) {
310: // expected result
311: }
312: }
313:
314: /**
315: * Test size() method
316: */
317: public void testSize() {
318: assertEquals("Empty", 0, emptyMap.size());
319: assertEquals("Not Empty", properties.length, decoratedMap
320: .size());
321: }
322:
323: /**
324: * Test values() method
325: */
326: public void testValues() {
327: Collection collection = modifiableMap.values();
328:
329: // Check the Collection can't be modified
330: checkUnmodifiable("values()", collection);
331:
332: assertEquals("values size", values.length, collection.size());
333:
334: // Collection should be ordered in same sequence as properties
335: Iterator iterator = collection.iterator();
336: int i = 0;
337: while (iterator.hasNext()) {
338: assertEquals("values(" + i + ")", values[i], iterator
339: .next());
340: i++;
341: }
342: }
343:
344: /**
345: * Check that a Collection is not modifiable
346: */
347: private void checkUnmodifiable(String desc, Collection collection) {
348: String testVal = "xyz";
349:
350: // Check can't add()
351: try {
352: collection.add(testVal);
353: fail(desc + ".add()");
354: } catch (UnsupportedOperationException ignore) {
355: // expected result
356: }
357:
358: // Check can't addAll()
359: List list = new ArrayList(1);
360: list.add(testVal);
361: try {
362: collection.addAll(list);
363: fail(desc + ".addAll()");
364: } catch (UnsupportedOperationException ignore) {
365: // expected result
366: }
367:
368: // Check can't clear()
369: try {
370: collection.clear();
371: fail(desc + ".clear()");
372: } catch (UnsupportedOperationException ignore) {
373: // expected result
374: }
375:
376: // Check can't remove()
377: try {
378: collection.remove("abc");
379: fail(desc + ".remove()");
380: } catch (UnsupportedOperationException ignore) {
381: // expected result
382: }
383:
384: // Check can't removeAll()
385: try {
386: collection.removeAll(list);
387: fail(desc + ".removeAll()");
388: } catch (UnsupportedOperationException ignore) {
389: // expected result
390: }
391:
392: // Check can't retainAll()
393: try {
394: collection.retainAll(list);
395: fail(desc + ".retainAll()");
396: } catch (UnsupportedOperationException ignore) {
397: // expected result
398: }
399: }
400: }
|