package com.github.ferstl.streams;

import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static java.lang.Thread.currentThread;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.matchesPattern;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;


public class ParallelStreamSupportTest {

  private static final Pattern FORK_JOIN_THREAD_NAME_PATTERN = Pattern.compile("ForkJoinPool-\\d+-worker-\\d+");

  private ForkJoinPool workerPool;

  @Before
  public void before() {
    // Precondition for all tests
    assertFalse("This test must not run in a ForkJoinPool", currentThread() instanceof ForkJoinWorkerThread);

    this.workerPool = new ForkJoinPool(1);
  }

  @After
  public void after() throws InterruptedException {
    this.workerPool.shutdown();
    this.workerPool.awaitTermination(1, TimeUnit.SECONDS);
  }


  @Test
  public void collectParallel() {
    List<String> result = ParallelStreamSupport.parallelStream(singletonList("a"), this.workerPool)
        .map(e -> Thread.currentThread().getName())
        .collect(toList());

    assertThat(result, contains(matchesPattern(FORK_JOIN_THREAD_NAME_PATTERN)));
  }
}

@@ -33,31 +33,8 @@
 s;%0A%0A
-import java.util.List;%0A
 impo
@@ -180,21 +180,21 @@
 til.
-regex.Pattern
+stream.Stream
 ;%0Aim
@@ -327,483 +327,384 @@
 tic 
-java.util.Collections.singletonList;%0Aimport static java.util.stream.Collectors.toList;%0Aimport static org.hamcrest.Matchers.contains;%0Aimport static org.hamcrest.Matchers.matchesPattern;%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertThat;%0A%0A%0Apublic class ParallelStreamSupportTest %7B%0A%0A  private static final Pattern FORK_JOIN_THREAD_NAME_PATTERN = Pattern.compile(%22ForkJoinPool-%5C%5Cd+-worker-%5C%5Cd+%22);%0A%0A  private ForkJoinPool workerPool;%0A%0A  @Before
+org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertSame;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.verify;%0A%0A%0Apublic class ParallelStreamSupportTest %7B%0A%0A  private ForkJoinPool workerPool;%0A%0A  private Stream%3CString%3E delegate;%0A  private ParallelStreamSupport%3CString%3E parallelStreamSupport;%0A%0A  @Before%0A  @SuppressWarnings(%22unchecked%22)
 %0A  p
@@ -913,16 +913,150 @@
 ool(1);%0A
+    this.delegate = mock(Stream.class);%0A    this.parallelStreamSupport = new ParallelStreamSupport%3C%3E(this.delegate, this.workerPool);%0A
   %7D%0A%0A  @
@@ -1236,22 +1236,17 @@
 oid 
-collectParalle
+sequentia
 l() 
@@ -1251,20 +1251,22 @@
 ) %7B%0A    
-List
+Stream
 %3CString%3E
@@ -1270,18 +1270,23 @@
 ng%3E 
-result = P
+stream = this.p
 aral
@@ -1306,219 +1306,112 @@
 ort.
-parallelStream(singletonList(%22a%22), this.workerPool)%0A        .map(e -%3E Thread.currentThread().getName())%0A        .collect(toList());%0A%0A    assertThat(result, contains(matchesPattern(FORK_JOIN_THREAD_NAME_PATTERN))
+sequential();%0A%0A    verify(this.delegate).sequential();%0A    assertSame(this.parallelStreamSupport, stream
 );%0A 

Add test for sequential()