MACAddress { public static MACAddress valueOf(String address) { String[] elements = address.split(":"); if (elements.length != MAC_ADDRESS_LENGTH) { throw new IllegalArgumentException( "Specified MAC Address must contain 12 hex digits" + " separated pairwise by :'s."); } byte[] addressInBytes = new byte[MAC_ADDRESS_LENGTH]; for (int i = 0; i < MAC_ADDRESS_LENGTH; i++) { String element = elements[i]; addressInBytes[i] = (byte)Integer.parseInt(element, 16); } return new MACAddress(addressInBytes); } MACAddress(byte[] address); static MACAddress valueOf(String address); static MACAddress valueOf(byte[] address); static MACAddress valueOf(long address); int length(); byte[] toBytes(); long toLong(); boolean isBroadcast(); boolean isMulticast(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); static final int MAC_ADDRESS_LENGTH; }
@Test public void testValueOf() { MACAddress address = MACAddress.valueOf("00:01:02:03:04:05"); assertEquals(address, MACAddress.valueOf(new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05})); assertEquals("00:01:02:03:04:05", address.toString()); address = MACAddress.valueOf("FF:FE:FD:10:20:30"); assertEquals(address, MACAddress.valueOf(new byte[]{(byte) 0xFF, (byte) 0xFE, (byte) 0xFD, 0x10, 0x20, 0x30})); assertEquals("FF:FE:FD:10:20:30", address.toString()); address = MACAddress.valueOf("00:11:22:aa:bb:cc"); assertEquals(address, MACAddress.valueOf(new byte[]{0x00, 0x11, 0x22, (byte)0xaa, (byte)0xbb, (byte)0xcc})); }
@Test(expected=NumberFormatException.class) public void testIllegalFormat() { MACAddress.valueOf("0T:00:01:02:03:04"); }
@Test(expected=IllegalArgumentException.class) public void testLongStringFields() { MACAddress.valueOf("00:01:02:03:04:05:06"); }
@Test(expected=IllegalArgumentException.class) public void testShortStringFields() { MACAddress.valueOf("00:01:02:03:04"); }
@Test(expected=IllegalArgumentException.class) public void testLongByteFields() { MACAddress.valueOf(new byte[]{0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}); }
@Test(expected=IllegalArgumentException.class) public void testShortByteField() { MACAddress.valueOf(new byte[]{0x01, 0x01, 0x02, 0x03, 0x04}); }