From 1f01b33c339dfd45cd23c948aa4e1ee14ce6fef7 Mon Sep 17 00:00:00 2001 From: Kamil Date: Tue, 27 Feb 2024 08:48:12 +0100 Subject: [PATCH] #2755 Signaling the data source that has a problem: - corrected test: InitializeWithErrorsDataSourceRtTest; - change type catch from IOException to Throwable; --- .../dataSource/vmstat/VMStatDataSourceRT.java | 2 +- .../InitializeWithErrorsDataSourceRtTest.java | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/com/serotonin/mango/rt/dataSource/vmstat/VMStatDataSourceRT.java b/src/com/serotonin/mango/rt/dataSource/vmstat/VMStatDataSourceRT.java index bb36105dde..9ff2fc32c1 100644 --- a/src/com/serotonin/mango/rt/dataSource/vmstat/VMStatDataSourceRT.java +++ b/src/com/serotonin/mango/rt/dataSource/vmstat/VMStatDataSourceRT.java @@ -139,7 +139,7 @@ else if ("st".equals(headerParts[i])) returnToNormal(DATA_SOURCE_EXCEPTION_EVENT, System.currentTimeMillis()); } - catch (IOException e) { + catch (Throwable e) { raiseEvent(DATA_SOURCE_EXCEPTION_EVENT, System.currentTimeMillis(), true, new LocalizableMessage( "event.initializationError", e.getMessage())); return; diff --git a/test/com/serotonin/mango/rt/dataSource/InitializeWithErrorsDataSourceRtTest.java b/test/com/serotonin/mango/rt/dataSource/InitializeWithErrorsDataSourceRtTest.java index e6b0307357..b1ae87e2f4 100644 --- a/test/com/serotonin/mango/rt/dataSource/InitializeWithErrorsDataSourceRtTest.java +++ b/test/com/serotonin/mango/rt/dataSource/InitializeWithErrorsDataSourceRtTest.java @@ -25,7 +25,6 @@ import com.serotonin.mango.rt.dataSource.ebro.EBI25DataSourceRT; import com.serotonin.mango.rt.dataSource.galil.GalilDataSourceRT; import com.serotonin.mango.rt.dataSource.http.HttpReceiverDataSourceRT; -import com.serotonin.mango.rt.dataSource.modbus.ModbusDataSource; import com.serotonin.mango.rt.dataSource.modbus.ModbusIpDataSource; import com.serotonin.mango.rt.dataSource.modbus.ModbusSerialDataSource; import com.serotonin.mango.rt.dataSource.nmea.NmeaDataSourceRT; @@ -51,7 +50,6 @@ import com.serotonin.mango.vo.dataSource.viconics.ViconicsDataSourceVO; import com.serotonin.mango.vo.dataSource.vmstat.VMStatDataSourceVO; import com.serotonin.mango.web.ContextWrapper; -import org.apache.derby.iapi.services.i18n.MessageService; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -65,14 +63,15 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunnerDelegate; import org.scada_lts.ds.messaging.MessagingDataSourceRT; +import org.scada_lts.ds.messaging.exception.MessagingServiceException; import org.scada_lts.ds.messaging.protocol.amqp.AmqpDataSourceVO; import org.scada_lts.ds.messaging.protocol.mqtt.MqttDataSourceVO; import org.scada_lts.ds.messaging.service.MessagingService; import org.scada_lts.ds.messaging.service.MessagingServiceFactory; +import java.io.IOException; import java.util.function.Supplier; -import static com.serotonin.mango.util.InitializeDataSourceRtMockUtils.modbusMock; import static com.serotonin.mango.util.InitializeDataSourceRtMockUtils.wrap; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -103,7 +102,7 @@ public static Object[][] data() { MessagingService messageServiceMock = mock(MessagingService.class); doAnswer(a -> { - throw new RuntimeException(); + throw new MessagingServiceException(); }).when(messageServiceMock).open(); return new Object[][] { @@ -166,6 +165,18 @@ public void config() { } reset(eventManager); + + java.lang.Runtime runtimeMock = mock(Runtime.class); + try { + doAnswer(a -> { + throw new IOException(); + }).when(runtimeMock).exec(anyString()); + PowerMockito.mockStatic(java.lang.Runtime.class); + when(java.lang.Runtime.getRuntime()).thenReturn(runtimeMock); + + } catch (Exception e) { + throw new RuntimeException(e); + } } @After @@ -209,5 +220,4 @@ public void when_invoke_initialize_with_errors_then_never_returnToNormal() { //then: Mockito.verify(eventManager, never()).returnToNormal(any(), anyLong()); } - }