]> git.basschouten.com Git - openhab-addons.git/blob
70ea4dbfe413a0f6ac42c8469179c89132cf93d2
[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 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;
20
21 import gnu.io.SerialPort;
22 import net.wimpi.modbus.Modbus;
23
24 /**
25  * @author Sami Salonen - Initial contribution
26  */
27 public class ModbusSlaveEndpointTestCase {
28
29     @Test
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);
34     }
35
36     @Test
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);
45     }
46
47     /**
48      * even though different echo parameter & baud rate, the endpoints are considered the same due to same port
49      */
50     @Test
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());
60     }
61
62     @Test
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());
72     }
73
74     @Test
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());
80     }
81
82     @Test
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());
88     }
89
90     @Test
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());
96     }
97
98     @Test
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());
106     }
107 }