1 package org.apache.maven.plugin.surefire.booterclient.lazytestprovider;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Queue;
25 import java.util.concurrent.Semaphore;
26
27
28
29
30
31
32
33
34
35
36
37
38 public class TestProvidingInputStream
39 extends InputStream
40 {
41 private final Queue<String> testItemQueue;
42
43 private byte[] currentBuffer;
44
45 private int currentPos;
46
47 private Semaphore semaphore = new Semaphore( 0 );
48
49 private FlushReceiverProvider flushReceiverProvider;
50
51 private boolean closed = false;
52
53
54
55
56
57
58 public TestProvidingInputStream( Queue<String> testItemQueue )
59 {
60 this.testItemQueue = testItemQueue;
61 }
62
63
64
65
66 public void setFlushReceiverProvider( FlushReceiverProvider flushReceiverProvider )
67 {
68 this.flushReceiverProvider = flushReceiverProvider;
69 }
70
71 @Override
72 public synchronized int read()
73 throws IOException
74 {
75 if ( null == currentBuffer )
76 {
77 if ( null != flushReceiverProvider && null != flushReceiverProvider.getFlushReceiver() )
78 {
79 flushReceiverProvider.getFlushReceiver().flush();
80 }
81
82 semaphore.acquireUninterruptibly();
83
84 if ( closed )
85 {
86 return -1;
87 }
88
89 String currentElement = testItemQueue.poll();
90 if ( null != currentElement )
91 {
92 currentBuffer = currentElement.getBytes();
93 currentPos = 0;
94 }
95 else
96 {
97 return -1;
98 }
99 }
100
101 if ( currentPos < currentBuffer.length )
102 {
103 return ( currentBuffer[currentPos++] & 0xff );
104 }
105 else
106 {
107 currentBuffer = null;
108 return ( '\n' & 0xff );
109 }
110 }
111
112
113
114
115 public void provideNewTest()
116 {
117 semaphore.release();
118 }
119
120 public void close()
121 {
122 closed = true;
123 semaphore.release();
124 }
125 }