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