Cleanup testutils 33/61033/5
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 2 Aug 2017 10:52:11 +0000 (12:52 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 16 Aug 2017 16:20:53 +0000 (16:20 +0000)
Cleans up sonar warnings and re-organizes code a bit.

Change-Id: I99761f484cd992d994f1bca7c681190a055793cd
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/testutils/src/main/java/org/opendaylight/yangtools/testutils/mockito/CallsRealOrExceptionAnswer.java
common/testutils/src/main/java/org/opendaylight/yangtools/testutils/mockito/MethodExtensions.java
common/testutils/src/main/java/org/opendaylight/yangtools/testutils/mockito/MoreAnswers.java
common/testutils/src/main/java/org/opendaylight/yangtools/testutils/mockito/ThrowsMethodExceptionAnswer.java
common/testutils/src/main/java/org/opendaylight/yangtools/testutils/mockito/UnstubbedMethodException.java
common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MikitoTest.java
common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoExampleTutorialTest.java

index 9426fd3f061f7f49a14fa64a6e07d7e75ad8eac8..fdf6c5a2a29772c23da8f78aa6864044e6675c98 100644 (file)
@@ -115,18 +115,21 @@ import org.mockito.stubbing.Answer;
 @Beta
 public class CallsRealOrExceptionAnswer implements Answer<Object>, Serializable {
     private static final long serialVersionUID = -3730024662402964588L;
+    static final CallsRealOrExceptionAnswer INSTANCE = new CallsRealOrExceptionAnswer();
+
+    private CallsRealOrExceptionAnswer() {
 
-    /**
-     * Use {@link MoreAnswers} to obtain an instance.
-     */
-    CallsRealOrExceptionAnswer() {
     }
 
     @Override
-    public Object answer(InvocationOnMock invocation) throws Throwable {
+    public Object answer(final InvocationOnMock invocation) throws Throwable {
         if (Modifier.isAbstract(invocation.getMethod().getModifiers())) {
             throw new UnstubbedMethodException(invocation.getMethod(), invocation.getMock());
         }
         return invocation.callRealMethod();
     }
+
+    Object readResolve() {
+        return INSTANCE;
+    }
 }
index c81bcc0a78dcc1f04b38481fb590832810861a55..4baf4a4203818fa3c3e37b6ed1abdd8ed2cb5746 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.yangtools.testutils.mockito;
 import java.lang.reflect.Method;
 import java.lang.reflect.Parameter;
 import java.lang.reflect.Type;
+import java.util.regex.Pattern;
+import javax.annotation.RegEx;
 
 /**
  * Nicer shorter toString() for {@link Method} than it's default.
@@ -19,22 +21,27 @@ import java.lang.reflect.Type;
  * @author Michael Vorburger
  */
 public final class MethodExtensions {
+    @RegEx
+    private static final String PARAM_PATTERN_STR = "\\[\\]$";
+    private static final Pattern PARAM_PATTERN = Pattern.compile(PARAM_PATTERN_STR);
 
     private MethodExtensions() {
     }
 
-    public static String toString(Method method) {
-        StringBuilder sb = new StringBuilder();
+    public static String toString(final Method method) {
+        final StringBuilder sb = new StringBuilder();
         sb.append(method.getName());
 
         // copy/paste from java.lang.reflect.Executable.sharedToGenericString(int, boolean)
         sb.append('(');
-        Type[] params = method.getGenericParameterTypes();
-        Parameter[] parameters = method.getParameters(); // NEW
+        final Type[] params = method.getGenericParameterTypes();
+        // NEW
+        final Parameter[] parameters = method.getParameters();
         for (int j = 0; j < params.length; j++) {
             String param = params[j].getTypeName();
-            if (method.isVarArgs() && j == params.length - 1) { // replace T[] with T...
-                param = param.replaceFirst("\\[\\]$", "...");
+            if (method.isVarArgs() && j == params.length - 1) {
+                // replace T[] with T...
+                param = PARAM_PATTERN.matcher(param).replaceFirst("...");
             }
             sb.append(param);
             // NEW
@@ -43,8 +50,9 @@ public final class MethodExtensions {
                 sb.append(parameters[j].getName());
             }
             // NEW END
-            if (j < (params.length - 1)) {
-                sb.append(", "); // NEW ", " instead of ','
+            if (j < params.length - 1) {
+                // NEW ", " instead of ','
+                sb.append(", ");
             }
         }
         sb.append(')');
index ce0089bf371b54bfa3e09f9a9c6d7ba1c0eb81d9..296d13e601547ef614b62e70e357ba5205d1738c 100644 (file)
@@ -21,13 +21,6 @@ import org.mockito.stubbing.Answer;
  */
 @SuppressWarnings("unchecked")
 public final class MoreAnswers {
-
-    private static final CallsRealOrExceptionAnswer REAL_OR_EXCEPTION
-        = new CallsRealOrExceptionAnswer();
-
-    private static final ThrowsMethodExceptionAnswer EXCEPTION
-        = new ThrowsMethodExceptionAnswer();
-
     private MoreAnswers() {
     }
 
@@ -37,7 +30,7 @@ public final class MoreAnswers {
      * @see CallsRealOrExceptionAnswer
      */
     public static <T> Answer<T> realOrException() {
-        return (Answer<T>) REAL_OR_EXCEPTION;
+        return (Answer<T>) CallsRealOrExceptionAnswer.INSTANCE;
     }
 
     /**
@@ -46,7 +39,6 @@ public final class MoreAnswers {
      * @see ThrowsMethodExceptionAnswer
      */
     public static <T> Answer<T> exception() {
-        return (Answer<T>) EXCEPTION;
+        return (Answer<T>) ThrowsMethodExceptionAnswer.INSTANCE;
     }
-
 }
index f5af1bfc4270bc148980b4052e15048ce09e947d..6750c304bb8403f565a74d890a1ed560e7ac6e64 100644 (file)
@@ -38,16 +38,18 @@ import org.mockito.stubbing.Answer;
 @Beta
 public class ThrowsMethodExceptionAnswer implements Answer<Object>, Serializable {
     private static final long serialVersionUID = -7316574192253912318L;
+    static final ThrowsMethodExceptionAnswer INSTANCE = new ThrowsMethodExceptionAnswer();
+
+    private ThrowsMethodExceptionAnswer() {
 
-    /**
-     * Use {@link MoreAnswers} to obtain an instance.
-     */
-    ThrowsMethodExceptionAnswer() {
     }
 
     @Override
-    public Void answer(InvocationOnMock invocation) throws Throwable {
+    public Void answer(final InvocationOnMock invocation) throws Throwable {
         throw new UnstubbedMethodException(invocation.getMethod());
     }
 
+    Object readResolve() {
+        return INSTANCE;
+    }
 }
index 5e84a020365f9f86d972fbd40f545fb5e6ab0304..f20b079e12ad0016e68e2595022c6f8a6b9dcd9f 100644 (file)
@@ -18,11 +18,11 @@ import org.mockito.internal.util.MockUtil;
 public class UnstubbedMethodException extends UnsupportedOperationException {
     private static final long serialVersionUID = 1L;
 
-    public UnstubbedMethodException(Method method) {
+    public UnstubbedMethodException(final Method method) {
         super(MethodExtensions.toString(method) + " is not stubbed in mock of " + method.getDeclaringClass().getName());
     }
 
-    public UnstubbedMethodException(Method method, Object mockAbstractFakeObject) {
+    public UnstubbedMethodException(final Method method, final Object mockAbstractFakeObject) {
         super(MethodExtensions.toString(method) + " is not implemented in "
                 + new MockUtil().getMockName(mockAbstractFakeObject).toString());
     }
index 7708a8779a482c29d8c3a3c0a06d665c6155ba38..346dd3c1d4abcb320147690ce45a18ba46e39287 100644 (file)
@@ -10,11 +10,11 @@ package org.opendaylight.yangtools.testutils.mockito.tests;
 import static com.google.common.truth.Truth.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
 
 import java.io.File;
 import org.junit.Test;
-import org.mockito.Mockito;
 import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
 
 /**
@@ -40,14 +40,14 @@ public class MikitoTest {
 
     @Test
     public void usingMikitoToCallStubbedMethod() {
-        SomeService service = Mockito.mock(MockSomeService.class, realOrException());
+        SomeService service = mock(MockSomeService.class, realOrException());
         assertEquals(123, service.foobar(new File("hello.txt")));
         assertEquals(0, service.foobar(new File("belo.txt")));
     }
 
     @Test
     public void usingMikitoToCallUnstubbedMethodAndExpectException() {
-        MockSomeService service = Mockito.mock(MockSomeService.class, realOrException());
+        MockSomeService service = mock(MockSomeService.class, realOrException());
         try {
             service.foo();
             fail();
@@ -58,13 +58,8 @@ public class MikitoTest {
 
     abstract static class MockSomeService implements SomeService {
         @Override
-        public int foobar(File file) {
-            if (file.getName().equals("hello.txt")) {
-                return 123;
-            } else {
-                return 0;
-            }
+        public int foobar(final File file) {
+            return "hello.txt".equals(file.getName()) ? 123 : 0;
         }
     }
-
 }
index 1679d7ee1afdc0efc6647f862f931313713a063a..f8c78be12b9878b80b7c8fcb1ac677a39e4a7537 100644 (file)
@@ -60,13 +60,10 @@ public class MockitoExampleTutorialTest {
     public void usingMockitoToStubComplexCase() {
         SomeService service = mock(SomeService.class);
         when(service.foobar(any())).thenAnswer(invocation -> {
-            // Urgh! This is ugly.. (Mockito 2.0 may be better, see http://site.mockito.org/mockito/docs/current/org/mockito/ArgumentMatcher.html)
+            // Urgh! This is ugly.. (Mockito 2.0 may be better,
+            // see http://site.mockito.org/mockito/docs/current/org/mockito/ArgumentMatcher.html)
             File file = invocation.getArgumentAt(0, File.class);
-            if (file.getName().equals("hello.txt")) {
-                return 123;
-            } else {
-                return 0;
-            }
+            return "hello.txt".equals(file.getName()) ? 123 : 0;
         });
         assertEquals(0, service.foobar(new File("belo.txt")));
     }
@@ -95,13 +92,10 @@ public class MockitoExampleTutorialTest {
     public void usingMockitoToStubComplexCaseAndExceptionIfNotStubbed() {
         SomeService service = mock(SomeService.class, exception());
         doAnswer(invocation -> {
-            // Urgh! This is ugly. Mockito may be better, see http://site.mockito.org/mockito/docs/current/org/mockito/ArgumentMatcher.html
+            // Urgh! This is ugly. Mockito may be better,
+            // see http://site.mockito.org/mockito/docs/current/org/mockito/ArgumentMatcher.html
             File file = (File) invocation.getArguments()[0];
-            if (file.getName().equals("hello.txt")) {
-                return 123;
-            } else {
-                return 0;
-            }
+            return "hello.txt".equals(file.getName()) ? 123 : 0;
         }).when(service).foobar(any());
         assertEquals(123, service.foobar(new File("hello.txt")));
         assertEquals(0, service.foobar(new File("belo.txt")));