001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.util;
051:
052: import java.util.*;
053: import junit.framework.*;
054:
055: public class ListMapTest extends TestCase {
056: // constants
057: private static final String KEY1 = "Key1", KEY2 = "Key2",
058: KEY3 = "Key3", KEY4 = "Key4", KEY5 = "Key5";
059: private static final String KEY1_VALUE = "Key1Value",
060: KEY2_VALUE = "Key2Value", KEY3_VALUE = "Key3Value",
061: KEY4_VALUE = "Key4Value", KEY5_VALUE = "Key5Value";
062:
063: // fixtures
064: private ListMap f_listMap = null;
065: private Set f_keySet = null;
066: private Collection f_values = null;
067:
068: public ListMapTest(java.lang.String testName) {
069: super (testName);
070: }
071:
072: public static void main(java.lang.String[] args) {
073: junit.textui.TestRunner.run(suite());
074: }
075:
076: public static Test suite() {
077: TestSuite suite = new TestSuite(ListMapTest.class);
078:
079: return suite;
080: }
081:
082: public void setUp() {
083: f_listMap = new ListMap();
084: f_listMap.put(KEY1, KEY1_VALUE);
085: f_listMap.put(KEY2, KEY2_VALUE);
086: f_listMap.put(KEY3, KEY3_VALUE);
087: f_listMap.put(KEY4, KEY4_VALUE);
088: f_listMap.put(KEY5, KEY5_VALUE);
089:
090: f_keySet = new HashSet();
091: f_keySet.add(KEY1);
092: f_keySet.add(KEY2);
093: f_keySet.add(KEY3);
094: f_keySet.add(KEY4);
095: f_keySet.add(KEY5);
096:
097: f_values = new ArrayList();
098: f_values.add(KEY1_VALUE);
099: f_values.add(KEY2_VALUE);
100: f_values.add(KEY3_VALUE);
101: f_values.add(KEY4_VALUE);
102: f_values.add(KEY5_VALUE);
103: }
104:
105: public void tearDown() {
106: f_listMap = null;
107: }
108:
109: /** Test of put method, of class org.jaffa.util.ListMap. */
110: public void testPut() {
111: f_listMap.put("Key6", "Key6Value");
112: if (!"Key6Value".equals(f_listMap.get("Key6")))
113: fail("Failed to add a key 'Key6' to the ListMap");
114: }
115:
116: /** Test of put method, of class org.jaffa.util.ListMap for substitution */
117: public void testPutAndSubstitute() {
118: // Add an existing key & see if it returns its old value
119: Object obj = f_listMap.put(KEY1, "Key1NewValue");
120: if (!KEY1_VALUE.equals(obj))
121: fail("Failed to substitute value for the key '" + KEY1
122: + "'");
123: }
124:
125: /** Test of put method, of class org.jaffa.util.ListMap for substitution */
126: public void testPutAndSubstitute1() {
127: // Add an existing key
128: f_listMap.put(KEY1, "Key1NewValue");
129:
130: // Now make sure that the new value was indeed stored
131: if (!"Key1NewValue".equals(f_listMap.get(KEY1)))
132: fail("Failed to add a new value for the key '" + KEY1
133: + "' to the ListMap");
134: }
135:
136: /** Test of remove method, of class org.jaffa.util.ListMap. */
137: public void testRemove() {
138: f_listMap.remove(KEY1);
139: if (f_listMap.get(KEY1) != null)
140: fail("Failed to remove the key '" + KEY1
141: + "' from the ListMap");
142: }
143:
144: /** Test of remove method, of class org.jaffa.util.ListMap,
145: making sure that the removed object is returned */
146: public void testRemove1() {
147: Object value = f_listMap.remove(KEY1);
148: if (!KEY1_VALUE.equals(value))
149: fail("Failed to get the value object for key '" + KEY1
150: + "' when removing it from the ListMap");
151: }
152:
153: /** Test of remove method, of class org.jaffa.util.ListMap from the keySet() */
154: public void testRemove2() {
155: Set keySet = f_listMap.keySet();
156: keySet.remove(KEY1);
157: if (f_listMap.get(KEY1) != null)
158: fail("Failed to remove the key '" + KEY1
159: + "' from the ListMap");
160: }
161:
162: /** Test of remove method, of class org.jaffa.util.ListMap from the keySet().Iterator() */
163: public void testRemove3() {
164: Iterator itr = f_listMap.keySet().iterator();
165: itr.next();
166: itr.remove();
167: if (f_listMap.get(KEY1) != null)
168: fail("Failed to remove the key '" + KEY1
169: + "' from the ListMap");
170:
171: // make sure no exception is raised
172: itr.next();
173: }
174:
175: /** Test of remove method, of class org.jaffa.util.ListMap from the entrySet() */
176: public void testRemove4() {
177: Set entrySet = f_listMap.entrySet();
178: Map.Entry entry = (Map.Entry) entrySet.iterator().next();
179: entrySet.remove(entry);
180: if (f_listMap.get(KEY1) != null)
181: fail("Failed to remove the key '" + KEY1
182: + "' from the ListMap");
183: }
184:
185: /** Test of remove method, of class org.jaffa.util.ListMap from the entrySet().Iterator() */
186: public void testRemove5() {
187: Iterator itr = f_listMap.entrySet().iterator();
188: itr.next();
189: itr.remove();
190: if (f_listMap.get(KEY1) != null)
191: fail("Failed to remove the key '" + KEY1
192: + "' from the ListMap");
193:
194: // make sure no exception is raised
195: itr.next();
196: }
197:
198: /** Test of remove method, of class org.jaffa.util.ListMap from the values() */
199: public void testRemove6() {
200: Collection values = f_listMap.values();
201: values.remove(KEY2_VALUE);
202: if (f_listMap.get(KEY2) != null)
203: fail("Failed to remove the key '" + KEY1
204: + "' from the ListMap");
205: }
206:
207: /** Test of remove method, of class org.jaffa.util.ListMap from the values().Iterator() */
208: public void testRemove7() {
209: Iterator itr = f_listMap.values().iterator();
210: itr.next();
211: itr.remove();
212: if (f_listMap.get(KEY1) != null)
213: fail("Failed to remove the key '" + KEY1
214: + "' from the ListMap");
215:
216: // make sure no exception is raised
217: itr.next();
218: }
219:
220: /** Test of keySet method, of class org.jaffa.util.ListMap. */
221: public void testKeySet() {
222: Set keySet = f_listMap.keySet();
223: if (keySet == null || f_keySet.size() != keySet.size())
224: fail("Failed to get the correct keySet from the ListMap");
225: }
226:
227: /** Test of keySet method, of class org.jaffa.util.ListMap. */
228: public void testKeySet1() {
229: Set keySet = f_listMap.keySet();
230: if (!f_keySet.containsAll(keySet))
231: fail("Failed to get the keySet from the ListMap");
232: }
233:
234: /** Test of keySet method, of class org.jaffa.util.ListMap.
235: Check if changes to the keySet affect the underlying ListMap */
236: public void testKeySet2() {
237: Set keySet = f_listMap.keySet();
238: keySet.remove(KEY1);
239: if (f_listMap.get(KEY1) != null)
240: fail("Failed to remove '"
241: + KEY1
242: + "' from the listMap by removing it from the keySet");
243: }
244:
245: /** Test of keySet method, of class org.jaffa.util.ListMap.
246: Check if changes to the keySet affect the underlying ListMap */
247: public void testKeySet3() {
248: Set keySet = f_listMap.keySet();
249: keySet.clear();
250: if (!f_listMap.isEmpty())
251: fail("Failed to clear the listMap by clearing its keySet");
252: }
253:
254: /** Test of keySet method, of class org.jaffa.util.ListMap.
255: Create a ListMap & add elemenst in a particular order
256: Make sure the collection returns values in the same order */
257: public void testKeySet4() {
258: // Create a ListMap & add elemenst in a particular order
259: ListMap listMap = new ListMap();
260: listMap.put(KEY1, KEY1_VALUE);
261: listMap.put(KEY5, KEY5_VALUE);
262: listMap.put(KEY2, KEY2_VALUE);
263: listMap.put(KEY4, KEY4_VALUE);
264: listMap.put(KEY3, KEY3_VALUE);
265:
266: // make sure the collection returns values in the same order
267: Iterator itr = listMap.keySet().iterator();
268: if (!KEY1.equals(itr.next()) || !KEY5.equals(itr.next())
269: || !KEY2.equals(itr.next()) || !KEY4.equals(itr.next())
270: || !KEY3.equals(itr.next()))
271: fail("Failed to read the Values in the Order of entry");
272: }
273:
274: /** Test of clear method, of class org.jaffa.util.ListMap. */
275: public void testClear() {
276: f_listMap.clear();
277: if (!f_listMap.isEmpty())
278: fail("Failed to clear the listMap");
279: }
280:
281: public void testValues() {
282: Collection values = f_listMap.values();
283: if (values == null || f_values.size() != values.size())
284: fail("Failed to get the correct Values from the ListMap");
285: }
286:
287: public void testValues1() {
288: Collection values = f_listMap.values();
289: if (!f_values.containsAll(values))
290: fail("Failed to get the Values from the ListMap");
291: }
292:
293: public void testValues2() {
294: // check if changes to the Values affect the underlying ListMap
295: Collection values = f_listMap.values();
296: values.remove(KEY1_VALUE);
297: if (f_listMap.get(KEY1) != null)
298: fail("Failed to un-touch the ListMap by updating the Values");
299: }
300:
301: public void testValues3() {
302: // check if changes to the Values affect the underlying ListMap
303: Collection values = f_listMap.values();
304: values.clear();
305: if (!f_listMap.isEmpty())
306: fail("Failed to un-touch the ListMap by updating the Values");
307: }
308:
309: public void testValues4() {
310: // Create a ListMap & add elemenst in a particular order
311: ListMap listMap = new ListMap();
312: listMap.put(KEY1, KEY1_VALUE);
313: listMap.put(KEY5, KEY5_VALUE);
314: listMap.put(KEY2, KEY2_VALUE);
315: listMap.put(KEY4, KEY4_VALUE);
316: listMap.put(KEY3, KEY3_VALUE);
317:
318: // make sure the collection returns values in the same order
319: Iterator itr = listMap.values().iterator();
320: if (!KEY1_VALUE.equals(itr.next())
321: || !KEY5_VALUE.equals(itr.next())
322: || !KEY2_VALUE.equals(itr.next())
323: || !KEY4_VALUE.equals(itr.next())
324: || !KEY3_VALUE.equals(itr.next()))
325: fail("Failed to read the Values in the Order of entry");
326: }
327:
328: public void testValues5() {
329: // Check if the values() method works when the listMap() is modified externally
330: Iterator itr = f_listMap.keySet().iterator();
331: Object key = itr.next();
332: Object value = f_listMap.get(key);
333:
334: // now modify the listMap externally
335: itr.remove();
336:
337: // check if the collection still has the 'value' in it.. it shouldnt
338: Collection values = f_listMap.values();
339: if (values.contains(value))
340: fail("The Values method still returns a removed key");
341: }
342:
343: /** Test of hashCode method, of class org.jaffa.util.ListMap. */
344: public void testHashCode() {
345: // get the hashcode
346: int hashCode1 = f_listMap.hashCode();
347:
348: // create a similar listMap & check its hashCode
349: ListMap listMap = new ListMap(f_listMap);
350: int hashCode2 = listMap.hashCode();
351:
352: // the 2 hashcodes should be the same
353: if (hashCode1 != hashCode2)
354: fail("Failed to get a consistent hashCode");
355: }
356:
357: public void testHashCode1() {
358: // get the hashcode
359: int hashCode1 = f_listMap.hashCode();
360:
361: // manipulate the listMap such that the net effect is unchanged & check its hashCode
362: f_listMap.remove(KEY1);
363: f_listMap.remove(KEY2);
364: f_listMap.put(KEY2, KEY2_VALUE);
365: f_listMap.put(KEY3, KEY3_VALUE); // just a substitution
366: f_listMap.put(KEY1, KEY1_VALUE);
367: int hashCode2 = f_listMap.hashCode();
368:
369: // the 2 hashcodes should be the same
370: if (hashCode1 != hashCode2)
371: fail("Failed to get a consistent hashCode");
372: }
373:
374: /** Test of containsKey method, of class org.jaffa.util.ListMap. */
375: public void testContainsKey() {
376: if (!f_listMap.containsKey(KEY1))
377: fail("Failed to find '" + KEY1 + "' in the listMap");
378: }
379:
380: public void testContainsKey1() {
381: if (f_listMap.containsKey("blahblahblah"))
382: fail("Method - containsKey - Failed");
383: }
384:
385: /** Test of size method, of class org.jaffa.util.ListMap. */
386: public void testSize() {
387: if (f_listMap.size() != f_keySet.size()
388: || f_listMap.size() != f_values.size())
389: fail("Method - size - Failed");
390: }
391:
392: /** Test of entrySet method, of class org.jaffa.util.ListMap. */
393: public void testEntrySet() {
394: Set entrySet = f_listMap.entrySet();
395: if (entrySet == null || f_keySet.size() != entrySet.size())
396: fail("Failed to get the correct entrySet from the ListMap");
397: }
398:
399: public void testEntrySet1() {
400: Set entrySet = f_listMap.entrySet();
401:
402: // create a keySet & a valuesList
403: Set keySet = new HashSet();
404: List valuesList = new ArrayList();
405: for (Iterator itr = entrySet.iterator(); itr.hasNext();) {
406: Map.Entry me = (Map.Entry) itr.next();
407: keySet.add(me.getKey());
408: valuesList.add(me.getValue());
409: }
410:
411: if (!f_keySet.containsAll(keySet)
412: || !f_values.containsAll(valuesList))
413: fail("Failed to get the entrySet from the ListMap");
414: }
415:
416: public void testEntrySet2() {
417: // check if changes to the entrySet affect the underlying ListMap
418: Set entrySet = f_listMap.entrySet();
419:
420: // get a Map.Entry & store its key
421: Map.Entry me = (Map.Entry) entrySet.iterator().next();
422: Object key = me.getKey();
423:
424: // remove the Map.Entry from the entrySet & check if 'key' still exists in the listMap
425: entrySet.remove(me);
426: if (f_listMap.get(key) != null)
427: fail("Failed to remove '"
428: + key
429: + "' from the listMap by removing it from the entrySet");
430: }
431:
432: public void testEntrySet3() {
433: // check if changes to the entrySet affect the underlying ListMap
434: Set entrySet = f_listMap.entrySet();
435: entrySet.clear();
436: if (!f_listMap.isEmpty())
437: fail("Failed to clear the listMap by clearing its entrySet");
438: }
439:
440: /** Test of containsValue method, of class org.jaffa.util.ListMap. */
441: public void testContainsValue() {
442: if (!f_listMap.containsValue(KEY1_VALUE))
443: fail("Method - containsValue - Failed");
444: }
445:
446: /** Test of putAll method, of class org.jaffa.util.ListMap. */
447: public void testPutAll() {
448: Map map = new HashMap();
449: map.put("KeyA", "KeyAValue");
450: map.put(KEY1, "Key1NewValue"); // should substitute the old value
451: map.put("KeyZ", "KeyZValue");
452: f_listMap.putAll(map);
453:
454: if (!"Key1NewValue".equals(f_listMap.get(KEY1))
455: || !"KeyAValue".equals(f_listMap.get("KeyA"))
456: || !"KeyZValue".equals(f_listMap.get("KeyZ")))
457: fail("Method - putAll - Failed");
458: }
459:
460: /** Test of equals method, of class org.jaffa.util.ListMap. */
461: public void testEquals() {
462: ListMap listMap = new ListMap();
463: listMap.put(KEY1, KEY1_VALUE);
464: listMap.put(KEY3, KEY3_VALUE);
465: listMap.put(KEY2, KEY2_VALUE);
466: listMap.put(KEY5, KEY5_VALUE);
467: listMap.put(KEY4, KEY4_VALUE);
468:
469: if (!f_listMap.equals(listMap))
470: fail("Method - equals - failed");
471: }
472:
473: public void testEquals1() {
474: ListMap listMap = new ListMap();
475: listMap.put(KEY1, KEY1_VALUE);
476: listMap.put(KEY3, KEY3_VALUE);
477: listMap.put(KEY2, KEY2_VALUE);
478: listMap.put(KEY5, KEY5_VALUE);
479: listMap.put("KeyZ", KEY4_VALUE);
480:
481: if (f_listMap.equals(listMap))
482: fail("Method - equals - failed");
483: }
484:
485: /** Test of isEmpty method, of class org.jaffa.util.ListMap. */
486: public void testIsEmpty() {
487: if (f_listMap.isEmpty())
488: fail("Method - isEmpty - failed");
489: }
490:
491: public void testIsEmpty1() {
492: f_listMap.clear();
493: if (!f_listMap.isEmpty())
494: fail("Method - isEmpty - failed");
495: }
496:
497: public void testIsEmpty2() {
498: // remove() all the elements as opposed to using a clear()
499: for (Iterator itr = f_listMap.keySet().iterator(); itr
500: .hasNext();) {
501: itr.next();
502: itr.remove();
503: }
504: if (!f_listMap.isEmpty())
505: fail("Method - isEmpty - failed");
506: }
507:
508: /** Test of get method, of class org.jaffa.util.ListMap. */
509: public void testGet() {
510: if (!KEY1_VALUE.equals(f_listMap.get(KEY1)))
511: fail("Method - get - failed");
512: }
513:
514: public void testGet1() {
515: if (f_listMap.get("blah") != null)
516: fail("Method - get - failed");
517: }
518: }
|