JUnit addon to run tests and suites concurrent, see also http://junit.org/node/589
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..137294d
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.directory.project</groupId>
+ <artifactId>project</artifactId>
+ <version>17</version>
+ </parent>
+ <groupId>org.apache.directory.junit</groupId>
+ <artifactId>junit-addons</artifactId>
+ <version>0.1-SNAPSHOT</version>
+ <name>junit-addons</name>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</version>
+ </dependency>
+ </dependencies>
+
+ <distributionManagement>
+ <site>
+ <id>apache.directory.buildtools.junit-addons</id>
+ <url>${distMgmtSiteUrl}</url>
+ </site>
+ </distributionManagement>
+
+ <issueManagement>
+ <system>JIRA</system>
+ <url>http://issues.apache.org/jira/browse/DIR</url>
+ </issueManagement>
+
+ <scm>
+ <connection>scm:svn:http://svn.apache.org/repos/asf/directory/buildtools/trunk/junit-addons</connection>
+ <developerConnection>scm:svn:https://svn.apache.org/repos/asf/directory/buildtools/trunk/junit-addons</developerConnection>
+ <url>http://svn.apache.org/viewvc/directory/buildtools/trunk/junit-addons</url>
+ </scm>
+</project>
diff --git a/src/main/java/org/apache/directory/junit/tools/Concurrent.java b/src/main/java/org/apache/directory/junit/tools/Concurrent.java
new file mode 100644
index 0000000..08bd4e8
--- /dev/null
+++ b/src/main/java/org/apache/directory/junit/tools/Concurrent.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.directory.junit.tools;
+
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+/**
+ * @author Mathieu Carbou (mathieu.carbou@gmail.com)
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(
+ { ElementType.TYPE })
+public @interface Concurrent
+{
+ int threads() default 5;
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/directory/junit/tools/ConcurrentJunitRunner.java b/src/main/java/org/apache/directory/junit/tools/ConcurrentJunitRunner.java
new file mode 100644
index 0000000..7646ccf
--- /dev/null
+++ b/src/main/java/org/apache/directory/junit/tools/ConcurrentJunitRunner.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.directory.junit.tools;
+
+
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.concurrent.CompletionService;
+import java.util.concurrent.ExecutorCompletionService;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.runners.BlockJUnit4ClassRunner;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.RunnerScheduler;
+
+
+/**
+ * @author Mathieu Carbou (mathieu.carbou@gmail.com)
+ */
+public class ConcurrentJunitRunner extends BlockJUnit4ClassRunner
+{
+ public ConcurrentJunitRunner( final Class<?> klass ) throws InitializationError
+ {
+ super( klass );
+ setScheduler( new RunnerScheduler()
+ {
+ ExecutorService executorService = Executors.newFixedThreadPool( klass
+ .isAnnotationPresent( Concurrent.class ) ? klass.getAnnotation( Concurrent.class ).threads() : 5,
+ new NamedThreadFactory( klass.getSimpleName() ) );
+ CompletionService<Void> completionService = new ExecutorCompletionService<Void>( executorService );
+ Queue<Future<Void>> tasks = new LinkedList<Future<Void>>();
+
+
+ @Override
+ public void schedule( Runnable childStatement )
+ {
+ tasks.offer( completionService.submit( childStatement, null ) );
+ }
+
+
+ @Override
+ public void finished()
+ {
+ try
+ {
+ while ( !tasks.isEmpty() )
+ tasks.remove( completionService.take() );
+ }
+ catch ( InterruptedException e )
+ {
+ Thread.currentThread().interrupt();
+ }
+ finally
+ {
+ while ( !tasks.isEmpty() )
+ tasks.poll().cancel( true );
+ executorService.shutdownNow();
+ }
+ }
+ } );
+ }
+
+ static final class NamedThreadFactory implements ThreadFactory
+ {
+ static final AtomicInteger poolNumber = new AtomicInteger( 1 );
+ final AtomicInteger threadNumber = new AtomicInteger( 1 );
+ final ThreadGroup group;
+
+
+ NamedThreadFactory( String poolName )
+ {
+ group = new ThreadGroup( poolName + "-" + poolNumber.getAndIncrement() );
+ }
+
+
+ @Override
+ public Thread newThread( Runnable r )
+ {
+ return new Thread( group, r, group.getName() + "-thread-" + threadNumber.getAndIncrement(), 0 );
+ }
+ }
+}
diff --git a/src/main/java/org/apache/directory/junit/tools/ConcurrentSuite.java b/src/main/java/org/apache/directory/junit/tools/ConcurrentSuite.java
new file mode 100644
index 0000000..1f39e03
--- /dev/null
+++ b/src/main/java/org/apache/directory/junit/tools/ConcurrentSuite.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.directory.junit.tools;
+
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.internal.builders.AllDefaultPossibilitiesBuilder;
+import org.junit.runner.Runner;
+import org.junit.runners.Suite;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.RunnerBuilder;
+
+
+/**
+ * @author Mathieu Carbou (mathieu.carbou@gmail.com)
+ */
+public final class ConcurrentSuite extends Suite
+{
+ public ConcurrentSuite( Class<?> klass ) throws InitializationError
+ {
+ super( klass, new AllDefaultPossibilitiesBuilder( true )
+ {
+ @Override
+ public Runner runnerForClass( Class<?> testClass ) throws Throwable
+ {
+ List<RunnerBuilder> builders = Arrays.asList( new RunnerBuilder()
+ {
+ @Override
+ public Runner runnerForClass( Class<?> testClass ) throws Throwable
+ {
+ Concurrent annotation = testClass.getAnnotation( Concurrent.class );
+ if ( annotation != null )
+ return new ConcurrentJunitRunner( testClass );
+ return null;
+ }
+ }, ignoredBuilder(), annotatedBuilder(), suiteMethodBuilder(), junit3Builder(), junit4Builder() );
+ for ( RunnerBuilder each : builders )
+ {
+ Runner runner = each.safeRunnerForClass( testClass );
+ if ( runner != null )
+ return runner;
+ }
+ return null;
+ }
+ } );
+ }
+}