package org.netmelody.panto.task;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Parallel;
import org.netmelody.panto.internal.RecordingJUnitTask;

public final class ParallelJUnitTask extends Task {
    private RecordingJUnitTask embeddedJUnitTask;
    private int stripeCount = -1;

    public void setStripeCount(int stripeCount) {
        if (stripeCount <= 0) {
            throw new BuildException("There must be at least one stripe");
        }
        this.stripeCount = stripeCount;
    }

    public RecordingJUnitTask createJUnit() {
        try {
            embeddedJUnitTask = new RecordingJUnitTask(getProject());
            return embeddedJUnitTask;
        }
        catch (Exception e) {
            throw new BuildException(e);
        }
    }
    
    @Override
    public void execute() throws BuildException {
        final Parallel parallelTask = (Parallel) getProject().createTask("parallel");
        parallelTask.init();
        parallelTask.setThreadsPerProcessor(1);
        
        if (stripeCount <= 0) {
            stripeCount = Runtime.getRuntime().availableProcessors();
        }
        for (int i = 0; i < stripeCount; i++) {
            parallelTask.addTask(embeddedJUnitTask.spawnTaskForStripe(i + 1, stripeCount));
        }
        
        parallelTask.perform();
    }
}

@@ -308,24 +308,58 @@
 dJUnitTask;%0A
+    private int threadCount = -1;%0A
     private 
@@ -593,16 +593,234 @@
 t;%0A    %7D
+%0A    %0A    public void setThreadCount(int threadCount) %7B%0A        if (threadCount %3C= 0) %7B%0A            throw new BuildException(%22There must be at least one thread%22);%0A        %7D%0A        this.threadCount = threadCount;%0A    %7D
 %0A%0A    pu
@@ -1262,32 +1262,76 @@
 init();%0A        
+%0A        if (threadCount %3E 0) %7B%0A            
 parallelTask.set
@@ -1340,23 +1340,25 @@
 read
-sPerProcessor(1
+Count(threadCount
 );%0A 
@@ -1356,32 +1356,33 @@
 Count);%0A        
+%7D
 %0A        if (str
@@ -1374,37 +1374,20 @@
         
-if (stripeCount %3C= 0)
+else
  %7B%0A     
@@ -1393,22 +1393,22 @@
         
-stripe
+thread
 Count = 
@@ -1447,24 +1447,166 @@
 ocessors();%0A
+            parallelTask.setThreadsPerProcessor(1);%0A        %7D%0A        %0A        if (stripeCount %3C= 0) %7B%0A            stripeCount = threadCount;%0A
         %7D%0A  

add threadCount property to ParallelJUnitTask