Skip to content

Commit 10d2953

Browse files
committed
added TestCase to cover General Exception
Trigger IllegalArgumentException by adding a new Channel, and filling it with Integer.MAX value (not fitting in short range), catch the log and check for "IllegalArgumentException" -> For me : easiest way to catch the logError
1 parent 47542c5 commit 10d2953

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

io.openems.edge.bridge.modbus/test/io/openems/edge/bridge/modbus/BridgeModbusTcpImplTest.java

+58-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.openems.edge.bridge.modbus;
22

33
import static io.openems.edge.bridge.modbus.api.ModbusComponent.ChannelId.MODBUS_COMMUNICATION_FAILED;
4+
import static org.junit.Assert.assertTrue;
45

56
import org.junit.Test;
67

@@ -24,6 +25,9 @@
2425
import io.openems.edge.common.test.ComponentTest;
2526
import io.openems.edge.common.test.TestUtils;
2627

28+
import java.io.ByteArrayOutputStream;
29+
import java.io.PrintStream;
30+
2731
public class BridgeModbusTcpImplTest {
2832

2933
private static final int UNIT_ID = 1;
@@ -43,6 +47,8 @@ public void test() throws Exception {
4347
var processImage = new SimpleProcessImage(UNIT_ID);
4448
Register register100 = new SimpleRegister(123);
4549
processImage.addRegister(100, register100);
50+
Register register101 = new SimpleRegister(321);
51+
processImage.addRegister(101, register101);
4652
slave.addProcessImage(UNIT_ID, processImage);
4753
slave.open();
4854

@@ -91,15 +97,63 @@ public void test() throws Exception {
9197
}
9298
}
9399

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+
94147
private static class MyModbusComponent extends DummyModbusComponent {
95148

96149
public MyModbusComponent(String id, AbstractModbusBridge bridge, int unitId) throws OpenemsException {
97150
super(id, bridge, unitId, ChannelId.values());
98151
}
99152

100153
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+
;
103157
private final Doc doc;
104158

105159
private ChannelId(Doc doc) {
@@ -116,7 +170,8 @@ public Doc doc() {
116170
protected ModbusProtocol defineModbusProtocol() {
117171
return new ModbusProtocol(this, //
118172
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)))); //
120175
}
121176

122177
}

0 commit comments

Comments
 (0)