001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.lang.test;
027:
028: import org.jicarilla.lang.SelectorSwitch;
029: import org.jicarilla.lang.TrueSelector;
030: import org.jicarilla.lang.FalseSelector;
031: import org.jicarilla.lang.EquivalenceSelector;
032: import org.jicarilla.lang.Selector;
033: import org.jicarilla.lang.Switch;
034: import org.jicarilla.lang.StringSelector;
035:
036: import junit.framework.TestCase;
037:
038: import java.util.ArrayList;
039: import java.util.LinkedList;
040: import java.util.Arrays;
041: import java.util.List;
042: import java.util.Iterator;
043: import java.util.Map;
044: import java.util.HashMap;
045: import java.util.Set;
046: import java.util.Collection;
047: import java.util.HashSet;
048:
049: /**
050: *
051: *
052: * @author <a href="mail at leosimons dot com">Leo Simons</a>
053: * @version $Id: SelectorSwitchTestCase.java,v 1.1 2004/03/23 13:37:58 lsimons Exp $
054: */
055: public class SelectorSwitchTestCase extends TestCase {
056: protected List m_backend = new ArrayList();
057: protected SelectorSwitch m_switch = new SelectorSwitch(m_backend);
058:
059: protected TrueSelector m_true = new TrueSelector();
060: protected FalseSelector m_false = new FalseSelector();
061: protected EquivalenceSelector m_selector = new EquivalenceSelector(
062: this );
063:
064: protected Object m_value1 = "value1";
065: protected Object m_value2 = "value2";
066: protected Object m_value3 = "value3";
067:
068: protected void doPopulate() {
069: m_backend.add(new ExposingSwitch.Entry(m_false, m_value1));
070: m_backend.add(new ExposingSwitch.Entry(m_false, m_value2));
071: m_backend.add(new ExposingSwitch.Entry(m_false, m_value1));
072: /*m_backend.add(
073: null
074: );*/
075: m_backend.add(new ExposingSwitch.Entry(m_selector, this ));
076: }
077:
078: protected Map getPopulationMap() {
079: Map map = new HashMap();
080: map.put(m_false, m_value1);
081: map.put(m_false, m_value2);
082: map.put(m_false, m_value1);
083: map.put(this , this );
084:
085: return map;
086: }
087:
088: protected void doAddTrueSelector() {
089: m_backend.add(new ExposingSwitch.Entry(new TrueSelector(),
090: m_value3));
091: }
092:
093: protected void doAddNullValue() {
094: m_backend.add(new ExposingSwitch.Entry(new FalseSelector(),
095: null));
096: }
097:
098: public void testConstructor() {
099: m_switch = new SelectorSwitch();
100:
101: m_switch = new SelectorSwitch(new ArrayList());
102: m_switch = new SelectorSwitch(new LinkedList());
103:
104: m_switch = new SelectorSwitch(Arrays
105: .asList(new SelectorSwitch[0])); // hmmm...not good :D
106:
107: m_switch = new SelectorSwitch(new HashMap());
108: m_switch = new SelectorSwitch(getPopulationMap());
109:
110: try {
111: ArrayList list = new ArrayList();
112: list.add(null);
113: m_switch = new SelectorSwitch(list);
114: fail();
115: } catch (AssertionError ae) {
116: }
117: }
118:
119: public void testSize() {
120: assertEquals(m_backend.size(), m_switch.size());
121:
122: doPopulate();
123: assertEquals(m_backend.size(), m_switch.size());
124: }
125:
126: public void testClear() {
127: doPopulate();
128:
129: m_switch.clear();
130: assertEquals(0, m_switch.size());
131: assertEquals(0, m_backend.size());
132: }
133:
134: public void testIsEmpty() {
135: assertTrue(m_switch.isEmpty());
136: doPopulate();
137: assertFalse(m_switch.isEmpty());
138: }
139:
140: public void testContainsKey() {
141: assertFalse(m_switch.containsKey(this ));
142: assertFalse(m_switch.containsKey("blah"));
143: assertFalse(m_switch.containsKey(null));
144:
145: doPopulate();
146: assertTrue(m_switch.containsKey(this ));
147: assertFalse(m_switch.containsKey("blah"));
148: assertFalse(m_switch.containsKey(null));
149:
150: doAddTrueSelector();
151: assertTrue(m_switch.containsKey(this ));
152: assertTrue(m_switch.containsKey("blah"));
153: assertTrue(m_switch.containsKey(null));
154: }
155:
156: public void testContainsValue() {
157: assertFalse(m_switch.containsValue(m_value1));
158: assertFalse(m_switch.containsValue(m_value2));
159: assertFalse(m_switch.containsValue(this ));
160: assertFalse(m_switch.containsValue("blah"));
161:
162: doPopulate();
163: assertTrue(m_switch.containsValue(m_value1));
164: assertTrue(m_switch.containsValue(m_value2));
165: assertTrue(m_switch.containsValue(this ));
166: assertFalse(m_switch.containsValue("blah"));
167:
168: doAddTrueSelector();
169: assertTrue(m_switch.containsValue(m_value3));
170: assertFalse(m_switch.containsValue("blah"));
171:
172: doAddNullValue();
173: assertFalse(m_switch.containsValue("blah"));
174: assertTrue(m_switch.containsValue(null));
175: }
176:
177: public void testValues() {
178: assertFalse(m_switch.values().iterator().hasNext());
179: doPopulate();
180:
181: Iterator it = m_switch.values().iterator();
182: int i = 0;
183: while (it.hasNext()) {
184: Object o = it.next();
185: switch (i) {
186: case 0:
187: assertEquals(m_value1, o);
188: break;
189: case 1:
190: assertEquals(m_value2, o);
191: break;
192: case 2:
193: assertEquals(m_value1, o);
194: break;
195: case 3:
196: assertEquals(this , o);
197: continue;
198: }
199: i++;
200: }
201: assertEquals(i, 3);
202:
203: int oldSize = m_backend.size();
204: it.remove();
205: assertEquals(oldSize - 1, m_backend.size());
206:
207: try {
208: m_switch.values().add("stuff"); // unmodifiable
209: fail("Expected an exception!");
210: } catch (Throwable th) {
211: }
212: }
213:
214: public void testPutAll() {
215: Map map = getPopulationMap();
216: m_switch.putAll(map);
217:
218: assertTrue(m_switch.containsKey(this ));
219: assertFalse(m_switch.containsKey("blah"));
220: assertFalse(m_switch.containsKey(null));
221: assertTrue(m_switch.containsValue(m_value1));
222:
223: assertFalse(m_switch.containsValue(m_value2));/* was replaced!!! */
224: assertTrue(m_switch.containsValue(this ));
225: assertFalse(m_switch.containsValue("blah"));
226:
227: try {
228: map = new MapWithNull();
229: m_switch.putAll(map);
230: fail("Expected an exception!");
231: } catch (AssertionError ae) {
232: }
233:
234: map = new HashMap();
235: map.put("not a selector", this );
236: m_switch.putAll(map);
237: assertTrue(m_switch.containsKey("not a selector"));
238: }
239:
240: public void testEntrySet() {
241: Set entries = m_switch.entrySet();
242: assertEquals(0, entries.size());
243:
244: doPopulate();
245: entries = m_switch.entrySet();
246: assertEquals(3, entries.size());
247:
248: Iterator it = entries.iterator();
249: while (it.hasNext()) {
250: Object o = it.next();
251:
252: doTestEntry(o);
253: }
254:
255: int oldSize = m_backend.size();
256: it.remove();
257: assertEquals(oldSize - 1, m_backend.size());
258:
259: // test some very specific stuff in equals()
260: it = entries.iterator();
261: Switch.Entry entry = (Switch.Entry) it.next(); // first entry in doPopulate
262: assertFalse(entry.equals(new ExposingSwitch.Entry(
263: new FalseSelector(), null)));
264: assertFalse(new ExposingSwitch.Entry(new FalseSelector(), null)
265: .equals(entry));
266:
267: // test the setValue
268: entry.setValue(m_value2);
269: assertEquals(((Switch.Entry) m_backend.get(0)).getValue(),
270: m_value2);
271:
272: // remove more than one item (everything)
273: it = entries.iterator();
274: while (it.hasNext()) {
275: it.next();
276: it.remove();
277: }
278: // we had one duplicate, so it should be in the list
279: assertEquals(1, entries.size());
280: assertEquals(1, m_backend.size());
281: it = entries.iterator();
282: while (it.hasNext()) {
283: it.next();
284: it.remove();
285: }
286:
287: // but now that should be fixed :D
288: assertEquals(0, entries.size());
289: assertEquals(0, m_backend.size());
290: }
291:
292: public void doTestEntry(Object o) {
293: assertTrue(o instanceof Switch.Entry);
294:
295: Switch.Entry entry = (Switch.Entry) o;
296:
297: Selector selector = entry.getSelector();
298: assertNotNull(selector);
299: assertTrue(selector instanceof Selector);
300:
301: entry.getValue(); // could be anything
302:
303: Object key = entry.getKey();
304: assertNotNull(key);
305: assertTrue(key instanceof Selector);
306:
307: assertFalse(entry.equals(null));
308: assertTrue(entry.equals(entry));
309: assertFalse(entry.equals(this ));
310:
311: Map map = new HashMap();
312: map.put("key", "value");
313: Set mapSet = map.entrySet();
314: Iterator it = mapSet.iterator();
315: Map.Entry me = (Map.Entry) it.next();
316: assertFalse(entry.equals(me));
317:
318: assertFalse(entry.equals(new Switch.Entry() {
319: public Object getKey() {
320: return null;
321: }
322:
323: public Selector getSelector() {
324: return null;
325: }
326:
327: public Object getValue() {
328: return "unique";
329: }
330:
331: public Object setValue(Object value) {
332: return null;
333: }
334: }));
335: assertFalse(entry.equals(new Switch.Entry() {
336: public Object getKey() {
337: return null;
338: }
339:
340: public Selector getSelector() {
341: return new StringSelector("");
342: }
343:
344: public Object getValue() {
345: return null;
346: }
347:
348: public Object setValue(Object value) {
349: return null;
350: }
351: }));
352: }
353:
354: public void testKeySet() {
355: Set keys = m_switch.keySet();
356: assertEquals(0, keys.size());
357:
358: doPopulate();
359: keys = m_switch.keySet();
360: assertEquals(3, keys.size());
361:
362: Iterator it = keys.iterator();
363: while (it.hasNext()) {
364: Object o = it.next();
365: assertTrue(o instanceof Selector);
366: }
367:
368: int oldSize = m_backend.size();
369: it.remove();
370: assertEquals(oldSize - 1, m_backend.size());
371: }
372:
373: public void testGet() {
374: assertNull(m_switch.get(this ));
375: assertNull(m_switch.get(null));
376: assertNull(m_switch.get(""));
377:
378: doPopulate();
379: assertEquals(this , m_switch.get(this ));
380: assertNull(m_switch.get(null));
381: assertNull(m_switch.get(""));
382: assertNull(m_switch.get(m_value1));
383: assertNull(m_switch.get(m_value2));
384: }
385:
386: public void testRemove() {
387: assertNull(m_switch.remove(this ));
388: assertNull(m_switch.remove(null));
389: assertNull(m_switch.remove(""));
390:
391: doPopulate();
392: assertEquals(this , m_switch.remove(this ));
393: assertNull(m_switch.remove(this ));
394: assertNull(m_switch.remove(null));
395: assertNull(m_switch.remove(""));
396: assertNull(m_switch.remove(m_value1));
397: assertNull(m_switch.remove(m_value2));
398: }
399:
400: public void testPut() {
401: assertNull(m_switch.put(this , this ));
402: assertEquals(this , m_switch.remove(this ));
403:
404: assertNull(m_switch.put("blah", this ));
405: assertNull(m_switch.remove(this ));
406: assertEquals(this , m_switch.remove("blah"));
407:
408: m_switch.put("blah", this );
409: assertEquals(this , m_switch.put("blah", this ));
410:
411: m_switch.put(this , null);
412:
413: try {
414: m_switch.put(null, this );
415: fail("Expected an exception!");
416: } catch (AssertionError ae) {
417: }
418: try {
419: m_switch.put(null, null);
420: fail("Expected an exception!");
421: } catch (AssertionError ae) {
422: }
423: }
424:
425: public void testEquals() {
426: assertTrue(m_switch.equals(m_switch));
427: assertFalse(m_switch.equals(null));
428: assertFalse(m_switch.equals(this ));
429:
430: Switch otherSwitch = new SelectorSwitch();
431: assertTrue(m_switch.equals(otherSwitch));
432:
433: Map map = getPopulationMap();
434:
435: m_switch.putAll(map);
436: assertFalse(m_switch.equals(otherSwitch));
437:
438: otherSwitch.putAll(map);
439: assertTrue(m_switch.equals(otherSwitch));
440: }
441:
442: public void testHashCode() {
443: assertEquals(m_switch.hashCode(), m_switch.hashCode());
444: Switch otherSwitch = new SelectorSwitch();
445: assertEquals(m_switch.hashCode(), otherSwitch.hashCode());
446:
447: Map map = getPopulationMap();
448:
449: m_switch.putAll(map);
450: assertFalse(m_switch.hashCode() == otherSwitch.hashCode());
451:
452: otherSwitch.putAll(map);
453: assertEquals(m_switch.hashCode(), otherSwitch.hashCode());
454:
455: m_switch.put(this , null);
456: assertFalse(m_switch.hashCode() == otherSwitch.hashCode());
457: otherSwitch.put(this , null);
458: assertEquals(m_switch.hashCode(), otherSwitch.hashCode());
459: }
460:
461: public void testEntryList() {
462: assertEquals(m_backend, m_switch.entryList());
463: }
464:
465: public void testCriteriaSet() {
466: assertEquals(m_switch.criteriaSet(), m_switch.criteriaSet());
467: Switch otherSwitch = new SelectorSwitch();
468: assertEquals(m_switch.criteriaSet(), otherSwitch.criteriaSet());
469:
470: Map map = getPopulationMap();
471:
472: m_switch.putAll(map);
473: assertFalse(m_switch.criteriaSet().equals(
474: otherSwitch.criteriaSet()));
475:
476: otherSwitch.putAll(map);
477: assertEquals(m_switch.criteriaSet(), otherSwitch.criteriaSet());
478: }
479:
480: public static class MapWithNull implements Map {
481: Set m_entrySet = new HashSet();
482:
483: public MapWithNull() {
484: m_entrySet.add(null);
485: }
486:
487: public int hashCode() {
488: return 0;
489: }
490:
491: public int size() {
492: return 0;
493: }
494:
495: public void clear() {
496: }
497:
498: public boolean isEmpty() {
499: return false;
500: }
501:
502: public boolean containsKey(Object key) {
503: return false;
504: }
505:
506: public boolean containsValue(Object value) {
507: return false;
508: }
509:
510: public boolean equals(Object o) {
511: return false;
512: }
513:
514: public Collection values() {
515: return null;
516: }
517:
518: public void putAll(Map t) {
519: }
520:
521: public Set entrySet() {
522: return m_entrySet;
523: }
524:
525: public Set keySet() {
526: return null;
527: }
528:
529: public Object get(Object key) {
530: return null;
531: }
532:
533: public Object remove(Object key) {
534: return null;
535: }
536:
537: public Object put(Object key, Object value) {
538: return null;
539: }
540: }
541:
542: public static class ExposingSwitch extends SelectorSwitch {
543: public static class Entry extends SelectorSwitch.Entry {
544: public Entry(Selector selector, Object value) {
545: super(selector, value);
546: }
547: }
548: }
549:
550: }
|