From 8677d84408d2495fe6c3e114e64accd28d6576ec Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 1 Aug 2014 02:17:13 +0200 Subject: [PATCH] Move reflection-based methods to their own class. --- src/test/java/net/pterodactylus/sone/TestUtil.java | 40 ++++++++++++++++++++++ .../sone/core/FreenetInterfaceTest.java | 33 ++---------------- 2 files changed, 43 insertions(+), 30 deletions(-) create mode 100644 src/test/java/net/pterodactylus/sone/TestUtil.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 index 0000000..d1a4d04 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/TestUtil.java @@ -0,0 +1,40 @@ +package net.pterodactylus.sone; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +/** + * Utilities for testing. + * + * @author David ‘Bombe’ Roden + */ +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 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); + } + } + +} diff --git a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java index 44820d4..9e8852b 100644 --- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java @@ -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.getPrivateField(insertToken, "clientPutter"), is(clientPutter)); + assertThat(TestUtil.getPrivateField(insertToken, "clientPutter"), is(clientPutter)); verify(eventBus).post(any(ImageInsertStartedEvent.class)); } - private 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"); -- 2.7.4