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 org.junit.Assert;
16 import org.junit.Test;
17 import org.openhab.io.transport.modbus.endpoint.ModbusSerialSlaveEndpoint;
18 import org.openhab.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint;
19 import org.openhab.io.transport.modbus.endpoint.ModbusUDPSlaveEndpoint;
21 import gnu.io.SerialPort;
22 import net.wimpi.modbus.Modbus;
25 * @author Sami Salonen - Initial contribution
27 public class ModbusSlaveEndpointTestCase {
30 public void testEqualsSameTcp() {
31 ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
32 ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
33 Assert.assertEquals(e1, e2);
37 public void testEqualsSameSerial2() {
38 ModbusSerialSlaveEndpoint e1 = new ModbusSerialSlaveEndpoint("port1", 9600, SerialPort.FLOWCONTROL_NONE,
39 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
40 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
41 ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint("port1", 9600, SerialPort.FLOWCONTROL_NONE,
42 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
43 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
44 Assert.assertEquals(e1, e2);
48 * even though different echo parameter & baud rate, the endpoints are considered the same due to same port
51 public void testEqualsSameSerial3() {
52 ModbusSerialSlaveEndpoint e1 = new ModbusSerialSlaveEndpoint("port1", 9600, SerialPort.FLOWCONTROL_NONE,
53 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
54 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
55 ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint("port1", 9600, SerialPort.FLOWCONTROL_NONE,
56 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
57 Modbus.DEFAULT_SERIAL_ENCODING, false, 500);
58 Assert.assertEquals(e1, e2);
59 Assert.assertEquals(e1.hashCode(), e2.hashCode());
63 public void testEqualsDifferentSerial() {
64 ModbusSerialSlaveEndpoint e1 = new ModbusSerialSlaveEndpoint("port1", 9600, SerialPort.FLOWCONTROL_NONE,
65 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
66 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
67 ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint("port2", 9600, SerialPort.FLOWCONTROL_NONE,
68 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
69 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
70 Assert.assertNotEquals(e1, e2);
71 Assert.assertNotEquals(e1.hashCode(), e2.hashCode());
75 public void testEqualsDifferentTCPPort() {
76 ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
77 ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.1", 501);
78 Assert.assertNotEquals(e1, e2);
79 Assert.assertNotEquals(e1.hashCode(), e2.hashCode());
83 public void testEqualsDifferentTCPHost() {
84 ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
85 ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.2", 501);
86 Assert.assertNotEquals(e1, e2);
87 Assert.assertNotEquals(e1.hashCode(), e2.hashCode());
91 public void testEqualsDifferentProtocol() {
92 ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
93 ModbusUDPSlaveEndpoint e2 = new ModbusUDPSlaveEndpoint("127.0.0.1", 500);
94 Assert.assertNotEquals(e1, e2);
95 Assert.assertNotEquals(e1.hashCode(), e2.hashCode());
99 public void testEqualsDifferentProtocol2() {
100 ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
101 ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint("port2", 9600, SerialPort.FLOWCONTROL_NONE,
102 SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
103 Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
104 Assert.assertNotEquals(e1, e2);
105 Assert.assertNotEquals(e1.hashCode(), e2.hashCode());