001: /*
002: * $Id: ContainUtilTest.java 478625 2006-11-23 17:31:52Z wsmoak $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.util;
022:
023: import java.util.ArrayList;
024: import java.util.LinkedHashSet;
025: import java.util.List;
026: import java.util.Set;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: *
032: * @version $Date: 2006-11-23 12:31:52 -0500 (Thu, 23 Nov 2006) $ $Id: ContainUtilTest.java 478625 2006-11-23 17:31:52Z wsmoak $
033: */
034: public class ContainUtilTest extends TestCase {
035:
036: public void testNull() throws Exception {
037: assertFalse(ContainUtil.contains(null, null));
038: assertFalse(ContainUtil.contains(new Object(), null));
039: assertFalse(ContainUtil.contains(null, new Object()));
040: }
041:
042: public void testSimpleList() throws Exception {
043: List<String> l = new ArrayList<String>();
044: l.add("one");
045: l.add("two");
046:
047: assertFalse(ContainUtil.contains(l, "three"));
048: assertTrue(ContainUtil.contains(l, "one"));
049: assertTrue(ContainUtil.contains(l, "two"));
050: }
051:
052: public void testSimpleSet() throws Exception {
053: Set<String> s = new LinkedHashSet<String>();
054: s.add("one");
055: s.add("two");
056:
057: assertFalse(ContainUtil.contains(s, "thre"));
058: assertTrue(ContainUtil.contains(s, "one"));
059: assertTrue(ContainUtil.contains(s, "two"));
060: }
061:
062: public void testComplexList() throws Exception {
063: List<MyObject> l = new ArrayList<MyObject>();
064: l.add(new MyObject("tm_jee", 20));
065: l.add(new MyObject("jenny", 22));
066:
067: assertFalse(ContainUtil.contains(l, new MyObject("paul", 50)));
068: assertFalse(ContainUtil.contains(l, new MyObject("tm_jee", 44)));
069: assertTrue(ContainUtil.contains(l, new MyObject("tm_jee", 20)));
070: assertTrue(ContainUtil.contains(l, new MyObject("jenny", 22)));
071: }
072:
073: public void testComplexMap() throws Exception {
074: Set<MyObject> s = new LinkedHashSet<MyObject>();
075: s.add(new MyObject("tm_jee", 20));
076: s.add(new MyObject("jenny", 22));
077:
078: assertFalse(ContainUtil.contains(s, new MyObject("paul", 50)));
079: assertFalse(ContainUtil.contains(s, new MyObject("tm_jee", 44)));
080: assertTrue(ContainUtil.contains(s, new MyObject("tm_jee", 20)));
081: assertTrue(ContainUtil.contains(s, new MyObject("jenny", 22)));
082: }
083:
084: public void testObject() throws Exception {
085: assertFalse(ContainUtil.contains("aaa", "bbb"));
086: assertFalse(ContainUtil.contains(new MyObject("tm_jee", 22),
087: new MyObject("tmjee", 22)));
088: assertTrue(ContainUtil.contains("apple", "apple"));
089: assertTrue(ContainUtil.contains(new MyObject("tm_jee", 22),
090: new MyObject("tm_jee", 22)));
091: }
092:
093: public static class MyObject {
094: private String name;
095: private Integer age;
096:
097: public MyObject(String name, Integer age) {
098: this .name = name;
099: this .age = age;
100: }
101:
102: @Override
103: public int hashCode() {
104: return this .name.hashCode();
105: }
106:
107: @Override
108: public boolean equals(Object obj) {
109: if (obj == null) {
110: return false;
111: }
112: if (!(obj instanceof MyObject)) {
113: return false;
114: }
115: MyObject tmp = (MyObject) obj;
116: if (tmp.name.equals(this .name) && tmp.age.equals(this .age)) {
117: return true;
118: }
119: return false;
120:
121: }
122: }
123: }
|