Coverage Report - org.apache.maven.scm.provider.perforce.command.tag.PerforceTagConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceTagConsumer
80 %
21/26
77 %
7/9
2
 
 1  
 package org.apache.maven.scm.provider.perforce.command.tag;
 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.List;
 24  
 
 25  
 import org.apache.maven.scm.ScmFile;
 26  
 import org.apache.maven.scm.ScmFileStatus;
 27  
 import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
 28  
 import org.apache.regexp.RE;
 29  
 import org.apache.regexp.RESyntaxException;
 30  
 import org.codehaus.plexus.util.cli.StreamConsumer;
 31  
 
 32  
 /**
 33  
  * @author Mike Perham
 34  
  * @author Olivier Lamy
 35  
  * @version $Id: PerforceTagConsumer.java 1056959 2011-01-09 15:02:55Z olamy $
 36  
  */
 37  
 public class PerforceTagConsumer
 38  
     extends AbstractPerforceConsumer
 39  
     implements StreamConsumer
 40  
 {
 41  
 
 42  
     private static final String LABEL_PATTERN = "^Label ([^ ]+) saved.$";
 43  
 
 44  
     private static final String SYNC_PATTERN = "^([^#]+)#\\d+ - (.*)";
 45  
 
 46  
     public static final int STATE_CREATE = 1;
 47  
 
 48  
     public static final int STATE_SYNC = 2;
 49  
 
 50  
     public static final int STATE_ERROR = 3;
 51  
 
 52  2
     private int currentState = STATE_CREATE;
 53  
 
 54  2
     private List<ScmFile> tagged = new ArrayList<ScmFile>();
 55  
 
 56  
     private RE syncRegexp;
 57  
 
 58  
     public PerforceTagConsumer()
 59  2
     {
 60  
         try
 61  
         {
 62  2
             syncRegexp = new RE( SYNC_PATTERN );
 63  
         }
 64  0
         catch ( RESyntaxException ignored )
 65  
         {
 66  0
             ignored.printStackTrace();
 67  2
         }
 68  2
     }
 69  
 
 70  
     /**
 71  
      * Return a list of Strings formatted like:
 72  
      * <p/>
 73  
      * <pre>
 74  
      * //depot/modules/cordoba/runtime-ear/pom.xml
 75  
      * //depot/modules/cordoba/runtime-ear/.runtime
 76  
      * </pre>
 77  
      */
 78  
     public List<ScmFile> getTagged()
 79  
     {
 80  1
         return tagged;
 81  
     }
 82  
 
 83  
     /*
 84  
      * We consume the output from 'p4 label -i' and 'p4 labelsync -l <tag>
 85  
      * <files...>'
 86  
      */
 87  
     /*
 88  
      * Label maven-scm-test saved.
 89  
      */
 90  
     /*
 91  
      * //depot/modules/cordoba/runtime-ear/pom.xml#4 - added
 92  
      * //depot/modules/cordoba/runtime-ear/.runtime#1 - added
 93  
      */
 94  
     /** {@inheritDoc} */
 95  
     public void consumeLine( String line )
 96  
     {
 97  5
         switch ( currentState )
 98  
         {
 99  
             case STATE_CREATE:
 100  2
                 if ( !new RE( LABEL_PATTERN ).match( line ) )
 101  
                 {
 102  0
                     error( line );
 103  0
                     break;
 104  
                 }
 105  2
                 currentState = STATE_SYNC;
 106  2
                 break;
 107  
             case STATE_SYNC:
 108  3
                 if ( !syncRegexp.match( line ) )
 109  
                 {
 110  1
                     error( line );
 111  1
                     break;
 112  
                 }
 113  2
                 tagged.add( new ScmFile( syncRegexp.getParen( 1 ), ScmFileStatus.TAGGED ) );
 114  2
                 break;
 115  
             default:
 116  0
                 error( line );
 117  
                 break;
 118  
         }
 119  5
     }
 120  
 
 121  
     private void error( String line )
 122  
     {
 123  1
         currentState = STATE_ERROR;
 124  1
         output.println( line );
 125  1
     }
 126  
 
 127  
     public boolean isSuccess()
 128  
     {
 129  2
         return currentState == STATE_SYNC;
 130  
     }
 131  
 
 132  
 }