Move classes for tests to test package
[Sone.git] / src / test / java / net / pterodactylus / sone / test / TestUtil.java
diff --git a/src/test/java/net/pterodactylus/sone/test/TestUtil.java b/src/test/java/net/pterodactylus/sone/test/TestUtil.java
new file mode 100644 (file)
index 0000000..8b3160f
--- /dev/null
@@ -0,0 +1,56 @@
+package net.pterodactylus.sone.test;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+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);
+               }
+       }
+
+       public static <T> T callPrivateMethod(Object object, String methodName) {
+               try {
+                       Method method = object.getClass().getDeclaredMethod(methodName, new Class[0]);
+                       method.setAccessible(true);
+                       return (T) method.invoke(object);
+               } catch (NoSuchMethodException e) {
+                       throw new RuntimeException(e);
+               } catch (InvocationTargetException e) {
+                       throw new RuntimeException(e);
+               } catch (IllegalAccessException e) {
+                       throw new RuntimeException(e);
+               }
+       }
+
+}