Coverage Report - org.apache.maven.scm.provider.perforce.command.login.PerforceLoginCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceLoginCommand
14 %
4/27
10 %
1/10
5
 
 1  
 package org.apache.maven.scm.provider.perforce.command.login;
 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.ByteArrayInputStream;
 23  
 import java.io.File;
 24  
 
 25  
 import org.apache.maven.scm.CommandParameters;
 26  
 import org.apache.maven.scm.ScmException;
 27  
 import org.apache.maven.scm.ScmFileSet;
 28  
 import org.apache.maven.scm.command.login.AbstractLoginCommand;
 29  
 import org.apache.maven.scm.command.login.LoginScmResult;
 30  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 31  
 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
 32  
 import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
 33  
 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
 34  
 import org.codehaus.plexus.util.StringUtils;
 35  
 import org.codehaus.plexus.util.cli.CommandLineException;
 36  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 37  
 import org.codehaus.plexus.util.cli.Commandline;
 38  
 
 39  
 /**
 40  
  * @author Mike Perham
 41  
  * @version $Id: PerforceLoginCommand.java 1306867 2012-03-29 13:45:10Z olamy $
 42  
  */
 43  0
 public class PerforceLoginCommand
 44  
     extends AbstractLoginCommand
 45  
     implements PerforceCommand
 46  
 {
 47  
     /** {@inheritDoc} */
 48  
     public LoginScmResult executeLoginCommand( ScmProviderRepository repo, ScmFileSet files, CommandParameters params )
 49  
         throws ScmException
 50  
     {
 51  0
         Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir() );
 52  0
         PerforceLoginConsumer consumer = new PerforceLoginConsumer();
 53  0
         boolean isSuccess = false;
 54  
 
 55  
         try
 56  
         {
 57  0
             String password = repo.getPassword();
 58  0
             if ( StringUtils.isEmpty( password ) )
 59  
             {
 60  0
                 if ( getLogger().isInfoEnabled() )
 61  
                 {
 62  0
                     getLogger().info( "No password found, proceeding without it." );
 63  
                 }
 64  0
                 isSuccess = true;
 65  
             }
 66  
             else
 67  
             {
 68  0
                 CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 69  0
                 int exitCode = CommandLineUtils.executeCommandLine( cl, new ByteArrayInputStream( password.getBytes() ),
 70  
                                                                     consumer, err );
 71  0
                 isSuccess = consumer.isSuccess();
 72  
 
 73  0
                 if ( !isSuccess )
 74  
                 {
 75  0
                     String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
 76  
 
 77  0
                     StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
 78  0
                     msg.append( '\n' );
 79  0
                     msg.append( "Command line was:" + cmdLine );
 80  
 
 81  0
                     throw new CommandLineException( msg.toString() );
 82  
                 }
 83  
             }
 84  
         }
 85  0
         catch ( CommandLineException e )
 86  
         {
 87  0
             throw new ScmException( e.getMessage(), e );
 88  0
         }
 89  
 
 90  0
         return new LoginScmResult( cl.toString(), isSuccess ? "Login successful" : "Login failed",
 91  
                                    consumer.getOutput(), isSuccess );
 92  
     }
 93  
 
 94  
     public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDir )
 95  
     {
 96  1
         Commandline command = PerforceScmProvider.createP4Command( repo, workingDir );
 97  
 
 98  1
         command.createArg().setValue( "login" );
 99  1
         if ( !StringUtils.isEmpty( repo.getUser() ) )
 100  
         {
 101  0
             command.createArg().setValue( repo.getUser() );
 102  
         }
 103  1
         return command;
 104  
     }
 105  
 }