|
1 |
| package com.agical.rmock.core.strategy.impl; |
|
2 |
| |
|
3 |
| import java.io.PrintWriter; |
|
4 |
| import java.io.StringWriter; |
|
5 |
| import java.util.ArrayList; |
|
6 |
| import java.util.Iterator; |
|
7 |
| import java.util.List; |
|
8 |
| |
|
9 |
| import com.agical.rmock.core.exception.RMockConcurrentExceptionsException; |
|
10 |
| import com.agical.rmock.core.strategy.TestStep; |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| public class ThreadedCompositeStep implements TestStep { |
|
18 |
| private List steps = new ArrayList(); |
|
19 |
| private List throwables = new ArrayList(); |
|
20 |
| |
|
21 |
17
| public void run() throws Throwable {
|
|
22 |
17
| Thread[] threads = new Thread[steps.size()];
|
|
23 |
17
| for (int i = 0; i < threads.length; i++) {
|
|
24 |
24
| threads[i] = new Thread( getRunnableForTestStep(i) );
|
|
25 |
| } |
|
26 |
17
| for (int i = 0; i < threads.length; i++) {
|
|
27 |
24
| threads[i].start();
|
|
28 |
| } |
|
29 |
17
| for (int i = 0; i < threads.length; i++) {
|
|
30 |
24
| threads[i].join();
|
|
31 |
| } |
|
32 |
17
| if( !throwables.isEmpty() ) {
|
|
33 |
2
| StringWriter stringWriter = new StringWriter();
|
|
34 |
2
| PrintWriter printWriter = new PrintWriter( stringWriter );
|
|
35 |
| |
|
36 |
2
| for (Iterator iter = throwables.iterator(); iter.hasNext();) {
|
|
37 |
3
| Throwable throwable = (Throwable) iter.next();
|
|
38 |
3
| throwable.printStackTrace(printWriter);
|
|
39 |
3
| if( iter.hasNext() ) {
|
|
40 |
1
| printWriter.print( "---- Next Exception: ---------------\n" );
|
|
41 |
| } |
|
42 |
| } |
|
43 |
2
| printWriter.close();
|
|
44 |
2
| stringWriter.close();
|
|
45 |
2
| throw new RMockConcurrentExceptionsException( "One or more exceptions were thrown while executing threaded tests:\n" + stringWriter.toString() );
|
|
46 |
| } |
|
47 |
| } |
|
48 |
| |
|
49 |
24
| private Runnable getRunnableForTestStep(final int i) {
|
|
50 |
24
| return new Runnable() {
|
|
51 |
24
| public void run() {
|
|
52 |
24
| try {
|
|
53 |
24
| ((TestStep)steps.get(i)).run();
|
|
54 |
| } catch (Throwable e) { |
|
55 |
3
| throwables.add( e );
|
|
56 |
| } |
|
57 |
| } |
|
58 |
| }; |
|
59 |
| } |
|
60 |
| |
|
61 |
26
| public void addStep(TestStep testStep) {
|
|
62 |
26
| steps.add( testStep );
|
|
63 |
| } |
|
64 |
| |
|
65 |
| } |