cdb7404cf934539461441796acdb0f13a0fd7c2d
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / Whitebox.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (c) 2007 Mockito contributors
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.provider;
25
26 import java.lang.reflect.Field;
27
28 public class  Whitebox {
29     public static Object getInternalState(Object target, String field) {
30         Class<?> c = target.getClass();
31         try {
32             Field f = getFieldFromHierarchy(c, field);
33             f.setAccessible(true);
34             return f.get(target);
35         } catch (Exception e) {
36             throw new RuntimeException("Unable to get internal state on a private field. Please report to mockito mailing list.", e);
37         }
38     }
39
40     public static void  setInternalState(Object target, String field, Object value) {
41         Class<?> c = target.getClass();
42         try {
43             Field f = getFieldFromHierarchy(c, field);
44             f.setAccessible(true);
45             f.set(target, value);
46         } catch (Exception e) {
47             throw new RuntimeException("Unable to set internal state on a private field. Please report to mockito mailing list.", e);
48         }
49     }
50
51     private static Field  getFieldFromHierarchy(Class<?> clazz, String field) {
52         Field f = getField(clazz, field);
53         while (f == null && clazz != Object.class) {
54             clazz = clazz.getSuperclass();
55             f = getField(clazz, field);
56         }
57         if (f == null) {
58             throw new RuntimeException(
59                     "You want me to get this field: '" + field +
60                     "' on this class: '" + clazz.getSimpleName() + 
61                     "' but this field is not declared withing hierarchy of this class!");
62         }
63         return f;
64     }
65
66     private static Field  getField(Class<?> clazz, String field) {
67         try {
68             return clazz.getDeclaredField(field);
69         } catch (NoSuchFieldException e) {
70             return null;
71         }
72     }
73 }
74