Release patch 2.0.4
[dmaap/dbcapi.git] / 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 org.junit.Assert.*;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.lang.reflect.Type;
27
28 import static java.lang.System.out;
29 import static java.lang.System.err;
30
31 public class ReflectionHarness {
32         private static final String  fmt = "%24s: %s%n";
33
34
35         // following 2 functions taken from: http://tutorials.jenkov.com/java-reflection/getters-setters.html
36         public static boolean isGetter(Method method){
37           if(!method.getName().startsWith("get"))      return false;
38           if(method.getParameterTypes().length != 0)   return false;  
39           if(void.class.equals(method.getReturnType())) return false;
40           return true;
41         }
42
43         public static boolean isSetter(Method method){
44           if(!method.getName().startsWith("set")) return false;
45           if(method.getParameterTypes().length != 1) return false;
46           return true;
47         }
48
49         private void testGetter( Class<?> c, Method m, Class<?>[] pType, String val ) {
50                 out.format( fmt, "testGetter: Method Name", m.getName() );
51                 Class retType = m.getReturnType();
52                 out.format( fmt, "testGetter: Return Type ", retType );
53                 out.format( fmt, "testGetter: val ", (val != null)?val:"null" );
54                 assertTrue( pType.length == 0 );
55
56                 try {
57                         Object t = c.newInstance();
58                         
59                                 try {
60                                         m.setAccessible(true);
61                                         Object o = m.invoke( t );
62                                         
63                                         if( retType.equals( Class.forName( "java.lang.String" ) ) ) {
64                                                 if ( val == null ) {
65                                                         out.format( fmt, "testGetter: expected null, got  ", (o != null)?o:"null" );
66                                                         assert( o == null );
67                                                 } else {
68                                                         out.format( fmt, "testGetter: expected val, got  ", (o != null)?o:"null" );
69                                                         assert( o.equals( val ) );
70                                                 }
71                                         } else {
72                                                 out.format( fmt, "testGetter: " + m.getName() + " untested retType", retType );
73
74                                         }
75                         
76                                 } catch (InvocationTargetException e ) {
77                                         Throwable cause = e.getCause();
78                                         err.format( "%s() returned %x%n", m.getName(), cause.getMessage() );
79                                 }
80                                         
81                 } catch (ClassNotFoundException nfe ){
82                 nfe.printStackTrace();
83                 } catch (IllegalArgumentException ae ) {
84                         ae.printStackTrace();
85                 } catch (InstantiationException ie ) {
86                         ie.printStackTrace();
87                 } catch (IllegalAccessException iae ) {
88                         iae.printStackTrace();
89                 }
90         }
91
92         private void testSetter( Class<?> c, Method m, Class<?>[] pType ) {
93                 //out.format( fmt, "testSetter: Method Name", m.getName() );
94                 Class retType = m.getReturnType();
95                 //out.format( fmt, "testSetter: Return Type ", retType );
96                 //out.format( fmt, "testSetter: val ", (val != null)?val:"null" );
97                 assertTrue( pType.length == 1 );
98
99                 try {
100                         Object t = c.newInstance();
101                         
102                                 try {
103                                         m.setAccessible(true);
104                                         //out.format( fmt, "testSetter: " + m.getName() + " to try pType", pType[0] );
105                                         if ( pType[0].equals( Class.forName( "java.lang.String" ) ) ) {
106                                                 String val = "Validate123";
107                                                 Object o = m.invoke( t, val );
108                                         } else if ( pType[0].equals( boolean.class ) ) { // note primitive class notation 
109                                                 boolean b = true;
110                                                 Object o = m.invoke( t, b );
111                                         } else {
112                                                 out.format( fmt, "testSetter: " + m.getName() + " untested pType", pType[0] );
113                                         }
114                         
115                                 } catch (InvocationTargetException e ) {
116                                         Throwable cause = e.getCause();
117                                         err.format( "%s() returned %x%n", m.getName(), cause.getMessage() );
118                                 }
119                                         
120                 } catch (ClassNotFoundException nfe ){
121                 nfe.printStackTrace();
122                 } catch (IllegalArgumentException ae ) {
123                         ae.printStackTrace();
124                 } catch (InstantiationException ie ) {
125                         ie.printStackTrace();
126                 } catch (IllegalAccessException iae ) {
127                         iae.printStackTrace();
128                 }
129         }
130
131     public void reflect(String... args) {
132                 try {
133             Class<?> c = Class.forName(args[0]);
134             Method[] allMethods = c.getDeclaredMethods();
135             String methodPrefix = args[1];
136             for (Method m : allMethods) {
137                         if (!m.getName().startsWith(methodPrefix)) {
138                         continue;
139                         }
140                         //out.format("%s%n", m.toGenericString());
141
142                         //out.format(fmt, "ReturnType", m.getReturnType());
143                         //out.format(fmt, "GenericReturnType", m.getGenericReturnType());
144
145                         Class<?>[] pType  = m.getParameterTypes();
146                         Type[] gpType = m.getGenericParameterTypes();
147                         for (int i = 0; i < pType.length; i++) {
148                         //out.format(fmt,"ParameterType", pType[i]);
149                         //out.format(fmt,"GenericParameterType", gpType[i]);
150                         }
151                         if ( isGetter( m ) ) {
152                                 testGetter( c, m, pType , args[2]);
153                         } else if ( isSetter( m ) ) {
154                                 testSetter( c, m, pType );
155                         }
156
157                         Class<?>[] xType  = m.getExceptionTypes();
158                         Type[] gxType = m.getGenericExceptionTypes();
159                         for (int i = 0; i < xType.length; i++) {
160                         //out.format(fmt,"ExceptionType", xType[i]);
161                         //out.format(fmt,"GenericExceptionType", gxType[i]);
162                         }
163             }
164
165         // production code should handle these exceptions more gracefully
166                 } catch (ClassNotFoundException x) {
167                 x.printStackTrace();
168                 }
169     }
170 }