summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakozak2011-11-23 03:05:28 (EST)
committer Winston Prakash2011-12-01 20:47:06 (EST)
commit56c5fc9054155b45f1e96333d178a32d818be428 (patch)
treee13ecb09f42e26bd7e22941e455ae822852bcba7
parent61f1551de1777d00e5d4b8782e9a1aee63669284 (diff)
downloadorg.eclipse.hudson.core-56c5fc9054155b45f1e96333d178a32d818be428.zip
org.eclipse.hudson.core-56c5fc9054155b45f1e96333d178a32d818be428.tar.gz
org.eclipse.hudson.core-56c5fc9054155b45f1e96333d178a32d818be428.tar.bz2
Implement equals/hashCode for PersistedList. Add Unit test for new methods
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
-rw-r--r--hudson-core/src/main/java/hudson/util/PersistedList.java31
-rw-r--r--hudson-core/src/test/java/hudson/util/PersistedListEqualsHashCodeTest.java87
2 files changed, 116 insertions, 2 deletions
diff --git a/hudson-core/src/main/java/hudson/util/PersistedList.java b/hudson-core/src/main/java/hudson/util/PersistedList.java
index ececc30..aef7002 100644
--- a/hudson-core/src/main/java/hudson/util/PersistedList.java
+++ b/hudson-core/src/main/java/hudson/util/PersistedList.java
@@ -1,6 +1,6 @@
/*******************************************************************************
*
- * Copyright (c) 2004-2009, Oracle Corporation
+ * Copyright (c) 2004-2011, Oracle Corporation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -9,7 +9,7 @@
*
* Contributors:
*
- *
+ * Kohsuke Kawaguchi, Nikita Levyankov
*
*
*******************************************************************************/
@@ -31,6 +31,8 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* Collection whose change is notified to the parent object for persistence.
@@ -208,5 +210,30 @@ public class PersistedList<T> implements Iterable<T> {
}
}
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ PersistedList that = (PersistedList) o;
+ return new EqualsBuilder()
+ .append(data, that.data)
+ .append(owner, that.owner)
+ .isEquals();
+ }
+
+ @Override
+ public int hashCode() {
+ return new HashCodeBuilder()
+ .append(data)
+ .append(owner)
+ .toHashCode();
+ }
}
+
diff --git a/hudson-core/src/test/java/hudson/util/PersistedListEqualsHashCodeTest.java b/hudson-core/src/test/java/hudson/util/PersistedListEqualsHashCodeTest.java
new file mode 100644
index 0000000..64b93fa
--- /dev/null
+++ b/hudson-core/src/test/java/hudson/util/PersistedListEqualsHashCodeTest.java
@@ -0,0 +1,87 @@
+/*******************************************************************************
+ *
+ * Copyright (c) 2011 Oracle Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *
+ * Nikita Levyankov
+ *
+ *
+ *******************************************************************************/
+
+package hudson.util;
+
+import hudson.model.FreeStyleProjectMock;
+import hudson.model.Saveable;
+import hudson.tasks.Shell;
+import java.io.IOException;
+import java.util.Arrays;
+import org.junit.Before;
+import org.junit.Test;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
+
+/**
+ * Verify equals and hashCode methods for PersistedList object.
+ * <p/>
+ * Date: 10/5/11
+ *
+ * @author Nikita Levyankov
+ */
+public class PersistedListEqualsHashCodeTest {
+
+ private Saveable owner1 = new FreeStyleProjectMock("test1");
+ private Saveable owner2 = new FreeStyleProjectMock("test1");
+
+ private PersistedList persistedList1;
+ private PersistedList persistedList2;
+ private PersistedList persistedList3;
+
+ @Before
+ public void setUp() {
+ persistedList1 = new PersistedList(owner1);
+ persistedList2 = new PersistedList(owner2);
+ persistedList3 = new PersistedList(owner1);
+ }
+
+ @Test
+ public void testHashCode() throws IOException {
+ assertEquals(new PersistedList().hashCode(), new PersistedList().hashCode());
+ assertEquals(persistedList1.hashCode(), new PersistedList(owner1).hashCode());
+ assertFalse(persistedList1.hashCode() == persistedList2.hashCode());
+ persistedList1.add(new Shell("echo 'test1'"));
+ assertFalse(persistedList1.hashCode() == persistedList2.hashCode());
+ persistedList2.add(new Shell("echo 'test1'"));
+ assertFalse(persistedList1.hashCode() == persistedList2.hashCode());
+ persistedList3.add(new Shell("echo 'test1'"));
+ assertEquals(persistedList1.hashCode(), persistedList3.hashCode());
+ persistedList3.replaceBy(Arrays.asList(new Shell("echo 'test2'")));
+ assertFalse(persistedList1.hashCode() == persistedList3.hashCode());
+ }
+
+ @Test
+ public void testEqual() throws IOException {
+ assertEquals(persistedList1, persistedList1);
+ assertFalse(persistedList1.equals(null));
+ assertFalse(persistedList1.equals(new Object()));
+ assertFalse(persistedList1.equals(persistedList2));
+ assertEquals(persistedList1, persistedList3);
+ persistedList1.add(new Shell("echo 'test1'"));
+ persistedList3.add(new Shell("echo 'test1'"));
+ assertEquals(persistedList1, persistedList3);
+ persistedList1.add(new Shell("echo 'test3'"));
+ persistedList1.add(new Shell("echo 'test2'"));
+ persistedList3.add(new Shell("echo 'test2'"));
+ persistedList3.add(new Shell("echo 'test3'"));
+ assertEquals(persistedList1, persistedList3);
+
+ persistedList3.replaceBy(Arrays.asList(new Shell("echo 'test2'")));
+ assertFalse(persistedList1.equals(persistedList3));
+ }
+}