2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lifx.internal.fields;
15 import static org.hamcrest.CoreMatchers.not;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.core.Is.is;
18 import static org.openhab.binding.lifx.internal.fields.MACAddress.BROADCAST_ADDRESS;
19 import static org.openhab.core.util.HexUtils.bytesToHex;
21 import java.nio.ByteBuffer;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.core.util.HexUtils;
28 * Tests {@link MACAddress}.
30 * @author Wouter Born - Initial contribution
33 public class MACAddressTest {
36 public void broadcastAddress() {
37 assertThat(BROADCAST_ADDRESS.getAsLabel(), is("000000000000"));
38 assertThat(BROADCAST_ADDRESS.getHex(), is("00:00:00:00:00:00"));
39 assertThat(bytesToHex(BROADCAST_ADDRESS.getBytes().array()), is("000000000000"));
43 public void defaultConstructor() {
44 MACAddress macAddress = new MACAddress();
45 assertThat(macAddress.getAsLabel(), is("000000000000"));
46 assertThat(macAddress.getHex(), is("00:00:00:00:00:00"));
50 public void constructFromByteBuffer() {
51 MACAddress macAddress = new MACAddress(ByteBuffer.wrap(HexUtils.hexToBytes("D073D5123456")));
52 assertThat(macAddress.getAsLabel(), is("D073D5123456"));
53 assertThat(macAddress.getHex(), is("D0:73:D5:12:34:56"));
54 assertThat(bytesToHex(macAddress.getBytes().array()), is("D073D5123456"));
58 public void constructFromString() {
59 MACAddress macAddress = new MACAddress("D073D5ABCDEF");
60 assertThat(macAddress.getAsLabel(), is("D073D5ABCDEF"));
61 assertThat(macAddress.getHex(), is("D0:73:D5:AB:CD:EF"));
62 assertThat(bytesToHex(macAddress.getBytes().array()), is("D073D5ABCDEF"));
66 public void broadcastAddressComparison() {
67 assertThat(BROADCAST_ADDRESS, is(BROADCAST_ADDRESS));
68 assertThat(BROADCAST_ADDRESS.hashCode(), is(BROADCAST_ADDRESS.hashCode()));
70 assertThat(BROADCAST_ADDRESS, is(new MACAddress()));
71 assertThat(BROADCAST_ADDRESS.hashCode(), is(new MACAddress().hashCode()));
73 assertThat(BROADCAST_ADDRESS, is(not(new MACAddress("D073D5ABCDEF"))));
74 assertThat(BROADCAST_ADDRESS, is(not(new MACAddress("D073D5ABCDEF").hashCode())));
78 public void macAddressComparison() {
79 assertThat(new MACAddress("D073D5ABCDEF"), is(new MACAddress("D073D5ABCDEF")));
80 assertThat(new MACAddress("D073D5ABCDEF").hashCode(), is(new MACAddress("D073D5ABCDEF").hashCode()));
82 assertThat(new MACAddress("D073D5ABCDEF"), is(not(BROADCAST_ADDRESS)));
83 assertThat(new MACAddress("D073D5ABCDEF").hashCode(), is(not(BROADCAST_ADDRESS.hashCode())));
85 assertThat(new MACAddress("D073D5ABCDEF"), is(not(new MACAddress("D073D5123456"))));
86 assertThat(new MACAddress("D073D5ABCDEF").hashCode(), is(not(new MACAddress("D073D5123456").hashCode())));