1
1
package io .openems .edge .bridge .modbus ;
2
2
3
3
import static io .openems .edge .bridge .modbus .api .ModbusComponent .ChannelId .MODBUS_COMMUNICATION_FAILED ;
4
+ import static org .junit .Assert .assertTrue ;
4
5
5
6
import org .junit .Test ;
6
7
24
25
import io .openems .edge .common .test .ComponentTest ;
25
26
import io .openems .edge .common .test .TestUtils ;
26
27
28
+ import java .io .ByteArrayOutputStream ;
29
+ import java .io .PrintStream ;
30
+
27
31
public class BridgeModbusTcpImplTest {
28
32
29
33
private static final int UNIT_ID = 1 ;
@@ -43,6 +47,8 @@ public void test() throws Exception {
43
47
var processImage = new SimpleProcessImage (UNIT_ID );
44
48
Register register100 = new SimpleRegister (123 );
45
49
processImage .addRegister (100 , register100 );
50
+ Register register101 = new SimpleRegister (321 );
51
+ processImage .addRegister (101 , register101 );
46
52
slave .addProcessImage (UNIT_ID , processImage );
47
53
slave .open ();
48
54
@@ -91,15 +97,63 @@ public void test() throws Exception {
91
97
}
92
98
}
93
99
100
+ @ Test
101
+ public void testTriggerLogIllegalArgumentException () throws Exception {
102
+ final ThrowingRunnable <Exception > sleep = () -> Thread .sleep (CYCLE_TIME );
103
+ var port = TestUtils .findRandomOpenPortOnAllLocalInterfaces ();
104
+ ModbusSlave slave = null ;
105
+ PrintStream originalOut = System .out ;
106
+ ByteArrayOutputStream outContent = new ByteArrayOutputStream ();
107
+ try {
108
+ /*
109
+ * Open Modbus/TCP Slave
110
+ */
111
+ slave = ModbusSlaveFactory .createTCPSlave (port , 1 );
112
+ var processImage = new SimpleProcessImage (UNIT_ID );
113
+ Register register100 = new SimpleRegister (123 );
114
+ Register register101 = new SimpleRegister (Integer .MAX_VALUE ); // this will cause the IllegalArgumentException
115
+ processImage .addRegister (100 , register100 );
116
+ processImage .addRegister (101 , register101 );
117
+ slave .addProcessImage (UNIT_ID , processImage );
118
+ slave .open ();
119
+ System .setOut (new PrintStream (outContent ));
120
+ /*
121
+ * Instantiate Modbus-Bridge
122
+ */
123
+ var sut = new BridgeModbusTcpImpl ();
124
+ var test = new ComponentTest (sut ) //
125
+ .activate (MyConfigTcp .create () //
126
+ .setId ("modbus0" ) //
127
+ .setIp ("127.0.0.1" ) //
128
+ .setPort (port ) //
129
+ .setInvalidateElementsAfterReadErrors (1 ) //
130
+ .setLogVerbosity (LogVerbosity .NONE ) //
131
+ .build ());
132
+ test .addComponent (new MyModbusComponent ("device0" , sut , UNIT_ID ));
133
+ test //
134
+ .next (new TestCase () //
135
+ .onAfterProcessImage (sleep )); //
136
+ assertTrue (outContent .toString ().contains ("IllegalArgumentException" ));
137
+
138
+ } finally {
139
+ if (slave != null ) {
140
+ slave .close ();
141
+ System .setOut (originalOut );
142
+ System .out .println (outContent );
143
+ }
144
+ }
145
+ }
146
+
94
147
private static class MyModbusComponent extends DummyModbusComponent {
95
148
96
149
public MyModbusComponent (String id , AbstractModbusBridge bridge , int unitId ) throws OpenemsException {
97
150
super (id , bridge , unitId , ChannelId .values ());
98
151
}
99
152
100
153
public enum ChannelId implements io .openems .edge .common .channel .ChannelId {
101
- REGISTER_100 (Doc .of (OpenemsType .INTEGER )); //
102
-
154
+ REGISTER_100 (Doc .of (OpenemsType .INTEGER )), //
155
+ REGISTER_101 (Doc .of (OpenemsType .SHORT )), //
156
+ ;
103
157
private final Doc doc ;
104
158
105
159
private ChannelId (Doc doc ) {
@@ -116,7 +170,8 @@ public Doc doc() {
116
170
protected ModbusProtocol defineModbusProtocol () {
117
171
return new ModbusProtocol (this , //
118
172
new FC3ReadRegistersTask (100 , Priority .HIGH , //
119
- m (ChannelId .REGISTER_100 , new UnsignedWordElement (100 )))); //
173
+ m (ChannelId .REGISTER_100 , new UnsignedWordElement (100 )),
174
+ m (ChannelId .REGISTER_101 , new UnsignedWordElement (101 )))); //
120
175
}
121
176
122
177
}
0 commit comments