Remove method to bookmark a post by ID.
[Sone.git] / src / test / java / net / pterodactylus / sone / TestUtil.java
1 package net.pterodactylus.sone;
2
3 import java.lang.reflect.Field;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6 import java.lang.reflect.Modifier;
7
8 /**
9  * Utilities for testing.
10  *
11  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
12  */
13 public class TestUtil {
14
15         public static void setFinalField(Object object, String fieldName, Object value) {
16                 try {
17                         Field clientCoreField = object.getClass().getField(fieldName);
18                         clientCoreField.setAccessible(true);
19                         Field modifiersField = Field.class.getDeclaredField("modifiers");
20                         modifiersField.setAccessible(true);
21                         modifiersField.setInt(clientCoreField, clientCoreField.getModifiers() & ~Modifier.FINAL);
22                         clientCoreField.set(object, value);
23                 } catch (NoSuchFieldException e) {
24                         throw new RuntimeException(e);
25                 } catch (IllegalAccessException e) {
26                         throw new RuntimeException(e);
27                 }
28         }
29
30         public static <T> T getPrivateField(Object object, String fieldName) {
31                 try {
32                         Field field = object.getClass().getDeclaredField(fieldName);
33                         field.setAccessible(true);
34                         return (T) field.get(object);
35                 } catch (NoSuchFieldException e) {
36                         throw new RuntimeException(e);
37                 } catch (IllegalAccessException e) {
38                         throw new RuntimeException(e);
39                 }
40         }
41
42         public static <T> T callPrivateMethod(Object object, String methodName) {
43                 try {
44                         Method method = object.getClass().getDeclaredMethod(methodName, new Class[0]);
45                         method.setAccessible(true);
46                         return (T) method.invoke(object);
47                 } catch (NoSuchMethodException e) {
48                         throw new RuntimeException(e);
49                 } catch (InvocationTargetException e) {
50                         throw new RuntimeException(e);
51                 } catch (IllegalAccessException e) {
52                         throw new RuntimeException(e);
53                 }
54         }
55
56 }