View Javadoc

1   /*
2    * $Id: ContainUtilTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.util;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Iterator;
27  import java.util.LinkedHashSet;
28  import java.util.List;
29  import java.util.Set;
30  
31  import junit.framework.TestCase;
32  
33  /***
34   *
35   * @version $Date: 2008-04-27 08:41:38 -0500 (Sun, 27 Apr 2008) $ $Id: ContainUtilTest.java 651946 2008-04-27 13:41:38Z apetrelli $
36   */
37  public class ContainUtilTest extends TestCase {
38  
39      public void testNull() throws Exception {
40          assertFalse(ContainUtil.contains(null, null));
41          assertFalse(ContainUtil.contains(new Object(), null));
42          assertFalse(ContainUtil.contains(null, new Object()));
43      }
44  
45      public void testSimpleList() throws Exception {
46          List<String> l = new ArrayList<String>();
47          l.add("one");
48          l.add("two");
49  
50          assertFalse(ContainUtil.contains(l, "three"));
51          assertTrue(ContainUtil.contains(l, "one"));
52          assertTrue(ContainUtil.contains(l, "two"));
53      }
54  
55      public void testSimpleSet() throws Exception {
56          Set<String> s = new LinkedHashSet<String>();
57          s.add("one");
58          s.add("two");
59  
60          assertFalse(ContainUtil.contains(s, "thre"));
61          assertTrue(ContainUtil.contains(s, "one"));
62          assertTrue(ContainUtil.contains(s, "two"));
63      }
64  
65      public void testComplexList() throws Exception {
66          List<MyObject> l = new ArrayList<MyObject>();
67          l.add(new MyObject("tm_jee", 20));
68          l.add(new MyObject("jenny", 22));
69  
70          assertFalse(ContainUtil.contains(l, new MyObject("paul", 50)));
71          assertFalse(ContainUtil.contains(l, new MyObject("tm_jee", 44)));
72          assertTrue(ContainUtil.contains(l, new MyObject("tm_jee", 20)));
73          assertTrue(ContainUtil.contains(l, new MyObject("jenny", 22)));
74      }
75  
76      public void testComplexMap() throws Exception {
77          Set<MyObject> s = new LinkedHashSet<MyObject>();
78          s.add(new MyObject("tm_jee", 20));
79          s.add(new MyObject("jenny", 22));
80  
81          assertFalse(ContainUtil.contains(s, new MyObject("paul", 50)));
82          assertFalse(ContainUtil.contains(s, new MyObject("tm_jee", 44)));
83          assertTrue(ContainUtil.contains(s, new MyObject("tm_jee", 20)));
84          assertTrue(ContainUtil.contains(s, new MyObject("jenny", 22)));
85      }
86  
87      public void testObject() throws Exception {
88          assertFalse(ContainUtil.contains("aaa", "bbb"));
89          assertFalse(ContainUtil.contains(new MyObject("tm_jee", 22), new MyObject("tmjee", 22)));
90          assertTrue(ContainUtil.contains("apple", "apple"));
91          assertTrue(ContainUtil.contains(new MyObject("tm_jee", 22), new MyObject("tm_jee", 22)));
92      }
93  
94      public void testIterableObject() throws Exception {
95      	MyIterableObject i = new MyIterableObject("one", "two");
96  
97          assertFalse(ContainUtil.contains(i, "thre"));
98          assertTrue(ContainUtil.contains(i, "one"));
99          assertTrue(ContainUtil.contains(i, "two"));
100     }
101     
102     public static class MyIterableObject implements Iterable<String> {
103     	private List<String> values;
104     	
105     	public MyIterableObject(String... strings) {
106     		values = Arrays.asList(strings);
107     	}
108     	
109 		public Iterator<String> iterator() {
110 			return values.iterator();
111 		}
112     }
113 
114     public static class MyObject {
115         private String name;
116         private Integer age;
117 
118         public MyObject(String name, Integer age) {
119             this.name = name;
120             this.age = age;
121         }
122 
123         @Override
124         public int hashCode() {
125             return this.name.hashCode();
126         }
127 
128         @Override
129         public boolean equals(Object obj) {
130             if (obj == null) { return false; }
131             if (! (obj instanceof MyObject)) { return false; }
132             MyObject tmp = (MyObject) obj;
133             if (
134                     tmp.name.equals(this.name) &&
135                     tmp.age.equals(this.age)
136                 ) {
137                 return true;
138             }
139             return false;
140 
141         }
142     }
143 }