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();
// If there is a field which is an XWikiExecutor type
// and has an @Requirement annotation, inject the current executor.
for (Field field : this.getTestClass().getJavaClass().getDeclaredFields()) {
if (field.getType() == XWikiExecutor.class
&& field.getAnnotation(Requirement.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.
}
}
}