2 * Copyright (c) 2010-2020 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.io.transport.modbus.test;
15 import static org.junit.jupiter.api.Assertions.*;
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;
22 import gnu.io.SerialPort;
23 import net.wimpi.modbus.Modbus;
26 * @author Sami Salonen - Initial contribution
28 public class ModbusSlaveEndpointTestCase {
31 public void testEqualsSameTcp() {
32 ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
33 ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
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);
49 * even though different echo parameter & baud rate, the endpoints are considered the same due to same port
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);
60 assertEquals(e1.hashCode(), e2.hashCode());
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());
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());
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());
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());
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());