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.model;
22 import static org.junit.Assert.*;
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.lang.reflect.Type;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
33 import static java.lang.System.out;
34 import static java.lang.System.err;
36 public class ReflectionHarness {
37 private static final String fmt = "%24s: %s%n";
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;
48 public static boolean isSetter(Method method){
49 if(!method.getName().startsWith("set")) return false;
50 if(method.getParameterTypes().length != 1) return false;
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 );
62 Object t = c.newInstance();
65 m.setAccessible(true);
66 Object o = m.invoke( t );
68 if( retType.equals( Class.forName( "java.lang.String" ) ) ) {
70 //out.format( fmt, "testGetter: expected null, got ", (o != null)?o:"null" );
73 //out.format( fmt, "testGetter: expected val, got ", (o != null)?o:"null" );
74 assert( o.equals( val ) );
77 out.format( fmt, "testGetter: " + m.getName() + " untested retType", retType );
81 } catch (InvocationTargetException e ) {
82 Throwable cause = e.getCause();
83 err.format( "%s() returned %x%n", m.getName(), cause.getMessage() );
86 } catch (ClassNotFoundException nfe ){
87 nfe.printStackTrace();
88 } catch (IllegalArgumentException ae ) {
90 } catch (InstantiationException ie ) {
92 } catch (IllegalAccessException iae ) {
93 iae.printStackTrace();
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 );
105 Object t = c.newInstance();
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
115 Object o = m.invoke( t, b );
117 out.format( fmt, "testSetter: " + m.getName() + " untested pType", pType[0] );
120 } catch (InvocationTargetException e ) {
121 Throwable cause = e.getCause();
122 err.format( "%s() returned %x%n", m.getName(), cause.getMessage() );
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();
136 public void reflect(String... args) {
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)) {
145 //out.format("%s%n", m.toGenericString());
147 //out.format(fmt, "ReturnType", m.getReturnType());
148 //out.format(fmt, "GenericReturnType", m.getGenericReturnType());
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]);
156 if ( isGetter( m ) ) {
157 testGetter( c, m, pType , args[2]);
158 } else if ( isSetter( m ) ) {
159 testSetter( c, m, pType );
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]);
170 // production code should handle these exceptions more gracefully
171 } catch (ClassNotFoundException x) {