e065d1e7a0af1fba7429416fe9bf85c171bfd7fc
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.provider;
26
27 import java.lang.reflect.Field;
28
29 public class  Whitebox {
30     public static Object getInternalState(Object target, String field) {
31         Class<?> c = target.getClass();
32         try {
33             Field f = getFieldFromHierarchy(c, field);
34             f.setAccessible(true);
35             return f.get(target);
36         } catch (Exception e) {
37             throw new RuntimeException("Unable to get internal state on a private field. Please report to mockito mailing list.", e);
38         }
39     }
40
41     public static void  setInternalState(Object target, String field, Object value) {
42         Class<?> c = target.getClass();
43         try {
44             Field f = getFieldFromHierarchy(c, field);
45             f.setAccessible(true);
46             f.set(target, value);
47         } catch (Exception e) {
48             throw new RuntimeException("Unable to set internal state on a private field. Please report to mockito mailing list.", e);
49         }
50     }
51
52     private static Field  getFieldFromHierarchy(Class<?> clazz, String field) {
53         Field f = getField(clazz, field);
54         while (f == null && clazz != Object.class) {
55             clazz = clazz.getSuperclass();
56             f = getField(clazz, field);
57         }
58         if (f == null) {
59             throw new RuntimeException(
60                     "You want me to get this field: '" + field +
61                     "' on this class: '" + clazz.getSimpleName() + 
62                     "' but this field is not declared withing hierarchy of this class!");
63         }
64         return f;
65     }
66
67     private static Field  getField(Class<?> clazz, String field) {
68         try {
69             return clazz.getDeclaredField(field);
70         } catch (NoSuchFieldException e) {
71             return null;
72         }
73     }
74 }
75