]> git.basschouten.com Git - openhab-addons.git/blob
35a7eb02f63c0d06b6900ae7d9699e0014e8c00b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.io.transport.modbus.test;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import org.junit.jupiter.api.Test;
18 import org.openhab.io.transport.modbus.endpoint.ModbusSerialSlaveEndpoint;
19 import org.openhab.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint;
20 import org.openhab.io.transport.modbus.endpoint.ModbusUDPSlaveEndpoint;
21
22 import gnu.io.SerialPort;
23 import net.wimpi.modbus.Modbus;
24
25 /**
26  * @author Sami Salonen - Initial contribution
27  */
28 public class ModbusSlaveEndpointTestCase {
29
30     @Test
31     public void testEqualsSameTcp() {
32         ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
33         ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
34         assertEquals(e1, e2);
35     }
36
37     @Test
38     public void testEqualsSameSerial2() {
39         ModbusSerialSlaveEndpoint e1 = new ModbusSerialSlaveEndpoint("port1", 9600, SerialPort.FLOWCONTROL_NONE,
40                 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
41                 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
42         ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint("port1", 9600, SerialPort.FLOWCONTROL_NONE,
43                 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
44                 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
45         assertEquals(e1, e2);
46     }
47
48     /**
49      * even though different echo parameter & baud rate, the endpoints are considered the same due to same port
50      */
51     @Test
52     public void testEqualsSameSerial3() {
53         ModbusSerialSlaveEndpoint e1 = new ModbusSerialSlaveEndpoint("port1", 9600, SerialPort.FLOWCONTROL_NONE,
54                 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
55                 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
56         ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint("port1", 9600, SerialPort.FLOWCONTROL_NONE,
57                 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
58                 Modbus.DEFAULT_SERIAL_ENCODING, false, 500);
59         assertEquals(e1, e2);
60         assertEquals(e1.hashCode(), e2.hashCode());
61     }
62
63     @Test
64     public void testEqualsDifferentSerial() {
65         ModbusSerialSlaveEndpoint e1 = new ModbusSerialSlaveEndpoint("port1", 9600, SerialPort.FLOWCONTROL_NONE,
66                 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
67                 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
68         ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint("port2", 9600, SerialPort.FLOWCONTROL_NONE,
69                 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
70                 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
71         assertNotEquals(e1, e2);
72         assertNotEquals(e1.hashCode(), e2.hashCode());
73     }
74
75     @Test
76     public void testEqualsDifferentTCPPort() {
77         ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
78         ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.1", 501);
79         assertNotEquals(e1, e2);
80         assertNotEquals(e1.hashCode(), e2.hashCode());
81     }
82
83     @Test
84     public void testEqualsDifferentTCPHost() {
85         ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
86         ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.2", 501);
87         assertNotEquals(e1, e2);
88         assertNotEquals(e1.hashCode(), e2.hashCode());
89     }
90
91     @Test
92     public void testEqualsDifferentProtocol() {
93         ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
94         ModbusUDPSlaveEndpoint e2 = new ModbusUDPSlaveEndpoint("127.0.0.1", 500);
95         assertNotEquals(e1, e2);
96         assertNotEquals(e1.hashCode(), e2.hashCode());
97     }
98
99     @Test
100     public void testEqualsDifferentProtocol2() {
101         ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
102         ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint("port2", 9600, SerialPort.FLOWCONTROL_NONE,
103                 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
104                 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
105         assertNotEquals(e1, e2);
106         assertNotEquals(e1.hashCode(), e2.hashCode());
107     }
108 }