Search in sources :

Example 1 with ClasspathClassesFinder

use of org.junit.extensions.cpsuite.ClasspathClassesFinder in project xwiki-platform by xwiki.

the class ForEachProfileSuite method run.

@Override
public void run(RunNotifier notifier) {
    // Get the list of test profiles.
    final List<Class<?>> profiles = new ClasspathClassesFinder(IsProfileTester.INSTANCE, "java.class.path").find();
    final Map<Profile, XWikiExecutor> executorByProfile = new HashMap<Profile, XWikiExecutor>();
    for (int i = 0; i < profiles.size(); i++) {
        try {
            // All executors are #0 because they will not be run in parallel.
            executorByProfile.put(((Class<Profile>) profiles.get(i)).newInstance(), new XWikiExecutor(0));
        } catch (Exception e) {
            throw new RuntimeException("Failed to instanciate configuration profile.", e);
        }
    }
    // Callback to setup executors in the suite class.
    try {
        for (Profile profile : executorByProfile.keySet()) {
            profile.apply(executorByProfile.get(profile));
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to initialize XWiki Executors", e);
    }
    for (final Profile profile : executorByProfile.keySet()) {
        final XWikiExecutor executor = executorByProfile.get(profile);
        try {
            try {
                executor.start();
            } catch (Exception e) {
                throw new RuntimeException("Failed to start XWiki", e);
            }
            try {
                Object instance = this.getTestClass().getJavaClass().newInstance();
                // and has an @Inject annotation, inject the current executor.
                for (Field field : this.getTestClass().getJavaClass().getDeclaredFields()) {
                    if (field.getType() == XWikiExecutor.class && field.getAnnotation(Inject.class) != null) {
                        field.setAccessible(true);
                        field.set(instance, executor);
                    }
                }
                // If the class is initializable then call initialize.
                final Class<?>[] interfaces = this.getTestClass().getJavaClass().getInterfaces();
                for (int i = 0; i < interfaces.length; i++) {
                    if (interfaces[i] == Initializable.class) {
                        this.getTestClass().getJavaClass().getMethod("initialize").invoke(instance);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException("Failed to prepare tests to run in config profile.", e);
            }
            super.run(notifier);
        } finally {
            try {
                executor.stop();
            } catch (Exception e) {
            // Squash this and let the original exception be thrown.
            }
        }
    }
}
Also used : XWikiExecutor(org.xwiki.test.integration.XWikiExecutor) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) ClasspathClassesFinder(org.junit.extensions.cpsuite.ClasspathClassesFinder)

Aggregations

Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 ClasspathClassesFinder (org.junit.extensions.cpsuite.ClasspathClassesFinder)1 XWikiExecutor (org.xwiki.test.integration.XWikiExecutor)1