Author: niallp
Date: Thu May 24 13:45:24 2007
New Revision: 541424

URL: http://svn.apache.org/viewvc?view=rev&rev=541424
Log:
BEANUTILS-233 Implement equals() and hashCode() methods for DynaProperty - 
thanks to Russell for the patch and test case!

Added:
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaPropertyTestCase.java
   (with props)
Modified:
    jakarta/commons/proper/beanutils/trunk/build.xml
    
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/DynaProperty.java

Modified: jakarta/commons/proper/beanutils/trunk/build.xml
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/build.xml?view=diff&rev=541424&r1=541423&r2=541424
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/build.xml (original)
+++ jakarta/commons/proper/beanutils/trunk/build.xml Thu May 24 13:45:24 2007
@@ -272,6 +272,7 @@
                                 test.lazy.dynamap,
                                 test.lazy.dynalist,
                                 test.dynabean.mapdecorator,
+                                test.dynaproperty,
                                 test.indexed.properties,
                                 test.mapped.properties
                                "
@@ -540,6 +541,21 @@
       <sysproperty key="org.apache.commons.logging.simplelog.defaultlog"
                  value="${test.level}"/>
       <arg value="org.apache.commons.beanutils.LazyDynaBeanTestCase"/>
+      <classpath refid="test.classpath"/>
+    </java>
+  </target>
+
+  <target name="test.dynaproperty" depends="compile.tests">
+    <echo message="Running DynaProperty tests ..."/>
+    <java classname="${test.runner}" fork="yes"
+        failonerror="${test.failonerror}">
+      <sysproperty key="org.apache.commons.logging.LogFactory"
+                 value="${test.factory}"/>
+      <sysproperty key="org.apache.commons.logging.Log"
+                 value="${test.log}"/>
+      <sysproperty key="org.apache.commons.logging.simplelog.defaultlog"
+                 value="${test.level}"/>
+      <arg value="org.apache.commons.beanutils.DynaPropertyTestCase"/>
       <classpath refid="test.classpath"/>
     </java>
   </target>

Modified: 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/DynaProperty.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/DynaProperty.java?view=diff&rev=541424&r1=541423&r2=541424
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/DynaProperty.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/DynaProperty.java
 Thu May 24 13:45:24 2007
@@ -201,6 +201,40 @@
 
     }
 
+    /**
+     * Checks this instance against the specified Object for equality. 
Overrides the
+     * default refererence test for equality provided by [EMAIL PROTECTED] 
java.lang.Object#equals(Object)}  
+     */
+    public boolean equals(final Object obj) {
+
+        boolean result = false;
+
+        result = (obj == this);
+
+        if ((!result) && obj instanceof DynaProperty) {
+            final DynaProperty that = (DynaProperty) obj;
+            result = 
+               ((this.name == null) ? (that.name == null) : 
(this.name.equals(that.name))) &&
+               ((this.type == null) ? (that.type == null) : 
(this.type.equals(that.type))) &&
+               ((this.contentType == null) ? (that.contentType == null) : 
(this.contentType.equals(that.contentType)));
+        }
+
+        return result;
+    }
+
+    /**
+     * @see java.lang.Object#hashCode
+     */
+    public int hashCode() {
+
+       int result = 1;
+       
+       result = result * 31 + ((name == null) ? 0 : name.hashCode());
+       result = result * 31 + ((type == null) ? 0 : type.hashCode());
+       result = result * 31 + ((contentType == null) ? 0 : 
contentType.hashCode());
+
+       return result;
+    }
 
     /**
      * Return a String representation of this Object.

Added: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaPropertyTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaPropertyTestCase.java?view=auto&rev=541424
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaPropertyTestCase.java
 (added)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaPropertyTestCase.java
 Thu May 24 13:45:24 2007
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.commons.beanutils;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import java.util.Collection;
+
+/**
+ * Test case for [EMAIL PROTECTED] DynaProperty}.
+ *
+ * @version $Revision$ $Date$
+ */
+public class DynaPropertyTestCase extends TestCase {
+
+       private DynaProperty testPropertyWithName;
+       private DynaProperty testProperty1Duplicate;
+       private DynaProperty testPropertyWithNameAndType;
+       private DynaProperty testProperty2Duplicate;
+       private DynaProperty testPropertyWithNameAndTypeAndContentType;
+       private DynaProperty testProperty3Duplicate;
+       
+    /**
+     * Construct a new instance of this test case.
+     *
+     * @param name Name of the test case
+     */
+    public DynaPropertyTestCase(String name) {
+        super(name);
+    }
+
+    /**
+     * Return the tests included in this test suite.
+     * @return a test suite
+     */
+    public static Test suite() {
+
+        return (new TestSuite(DynaPropertyTestCase.class));
+
+    }
+
+    /**
+     * Set up instance variables required by this test case.
+     */
+    protected void setUp() throws Exception {
+
+               super.setUp();
+               
+               testPropertyWithName = new DynaProperty("test1");
+               testProperty1Duplicate = new DynaProperty("test1");
+
+               testPropertyWithNameAndType = new DynaProperty("test2", 
Integer.class);
+               testProperty2Duplicate = new DynaProperty("test2", 
Integer.class);
+
+               testPropertyWithNameAndTypeAndContentType = new 
DynaProperty("test3", Collection.class, Short.class);
+               testProperty3Duplicate = new DynaProperty("test3", 
Collection.class, Short.class);
+       }
+
+    /**
+     * Tear down instance variables required by this test case.
+     */
+       protected void tearDown() throws Exception {
+
+               testPropertyWithName = testProperty1Duplicate = null;
+               testPropertyWithNameAndType = testProperty2Duplicate = null;
+               testPropertyWithNameAndTypeAndContentType = 
testProperty3Duplicate = null;
+               super.tearDown();
+       }
+
+    /**
+        * Class under test for int hashCode(Object)
+        */
+       public void testHashCode() {
+
+               final int initialHashCode = 
testPropertyWithNameAndTypeAndContentType.hashCode();
+               assertEquals(testPropertyWithName.hashCode(), 
testProperty1Duplicate.hashCode());
+               assertEquals(testPropertyWithNameAndType.hashCode(), 
testProperty2Duplicate.hashCode());
+               
assertEquals(testPropertyWithNameAndTypeAndContentType.hashCode(), 
testProperty3Duplicate.hashCode());
+               assertEquals(initialHashCode, 
testPropertyWithNameAndTypeAndContentType.hashCode());
+       }
+
+       /**
+        * Class under test for boolean equals(Object)
+        */
+       public void testEqualsObject() {
+
+               assertEquals(testPropertyWithName, testProperty1Duplicate);
+               assertEquals(testPropertyWithNameAndType, 
testProperty2Duplicate);
+               assertEquals(testPropertyWithNameAndTypeAndContentType, 
testProperty3Duplicate);
+               
assertFalse(testPropertyWithName.equals(testPropertyWithNameAndType));
+               
assertFalse(testPropertyWithNameAndType.equals(testPropertyWithNameAndTypeAndContentType));
+        assertFalse(testPropertyWithName.equals(null));
+       }
+
+}

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaPropertyTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/DynaPropertyTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to