You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/11/24 14:55:05 UTC
[incubator-plc4x] branch develop updated: [plc4j-opm] fixed some
sonar issues.
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 70445ae [plc4j-opm] fixed some sonar issues.
70445ae is described below
commit 70445ae1e8de6d772daff15c86a7dbb9f2f13307
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Sat Nov 24 15:47:02 2018 +0100
[plc4j-opm] fixed some sonar issues.
---
.../plc4x/java/opm/PlcEntityInterceptor.java | 3 +-
.../apache/plc4x/java/opm/PlcEntityManager.java | 5 +-
.../apache/plc4x/java/opm/ConnectedEntityTest.java | 63 ++++++++++----------
.../plc4x/java/opm/PlcEntityInterceptorTest.java | 68 +++++++++-------------
.../plc4x/java/opm/PlcEntityManagerTest.java | 3 +
5 files changed, 68 insertions(+), 74 deletions(-)
diff --git a/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityInterceptor.java b/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityInterceptor.java
index 5d0573b..ea0bb2e 100644
--- a/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityInterceptor.java
+++ b/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityInterceptor.java
@@ -87,7 +87,7 @@ public class PlcEntityInterceptor {
* @return possible result of the original methods invocation
* @throws OPMException Problems with plc / proxying
*/
- @SuppressWarnings("unused")
+ @SuppressWarnings({"unused", "squid:S00107"})
@RuntimeType
public static Object interceptGetter(@This Object proxy, @Origin Method method, @SuperCall Callable<?> callable,
@FieldValue(PlcEntityManager.PLC_ADDRESS_FIELD_NAME) String address,
@@ -151,6 +151,7 @@ public class PlcEntityInterceptor {
}
}
+ @SuppressWarnings({"unused", "squid:S00107"})
@RuntimeType
public static Object interceptSetter(@This Object proxy, @Origin Method method, @SuperCall Callable<?> callable,
@FieldValue(PlcEntityManager.PLC_ADDRESS_FIELD_NAME) String address,
diff --git a/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityManager.java b/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityManager.java
index 7188d4e..af0d2c0 100644
--- a/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityManager.java
+++ b/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityManager.java
@@ -24,6 +24,7 @@ import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.implementation.MethodDelegation;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.plc4x.java.PlcDriverManager;
+import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -186,7 +187,7 @@ public class PlcEntityManager {
try {
return field.get(object);
} catch (IllegalAccessException e) {
- throw new RuntimeException(e);
+ throw new PlcRuntimeException(e);
}
}
@@ -194,7 +195,7 @@ public class PlcEntityManager {
try {
field.set(object, value);
} catch (IllegalAccessException e) {
- throw new RuntimeException(e);
+ throw new PlcRuntimeException(e);
}
}
diff --git a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/ConnectedEntityTest.java b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/ConnectedEntityTest.java
index cec9809..b592fbf 100644
--- a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/ConnectedEntityTest.java
+++ b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/ConnectedEntityTest.java
@@ -21,13 +21,15 @@ package org.apache.plc4x.java.opm;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.plc4x.java.PlcDriverManager;
-import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.base.messages.items.DefaultStringFieldItem;
import org.apache.plc4x.java.mock.MockDevice;
import org.apache.plc4x.java.mock.PlcMockConnection;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
import java.util.stream.IntStream;
@@ -38,55 +40,54 @@ import static org.mockito.Mockito.*;
/**
* Tests for Connected Entities.
*/
+@ExtendWith(MockitoExtension.class)
public class ConnectedEntityTest {
- @Test
- public void useCache() throws PlcConnectionException, OPMException {
- // Mock
- PlcDriverManager driverManager = new PlcDriverManager();
- PlcMockConnection connection = (PlcMockConnection) driverManager.getConnection("mock:cached");
- MockDevice mock = Mockito.mock(MockDevice.class);
- when(mock.read(any())).thenReturn(Pair.of(PlcResponseCode.OK, new DefaultStringFieldItem("hallo")));
- connection.setDevice(mock);
- PlcEntityManager entityManager = new PlcEntityManager(driverManager);
+ PlcDriverManager driverManager;
+
+ PlcMockConnection connection;
+
+ PlcEntityManager entityManager;
+
+ @Mock
+ MockDevice mockDevice;
+ @BeforeEach
+ void setUp() throws Exception {
+ driverManager = new PlcDriverManager();
+ connection = (PlcMockConnection) driverManager.getConnection("mock:cached");
+ when(mockDevice.read(any()))
+ .thenReturn(Pair.of(PlcResponseCode.OK, new DefaultStringFieldItem("hallo")));
+ connection.setDevice(mockDevice);
+ entityManager = new PlcEntityManager(driverManager);
+ }
+
+ @Test
+ void useCache() throws OPMException {
// Trigger a fetch
CachingEntity entity = entityManager.connect(CachingEntity.class, "mock:cached");
// Trigger second fetch
assertEquals("hallo", entity.getField());
- verify(mock, timeout(1_000).times(1)).read(any());
+ verify(mockDevice, timeout(1_000).times(1)).read(any());
}
@Test
- public void useCache_timeout_refetches() throws PlcConnectionException, OPMException, InterruptedException {
- // Mock
- PlcDriverManager driverManager = new PlcDriverManager();
- PlcMockConnection connection = (PlcMockConnection) driverManager.getConnection("mock:cached");
- MockDevice mock = Mockito.mock(MockDevice.class);
- when(mock.read(any())).thenReturn(Pair.of(PlcResponseCode.OK, new DefaultStringFieldItem("hallo")));
- connection.setDevice(mock);
- PlcEntityManager entityManager = new PlcEntityManager(driverManager);
-
+ void useCache_timeout_refetches() throws OPMException, InterruptedException {
// Trigger a fetch
CachingEntity entity = entityManager.connect(CachingEntity.class, "mock:cached");
Thread.sleep(500);
// Trigger second fetch
assertEquals("hallo", entity.getField());
- verify(mock, timeout(1_000).times(2)).read(any());
+ verify(mockDevice, timeout(1_000).times(2)).read(any());
}
@Test
- public void cache_manyRequests_onlyOneToPlc() throws PlcConnectionException, OPMException {
+ void cache_manyRequests_onlyOneToPlc() throws OPMException {
// Mock
- PlcDriverManager driverManager = new PlcDriverManager();
- PlcMockConnection connection = (PlcMockConnection) driverManager.getConnection("mock:cached");
- MockDevice mock = Mockito.mock(MockDevice.class);
- when(mock.read(any())).thenReturn(Pair.of(PlcResponseCode.OK, new DefaultStringFieldItem("hallo")));
- when(mock.write(any(), any())).thenReturn(PlcResponseCode.OK);
- connection.setDevice(mock);
- PlcEntityManager entityManager = new PlcEntityManager(driverManager);
+ when(mockDevice.write(any(), any()))
+ .thenReturn(PlcResponseCode.OK);
// Trigger a fetch
CachingEntity entity = entityManager.connect(CachingEntity.class, "mock:cached");
@@ -94,7 +95,7 @@ public class ConnectedEntityTest {
IntStream.range(1, 10).forEach(i -> entity.getField());
IntStream.range(1, 10).forEach(i -> entity.dummyMethod());
- verify(mock, timeout(1_000).times(1)).read(any());
+ verify(mockDevice, timeout(1_000).times(1)).read(any());
}
@PlcEntity
diff --git a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityInterceptorTest.java b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityInterceptorTest.java
index d30fc92..7ec939b 100644
--- a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityInterceptorTest.java
+++ b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityInterceptorTest.java
@@ -26,7 +26,6 @@ import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.base.messages.DefaultPlcReadResponse;
import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,41 +36,30 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
-import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class PlcEntityInterceptorTest implements WithAssertions {
private static final Logger LOGGER = LoggerFactory.getLogger(PlcEntityInterceptorTest.class);
- private void runGetPlcResponseWIthException(Answer a) throws InterruptedException, ExecutionException, TimeoutException, OPMException {
- PlcReadRequest request = Mockito.mock(PlcReadRequest.class);
- CompletableFuture future = Mockito.mock(CompletableFuture.class);
- when(future.get(anyLong(), any())).then(a);
- when(request.execute()).thenReturn(future);
-
- PlcEntityInterceptor.getPlcReadResponse(request);
- }
-
@Test
public void getPlcReadResponse_catchesInterruptedException_rethrows() throws InterruptedException {
AtomicBoolean exceptionWasThrown = new AtomicBoolean(false);
// Run in different Thread
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- runGetPlcResponseWIthException(invocation -> {
- throw new InterruptedException();
- });
- } catch (InterruptedException | ExecutionException | TimeoutException e) {
- LOGGER.warn("Fetched exception", e);
- } catch (OPMException e) {
- exceptionWasThrown.set(true);
- }
+ Thread thread = new Thread(() -> {
+ try {
+ runGetPlcResponseWIthException(invocation -> {
+ throw new InterruptedException();
+ });
+ } catch (InterruptedException | ExecutionException | TimeoutException e) {
+ LOGGER.warn("Fetched exception", e);
+ Thread.currentThread().interrupt();
+ } catch (OPMException e) {
+ exceptionWasThrown.set(true);
}
});
thread.start();
@@ -89,9 +77,8 @@ public class PlcEntityInterceptorTest implements WithAssertions {
@Test
public void getPlcReadResponse_timeoutOnGet_rethrows() {
- PlcReadRequest request = Mockito.mock(PlcReadRequest.class);
- CompletableFuture future = new CompletableFuture<>();
- when(request.execute()).thenReturn(future);
+ PlcReadRequest request = mock(PlcReadRequest.class);
+ when(request.execute()).thenReturn(new CompletableFuture<>());
assertThatThrownBy(() -> PlcEntityInterceptor.getPlcReadResponse(request))
.isInstanceOf(OPMException.class);
@@ -100,13 +87,9 @@ public class PlcEntityInterceptorTest implements WithAssertions {
@Test
public void getTyped_notOkResponse_throws() {
DefaultPlcReadResponse response = new DefaultPlcReadResponse(null, Collections.singletonMap("field", Pair.of(PlcResponseCode.NOT_FOUND, null)));
- String message = null;
- try {
- PlcEntityInterceptor.getTyped(Long.class, response, "field");
- } catch (PlcRuntimeException e) {
- message = e.getMessage();
- }
- assertEquals("Unable to read specified field 'field', response code was 'NOT_FOUND'", message);
+ assertThatThrownBy(() -> PlcEntityInterceptor.getTyped(Long.class, response, "field"))
+ .isInstanceOf(PlcRuntimeException.class)
+ .hasMessage("Unable to read specified field 'field', response code was 'NOT_FOUND'");
}
@Test
@@ -114,13 +97,18 @@ public class PlcEntityInterceptorTest implements WithAssertions {
PlcEntityManager entityManager = new PlcEntityManager();
BadEntity entity = entityManager.connect(BadEntity.class, "test:test");
- String message = null;
- try {
- entity.getField1();
- } catch (Exception e) {
- message = e.getMessage();
- }
- assertEquals("Unable to identify field with name 'field1' for call to 'getField1'", message);
+ assertThatThrownBy(entity::getField1)
+ .isInstanceOf(OPMException.class)
+ .hasMessage("Unable to identify field with name 'field1' for call to 'getField1'");
+ }
+
+ private void runGetPlcResponseWIthException(Answer a) throws InterruptedException, ExecutionException, TimeoutException, OPMException {
+ PlcReadRequest request = mock(PlcReadRequest.class);
+ CompletableFuture future = mock(CompletableFuture.class);
+ when(future.get(anyLong(), any())).then(a);
+ when(request.execute()).thenReturn(future);
+
+ PlcEntityInterceptor.getPlcReadResponse(request);
}
@PlcEntity
diff --git a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java
index 940719b..40ee6ef 100644
--- a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java
+++ b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java
@@ -33,7 +33,9 @@ import org.apache.plc4x.java.mock.PlcMockConnection;
import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
+import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -41,6 +43,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
+@ExtendWith(MockitoExtension.class)
public class PlcEntityManagerTest implements WithAssertions {
@Nested