/*
- * 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;
* 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
*/
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) {
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);
}
}
}
}
/**
- * 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;
}
}
--- /dev/null
+/*
+ * 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 + "]";
+ }
+}
+