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 java.lang.System.err;
23 import static java.lang.System.out;
24 import static org.junit.Assert.assertTrue;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.lang.reflect.Type;
30 public class ReflectionHarness {
31 private static final String fmt = "%24s: %s%n";
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;
42 public static boolean isSetter(Method method){
43 if(!method.getName().startsWith("set")) return false;
44 if(method.getParameterTypes().length != 1) return false;
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 );
56 Object t = c.newInstance();
59 m.setAccessible(true);
60 Object o = m.invoke( t );
62 if( retType.equals( Class.forName( "java.lang.String" ) ) ) {
64 out.format( fmt, "testGetter: expected null, got ", (o != null)?o:"null" );
67 out.format( fmt, "testGetter: expected val, got ", (o != null)?o:"null" );
68 assert( o.equals( val ) );
71 out.format( fmt, "testGetter: " + m.getName() + " untested retType", retType );
75 } catch (InvocationTargetException e ) {
76 Throwable cause = e.getCause();
77 err.format( "%s() returned %x%n", m.getName(), cause.getMessage() );
80 } catch (ClassNotFoundException nfe ){
81 nfe.printStackTrace();
82 } catch (IllegalArgumentException ae ) {
84 } catch (InstantiationException ie ) {
86 } catch (IllegalAccessException iae ) {
87 iae.printStackTrace();
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 );
99 Object t = c.newInstance();
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
109 Object o = m.invoke( t, b );
111 out.format( fmt, "testSetter: " + m.getName() + " untested pType", pType[0] );
114 } catch (InvocationTargetException e ) {
115 Throwable cause = e.getCause();
116 err.format( "%s() returned %x%n", m.getName(), cause.getMessage() );
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();
130 public void reflect(String... args) {
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)) {
139 //out.format("%s%n", m.toGenericString());
141 //out.format(fmt, "ReturnType", m.getReturnType());
142 //out.format(fmt, "GenericReturnType", m.getGenericReturnType());
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]);
150 if ( isGetter( m ) ) {
151 testGetter( c, m, pType , args[2]);
152 } else if ( isSetter( m ) ) {
153 testSetter( c, m, pType );
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]);
164 // production code should handle these exceptions more gracefully
165 } catch (ClassNotFoundException x) {