View Javadoc

1   /*
2    * $Id: ContainUtilTest.java 478625 2006-11-23 17:31:52Z wsmoak $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.util;
22  
23  import java.util.ArrayList;
24  import java.util.LinkedHashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import junit.framework.TestCase;
29  
30  /***
31   *
32   * @version $Date: 2006-11-23 18:31:52 +0100 (Do, 23 Nov 2006) $ $Id: ContainUtilTest.java 478625 2006-11-23 17:31:52Z wsmoak $
33   */
34  public class ContainUtilTest extends TestCase {
35  
36      public void testNull() throws Exception {
37          assertFalse(ContainUtil.contains(null, null));
38          assertFalse(ContainUtil.contains(new Object(), null));
39          assertFalse(ContainUtil.contains(null, new Object()));
40      }
41  
42      public void testSimpleList() throws Exception {
43          List<String> l = new ArrayList<String>();
44          l.add("one");
45          l.add("two");
46  
47          assertFalse(ContainUtil.contains(l, "three"));
48          assertTrue(ContainUtil.contains(l, "one"));
49          assertTrue(ContainUtil.contains(l, "two"));
50      }
51  
52      public void testSimpleSet() throws Exception {
53          Set<String> s = new LinkedHashSet<String>();
54          s.add("one");
55          s.add("two");
56  
57          assertFalse(ContainUtil.contains(s, "thre"));
58          assertTrue(ContainUtil.contains(s, "one"));
59          assertTrue(ContainUtil.contains(s, "two"));
60      }
61  
62      public void testComplexList() throws Exception {
63          List<MyObject> l = new ArrayList<MyObject>();
64          l.add(new MyObject("tm_jee", 20));
65          l.add(new MyObject("jenny", 22));
66  
67          assertFalse(ContainUtil.contains(l, new MyObject("paul", 50)));
68          assertFalse(ContainUtil.contains(l, new MyObject("tm_jee", 44)));
69          assertTrue(ContainUtil.contains(l, new MyObject("tm_jee", 20)));
70          assertTrue(ContainUtil.contains(l, new MyObject("jenny", 22)));
71      }
72  
73      public void testComplexMap() throws Exception {
74          Set<MyObject> s = new LinkedHashSet<MyObject>();
75          s.add(new MyObject("tm_jee", 20));
76          s.add(new MyObject("jenny", 22));
77  
78          assertFalse(ContainUtil.contains(s, new MyObject("paul", 50)));
79          assertFalse(ContainUtil.contains(s, new MyObject("tm_jee", 44)));
80          assertTrue(ContainUtil.contains(s, new MyObject("tm_jee", 20)));
81          assertTrue(ContainUtil.contains(s, new MyObject("jenny", 22)));
82      }
83  
84      public void testObject() throws Exception {
85          assertFalse(ContainUtil.contains("aaa", "bbb"));
86          assertFalse(ContainUtil.contains(new MyObject("tm_jee", 22), new MyObject("tmjee", 22)));
87          assertTrue(ContainUtil.contains("apple", "apple"));
88          assertTrue(ContainUtil.contains(new MyObject("tm_jee", 22), new MyObject("tm_jee", 22)));
89      }
90  
91  
92      public static class MyObject {
93          private String name;
94          private Integer age;
95  
96          public MyObject(String name, Integer age) {
97              this.name = name;
98              this.age = age;
99          }
100 
101         @Override
102         public int hashCode() {
103             return this.name.hashCode();
104         }
105 
106         @Override
107         public boolean equals(Object obj) {
108             if (obj == null) { return false; }
109             if (! (obj instanceof MyObject)) { return false; }
110             MyObject tmp = (MyObject) obj;
111             if (
112                     tmp.name.equals(this.name) &&
113                     tmp.age.equals(this.age)
114                 ) {
115                 return true;
116             }
117             return false;
118 
119         }
120     }
121 }