View Javadoc
1   package org.apache.maven.surefire.booter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.BufferedReader;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.io.PrintStream;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import org.apache.maven.surefire.util.ReflectionUtils;
33  import org.apache.maven.surefire.util.TestsToRun;
34  
35  /**
36   * A variant of TestsToRun that is provided with test class names
37   * from an {@link InputStream} (e.g. {@code System.in}). The method
38   * {@link #iterator()} returns an Iterator that blocks on calls to
39   * {@link Iterator#hasNext()} until new classes are available, or no more
40   * classes will be available.
41   *
42   * @author Andreas Gudian
43   */
44  class LazyTestsToRun
45      extends TestsToRun
46  {
47      private final List<Class> workQueue = new ArrayList<Class>();
48  
49      private BufferedReader inputReader;
50  
51      private boolean streamClosed = false;
52  
53      private PrintStream originalOutStream;
54  
55      /**
56       * C'tor
57       *
58       * @param testSource        source to read the tests from
59       * @param originalOutStream the output stream to use when requesting new new tests
60       */
61      public LazyTestsToRun( InputStream testSource, PrintStream originalOutStream )
62      {
63          super( Collections.<Class>emptyList() );
64  
65          this.originalOutStream = originalOutStream;
66  
67          inputReader = new BufferedReader( new InputStreamReader( testSource ) );
68      }
69  
70      protected void addWorkItem( String className )
71      {
72          synchronized ( workQueue )
73          {
74              workQueue.add( ReflectionUtils.loadClass( Thread.currentThread().getContextClassLoader(), className ) );
75          }
76      }
77  
78      protected void requestNextTest()
79      {
80          StringBuilder sb = new StringBuilder();
81          sb.append( (char) ForkingRunListener.BOOTERCODE_NEXT_TEST ).append( ",0,want more!\n" );
82          originalOutStream.print( sb.toString() );
83      }
84  
85      private class BlockingIterator
86          implements Iterator<Class>
87      {
88          private int lastPos = -1;
89  
90          public boolean hasNext()
91          {
92              int nextPos = lastPos + 1;
93              synchronized ( workQueue )
94              {
95                  if ( workQueue.size() > nextPos )
96                  {
97                      return true;
98                  }
99                  else
100                 {
101                     if ( needsToWaitForInput( nextPos ) )
102                     {
103                         requestNextTest();
104 
105                         String nextClassName;
106                         try
107                         {
108                             nextClassName = inputReader.readLine();
109                         }
110                         catch ( IOException e )
111                         {
112                             streamClosed = true;
113                             return false;
114                         }
115 
116                         if ( null == nextClassName )
117                         {
118                             streamClosed = true;
119                         }
120                         else
121                         {
122                             addWorkItem( nextClassName );
123                         }
124                     }
125 
126                     return ( workQueue.size() > nextPos );
127                 }
128             }
129         }
130 
131         private boolean needsToWaitForInput( int nextPos )
132         {
133             return workQueue.size() == nextPos && !streamClosed;
134         }
135 
136         public Class next()
137         {
138             synchronized ( workQueue )
139             {
140                 return workQueue.get( ++lastPos );
141             }
142         }
143 
144         public void remove()
145         {
146             throw new UnsupportedOperationException();
147         }
148 
149     }
150 
151     /* (non-Javadoc)
152       * @see org.apache.maven.surefire.util.TestsToRun#iterator()
153       */
154     public Iterator<Class> iterator()
155     {
156         return new BlockingIterator();
157     }
158 
159     /* (non-Javadoc)
160       * @see org.apache.maven.surefire.util.TestsToRun#toString()
161       */
162     public String toString()
163     {
164         StringBuilder sb = new StringBuilder( "LazyTestsToRun " );
165         synchronized ( workQueue )
166         {
167             sb.append( "(more items expected: " ).append( !streamClosed ).append( "): " );
168             sb.append( workQueue );
169         }
170 
171         return sb.toString();
172     }
173 
174     /* (non-Javadoc)
175      * @see org.apache.maven.surefire.util.TestsToRun#allowEagerReading()
176      */
177     public boolean allowEagerReading()
178     {
179         return false;
180     }
181 
182 }