Coverage Report - org.apache.maven.scm.provider.perforce.command.edit.PerforceEditCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceEditCommand
35 %
12/34
25 %
3/12
6
 
 1  
 package org.apache.maven.scm.provider.perforce.command.edit;
 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.maven.scm.ScmException;
 23  
 import org.apache.maven.scm.ScmFileSet;
 24  
 import org.apache.maven.scm.ScmResult;
 25  
 import org.apache.maven.scm.command.edit.AbstractEditCommand;
 26  
 import org.apache.maven.scm.command.edit.EditScmResult;
 27  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 28  
 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
 29  
 import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
 30  
 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
 31  
 import org.codehaus.plexus.util.cli.CommandLineException;
 32  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 33  
 import org.codehaus.plexus.util.cli.Commandline;
 34  
 
 35  
 import java.io.File;
 36  
 import java.io.IOException;
 37  
 import java.util.List;
 38  
 
 39  
 /**
 40  
  * @author Mike Perham
 41  
  * @version $Id: PerforceEditCommand.java 1329116 2012-04-23 08:20:47Z olamy $
 42  
  */
 43  0
 public class PerforceEditCommand
 44  
     extends AbstractEditCommand
 45  
     implements PerforceCommand
 46  
 {
 47  
     /** {@inheritDoc} */
 48  
     protected ScmResult executeEditCommand( ScmProviderRepository repo, ScmFileSet files )
 49  
         throws ScmException
 50  
     {
 51  0
         Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files );
 52  0
         PerforceEditConsumer consumer = new PerforceEditConsumer();
 53  
         try
 54  
         {
 55  0
             if ( getLogger().isDebugEnabled() )
 56  
             {
 57  0
                 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
 58  
             }
 59  
 
 60  0
             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 61  0
             int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
 62  
 
 63  0
             if ( exitCode != 0 )
 64  
             {
 65  0
                 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
 66  
 
 67  0
                 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
 68  0
                 msg.append( '\n' );
 69  0
                 msg.append( "Command line was:" + cmdLine );
 70  
 
 71  0
                 throw new CommandLineException( msg.toString() );
 72  
             }
 73  
         }
 74  0
         catch ( CommandLineException e )
 75  
         {
 76  0
             if ( getLogger().isErrorEnabled() )
 77  
             {
 78  0
                 getLogger().error( "CommandLineException " + e.getMessage(), e );
 79  
             }
 80  0
         }
 81  
 
 82  0
         if ( consumer.isSuccess() )
 83  
         {
 84  0
             return new EditScmResult( cl.toString(), consumer.getEdits() );
 85  
         }
 86  
 
 87  0
         return new EditScmResult( cl.toString(), "Unable to edit file(s)", consumer.getErrorMessage(), false );
 88  
     }
 89  
 
 90  
     public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
 91  
                                                  ScmFileSet files )
 92  
     {
 93  2
         Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
 94  
 
 95  2
         command.createArg().setValue( "edit" );
 96  
 
 97  
         try
 98  
         {
 99  2
             String candir = workingDirectory.getCanonicalPath();
 100  2
             List<File> fs = files.getFileList();
 101  6
             for ( int i = 0; i < fs.size(); i++ )
 102  
             {
 103  4
                 File file = new File( workingDirectory, fs.get( i ).getPath() );
 104  
                 // I want to use relative paths to add files to make testing
 105  
                 // simpler.
 106  
                 // Otherwise the absolute path will be different on everyone's
 107  
                 // machine
 108  
                 // and testing will be a little more painful.
 109  4
                 String canfile = file.getCanonicalPath();
 110  4
                 if ( canfile.startsWith( candir ) )
 111  
                 {
 112  4
                     canfile = canfile.substring( candir.length() + 1 );
 113  
                 }
 114  4
                 command.createArg().setValue( canfile );
 115  
             }
 116  
         }
 117  0
         catch ( IOException e )
 118  
         {
 119  0
             e.printStackTrace();
 120  2
         }
 121  2
         return command;
 122  
     }
 123  
 }