[vlma-devel] commit: Test equals and hashCode methods for objects that are put in HashMaps or HashSets . (Adrien Grand )

git version control git at videolan.org
Fri Sep 12 23:56:45 CEST 2008


vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Fri Sep 12 23:57:25 2008 +0200| [2695851abcd75253bbd6ace2457413e5c2bc6c74] | committer: Adrien Grand 

Test equals and hashCode methods for objects that are put in HashMaps or HashSets.

Since these collections use hashCode for the storage, two (Order|MediaGroup)s
that are equal must have the same hashCode.

> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=2695851abcd75253bbd6ace2457413e5c2bc6c74
---

 vlma-api/pom.xml                                   |    9 +++
 .../java/org/videolan/vlma/model/MediaGroup.java   |   32 +++++++---
 .../org/videolan/vlma/model/MediaGroupTest.java    |   47 ++++++++++++++++
 .../java/org/videolan/vlma/model/OrderTest.java    |   59 ++++++++++++++++++++
 4 files changed, 137 insertions(+), 10 deletions(-)

diff --git a/vlma-api/pom.xml b/vlma-api/pom.xml
index 695c066..0c8e533 100644
--- a/vlma-api/pom.xml
+++ b/vlma-api/pom.xml
@@ -38,4 +38,13 @@
     <modelVersion>4.0.0</modelVersion>
     <packaging>jar</packaging>
 
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.4</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
 </project>
diff --git a/vlma-api/src/main/java/org/videolan/vlma/model/MediaGroup.java b/vlma-api/src/main/java/org/videolan/vlma/model/MediaGroup.java
index 493c5b4..fd71d21 100644
--- a/vlma-api/src/main/java/org/videolan/vlma/model/MediaGroup.java
+++ b/vlma-api/src/main/java/org/videolan/vlma/model/MediaGroup.java
@@ -52,18 +52,30 @@ public class MediaGroup extends ArrayList<Media> implements Comparable<MediaGrou
      */
     @Override
     public boolean equals(Object o) {
-        if (o == null) return false;
-        if (o instanceof MediaGroup) {
-            MediaGroup g = (MediaGroup) o;
-            if (g.size() != this.size())
+    if (o == null || !(o instanceof MediaGroup)) return false;
+        MediaGroup g = (MediaGroup) o;
+        if (g.size() != this.size())
+            return false;
+        for (Media media : this) {
+            if (!g.contains(media))
                 return false;
-            for (Media media : this) {
-                if (!g.contains(media))
-                    return false;
-            }
-            return true;
         }
-        return false;
+        return true;
+    }
+
+    /**
+     * Computes the MediaGroup hashCode.
+     *
+     * This implementation does not take into account the position of the media
+     * in the group.
+     */
+    @Override
+    public int hashCode() {
+        int result = 0;
+        for(Media media : this) {
+            result += media.hashCode();
+        }
+        return result;
     }
 
     /**
diff --git a/vlma-api/src/test/java/org/videolan/vlma/model/MediaGroupTest.java b/vlma-api/src/test/java/org/videolan/vlma/model/MediaGroupTest.java
new file mode 100644
index 0000000..e78e8c5
--- /dev/null
+++ b/vlma-api/src/test/java/org/videolan/vlma/model/MediaGroupTest.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2008 the VideoLAN team
+ *
+ * This file is part of VLMa.
+ *
+ * VLMa is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * VLMa is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with VLMa. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package org.videolan.vlma.model;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class MediaGroupTest {
+
+    private MediaGroup group;
+
+    @Test
+    public void testEquals() throws Exception {
+        FilesChannel media1 = new FilesChannel();
+        media1.setName("media1");
+        FilesChannel media2 = new FilesChannel();
+        media2.setName("media2");
+        group = new MediaGroup();
+        group.add(media1);
+        group.add(media2);
+        MediaGroup group2 = new MediaGroup();
+        group2.add(media2);
+        group2.add(media1);
+        assertEquals(group, group2);
+        assertEquals(group.hashCode(), group2.hashCode());
+    }
+
+}
diff --git a/vlma-api/src/test/java/org/videolan/vlma/model/OrderTest.java b/vlma-api/src/test/java/org/videolan/vlma/model/OrderTest.java
new file mode 100644
index 0000000..1ba012b
--- /dev/null
+++ b/vlma-api/src/test/java/org/videolan/vlma/model/OrderTest.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2008 the VideoLAN team
+ *
+ * This file is part of VLMa.
+ *
+ * VLMa is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * VLMa is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with VLMa. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package org.videolan.vlma.model;
+
+import static org.junit.Assert.*;
+
+import java.net.InetAddress;
+
+import org.junit.Test;
+
+public class OrderTest {
+
+    private Order order;
+
+    @Test
+    public void testEquals() throws Exception {
+        FilesChannel media1 = new FilesChannel();
+        media1.setName("media1");
+        FilesChannel media2 = new FilesChannel();
+        media2.setName("media2");
+        MediaGroup group = new MediaGroup();
+        group.add(media1);
+        group.add(media2);
+        MediaGroup group2 = new MediaGroup();
+        group2.add(media2);
+        group2.add(media1);
+        Adapter adapter = new FilesAdapter();
+        Server server = new Server();
+        server.setIp(InetAddress.getLocalHost());
+        adapter.setName("adapter");
+        adapter.setServer(server);
+        Adapter adapter2 = new FilesAdapter();
+        adapter2.setName("adapter");
+        adapter2.setServer(server);
+        order = new Order(adapter, group);
+        Order order2 = new Order(adapter2, group2);
+        assertEquals(order, order2);
+        assertEquals(order.hashCode(), order2.hashCode());
+    }
+
+}



More information about the vlma-devel mailing list