Coverage Report - org.apache.maven.scm.provider.clearcase.command.add.ClearCaseAddCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
ClearCaseAddCommand
30 %
11/36
12 %
2/16
6,5
 
 1  
 package org.apache.maven.scm.provider.clearcase.command.add;
 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.add.AbstractAddCommand;
 26  
 import org.apache.maven.scm.command.status.StatusScmResult;
 27  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 28  
 import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
 29  
 import org.apache.maven.scm.provider.clearcase.command.edit.ClearCaseEditCommand;
 30  
 import org.codehaus.plexus.util.cli.CommandLineException;
 31  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 32  
 import org.codehaus.plexus.util.cli.Commandline;
 33  
 
 34  
 import java.io.File;
 35  
 
 36  
 /**
 37  
  * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
 38  
  * @version $Id: ClearCaseAddCommand.java 1054126 2010-12-31 15:18:11Z olamy $
 39  
  */
 40  0
 public class ClearCaseAddCommand
 41  
     extends AbstractAddCommand
 42  
     implements ClearCaseCommand
 43  
 {
 44  
     /** {@inheritDoc} */
 45  
     protected ScmResult executeAddCommand( ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet,
 46  
                                            String string, boolean b )
 47  
         throws ScmException
 48  
     {
 49  0
         if ( getLogger().isDebugEnabled() )
 50  
         {
 51  0
             getLogger().debug( "executing add command..." );
 52  
         }
 53  0
         Commandline cl = createCommandLine( scmFileSet );
 54  
 
 55  0
         ClearCaseAddConsumer consumer = new ClearCaseAddConsumer( getLogger() );
 56  
 
 57  0
         CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
 58  
 
 59  
         int exitCode;
 60  
 
 61  
         try
 62  
         {
 63  
             // First we need to 'check out' the current directory
 64  0
             Commandline checkoutCurrentDirCommandLine =
 65  
                 ClearCaseEditCommand.createCheckoutCurrentDirCommandLine( scmFileSet );
 66  0
             if ( getLogger().isDebugEnabled() )
 67  
             {
 68  0
                 getLogger().debug(
 69  
                                    "Executing: "
 70  
                                        + checkoutCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
 71  
                                        + ">>" + checkoutCurrentDirCommandLine.toString() );
 72  
             }
 73  0
             exitCode = CommandLineUtils.executeCommandLine( checkoutCurrentDirCommandLine,
 74  
                                                             new CommandLineUtils.StringStreamConsumer(), stderr );
 75  
 
 76  0
             if ( exitCode == 0 )
 77  
             {
 78  
                 // Then we add the file
 79  0
                 if ( getLogger().isDebugEnabled() )
 80  
                 {
 81  0
                     getLogger().debug(
 82  
                                        "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
 83  
                                            + cl.toString() );
 84  
                 }
 85  0
                 exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
 86  
 
 87  0
                 if ( exitCode == 0 )
 88  
                 {
 89  
                     // Then we check in the current directory again.
 90  0
                     Commandline checkinCurrentDirCommandLine =
 91  
                         ClearCaseEditCommand.createCheckinCurrentDirCommandLine( scmFileSet );
 92  0
                     if ( getLogger().isDebugEnabled() )
 93  
                     {
 94  0
                         getLogger().debug(
 95  
                                            "Executing: "
 96  
                                                + checkinCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
 97  
                                                + ">>" + checkinCurrentDirCommandLine.toString() );
 98  
                     }
 99  0
                     exitCode = CommandLineUtils.executeCommandLine( checkinCurrentDirCommandLine,
 100  
                                                                     new CommandLineUtils.StringStreamConsumer(),
 101  
                                                                     stderr );
 102  
                 }
 103  
             }
 104  
         }
 105  0
         catch ( CommandLineException ex )
 106  
         {
 107  0
             throw new ScmException( "Error while executing clearcase command.", ex );
 108  0
         }
 109  
 
 110  0
         if ( exitCode != 0 )
 111  
         {
 112  0
             return new StatusScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
 113  
         }
 114  
 
 115  0
         return new StatusScmResult( cl.toString(), consumer.getAddedFiles() );
 116  
     }
 117  
 
 118  
     // ----------------------------------------------------------------------
 119  
     //
 120  
     // ----------------------------------------------------------------------
 121  
 
 122  
     public static Commandline createCommandLine( ScmFileSet scmFileSet )
 123  
     {
 124  1
         Commandline command = new Commandline();
 125  
 
 126  1
         File workingDirectory = scmFileSet.getBasedir();
 127  
 
 128  1
         command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
 129  
 
 130  1
         command.setExecutable( "cleartool" );
 131  
 
 132  1
         command.createArg().setValue( "mkelem" );
 133  
 
 134  1
         command.createArg().setValue( "-c" );
 135  
 
 136  1
         command.createArg().setValue( "new file" );
 137  
 
 138  1
         command.createArg().setValue( "-nco" );
 139  
 
 140  1
         for ( File file : scmFileSet.getFileList() )
 141  
         {
 142  1
             command.createArg().setValue( file.getName() );
 143  
         }
 144  
 
 145  1
         return command;
 146  
     }
 147  
 
 148  
 }