View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * An {@link java.io.InputStream} that, when read, provides test class names out of a queue.
39   * <br>
40   * The Stream provides only one test at a time, but only after {@link #provideNewTest()} has been invoked.
41   * <br>
42   * The instance is used only in reusable forks in {@link org.apache.maven.plugin.surefire.booterclient.ForkStarter}
43   * by one Thread.
44   *
45   * @author Andreas Gudian
46   * @author Tibor Digana (tibor17)
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       * C'tor
59       *
60       * @param testClassNames source of the tests to be read from this stream
61       */
62      public TestProvidingInputStream(Queue<String> testClassNames) {
63          this.testClassNames = testClassNames;
64      }
65  
66      /**
67       * For testing purposes.
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      * Signal that a new test is to be provided.
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 }