1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.surefire.booterclient.lazytestprovider;
20
21 import java.io.IOException;
22 import java.util.Queue;
23 import java.util.concurrent.ConcurrentLinkedQueue;
24 import java.util.concurrent.Semaphore;
25 import java.util.concurrent.atomic.AtomicBoolean;
26
27 import org.apache.maven.surefire.api.booter.Command;
28 import org.apache.maven.surefire.api.booter.Shutdown;
29
30 import static org.apache.maven.surefire.api.booter.Command.BYE_ACK;
31 import static org.apache.maven.surefire.api.booter.Command.NOOP;
32 import static org.apache.maven.surefire.api.booter.Command.SKIP_SINCE_NEXT_TEST;
33 import static org.apache.maven.surefire.api.booter.Command.TEST_SET_FINISHED;
34 import static org.apache.maven.surefire.api.booter.Command.toRunClass;
35 import static org.apache.maven.surefire.api.booter.Command.toShutdown;
36
37
38
39
40
41
42
43
44
45
46
47
48 public final class TestProvidingInputStream extends DefaultCommandReader {
49 private final Semaphore barrier = new Semaphore(0);
50
51 private final Queue<Command> commands = new ConcurrentLinkedQueue<>();
52
53 private final AtomicBoolean closed = new AtomicBoolean();
54
55 private final Queue<String> testClassNames;
56
57
58
59
60
61
62 public TestProvidingInputStream(Queue<String> testClassNames) {
63 this.testClassNames = testClassNames;
64 }
65
66
67
68
69 void testSetFinished() {
70 if (canContinue()) {
71 commands.add(TEST_SET_FINISHED);
72 barrier.release();
73 }
74 }
75
76 @Override
77 public void skipSinceNextTest() {
78 if (canContinue()) {
79 commands.add(SKIP_SINCE_NEXT_TEST);
80 barrier.release();
81 }
82 }
83
84 @Override
85 public void shutdown(Shutdown shutdownType) {
86 if (canContinue()) {
87 commands.add(toShutdown(shutdownType));
88 barrier.release();
89 }
90 }
91
92 @Override
93 public void noop() {
94 if (canContinue()) {
95 commands.add(NOOP);
96 barrier.release();
97 }
98 }
99
100 @Override
101 public void acknowledgeByeEventReceived() {
102 if (canContinue()) {
103 commands.add(BYE_ACK);
104 barrier.release();
105 }
106 }
107
108 @Override
109 protected Command nextCommand() {
110 Command cmd = commands.poll();
111 if (cmd == null) {
112 String cmdData = testClassNames.poll();
113 return cmdData == null ? TEST_SET_FINISHED : toRunClass(cmdData);
114 } else {
115 return cmd;
116 }
117 }
118
119 @Override
120 protected void beforeNextCommand() throws IOException {
121 awaitNextTest();
122 }
123
124 @Override
125 public boolean isClosed() {
126 return closed.get();
127 }
128
129
130
131
132 @Override
133 public void provideNewTest() {
134 if (canContinue()) {
135 barrier.release();
136 }
137 }
138
139 @Override
140 public void close() {
141 if (closed.compareAndSet(false, true)) {
142 barrier.drainPermits();
143 barrier.release();
144 }
145 }
146
147 private void awaitNextTest() throws IOException {
148 try {
149 barrier.acquire();
150 } catch (InterruptedException e) {
151 throw new IOException(e.getLocalizedMessage(), e);
152 }
153 }
154 }