Move reflection-based methods to their own class.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Aug 2014 00:17:13 +0000 (02:17 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Aug 2014 00:18:29 +0000 (02:18 +0200)
src/test/java/net/pterodactylus/sone/TestUtil.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java

diff --git a/src/test/java/net/pterodactylus/sone/TestUtil.java b/src/test/java/net/pterodactylus/sone/TestUtil.java
new file mode 100644 (file)
index 0000000..d1a4d04
--- /dev/null
@@ -0,0 +1,40 @@
+package net.pterodactylus.sone;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+/**
+ * Utilities for testing.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class TestUtil {
+
+       public static void setFinalField(Object object, String fieldName, Object value) {
+               try {
+                       Field clientCoreField = object.getClass().getField(fieldName);
+                       clientCoreField.setAccessible(true);
+                       Field modifiersField = Field.class.getDeclaredField("modifiers");
+                       modifiersField.setAccessible(true);
+                       modifiersField.setInt(clientCoreField, clientCoreField.getModifiers() & ~Modifier.FINAL);
+                       clientCoreField.set(object, value);
+               } catch (NoSuchFieldException e) {
+                       throw new RuntimeException(e);
+               } catch (IllegalAccessException e) {
+                       throw new RuntimeException(e);
+               }
+       }
+
+       public static <T> T getPrivateField(Object object, String fieldName) {
+               try {
+                       Field field = object.getClass().getDeclaredField(fieldName);
+                       field.setAccessible(true);
+                       return (T) field.get(object);
+               } catch (NoSuchFieldException e) {
+                       throw new RuntimeException(e);
+               } catch (IllegalAccessException e) {
+                       throw new RuntimeException(e);
+               }
+       }
+
+}
index 44820d4..9e8852b 100644 (file)
@@ -7,6 +7,7 @@ import static java.lang.System.currentTimeMillis;
 import static java.util.concurrent.TimeUnit.DAYS;
 import static java.util.concurrent.TimeUnit.SECONDS;
 import static net.pterodactylus.sone.Matchers.delivers;
+import static net.pterodactylus.sone.TestUtil.setFinalField;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.notNullValue;
@@ -25,12 +26,11 @@ import static org.mockito.Mockito.when;
 import static org.mockito.Mockito.withSettings;
 
 import java.io.IOException;
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
 import java.net.MalformedURLException;
 import java.util.HashMap;
 import java.util.concurrent.CountDownLatch;
 
+import net.pterodactylus.sone.TestUtil;
 import net.pterodactylus.sone.core.FreenetInterface.Callback;
 import net.pterodactylus.sone.core.FreenetInterface.Fetched;
 import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
@@ -147,21 +147,6 @@ public class FreenetInterfaceTest {
                return new FetchResult(clientMetadata, bucket);
        }
 
-       private void setFinalField(Object object, String fieldName, Object value) {
-               try {
-                       Field clientCoreField = object.getClass().getField(fieldName);
-                       clientCoreField.setAccessible(true);
-                       Field modifiersField = Field.class.getDeclaredField("modifiers");
-                       modifiersField.setAccessible(true);
-                       modifiersField.setInt(clientCoreField, clientCoreField.getModifiers() & ~Modifier.FINAL);
-                       clientCoreField.set(object, value);
-               } catch (NoSuchFieldException e) {
-                       throw new RuntimeException(e);
-               } catch (IllegalAccessException e) {
-                       throw new RuntimeException(e);
-               }
-       }
-
        @Test
        public void insertingAnImage() throws SoneException, InsertException, IOException {
                TemporaryImage temporaryImage = new TemporaryImage("image-id");
@@ -177,22 +162,10 @@ public class FreenetInterfaceTest {
                when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq(false), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenReturn(clientPutter);
                freenetInterface.insertImage(temporaryImage, image, insertToken);
                assertThat(insertBlockCaptor.getValue().getData().getInputStream(), delivers(new byte[] { 1, 2, 3, 4 }));
-               assertThat(this.<ClientPutter>getPrivateField(insertToken, "clientPutter"), is(clientPutter));
+               assertThat(TestUtil.<ClientPutter>getPrivateField(insertToken, "clientPutter"), is(clientPutter));
                verify(eventBus).post(any(ImageInsertStartedEvent.class));
        }
 
-       private <T> T getPrivateField(Object object, String fieldName) {
-               try {
-                       Field field = object.getClass().getDeclaredField(fieldName);
-                       field.setAccessible(true);
-                       return (T) field.get(object);
-               } catch (NoSuchFieldException e) {
-                       throw new RuntimeException(e);
-               } catch (IllegalAccessException e) {
-                       throw new RuntimeException(e);
-               }
-       }
-
        @Test(expected = SoneInsertException.class)
        public void insertExceptionCausesASoneException() throws InsertException, SoneException, IOException {
                TemporaryImage temporaryImage = new TemporaryImage("image-id");