Coverage Report - org.apache.maven.surefire.booter.LazyTestsToRun
 
Classes in this File Line Coverage Branch Coverage Complexity
LazyTestsToRun
0%
0/23
0%
0/2
1,8
LazyTestsToRun$1
N/A
N/A
1,8
LazyTestsToRun$BlockingIterator
0%
0/23
0%
0/12
1,8
 
 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  0
 class LazyTestsToRun
 45  
     extends TestsToRun
 46  
 {
 47  0
     private final List<Class> workQueue = new ArrayList<Class>();
 48  
 
 49  
     private BufferedReader inputReader;
 50  
 
 51  0
     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  0
         super( Collections.<Class>emptyList() );
 64  
 
 65  0
         this.originalOutStream = originalOutStream;
 66  
 
 67  0
         inputReader = new BufferedReader( new InputStreamReader( testSource ) );
 68  0
     }
 69  
 
 70  
     protected void addWorkItem( String className )
 71  
     {
 72  0
         synchronized ( workQueue )
 73  
         {
 74  0
             workQueue.add( ReflectionUtils.loadClass( Thread.currentThread().getContextClassLoader(), className ) );
 75  0
         }
 76  0
     }
 77  
 
 78  
     protected void requestNextTest()
 79  
     {
 80  0
         StringBuilder sb = new StringBuilder();
 81  0
         sb.append( (char) ForkingRunListener.BOOTERCODE_NEXT_TEST ).append( ",0,want more!\n" );
 82  0
         originalOutStream.print( sb.toString() );
 83  0
     }
 84  
 
 85  0
     private class BlockingIterator
 86  
         implements Iterator<Class>
 87  
     {
 88  0
         private int lastPos = -1;
 89  
 
 90  
         public boolean hasNext()
 91  
         {
 92  0
             int nextPos = lastPos + 1;
 93  0
             synchronized ( workQueue )
 94  
             {
 95  0
                 if ( workQueue.size() > nextPos )
 96  
                 {
 97  0
                     return true;
 98  
                 }
 99  
                 else
 100  
                 {
 101  0
                     if ( needsToWaitForInput( nextPos ) )
 102  
                     {
 103  0
                         requestNextTest();
 104  
 
 105  
                         String nextClassName;
 106  
                         try
 107  
                         {
 108  0
                             nextClassName = inputReader.readLine();
 109  
                         }
 110  0
                         catch ( IOException e )
 111  
                         {
 112  0
                             streamClosed = true;
 113  0
                             return false;
 114  0
                         }
 115  
 
 116  0
                         if ( null == nextClassName )
 117  
                         {
 118  0
                             streamClosed = true;
 119  
                         }
 120  
                         else
 121  
                         {
 122  0
                             addWorkItem( nextClassName );
 123  
                         }
 124  
                     }
 125  
 
 126  0
                     return ( workQueue.size() > nextPos );
 127  
                 }
 128  0
             }
 129  
         }
 130  
 
 131  
         private boolean needsToWaitForInput( int nextPos )
 132  
         {
 133  0
             return workQueue.size() == nextPos && !streamClosed;
 134  
         }
 135  
 
 136  
         public Class next()
 137  
         {
 138  0
             synchronized ( workQueue )
 139  
             {
 140  0
                 return workQueue.get( ++lastPos );
 141  0
             }
 142  
         }
 143  
 
 144  
         public void remove()
 145  
         {
 146  0
             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  0
         return new BlockingIterator();
 157  
     }
 158  
 
 159  
     /* (non-Javadoc)
 160  
       * @see org.apache.maven.surefire.util.TestsToRun#toString()
 161  
       */
 162  
     public String toString()
 163  
     {
 164  0
         StringBuilder sb = new StringBuilder( "LazyTestsToRun " );
 165  0
         synchronized ( workQueue )
 166  
         {
 167  0
             sb.append( "(more items expected: " ).append( !streamClosed ).append( "): " );
 168  0
             sb.append( workQueue );
 169  0
         }
 170  
 
 171  0
         return sb.toString();
 172  
     }
 173  
 
 174  
     /* (non-Javadoc)
 175  
      * @see org.apache.maven.surefire.util.TestsToRun#allowEagerReading()
 176  
      */
 177  
     public boolean allowEagerReading() {
 178  0
         return false;
 179  
     }
 180  
 
 181  
 }