[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / testframework / ReflectionHarness.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dmaap.dbcapi.testframework;
21
22 import static java.lang.System.err;
23 import static java.lang.System.out;
24 import static org.junit.Assert.assertTrue;
25
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.lang.reflect.Type;
29
30 public class ReflectionHarness {
31         private static final String  fmt = "%24s: %s%n";
32
33
34         // following 2 functions taken from: http://tutorials.jenkov.com/java-reflection/getters-setters.html
35         public static boolean isGetter(Method method){
36           if(!method.getName().startsWith("get"))      return false;
37           if(method.getParameterTypes().length != 0)   return false;  
38           if(void.class.equals(method.getReturnType())) return false;
39           return true;
40         }
41
42         public static boolean isSetter(Method method){
43           if(!method.getName().startsWith("set")) return false;
44           if(method.getParameterTypes().length != 1) return false;
45           return true;
46         }
47
48         private void testGetter( Class<?> c, Method m, Class<?>[] pType, String val ) {
49                 out.format( fmt, "testGetter: Method Name", m.getName() );
50                 Class retType = m.getReturnType();
51                 out.format( fmt, "testGetter: Return Type ", retType );
52                 out.format( fmt, "testGetter: val ", (val != null)?val:"null" );
53                 assertTrue( pType.length == 0 );
54
55                 try {
56                         Object t = c.newInstance();
57                         
58                                 try {
59                                         m.setAccessible(true);
60                                         Object o = m.invoke( t );
61                                         
62                                         if( retType.equals( Class.forName( "java.lang.String" ) ) ) {
63                                                 if ( val == null ) {
64                                                         out.format( fmt, "testGetter: expected null, got  ", (o != null)?o:"null" );
65                                                         assert( o == null );
66                                                 } else {
67                                                         out.format( fmt, "testGetter: expected val, got  ", (o != null)?o:"null" );
68                                                         assert( o.equals( val ) );
69                                                 }
70                                         } else {
71                                                 out.format( fmt, "testGetter: " + m.getName() + " untested retType", retType );
72
73                                         }
74                         
75                                 } catch (InvocationTargetException e ) {
76                                         Throwable cause = e.getCause();
77                                         err.format( "%s() returned %x%n", m.getName(), cause.getMessage() );
78                                 }
79                                         
80                 } catch (ClassNotFoundException nfe ){
81                 nfe.printStackTrace();
82                 } catch (IllegalArgumentException ae ) {
83                         ae.printStackTrace();
84                 } catch (InstantiationException ie ) {
85                         ie.printStackTrace();
86                 } catch (IllegalAccessException iae ) {
87                         iae.printStackTrace();
88                 }
89         }
90
91         private void testSetter( Class<?> c, Method m, Class<?>[] pType ) {
92                 //out.format( fmt, "testSetter: Method Name", m.getName() );
93                 Class retType = m.getReturnType();
94                 //out.format( fmt, "testSetter: Return Type ", retType );
95                 //out.format( fmt, "testSetter: val ", (val != null)?val:"null" );
96                 assertTrue( pType.length == 1 );
97
98                 try {
99                         Object t = c.newInstance();
100                         
101                                 try {
102                                         m.setAccessible(true);
103                                         //out.format( fmt, "testSetter: " + m.getName() + " to try pType", pType[0] );
104                                         if ( pType[0].equals( Class.forName( "java.lang.String" ) ) ) {
105                                                 String val = "Validate123";
106                                                 Object o = m.invoke( t, val );
107                                         } else if ( pType[0].equals( boolean.class ) ) { // note primitive class notation 
108                                                 boolean b = true;
109                                                 Object o = m.invoke( t, b );
110                                         } else {
111                                                 out.format( fmt, "testSetter: " + m.getName() + " untested pType", pType[0] );
112                                         }
113                         
114                                 } catch (InvocationTargetException e ) {
115                                         Throwable cause = e.getCause();
116                                         err.format( "%s() returned %x%n", m.getName(), cause.getMessage() );
117                                 }
118                                         
119                 } catch (ClassNotFoundException nfe ){
120                 nfe.printStackTrace();
121                 } catch (IllegalArgumentException ae ) {
122                         ae.printStackTrace();
123                 } catch (InstantiationException ie ) {
124                         ie.printStackTrace();
125                 } catch (IllegalAccessException iae ) {
126                         iae.printStackTrace();
127                 }
128         }
129
130     public void reflect(String... args) {
131                 try {
132             Class<?> c = Class.forName(args[0]);
133             Method[] allMethods = c.getDeclaredMethods();
134             String methodPrefix = args[1];
135             for (Method m : allMethods) {
136                         if (!m.getName().startsWith(methodPrefix)) {
137                         continue;
138                         }
139                         //out.format("%s%n", m.toGenericString());
140
141                         //out.format(fmt, "ReturnType", m.getReturnType());
142                         //out.format(fmt, "GenericReturnType", m.getGenericReturnType());
143
144                         Class<?>[] pType  = m.getParameterTypes();
145                         Type[] gpType = m.getGenericParameterTypes();
146                         for (int i = 0; i < pType.length; i++) {
147                         //out.format(fmt,"ParameterType", pType[i]);
148                         //out.format(fmt,"GenericParameterType", gpType[i]);
149                         }
150                         if ( isGetter( m ) ) {
151                                 testGetter( c, m, pType , args[2]);
152                         } else if ( isSetter( m ) ) {
153                                 testSetter( c, m, pType );
154                         }
155
156                         Class<?>[] xType  = m.getExceptionTypes();
157                         Type[] gxType = m.getGenericExceptionTypes();
158                         for (int i = 0; i < xType.length; i++) {
159                         //out.format(fmt,"ExceptionType", xType[i]);
160                         //out.format(fmt,"GenericExceptionType", gxType[i]);
161                         }
162             }
163
164         // production code should handle these exceptions more gracefully
165                 } catch (ClassNotFoundException x) {
166                 x.printStackTrace();
167                 }
168     }
169 }