Fixed unstable unit test. 77/5577/1
authorShigeru Yasuda <s-yasuda@da.jp.nec.com>
Mon, 10 Mar 2014 09:04:56 +0000 (18:04 +0900)
committerShigeru Yasuda <s-yasuda@da.jp.nec.com>
Mon, 10 Mar 2014 09:04:56 +0000 (18:04 +0900)
A MacAddressEntry instance keeps a set of InetAddress instances using
an unorderd set. So string representation of a MacAddressEntry instance
is unstable because it is affected by the order of InetAddress instances.

Change-Id: Ic91af42fcfa851c67d0197d7ad34c2ae1e974413
Signed-off-by: Shigeru Yasuda <s-yasuda@da.jp.nec.com>
manager/api/src/test/java/org/opendaylight/vtn/manager/MacAddressEntryTest.java
manager/api/src/test/java/org/opendaylight/vtn/manager/TestDataLink.java [new file with mode: 0644]

index 713e0b964da4d5bb12d7efada9a49cd9381fd55a..84dc70b70948ba958487cbe4cde215f7d9e52b9b 100644 (file)
@@ -1,11 +1,12 @@
 /*
- * Copyright (c) 2013 NEC Corporation
+ * Copyright (c) 2013-2014 NEC 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
  */
+
 package org.opendaylight.vtn.manager;
 
 import java.net.InetAddress;
@@ -22,24 +23,6 @@ import org.opendaylight.controller.sal.packet.address.EthernetAddress;
  * JUnit test for {@link MacAddressEntry}
  */
 public class MacAddressEntryTest extends TestBase {
-
-    // The Test class which implemented DataLinkAddress class.
-    class TestDataLink extends DataLinkAddress {
-        private static final long serialVersionUID = 5664547077536394233L;
-
-        TestDataLink() {
-
-        }
-
-        TestDataLink(String name) {
-            super(name);
-        }
-
-        public TestDataLink clone() {
-            return new TestDataLink(this.getName());
-        }
-    }
-
     /**
      * Test case for {@link MacAddressEntry} and getter method
      */
@@ -138,7 +121,6 @@ public class MacAddressEntryTest extends TestBase {
     public void testToString() {
         List<Set<InetAddress>> ips = createInetAddresses();
         String prefix = "MacAddressEntry[";
-        String suffix = "]";
         short vlans[] = { -10, 0, 1, 100, 4095 };
         for (NodeConnector nc : createNodeConnectors(3, false)) {
             for (Set<InetAddress> ipset : ips) {
@@ -150,10 +132,22 @@ public class MacAddressEntryTest extends TestBase {
                         String a = "address=" + ea;
                         String v = "vlan=" + vlan;
                         String c = "connector=" + nc;
-                        String i = "ipaddr=" + toString(ipset);
+
+                        // IP addresses are kept by unordered set.
+                        // So string representation of the object may be
+                        // changed by its order.
                         String required =
-                            joinStrings(prefix, suffix, ",", a, v, c, i);
-                        assertEquals(required, mae.toString());
+                            joinStrings(prefix, null, ",", a, v, c);
+                        String str = mae.toString();
+                        int idx = str.indexOf(",ipaddr={");
+                        assertTrue(idx > 0);
+                        assertEquals(required, str.substring(0, idx));
+
+                        Set<InetAddress> addrSet = getInetAddressSet(str);
+                        if (ipset == null) {
+                            ipset = new HashSet<InetAddress>();
+                        }
+                        assertEquals(ipset, addrSet);
                     }
                 }
             }
@@ -181,24 +175,35 @@ public class MacAddressEntryTest extends TestBase {
     }
 
     /**
-     * Return a string representation of the given {@code InetAddress} set.
+     * Construct a set of {@link InetAddress} instances from a string returned
+     * by {@link MacAddressEntry#toString()}.
      *
-     * @param ipaddrs  An set of {@code InetAddress}.
-     * @return  A string representation of the given set.
+     * @param str  A string returned by {@link MacAddressEntry#toString()}.
+     * @return  A set of {@link InetAddress} instances.
      */
-    private String toString(Set<InetAddress> ipaddrs) {
-        StringBuilder builder = new StringBuilder("{");
-        if (ipaddrs != null) {
-            char sep = 0;
-            for (InetAddress ip: ipaddrs) {
-                if (sep != 0) {
-                    builder.append(',');
-                }
-                builder.append(ip.getHostAddress());
-                sep = ',';
+    private Set<InetAddress> getInetAddressSet(String str) {
+        HashSet<InetAddress> set = new HashSet<InetAddress>();
+        String prefix = ",ipaddr={";
+        int start = str.indexOf(prefix);
+        if (start < 0) {
+            return set;
+        }
+
+        start += prefix.length();
+        int end = str.lastIndexOf("}]");
+        if (end < start + 1) {
+            return set;
+        }
+
+        String istr = str.substring(start, end);
+        try {
+            for (String s: istr.split(",")) {
+                set.add(InetAddress.getByName(s));
             }
+        } catch (Exception e) {
+            unexpected(e);
         }
 
-        return builder.append('}').toString();
+        return set;
     }
 }
diff --git a/manager/api/src/test/java/org/opendaylight/vtn/manager/TestDataLink.java b/manager/api/src/test/java/org/opendaylight/vtn/manager/TestDataLink.java
new file mode 100644 (file)
index 0000000..1997372
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2014 NEC 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
+ */
+
+package org.opendaylight.vtn.manager;
+
+import org.opendaylight.controller.sal.packet.address.DataLinkAddress;
+
+/**
+ * A pseudo data link address for test.
+ */
+public class TestDataLink extends DataLinkAddress {
+    /**
+     * Version number for serialization.
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * The name of this address type.
+     */
+    private static final String  ADDRESS_NAME = "Test Address";
+
+    /**
+     * A string which identifies the address.
+     */
+    private final String  address;
+
+    /**
+     * Construct a new address.
+     */
+    public TestDataLink() {
+        this("");
+    }
+
+    /**
+     * Construct a new address.
+     *
+     * @param addr  A string which identifies the address.
+     */
+    public TestDataLink(String addr) {
+        super(ADDRESS_NAME);
+        address = (addr == null) ? "" : addr;
+    }
+
+    /**
+     * Return a clone of this object.
+     *
+     * @return  A clone of this object.
+     */
+    @Override
+    public TestDataLink clone() {
+        return new TestDataLink(new String(address));
+    }
+
+    /**
+     * Determine whether the given object is identical to this object.
+     *
+     * @param o  An object to be compared.
+     * @return   {@code true} if identical. Otherwise {@code false}.
+     */
+    @Override
+    public boolean equals(Object o) {
+        if (o == this) {
+            return true;
+        }
+        if (!(o instanceof TestDataLink)) {
+            return false;
+        }
+
+        TestDataLink dl = (TestDataLink)o;
+        return address.equals(dl.address);
+    }
+
+    /**
+     * Return the hash code of this object.
+     *
+     * @return  The hash code.
+     */
+    @Override
+    public int hashCode() {
+        return address.hashCode();
+    }
+
+    /**
+     * Return a string representation of this object.
+     *
+     * @return  A string representation of this object.
+     */
+    @Override
+    public String toString() {
+        return "TestDataLink [" + address + "]";
+    }
+}
+