Search in sources :

Example 1 with TestingModelFactory

use of org.apache.jena.rdf.model.test.helpers.TestingModelFactory in project jena by apache.

the class AbstractTestPackage method addTest.

/**
	 * Adds a test to the test suite by looking for the standard test methods.
	 * These are
	 * methods that start with "test" and have no arguments.
	 * 
	 * @param testClass
	 * @param constructorArgs
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws IllegalArgumentException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
private void addTest(final Class<? extends TestCase> testClass, final Object... constructorArgs) {
    final Object[] args = new Object[constructorArgs.length + 1];
    System.arraycopy(constructorArgs, 0, args, 0, constructorArgs.length);
    final List<Class<?>> parameterTypes = new ArrayList<>();
    for (final Object o : constructorArgs) {
        if (o instanceof TestingModelFactory) {
            parameterTypes.add(TestingModelFactory.class);
        } else {
            parameterTypes.add(o.getClass());
        }
    }
    parameterTypes.add(String.class);
    Constructor<TestCase> c;
    try {
        @SuppressWarnings("unchecked") Constructor<TestCase> cc = (Constructor<TestCase>) testClass.getConstructor(parameterTypes.toArray(new Class[parameterTypes.size()]));
        c = cc;
    } catch (final SecurityException | NoSuchMethodException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage(), e);
    }
    for (final Method m : testClass.getMethods()) {
        if (m.getParameterTypes().length == 0) {
            if (m.getName().startsWith("test")) {
                args[constructorArgs.length] = m.getName();
                try {
                    addTest(c.newInstance(args));
                } catch (final IllegalArgumentException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e.getMessage(), e);
                }
            }
        }
    }
}
Also used : Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) TestCase(junit.framework.TestCase) TestingModelFactory(org.apache.jena.rdf.model.test.helpers.TestingModelFactory)

Aggregations

Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 TestCase (junit.framework.TestCase)1 TestingModelFactory (org.apache.jena.rdf.model.test.helpers.TestingModelFactory)1