2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dmaap.dbcapi.testframework;
22 import static org.junit.Assert.*;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.lang.reflect.Type;
28 import static java.lang.System.out;
29 import static java.lang.System.err;
31 public class ReflectionHarness {
32 private static final String fmt = "%24s: %s%n";
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;
43 public static boolean isSetter(Method method){
44 if(!method.getName().startsWith("set")) return false;
45 if(method.getParameterTypes().length != 1) return false;
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 );
57 Object t = c.newInstance();
60 m.setAccessible(true);
61 Object o = m.invoke( t );
63 if( retType.equals( Class.forName( "java.lang.String" ) ) ) {
65 out.format( fmt, "testGetter: expected null, got ", (o != null)?o:"null" );
68 out.format( fmt, "testGetter: expected val, got ", (o != null)?o:"null" );
69 assert( o.equals( val ) );
72 out.format( fmt, "testGetter: " + m.getName() + " untested retType", retType );
76 } catch (InvocationTargetException e ) {
77 Throwable cause = e.getCause();
78 err.format( "%s() returned %x%n", m.getName(), cause.getMessage() );
81 } catch (ClassNotFoundException nfe ){
82 nfe.printStackTrace();
83 } catch (IllegalArgumentException ae ) {
85 } catch (InstantiationException ie ) {
87 } catch (IllegalAccessException iae ) {
88 iae.printStackTrace();
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 );
100 Object t = c.newInstance();
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
110 Object o = m.invoke( t, b );
112 out.format( fmt, "testSetter: " + m.getName() + " untested pType", pType[0] );
115 } catch (InvocationTargetException e ) {
116 Throwable cause = e.getCause();
117 err.format( "%s() returned %x%n", m.getName(), cause.getMessage() );
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();
131 public void reflect(String... args) {
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)) {
140 //out.format("%s%n", m.toGenericString());
142 //out.format(fmt, "ReturnType", m.getReturnType());
143 //out.format(fmt, "GenericReturnType", m.getGenericReturnType());
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]);
151 if ( isGetter( m ) ) {
152 testGetter( c, m, pType , args[2]);
153 } else if ( isSetter( m ) ) {
154 testSetter( c, m, pType );
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]);
165 // production code should handle these exceptions more gracefully
166 } catch (ClassNotFoundException x) {