]> git.basschouten.com Git - openhab-addons.git/blob
c8032617750b34a6ff3c1f79f1ff54398dacffb9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lifx.internal.fields;
14
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;
20
21 import java.nio.ByteBuffer;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.core.util.HexUtils;
26
27 /**
28  * Tests {@link MACAddress}.
29  *
30  * @author Wouter Born - Initial contribution
31  */
32 @NonNullByDefault
33 public class MACAddressTest {
34
35     @Test
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"));
40     }
41
42     @Test
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"));
47     }
48
49     @Test
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"));
55     }
56
57     @Test
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"));
63     }
64
65     @Test
66     public void broadcastAddressComparison() {
67         assertThat(BROADCAST_ADDRESS, is(BROADCAST_ADDRESS));
68         assertThat(BROADCAST_ADDRESS.hashCode(), is(BROADCAST_ADDRESS.hashCode()));
69
70         assertThat(BROADCAST_ADDRESS, is(new MACAddress()));
71         assertThat(BROADCAST_ADDRESS.hashCode(), is(new MACAddress().hashCode()));
72
73         assertThat(BROADCAST_ADDRESS, is(not(new MACAddress("D073D5ABCDEF"))));
74         assertThat(BROADCAST_ADDRESS, is(not(new MACAddress("D073D5ABCDEF").hashCode())));
75     }
76
77     @Test
78     public void macAddressComparison() {
79         assertThat(new MACAddress("D073D5ABCDEF"), is(new MACAddress("D073D5ABCDEF")));
80         assertThat(new MACAddress("D073D5ABCDEF").hashCode(), is(new MACAddress("D073D5ABCDEF").hashCode()));
81
82         assertThat(new MACAddress("D073D5ABCDEF"), is(not(BROADCAST_ADDRESS)));
83         assertThat(new MACAddress("D073D5ABCDEF").hashCode(), is(not(BROADCAST_ADDRESS.hashCode())));
84
85         assertThat(new MACAddress("D073D5ABCDEF"), is(not(new MACAddress("D073D5123456"))));
86         assertThat(new MACAddress("D073D5ABCDEF").hashCode(), is(not(new MACAddress("D073D5123456").hashCode())));
87     }
88 }