View Javadoc

1   /*
2    * $Id: ContainUtilTest.java 802937 2009-08-10 21:57:37Z musachy $
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: 2009-08-10 17:57:37 -0400 (Mon, 10 Aug 2009) $ $Id: ContainUtilTest.java 802937 2009-08-10 21:57:37Z musachy $
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 testNullInAray()throws Exception {
46          assertTrue(ContainUtil.contains(new String[] {"a", null, "b"}, "b"));
47      }
48  
49      public void testSimpleList() throws Exception {
50          List<String> l = new ArrayList<String>();
51          l.add("one");
52          l.add("two");
53  
54          assertFalse(ContainUtil.contains(l, "three"));
55          assertTrue(ContainUtil.contains(l, "one"));
56          assertTrue(ContainUtil.contains(l, "two"));
57      }
58  
59      public void testSimpleSet() throws Exception {
60          Set<String> s = new LinkedHashSet<String>();
61          s.add("one");
62          s.add("two");
63  
64          assertFalse(ContainUtil.contains(s, "thre"));
65          assertTrue(ContainUtil.contains(s, "one"));
66          assertTrue(ContainUtil.contains(s, "two"));
67      }
68  
69      public void testComplexList() throws Exception {
70          List<MyObject> l = new ArrayList<MyObject>();
71          l.add(new MyObject("tm_jee", 20));
72          l.add(new MyObject("jenny", 22));
73  
74          assertFalse(ContainUtil.contains(l, new MyObject("paul", 50)));
75          assertFalse(ContainUtil.contains(l, new MyObject("tm_jee", 44)));
76          assertTrue(ContainUtil.contains(l, new MyObject("tm_jee", 20)));
77          assertTrue(ContainUtil.contains(l, new MyObject("jenny", 22)));
78      }
79  
80      public void testComplexMap() throws Exception {
81          Set<MyObject> s = new LinkedHashSet<MyObject>();
82          s.add(new MyObject("tm_jee", 20));
83          s.add(new MyObject("jenny", 22));
84  
85          assertFalse(ContainUtil.contains(s, new MyObject("paul", 50)));
86          assertFalse(ContainUtil.contains(s, new MyObject("tm_jee", 44)));
87          assertTrue(ContainUtil.contains(s, new MyObject("tm_jee", 20)));
88          assertTrue(ContainUtil.contains(s, new MyObject("jenny", 22)));
89      }
90  
91      public void testObject() throws Exception {
92          assertFalse(ContainUtil.contains("aaa", "bbb"));
93          assertFalse(ContainUtil.contains(new MyObject("tm_jee", 22), new MyObject("tmjee", 22)));
94          assertTrue(ContainUtil.contains("apple", "apple"));
95          assertTrue(ContainUtil.contains(new MyObject("tm_jee", 22), new MyObject("tm_jee", 22)));
96      }
97  
98      public void testIterableObject() throws Exception {
99      	MyIterableObject i = new MyIterableObject("one", "two");
100 
101         assertFalse(ContainUtil.contains(i, "thre"));
102         assertTrue(ContainUtil.contains(i, "one"));
103         assertTrue(ContainUtil.contains(i, "two"));
104     }
105     
106     public static class MyIterableObject implements Iterable<String> {
107     	private List<String> values;
108     	
109     	public MyIterableObject(String... strings) {
110     		values = Arrays.asList(strings);
111     	}
112     	
113 		public Iterator<String> iterator() {
114 			return values.iterator();
115 		}
116     }
117 
118     public static class MyObject {
119         private String name;
120         private Integer age;
121 
122         public MyObject(String name, Integer age) {
123             this.name = name;
124             this.age = age;
125         }
126 
127         @Override
128         public int hashCode() {
129             return this.name.hashCode();
130         }
131 
132         @Override
133         public boolean equals(Object obj) {
134             if (obj == null) { return false; }
135             if (! (obj instanceof MyObject)) { return false; }
136             MyObject tmp = (MyObject) obj;
137             if (
138                     tmp.name.equals(this.name) &&
139                     tmp.age.equals(this.age)
140                 ) {
141                 return true;
142             }
143             return false;
144 
145         }
146     }
147 }