001: package com.opensymphony.webwork.util;
002:
003: import java.util.ArrayList;
004: import java.util.LinkedHashSet;
005: import java.util.List;
006: import java.util.Set;
007:
008: import junit.framework.TestCase;
009:
010: import com.opensymphony.webwork.util.ContainUtil;
011:
012: public class ContainUtilTest extends TestCase {
013:
014: public void testNull() throws Exception {
015: assertFalse(ContainUtil.contains(null, null));
016: assertFalse(ContainUtil.contains(new Object(), null));
017: assertFalse(ContainUtil.contains(null, new Object()));
018: }
019:
020: public void testSimpleList() throws Exception {
021: List l = new ArrayList();
022: l.add("one");
023: l.add("two");
024:
025: assertFalse(ContainUtil.contains(l, "three"));
026: assertTrue(ContainUtil.contains(l, "one"));
027: assertTrue(ContainUtil.contains(l, "two"));
028: }
029:
030: public void testSimpleSet() throws Exception {
031: Set s = new LinkedHashSet();
032: s.add("one");
033: s.add("two");
034:
035: assertFalse(ContainUtil.contains(s, "thre"));
036: assertTrue(ContainUtil.contains(s, "one"));
037: assertTrue(ContainUtil.contains(s, "two"));
038: }
039:
040: public void testComplexList() throws Exception {
041: List l = new ArrayList();
042: l.add(new MyObject("tm_jee", Integer.valueOf("20")));
043: l.add(new MyObject("jenny", Integer.valueOf("22")));
044:
045: assertFalse(ContainUtil.contains(l, new MyObject("paul",
046: Integer.valueOf("50"))));
047: assertFalse(ContainUtil.contains(l, new MyObject("tm_jee",
048: Integer.valueOf("44"))));
049: assertTrue(ContainUtil.contains(l, new MyObject("tm_jee",
050: Integer.valueOf("20"))));
051: assertTrue(ContainUtil.contains(l, new MyObject("jenny",
052: Integer.valueOf("22"))));
053: }
054:
055: public void testComplexMap() throws Exception {
056: Set s = new LinkedHashSet();
057: s.add(new MyObject("tm_jee", Integer.valueOf("20")));
058: s.add(new MyObject("jenny", Integer.valueOf("22")));
059:
060: assertFalse(ContainUtil.contains(s, new MyObject("paul",
061: Integer.valueOf("50"))));
062: assertFalse(ContainUtil.contains(s, new MyObject("tm_jee",
063: Integer.valueOf("44"))));
064: assertTrue(ContainUtil.contains(s, new MyObject("tm_jee",
065: Integer.valueOf("20"))));
066: assertTrue(ContainUtil.contains(s, new MyObject("jenny",
067: Integer.valueOf("22"))));
068: }
069:
070: public void testObject() throws Exception {
071: assertFalse(ContainUtil.contains("aaa", "bbb"));
072: assertFalse(ContainUtil.contains(new MyObject("tm_jee", Integer
073: .valueOf("22")), new MyObject("tmjee", Integer
074: .valueOf("22"))));
075: assertTrue(ContainUtil.contains("apple", "apple"));
076: assertTrue(ContainUtil.contains(new MyObject("tm_jee", Integer
077: .valueOf("22")), new MyObject("tm_jee", Integer
078: .valueOf("22"))));
079: }
080:
081: public static class MyObject {
082: private String name;
083: private Integer age;
084:
085: public MyObject(String name, Integer age) {
086: this .name = name;
087: this .age = age;
088: }
089:
090: public int hashCode() {
091: return this .name.hashCode();
092: }
093:
094: public boolean equals(Object obj) {
095: if (obj == null) {
096: return false;
097: }
098: if (!(obj instanceof MyObject)) {
099: return false;
100: }
101: MyObject tmp = (MyObject) obj;
102: if (tmp.name.equals(this .name) && tmp.age.equals(this .age)) {
103: return true;
104: }
105: return false;
106:
107: }
108: }
109: }
|