Coverage Report - org.apache.maven.scm.provider.perforce.command.checkin.PerforceCheckInConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceCheckInConsumer
81 %
39/48
76 %
16/21
3,6
 
 1  
 package org.apache.maven.scm.provider.perforce.command.checkin;
 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 org.apache.regexp.RE;
 23  
 import org.apache.regexp.RESyntaxException;
 24  
 import org.codehaus.plexus.util.cli.StreamConsumer;
 25  
 
 26  
 import java.io.PrintWriter;
 27  
 import java.io.StringWriter;
 28  
 
 29  
 /**
 30  
  * @author Mike Perham
 31  
  * @version $Id: PerforceChangeLogConsumer.java 331276 2005-11-07 15:04:54Z
 32  
  *          evenisse $
 33  
  */
 34  
 public class PerforceCheckInConsumer
 35  
     implements StreamConsumer
 36  
 {
 37  
     private static final String CREATED_PATTERN = "^Change \\d+ created .+$";
 38  
 
 39  
     private static final String SUBMITTING_PATTERN = "^Submitting change \\d+\\.$";
 40  
 
 41  
     private static final String LOCKING_PATTERN = "^Locking \\d+ files \\.\\.\\.$";
 42  
 
 43  
     private static final String OP_PATTERN = "^[a-z]+ //[^#]+#\\d+$";
 44  
 
 45  
     // SCM-181 Two possible messages:
 46  
     // "Change 94821 renamed change 94823 and submitted."
 47  
     // "Change 94821 submitted."
 48  
     private static final String COMPLETE_PATTERN = "^Change \\d+ .*submitted.$";
 49  
 
 50  
     public static final int STATE_CREATED = 1;
 51  
 
 52  
     public static final int STATE_SUBMITTING = 2;
 53  
 
 54  
     public static final int STATE_LOCKING = 3;
 55  
 
 56  
     public static final int STATE_OP = 4;
 57  
 
 58  
     public static final int STATE_COMPLETE = 5;
 59  
 
 60  
     public static final int STATE_ERROR = 6;
 61  
 
 62  3
     private StringWriter errors = new StringWriter();
 63  
 
 64  3
     private PrintWriter errorOutput = new PrintWriter( errors );
 65  
 
 66  3
     private int currentState = STATE_CREATED;
 67  
 
 68  
     private RE opRegexp;
 69  
 
 70  
     public PerforceCheckInConsumer()
 71  3
     {
 72  
         try
 73  
         {
 74  3
             opRegexp = new RE( OP_PATTERN );
 75  
         }
 76  0
         catch ( RESyntaxException ignored )
 77  
         {
 78  0
             ignored.printStackTrace();
 79  3
         }
 80  3
     }
 81  
 
 82  
     /*
 83  
      * Change 80835 created with 1 open file(s). Submitting change 80835.
 84  
      * Locking 1 files ... add //depot/modules/cordoba/runtime-ear/foo.xml#1
 85  
      * Change 80835 submitted.
 86  
      */
 87  
     /*
 88  
      * Submitting change 80837. Locking 1 files ... edit
 89  
      * //depot/modules/cordoba/runtime-ear/Foo.java#2 Submit validation failed --
 90  
      * fix problems then use 'p4 submit -c 80837'. 'checkstyle' validation
 91  
      * failed:
 92  
      *
 93  
      * depot/modules/cordoba/runtime-ear/Foo.java:3:1: Got an exception -
 94  
      * expecting EOF, found '}'
 95  
      */
 96  
     /** {@inheritDoc} */
 97  
     public void consumeLine( String line )
 98  
     {
 99  18
         if ( line.startsWith( "... " ) )
 100  
         {
 101  
             //TODO log this somehow?
 102  
             //System.out.println("Perforce: " + line);
 103  0
             return;
 104  
         }
 105  
 
 106  18
         switch ( currentState )
 107  
         {
 108  
             case STATE_CREATED:
 109  3
                 boolean created = new RE( CREATED_PATTERN ).match( line );
 110  3
                 if ( created )
 111  
                 {
 112  3
                     currentState++;
 113  3
                     break;
 114  
                 }
 115  0
                 error( line );
 116  0
                 break;
 117  
             case STATE_SUBMITTING:
 118  3
                 boolean submitting = new RE( SUBMITTING_PATTERN ).match( line );
 119  3
                 if ( submitting )
 120  
                 {
 121  3
                     currentState++;
 122  3
                     break;
 123  
                 }
 124  0
                 error( line );
 125  0
                 break;
 126  
             case STATE_LOCKING:
 127  3
                 boolean locked = new RE( LOCKING_PATTERN ).match( line );
 128  3
                 if ( locked )
 129  
                 {
 130  3
                     currentState++;
 131  3
                     break;
 132  
                 }
 133  0
                 error( line );
 134  0
                 break;
 135  
             case STATE_OP:
 136  6
                 boolean operation = opRegexp.match( line );
 137  6
                 if ( operation )
 138  
                 {
 139  3
                     break;
 140  
                 }
 141  3
                 else if ( new RE( COMPLETE_PATTERN ).match( line ) )
 142  
                 {
 143  2
                     currentState++;
 144  2
                     break;
 145  
                 }
 146  1
                 error( line );
 147  1
                 break;
 148  
             case STATE_ERROR:
 149  3
                 error( line );
 150  3
                 break;
 151  
             default:
 152  
         }
 153  18
     }
 154  
 
 155  
     private void error( String line )
 156  
     {
 157  
         //        if (currentState != STATE_ERROR) {
 158  
         //            System.out.println("Unable to match: " + line + " State: " +
 159  
         // currentState);
 160  
         //            new Exception().printStackTrace();
 161  
         //        }
 162  4
         currentState = STATE_ERROR;
 163  4
         errorOutput.println( line );
 164  4
     }
 165  
 
 166  
     public boolean isSuccess()
 167  
     {
 168  3
         return currentState == STATE_COMPLETE;
 169  
     }
 170  
 
 171  
     public String getOutput()
 172  
     {
 173  7
         errorOutput.flush();
 174  7
         errors.flush();
 175  7
         return errors.toString();
 176  
     }
 177  
 }