Search in sources :

Example 6 with MethodFinder

use of com.axway.ats.core.reflect.MethodFinder in project ats-framework by Axway.

the class Test_MethodFinder method findMethodConstructorMethodListNegative.

@Test(expected = NoSuchMethodException.class)
public void findMethodConstructorMethodListNegative() throws NoSuchMethodException, AmbiguousMethodException {
    MethodFinder methodFinder = new MethodFinder("methods", Arrays.asList(FileInputStream.class.getDeclaredMethods()));
    //one argument
    methodFinder.findMethod(new Class<?>[] { Integer.class, Integer.class, Integer.class });
}
Also used : MethodFinder(com.axway.ats.core.reflect.MethodFinder) BaseTest(com.axway.ats.core.BaseTest) Test(org.junit.Test)

Example 7 with MethodFinder

use of com.axway.ats.core.reflect.MethodFinder in project ats-framework by Axway.

the class UiDriver method getCustomDriver.

/**
    * This method allows you to load your own driver implementation which you can use as any other UI Engine driver
    * @param driverClassName the full class name of the custom driver class
    * @param parameterTypes the parameters that the constructor requires 
    * @param constructorArguments the arguments, which will be passed to the constructor
    * @return new UiDriver instance
    */
@PublicAtsApi
public static UiDriver getCustomDriver(String driverClassName, Class<?>[] parameterTypes, Object[] constructorArguments) throws Exception {
    UiDriver driver = null;
    try {
        Class<?> clss = Class.forName(driverClassName);
        Constructor<?> constructor = new MethodFinder(clss).findConstructor(parameterTypes);
        driver = (UiDriver) constructor.newInstance(constructorArguments);
    } catch (Exception e) {
        throw new Exception("Error while loading custom driver '" + driverClassName + "'", e);
    }
    return driver;
}
Also used : MethodFinder(com.axway.ats.core.reflect.MethodFinder) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 8 with MethodFinder

use of com.axway.ats.core.reflect.MethodFinder in project ats-framework by Axway.

the class ActionMethodContainer method add.

/**
     * Add a new action method
     *
     * @param actionMethod the action method to add
     * @return
     * @throws ActionAlreadyDefinedException if the new action has the exact same arguments of an existing one
     */
public boolean add(ActionMethod actionMethod) throws ActionAlreadyDefinedException {
    // if there is at least one method check for ambiguity
    if (actionMethods.size() > 0) {
        Class<?>[] newMethodParamTypes = actionMethod.getMethod().getParameterTypes();
        //find the action method implementation based on the arguments
        MethodFinder methodFinder = new MethodFinder("methods for action " + actionName, getMethods(), customComparisonRules);
        //get the most specific method which accepts these parameters
        try {
            Method mostSpecificMethod = methodFinder.findMethod(newMethodParamTypes);
            //then we have ambiguity, which should not be allowed
            if (Arrays.equals(newMethodParamTypes, mostSpecificMethod.getParameterTypes())) {
                throw new ActionAlreadyDefinedException(actionName, componentName, mostSpecificMethod);
            }
        } catch (NoSuchMethodException e) {
        //method which accepts there argument does not exist, we can safely add it
        } catch (AmbiguousMethodException e) {
            //unless they are exactly the same, which we don't allow to happen
            throw new RuntimeException("AmbiguousMethodException caught while searching for action methods");
        }
    }
    //add the action method
    return actionMethods.add(actionMethod);
}
Also used : MethodFinder(com.axway.ats.core.reflect.MethodFinder) Method(java.lang.reflect.Method) ActionAlreadyDefinedException(com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException) AmbiguousMethodException(com.axway.ats.core.reflect.AmbiguousMethodException)

Example 9 with MethodFinder

use of com.axway.ats.core.reflect.MethodFinder in project ats-framework by Axway.

the class Test_MethodFinder method findMethodConstructorMethodListNegativeAmbigous.

@Test(expected = AmbiguousMethodException.class)
public void findMethodConstructorMethodListNegativeAmbigous() throws NoSuchMethodException, AmbiguousMethodException {
    MethodFinder methodFinder = new MethodFinder("ambiguous", Arrays.asList(MethodFinderTester.class.getDeclaredMethods()));
    //no arguments
    methodFinder.findMethod("ambiguousMethod", new Class<?>[] { Void.TYPE });
}
Also used : MethodFinder(com.axway.ats.core.reflect.MethodFinder) BaseTest(com.axway.ats.core.BaseTest) Test(org.junit.Test)

Example 10 with MethodFinder

use of com.axway.ats.core.reflect.MethodFinder in project ats-framework by Axway.

the class Test_MethodFinder method findMethodWithNamePositive.

@Test
public void findMethodWithNamePositive() throws NoSuchMethodException, AmbiguousMethodException {
    MethodFinder methodFinder = new MethodFinder(FileInputStream.class);
    //null argument
    assertNotNull(methodFinder.findMethod("close", null));
    //one argument
    assertNotNull(methodFinder.findMethod("read", new Class<?>[] { (new byte[] {}).getClass() }));
    //several arguments
    assertNotNull(methodFinder.findMethod("read", new Class<?>[] { (new byte[] {}).getClass(), Integer.TYPE, Integer.TYPE }));
    //unboxing
    assertNotNull(methodFinder.findMethod("read", new Class<?>[] { (new byte[] {}).getClass(), Integer.class, Integer.class }));
    //widening conversion
    assertNotNull(methodFinder.findMethod("read", new Class<?>[] { (new byte[] {}).getClass(), Byte.class, Short.TYPE }));
}
Also used : MethodFinder(com.axway.ats.core.reflect.MethodFinder) BaseTest(com.axway.ats.core.BaseTest) Test(org.junit.Test)

Aggregations

MethodFinder (com.axway.ats.core.reflect.MethodFinder)17 Test (org.junit.Test)13 BaseTest (com.axway.ats.core.BaseTest)10 ArrayList (java.util.ArrayList)5 TypeComparisonRule (com.axway.ats.core.reflect.TypeComparisonRule)4 BaseTest (com.axway.ats.agent.core.BaseTest)3 AmbiguousMethodException (com.axway.ats.core.reflect.AmbiguousMethodException)3 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ActionAlreadyDefinedException (com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)1 MssqlDbProvider (com.axway.ats.core.dbaccess.mssql.MssqlDbProvider)1 MysqlDbProvider (com.axway.ats.core.dbaccess.mysql.MysqlDbProvider)1 OracleDbProvider (com.axway.ats.core.dbaccess.oracle.OracleDbProvider)1 File (java.io.File)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1