aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakozak2011-11-23 02:34:58 (EST)
committerWinston Prakash2011-12-01 20:47:05 (EST)
commitc49257f048f6ffd05f87d9a4f0cbab9915e02a92 (patch)
tree77edb1c83f7815b27ca2777388fccc288376455c
parentf0008b731f54baa624f70eca07a7b11358bf229f (diff)
downloadorg.eclipse.hudson.core-c49257f048f6ffd05f87d9a4f0cbab9915e02a92.zip
org.eclipse.hudson.core-c49257f048f6ffd05f87d9a4f0cbab9915e02a92.tar.gz
org.eclipse.hudson.core-c49257f048f6ffd05f87d9a4f0cbab9915e02a92.tar.bz2
Implement equals/hashCode methods for CopyOnWriteList class, cover logic with unit test
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
-rw-r--r--hudson-core/src/main/java/hudson/util/CopyOnWriteList.java24
-rw-r--r--hudson-core/src/test/java/hudson/util/CopyOnWriteListEqualsHashCodeTest.java73
2 files changed, 94 insertions, 3 deletions
diff --git a/hudson-core/src/main/java/hudson/util/CopyOnWriteList.java b/hudson-core/src/main/java/hudson/util/CopyOnWriteList.java
index cd68365..84b6660 100644
--- a/hudson-core/src/main/java/hudson/util/CopyOnWriteList.java
+++ b/hudson-core/src/main/java/hudson/util/CopyOnWriteList.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
+* Kohsuke Kawaguchi, Nikita Levyankov
*
*
*******************************************************************************/
@@ -31,8 +31,8 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;
-import java.util.logging.Level;
import java.util.logging.Logger;
+import org.apache.commons.collections.CollectionUtils;
import static java.util.logging.Level.WARNING;
@@ -196,5 +196,23 @@ public class CopyOnWriteList<E> implements Iterable<E> {
}
}
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ CopyOnWriteList that = (CopyOnWriteList) o;
+ return CollectionUtils.isEqualCollection(this.core, that.core);
+ }
+
+ @Override
+ public int hashCode() {
+ return core != null ? core.hashCode() : 0;
+ }
+
private static final Logger LOGGER = Logger.getLogger(CopyOnWriteList.class.getName());
}
diff --git a/hudson-core/src/test/java/hudson/util/CopyOnWriteListEqualsHashCodeTest.java b/hudson-core/src/test/java/hudson/util/CopyOnWriteListEqualsHashCodeTest.java
new file mode 100644
index 0000000..2a38566
--- /dev/null
+++ b/hudson-core/src/test/java/hudson/util/CopyOnWriteListEqualsHashCodeTest.java
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ *
+ * 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.tasks.Builder;
+import hudson.tasks.Shell;
+import java.util.ArrayList;
+import java.util.List;
+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 CopyOnWriteList object.
+ * <p/>
+ * Date: 10/5/11
+ *
+ * @author Nikita Levyankov
+ */
+public class CopyOnWriteListEqualsHashCodeTest {
+ private List<Builder> data1;
+ private List<Builder> data2;
+
+ @Before
+ public void setUp() {
+ data1 = new ArrayList<Builder>();
+ data1.add(new Shell("echo 'test'"));
+ data1.add(new Shell("echo 'test1'"));
+ data1.add(new Shell("echo 'test2'"));
+
+ data2 = new ArrayList<Builder>();
+ data2.add(new Shell("echo 'test1'"));
+ data2.add(new Shell("echo 'test'"));
+ }
+
+ @Test
+ public void testHashCode() {
+ assertEquals(new CopyOnWriteList(data1).hashCode(), new CopyOnWriteList(data1).hashCode());
+
+ assertFalse(new CopyOnWriteList(data1).hashCode() == new CopyOnWriteList(data2).hashCode());
+ data2.add(new Shell("echo 'test2'"));
+ assertFalse(new CopyOnWriteList(data1).hashCode() == new CopyOnWriteList(data2).hashCode());
+ }
+
+ @Test
+ public void testEqual() {
+ CopyOnWriteList list = new CopyOnWriteList();
+ assertEquals(list, list);
+ assertFalse(list.equals(null));
+ assertFalse(list.equals(new Object()));
+ assertEquals(new CopyOnWriteList(new ArrayList()), new CopyOnWriteList(new ArrayList()));
+
+ assertFalse(new CopyOnWriteList(data1).equals(new CopyOnWriteList(data2)));
+ assertEquals(new CopyOnWriteList(data1), new CopyOnWriteList(data1));
+ data2.add(new Shell("echo 'test2'"));
+ assertEquals(new CopyOnWriteList(data1), new CopyOnWriteList(data2));
+ }
+}