Coverage Report - org.apache.maven.plugin.invoker.InvokerSession
 
Classes in this File Line Coverage Branch Coverage Complexity
InvokerSession
0%
0/74
0%
0/38
2.308
 
 1  
 package org.apache.maven.plugin.invoker;
 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.util.ArrayList;
 23  
 import java.util.Arrays;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.maven.plugin.MojoFailureException;
 28  
 import org.apache.maven.plugin.invoker.model.BuildJob;
 29  
 import org.apache.maven.plugin.logging.Log;
 30  
 
 31  
 /**
 32  
  * Tracks a set of build jobs and their results.
 33  
  * 
 34  
  * @author Benjamin Bentmann
 35  
  */
 36  
 class InvokerSession
 37  
 {
 38  
 
 39  
     private List buildJobs;
 40  
 
 41  
     private List failedJobs;
 42  
 
 43  
     private List errorJobs;
 44  
 
 45  
     private List successfulJobs;
 46  
 
 47  
     private List skippedJobs;
 48  
 
 49  
     /**
 50  
      * Creates a new empty session.
 51  
      */
 52  
     public InvokerSession()
 53  0
     {
 54  0
         buildJobs = new ArrayList();
 55  0
     }
 56  
 
 57  
     /**
 58  
      * Creates a session that initially contains the specified build jobs.
 59  
      * 
 60  
      * @param buildJobs The build jobs to set, must not be <code>null</code>.
 61  
      */
 62  
     public InvokerSession( BuildJob[] buildJobs )
 63  0
     {
 64  0
         this.buildJobs = new ArrayList( Arrays.asList( buildJobs ) );
 65  0
     }
 66  
 
 67  
     /**
 68  
      * Adds the specified build job to this session.
 69  
      * 
 70  
      * @param buildJob The build job to add, must not be <code>null</code>.
 71  
      */
 72  
     public void addJob( BuildJob buildJob )
 73  
     {
 74  0
         buildJobs.add( buildJob );
 75  
 
 76  0
         resetStats();
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Sets the build jobs of this session.
 81  
      * 
 82  
      * @param buildJobs The build jobs to set, must not be <code>null</code>.
 83  
      */
 84  
     public void setJobs( List buildJobs )
 85  
     {
 86  0
         this.buildJobs = new ArrayList( buildJobs );
 87  
 
 88  0
         resetStats();
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Gets the build jobs in this session.
 93  
      * 
 94  
      * @return The build jobs in this session, can be empty but never <code>null</code>.
 95  
      */
 96  
     public List getJobs()
 97  
     {
 98  0
         return buildJobs;
 99  
     }
 100  
 
 101  
     /**
 102  
      * Gets the successful build jobs in this session.
 103  
      * 
 104  
      * @return The successful build jobs in this session, can be empty but never <code>null</code>.
 105  
      */
 106  
     public List getSuccessfulJobs()
 107  
     {
 108  0
         updateStats();
 109  
 
 110  0
         return successfulJobs;
 111  
     }
 112  
 
 113  
     /**
 114  
      * Gets the failed build jobs in this session.
 115  
      * 
 116  
      * @return The failed build jobs in this session, can be empty but never <code>null</code>.
 117  
      */
 118  
     public List getFailedJobs()
 119  
     {
 120  0
         updateStats();
 121  
 
 122  0
         return failedJobs;
 123  
     }
 124  
 
 125  
     /**
 126  
      * Gets the build jobs which had errors for this session.
 127  
      *
 128  
      * @return The build jobs in error for this session, can be empty but never <code>null</code>.
 129  
      */
 130  
     public List getErrorJobs()
 131  
     {
 132  0
         updateStats();
 133  
 
 134  0
         return errorJobs;
 135  
     } 
 136  
 
 137  
     /**
 138  
      * Gets the skipped build jobs in this session.
 139  
      * 
 140  
      * @return The skipped build jobs in this session, can be empty but never <code>null</code>.
 141  
      */
 142  
     public List getSkippedJobs()
 143  
     {
 144  0
         updateStats();
 145  
 
 146  0
         return skippedJobs;
 147  
     }
 148  
 
 149  
     private void resetStats()
 150  
     {
 151  0
         successfulJobs = null;
 152  0
         failedJobs = null;
 153  0
         skippedJobs = null;
 154  0
         errorJobs = null;
 155  0
     }
 156  
 
 157  
     private void updateStats()
 158  
     {
 159  0
         if ( successfulJobs != null && skippedJobs != null && failedJobs != null && errorJobs != null )
 160  
         {
 161  0
             return;
 162  
         }
 163  
 
 164  0
         successfulJobs = new ArrayList();
 165  0
         failedJobs = new ArrayList();
 166  0
         skippedJobs = new ArrayList();
 167  0
         errorJobs = new ArrayList();
 168  
 
 169  0
         for ( Iterator iterator = buildJobs.iterator(); iterator.hasNext(); )
 170  
         {
 171  0
             BuildJob buildJob = (BuildJob) iterator.next();
 172  
 
 173  0
             if ( BuildJob.Result.SUCCESS.equals( buildJob.getResult() ) )
 174  
             {
 175  0
                 successfulJobs.add( buildJob );
 176  
             }
 177  0
             else if ( BuildJob.Result.SKIPPED.equals( buildJob.getResult() ) )
 178  
             {
 179  0
                 skippedJobs.add( buildJob );
 180  
             }
 181  0
             else if ( BuildJob.Result.ERROR.equals( buildJob.getResult() ) )
 182  
             {
 183  0
                 errorJobs.add( buildJob );
 184  
             }
 185  0
             else if ( buildJob.getResult() != null )
 186  
             {
 187  0
                 failedJobs.add( buildJob );
 188  
             }
 189  
         }
 190  0
     }
 191  
 
 192  
     /**
 193  
      * Prints a summary of this session to the specified logger.
 194  
      * 
 195  
      * @param logger The mojo logger to output messages to, must not be <code>null</code>.
 196  
      * @param ignoreFailures A flag whether failures should be ignored or whether a build failure should be signaled.
 197  
      */
 198  
     public void logSummary( Log logger, boolean ignoreFailures )
 199  
     {
 200  0
         updateStats();
 201  
 
 202  0
         String separator = "-------------------------------------------------";
 203  
 
 204  0
         logger.info( separator );
 205  0
         logger.info( "Build Summary:" );
 206  0
         logger.info( "  Passed: " + successfulJobs.size() + ", Failed: " + failedJobs.size() + ", Errors: "
 207  
             + errorJobs.size() + ", Skipped: " + skippedJobs.size() );
 208  0
         logger.info( separator );
 209  
 
 210  0
         if ( !failedJobs.isEmpty() )
 211  
         {
 212  0
             String heading = "The following builds failed:";
 213  0
             if ( ignoreFailures )
 214  
             {
 215  0
                 logger.warn( heading );
 216  
             }
 217  
             else
 218  
             {
 219  0
                 logger.error( heading );
 220  
             }
 221  
 
 222  0
             for ( Iterator it = failedJobs.iterator(); it.hasNext(); )
 223  
             {
 224  0
                 BuildJob buildJob = (BuildJob) it.next();
 225  
 
 226  0
                 String item = "*  " + buildJob.getProject();
 227  0
                 if ( ignoreFailures )
 228  
                 {
 229  0
                     logger.warn( item );
 230  
                 }
 231  
                 else
 232  
                 {
 233  0
                     logger.error( item );
 234  
                 }
 235  
             }
 236  
 
 237  0
             logger.info( separator );
 238  
         }
 239  0
     }
 240  
 
 241  
     /**
 242  
      * Handles the build failures in this session.
 243  
      * 
 244  
      * @param logger The mojo logger to output messages to, must not be <code>null</code>.
 245  
      * @param ignoreFailures A flag whether failures should be ignored or whether a build failure should be signaled.
 246  
      * @throws MojoFailureException If failures are present and not ignored.
 247  
      */
 248  
     public void handleFailures( Log logger, boolean ignoreFailures )
 249  
         throws MojoFailureException
 250  
     {
 251  0
         updateStats();
 252  
 
 253  0
         if ( !failedJobs.isEmpty() )
 254  
         {
 255  0
             String message = failedJobs.size() + " build" + ( failedJobs.size() == 1 ? "" : "s" ) + " failed.";
 256  
 
 257  0
             if ( ignoreFailures )
 258  
             {
 259  0
                 logger.warn( "Ignoring that " + message );
 260  
             }
 261  
             else
 262  
             {
 263  0
                 throw new MojoFailureException( this, message, message );
 264  
             }
 265  
         }
 266  0
         if ( !errorJobs.isEmpty() )
 267  
         {
 268  0
              String message = errorJobs.size() + " build" + ( errorJobs.size() == 1 ? "" : "s" ) + " in error.";
 269  
 
 270  0
             if ( ignoreFailures )
 271  
             {
 272  0
                 logger.warn( "Ignoring that " + message );
 273  
             }
 274  
             else
 275  
             {
 276  0
                 throw new MojoFailureException( this, message, message );
 277  
             }
 278  
         }
 279  0
     }
 280  
 
 281  
 }