All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xlcloud.test.TestUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xlcloud.test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Utitilty for tests uses Reflection api to manipulate objects
 * 
 * @author Michał Kamiński, AMG.net
 */
public class TestUtils {

    @SuppressWarnings( { "rawtypes", "unchecked" } )
    public static  T invokeMethod(Class returnClass, String methodName, Object ivokeObject, Object... objects) throws Exception {
        Class[] classes = new Class[objects.length];
        for (int i = 0; i < objects.length; i++) {
            classes[i] = objects[i].getClass();
        }
        Method method = ivokeObject.getClass().getDeclaredMethod(methodName, classes);
        method.setAccessible(true);
        return (T) method.invoke(ivokeObject, objects);
    }

    public static Object invokeMethod(String methodName, Object ivokeObject, Object... objects) throws Exception {
        return invokeMethod(Object.class, methodName, ivokeObject, objects);
    }

    public static void setObjectProperty(Object instance, String propertyName, Object propertyValue) throws Exception {
        Field property = findDeclaredField(instance.getClass(), propertyName);
        property.setAccessible(true);
        property.set(instance, propertyValue);
    }

    public static Object getObjectProperty(Object instance, String propertyName) throws Exception {
        Field property = findDeclaredField(instance.getClass(), propertyName);
        property.setAccessible(true);
        return property.get(instance);
    }

    private static Field findDeclaredField(Class clazz, String fieldName) throws NoSuchFieldException {
        Field field = null;
        /*
         * Method getDeclaredField() does not return fields inherited from
         * superclasses, so we need to search the superclasses explicitly.
         */
        while (field == null && clazz != null) {
            try {
                field = clazz.getDeclaredField(fieldName);
            } catch (NoSuchFieldException e) {
                clazz = clazz.getSuperclass();
            }
        }
        if (field != null) {
            return field;
        } else {
            throw new NoSuchFieldException("No such field: " + fieldName);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy