Author: tmjee Date: Sat Oct 14 04:56:33 2006 New Revision: 463918 URL: http://svn.apache.org/viewvc?view=rev&rev=463918 Log: WW-1471 CheckboxList with value as Map should have the map keys compared instead of its value.
Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ContainUtilTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ContainUtil.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ContainUtil.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ContainUtil.java?view=diff&rev=463918&r1=463917&r2=463918 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ContainUtil.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/ContainUtil.java Sat Oct 14 04:56:33 2006 @@ -26,9 +26,45 @@ * <code>ContainUtil</code> will check if object 1 contains object 2. * Object 1 may be an Object, array, Collection, or a Map * + * @version $Date$ $Id$ */ public class ContainUtil { + /** + * Determine if <code>obj2</code> exists in <code>obj1</code>. + * + * <table borer="1"> + * <tr> + * <td>Type Of obj1</td> + * <td>Comparison type</td> + * </tr> + * <tr> + * <td>null<td> + * <td>always return false</td> + * </tr> + * <tr> + * <td>Map</td> + * <td>Map containsKey(obj2)</td> + * </tr> + * <tr> + * <td>Collection</td> + * <td>Collection contains(obj2)</td> + * </tr> + * <tr> + * <td>Array</td> + * <td>there's an array element (e) where e.equals(obj2)</td> + * </tr> + * <tr> + * <td>Object</td> + * <td>obj1.equals(obj2)</td> + * </tr> + * </table> + * + * + * @param obj1 + * @param obj2 + * @return + */ public static boolean contains(Object obj1, Object obj2) { if ((obj1 == null) || (obj2 == null)) { //log.debug("obj1 or obj2 are null."); @@ -36,7 +72,7 @@ } if (obj1 instanceof Map) { - if (((Map) obj1).containsValue(obj2)) { + if (((Map) obj1).containsKey(obj2)) { //log.debug("obj1 is a map and contains obj2"); return true; } Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ContainUtilTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ContainUtilTest.java?view=auto&rev=463918 ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ContainUtilTest.java (added) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/util/ContainUtilTest.java Sat Oct 14 04:56:33 2006 @@ -0,0 +1,118 @@ +/* + * $Id: ContainUtil.java 418521 2006-07-01 23:36:50Z mrdon $ + * + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.struts2.util; + +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +import junit.framework.TestCase; + +/** + * + * @version $Date$ $Id$ + */ +public class ContainUtilTest extends TestCase { + + public void testNull() throws Exception { + assertFalse(ContainUtil.contains(null, null)); + assertFalse(ContainUtil.contains(new Object(), null)); + assertFalse(ContainUtil.contains(null, new Object())); + } + + public void testSimpleList() throws Exception { + List<String> l = new ArrayList<String>(); + l.add("one"); + l.add("two"); + + assertFalse(ContainUtil.contains(l, "three")); + assertTrue(ContainUtil.contains(l, "one")); + assertTrue(ContainUtil.contains(l, "two")); + } + + public void testSimpleSet() throws Exception { + Set<String> s = new LinkedHashSet<String>(); + s.add("one"); + s.add("two"); + + assertFalse(ContainUtil.contains(s, "thre")); + assertTrue(ContainUtil.contains(s, "one")); + assertTrue(ContainUtil.contains(s, "two")); + } + + public void testComplexList() throws Exception { + List<MyObject> l = new ArrayList<MyObject>(); + l.add(new MyObject("tm_jee", 20)); + l.add(new MyObject("jenny", 22)); + + assertFalse(ContainUtil.contains(l, new MyObject("paul", 50))); + assertFalse(ContainUtil.contains(l, new MyObject("tm_jee", 44))); + assertTrue(ContainUtil.contains(l, new MyObject("tm_jee", 20))); + assertTrue(ContainUtil.contains(l, new MyObject("jenny", 22))); + } + + public void testComplexMap() throws Exception { + Set<MyObject> s = new LinkedHashSet<MyObject>(); + s.add(new MyObject("tm_jee", 20)); + s.add(new MyObject("jenny", 22)); + + assertFalse(ContainUtil.contains(s, new MyObject("paul", 50))); + assertFalse(ContainUtil.contains(s, new MyObject("tm_jee", 44))); + assertTrue(ContainUtil.contains(s, new MyObject("tm_jee", 20))); + assertTrue(ContainUtil.contains(s, new MyObject("jenny", 22))); + } + + public void testObject() throws Exception { + assertFalse(ContainUtil.contains("aaa", "bbb")); + assertFalse(ContainUtil.contains(new MyObject("tm_jee", 22), new MyObject("tmjee", 22))); + assertTrue(ContainUtil.contains("apple", "apple")); + assertTrue(ContainUtil.contains(new MyObject("tm_jee", 22), new MyObject("tm_jee", 22))); + } + + + public static class MyObject { + private String name; + private Integer age; + + public MyObject(String name, Integer age) { + this.name = name; + this.age = age; + } + + @Override + public int hashCode() { + return this.name.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { return false; } + if (! (obj instanceof MyObject)) { return false; } + MyObject tmp = (MyObject) obj; + if ( + tmp.name.equals(this.name) && + tmp.age.equals(this.age) + ) { + return true; + } + return false; + + } + } +}