/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.test.integration;

import java.util.ArrayList;
import java.util.List;

import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;

/**
 * Starts/Stop XWiki before/after all tests and run all tests found in the current classloader using
 * <a href="http://www.johanneslink.net/projects/cpsuite.jsp">cpsuite</a> (we extend it).
 *
 * Tests can be filtered by passing the "pattern" System Property.
 *
 * @version $Id$
 * @since 3.0RC1
 */
public class XWikiExecutorSuite extends ClasspathSuite
{
    public static final String PATTERN = ".*" + System.getProperty("pattern", "");

    public XWikiExecutorSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError
    {
        super(klass, builder);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected List<Runner> getChildren()
    {
        List<Runner> runners = new ArrayList<Runner>();

        // Filter classes to run
        for (Runner runner : super.getChildren()) {
            if (runner.getDescription().getClassName().matches(PATTERN)) {
                runners.add(runner);
            }
        }

        return runners;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void run(RunNotifier notifier)
    {
        XWikiExecutor executor = new XWikiExecutor(0);

        try {
            executor.start();
        } catch (Exception e) {
            throw new RuntimeException("Failed to start XWiki", e);
        }

        try {
            super.run(notifier);
        } finally {
            try {
                executor.stop();
            } catch (Exception e) {
                throw new RuntimeException("Failed to stop XWiki", e);
            }
        }
    }
}

@@ -924,16 +924,210 @@
 ation;%0A%0A
+import java.lang.annotation.ElementType;%0Aimport java.lang.annotation.Retention;%0Aimport java.lang.annotation.RetentionPolicy;%0Aimport java.lang.annotation.Target;%0Aimport java.lang.reflect.Method;%0A
 import j
@@ -1846,24 +1846,101 @@
 ern%22, %22%22);%0A%0A
+    private List%3CXWikiExecutor%3E executors = new ArrayList%3CXWikiExecutor%3E();%0A%0A
     public X
@@ -2059,32 +2059,307 @@
 uilder);%0A    %7D%0A%0A
+    @Retention(RetentionPolicy.RUNTIME)%0A    @Target(ElementType.TYPE)%0A    public @interface Executors %7B%0A        public int value() default 1;%0A    %7D%0A%0A    @Retention(RetentionPolicy.RUNTIME)%0A    @Target(ElementType.METHOD)%0A    public static @interface Initialized%0A    %7B%0A    %7D%0A%0A
     /**%0A     * %7B
@@ -2861,50 +2861,953 @@
     
-XWikiExecutor executor = new
+// Construct as many executors as specified in the Executors annotation or 1 if annotation is not present.%0A        int executorNb = 1;%0A        Executors executorsAnnotation = getClass().getAnnotation(Executors.class);%0A        if (executorsAnnotation != null) %7B%0A            executorNb = executorsAnnotation.value();%0A        %7D%0A%0A        for (int i = 0; i %3C executorNb; i++) %7B%0A            this.executors.add(new XWikiExecutor(i));%0A        %7D%0A%0A        // Callback to setup executors in the suite class.%0A        try %7B%0A            for (Method method : getClass().getMethods()) %7B%0A                Initialized initializedAnnotation = method.getAnnotation(Initialized.class);%0A                if (initializedAnnotation != null) %7B%0A                    // Call it!%0A                    method.invoke(this, this.executors);%0A                %7D%0A            %7D%0A        %7D catch (Exception e) %7B%0A            throw new RuntimeException(%22Failed to initialize
  XWiki
+ 
 Executor
 (0);
@@ -3806,12 +3806,25 @@
 utor
-(0);
+s%22, e);%0A        %7D
 %0A%0A  
@@ -3827,32 +3827,96 @@
 %0A%0A        try %7B%0A
+            for (XWikiExecutor executor : this.executors) %7B%0A    
             exec
@@ -3925,24 +3925,38 @@
 or.start();%0A
+            %7D%0A
         %7D ca
@@ -4159,24 +4159,110 @@
     
-executor.stop();
+for (XWikiExecutor executor : this.executors) %7B%0A                    executor.stop();%0A                %7D
 %0A   

Add JUnit4 Suite for starting/stopping XE + Running all JUnit tests in the current classloader