Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 408   Methods: 15
NCLOC: 184   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
StarTeamCheckin.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.taskdefs.optional.starteam;
 55   
 
 56   
 import com.starbase.starteam.File;
 57   
 import com.starbase.starteam.Folder;
 58   
 import com.starbase.starteam.Item;
 59   
 import com.starbase.starteam.Status;
 60   
 import com.starbase.starteam.TypeNames;
 61   
 import com.starbase.starteam.View;
 62   
 import com.starbase.starteam.ViewConfiguration;
 63   
 import java.io.FileInputStream;
 64   
 import java.io.IOException;
 65   
 import java.util.Enumeration;
 66   
 import java.util.Hashtable;
 67   
 import org.apache.tools.ant.BuildException;
 68   
 import org.apache.tools.ant.Project;
 69   
 
 70   
 /**
 71   
  * Checks files into a StarTeam project.  
 72   
  * Optionally adds files and in the local tree that
 73   
  * are not managed by the repository to its control.
 74   
  * Created: Sat Dec 15 20:26:07 2001
 75   
  *
 76   
  * @author <a href="mailto:scohen@localhost.localdomain">Steve Cohen</a>
 77   
  * @version 1.0
 78   
  *
 79   
  * @ant.task name="stcheckin" category="scm" product="Starteam"
 80   
  */
 81   
 public class StarTeamCheckin extends TreeBasedTask {
 82   
 
 83  0
     public StarTeamCheckin() {
 84   
         // we want this to have a false default, unlike for Checkin.
 85  0
         setRecursive(false);
 86   
     }
 87   
 
 88   
     private boolean createFolders = true;
 89   
 
 90   
     /**
 91   
      * The comment which will be stored with the checkin.
 92   
      */
 93   
     private String comment = null;
 94   
 
 95   
     /**
 96   
      * holder for the add Uncontrolled attribute.  If true, all
 97   
      * local files not in StarTeam will be added to the repository.
 98   
      */
 99   
     private boolean addUncontrolled = false;
 100   
 
 101   
     /**
 102   
      * Sets the value of createFolders
 103   
      *
 104   
      * @param argCreateFolders Value to assign to this.createFolders
 105   
      */
 106  0
     public void setCreateFolders(boolean argCreateFolders) {
 107  0
         this.createFolders = argCreateFolders;
 108   
     }
 109   
 
 110   
 
 111   
     /**
 112   
      * Get the comment attribute for this operation
 113   
      * @return value of comment.
 114   
      */
 115  0
     public String getComment() {
 116  0
         return this.comment;
 117   
     }
 118   
 
 119   
     /**
 120   
      * Optional checkin comment to be saved with the file.
 121   
      * @param comment  Value to assign to comment.
 122   
      */
 123  0
     public void setComment(String comment) {
 124  0
         this.comment = comment;
 125   
     }
 126   
 
 127   
     /**
 128   
      * Get the value of addUncontrolled.
 129   
      * @return value of addUncontrolled.
 130   
      */
 131  0
     public boolean isAddUncontrolled() {
 132  0
         return this.addUncontrolled;
 133   
     }
 134   
 
 135   
     /**
 136   
      * if true, any files or folders NOT in StarTeam will be 
 137   
      * added to the repository.  Defaults to "false".
 138   
      * @param addUncontrolled  Value to assign to addUncontrolled.
 139   
      */
 140  0
     public void setAddUncontrolled(boolean addUncontrolled) {
 141  0
         this.addUncontrolled = addUncontrolled;
 142   
     }
 143   
 
 144   
     /**
 145   
      * This attribute tells whether unlocked files on checkin (so that
 146   
      * other users may access them) checkout or to leave the checkout status
 147   
      * alone (default).
 148   
      * @see #setUnlocked(boolean)
 149   
      */
 150   
     private int lockStatus = Item.LockType.UNCHANGED;
 151   
 
 152   
     /**
 153   
      * Set to do an unlocked checkout; optional, default is false;
 154   
      * If true, file will be unlocked so that other users may
 155   
      * change it.  If false, lock status will not change.     
 156   
      * @param v  true means do an unlocked checkout
 157   
      *           false means leave status alone.
 158   
      */
 159  0
     public void setUnlocked(boolean v) {
 160  0
         if (v) {
 161  0
             this.lockStatus = Item.LockType.UNLOCKED;
 162   
         } else {
 163  0
             this.lockStatus = Item.LockType.UNCHANGED;
 164   
         }
 165   
     }
 166   
 
 167   
     /**
 168   
      * Override of base-class abstract function creates an
 169   
      * appropriately configured view.  For checkins this is
 170   
      * always the current or "tip" view.
 171   
      *
 172   
      * @param raw the unconfigured <code>View</code>
 173   
      * @return the snapshot <code>View</code> appropriately configured.
 174   
      */
 175  0
     protected View createSnapshotView(View raw) {
 176  0
         return new View(raw, ViewConfiguration.createTip());
 177   
     }
 178   
 
 179   
     /**
 180   
      * Implements base-class abstract function to define tests for
 181   
      * any preconditons required by the task.
 182   
      *
 183   
      * @exception BuildException thrown if both rootLocalFolder 
 184   
      * and viewRootLocalFolder are defined
 185   
      */
 186  0
     protected void testPreconditions() throws BuildException {
 187   
     }
 188   
     /**
 189   
      * Implements base-class abstract function to emit to the log an 
 190   
      * entry describing the parameters that will be used by this operation.
 191   
      *
 192   
      * @param starteamrootFolder
 193   
      *               root folder in StarTeam for the operation
 194   
      * @param targetrootFolder
 195   
      *               root local folder for the operation 
 196   
      * (whether specified by the user or not).
 197   
      */
 198  0
     protected void logOperationDescription(
 199   
         Folder starteamrootFolder, java.io.File targetrootFolder) 
 200   
     {
 201  0
         log((this.isRecursive() ? "Recursive" : "Non-recursive")
 202   
             +" Checkin from" 
 203   
             + (null == getRootLocalFolder() ? " (default): " : ": ") 
 204   
             + targetrootFolder.getAbsolutePath());
 205   
         
 206  0
         log("Checking in to: " + starteamrootFolder.getFolderHierarchy());
 207  0
         logIncludes();
 208  0
         logExcludes();
 209   
 
 210  0
         if (this.lockStatus == Item.LockType.UNLOCKED) {
 211  0
             log("  Items will be checked in unlocked.");
 212   
         } 
 213   
         else {
 214  0
             log("  Items will be checked in with no change in lock status.");
 215   
         }
 216   
 
 217  0
         if (this.isForced()) {
 218  0
             log("  Items will be checked in in accordance with repository status and regardless of lock status.");
 219   
         } 
 220   
         else {
 221  0
             log("  Items will be checked in regardless of repository status only if locked." );
 222   
         }
 223   
 
 224   
 
 225   
     }
 226   
 
 227   
     /**
 228   
      * Implements base-class abstract function to perform the checkout
 229   
      * operation on the files in each folder of the tree.
 230   
      *
 231   
      * @param starteamFolder the StarTeam folder to which files
 232   
      *                       will be checked in
 233   
      * @param localFolder local folder from which files will be checked in
 234   
      * @exception BuildException if any error occurs
 235   
      */
 236  0
     protected void visit(Folder starteamFolder, java.io.File targetFolder)
 237   
             throws BuildException 
 238   
     {
 239  0
         try {
 240  0
             if (null != getRootLocalFolder()) {
 241  0
                 starteamFolder.setAlternatePathFragment(
 242   
                     targetFolder.getAbsolutePath());
 243   
             }
 244   
 
 245  0
             Folder[] foldersList = starteamFolder.getSubFolders();
 246  0
             Item[] stFiles = starteamFolder.getItems(getTypeNames().FILE);
 247   
             
 248   
             // note, it's important to scan the items BEFORE we make the
 249   
             // UnmatchedFileMap because that creates a bunch of NEW
 250   
             // folders and files (unattached to repository) and we
 251   
             // don't want to include those in our traversal.
 252   
 
 253  0
             UnmatchedFileMap ufm = 
 254   
                 new CheckinMap().init(
 255   
                     targetFolder.getAbsoluteFile(), starteamFolder);
 256   
 
 257   
 
 258  0
             for (int i = 0, size = foldersList.length; i < size; i++) {
 259  0
                 Folder stFolder = foldersList[i];
 260  0
                 java.io.File subfolder = 
 261   
                     new java.io.File(targetFolder, stFolder.getName());
 262   
 
 263  0
                 ufm.removeControlledItem(subfolder);
 264   
 
 265  0
                 if (isRecursive()) {
 266  0
                     visit(stFolder, subfolder);
 267   
                 }
 268   
             }
 269   
 
 270   
            
 271  0
             for (int i = 0, size = stFiles.length; i < size; i++) {
 272  0
                 com.starbase.starteam.File stFile = 
 273   
                     (com.starbase.starteam.File) stFiles[i];
 274  0
                 processFile(stFile);
 275   
                 
 276  0
                 ufm.removeControlledItem(
 277   
                     new java.io.File(targetFolder, stFile.getName()));
 278   
             }
 279   
 
 280  0
             if (this.addUncontrolled) {
 281  0
                 ufm.processUncontrolledItems();
 282   
             }
 283   
 
 284   
         } catch (IOException e) {
 285  0
             throw new BuildException(e);
 286   
         }
 287   
 
 288   
     }
 289   
 
 290   
     /**
 291   
      * provides a string showing from and to full paths for logging
 292   
      * 
 293   
      * @param remotefile the Star Team file being processed.
 294   
      * 
 295   
      * @return a string showing from and to full paths
 296   
      */
 297  0
     private String describeCheckin(com.starbase.starteam.File remotefile)
 298   
     {
 299  0
         StringBuffer sb = new StringBuffer();
 300  0
         sb.append(remotefile.getFullName())
 301   
           .append(" --> ")
 302   
           .append(getFullRepositoryPath(remotefile));
 303  0
         return sb.toString();
 304   
     }
 305   
 
 306   
     /**
 307   
      * Processes (checks-out) <code>stFiles</code>files from StarTeam folder.
 308   
      *
 309   
      * @param eachFile repository file to process
 310   
      * @param targetFolder a java.io.File (Folder) to work
 311   
      * @throws IOException when StarTeam API fails to work with files
 312   
      */
 313  0
     private void processFile(com.starbase.starteam.File eachFile)
 314   
         throws IOException {
 315  0
         String filename = eachFile.getName();
 316   
 
 317   
         // If the file doesn't pass the include/exclude tests, skip it.
 318  0
         if (!shouldProcess(filename)) {
 319  0
             log("Excluding " + getFullRepositoryPath(eachFile));
 320  0
                 return;
 321   
         }
 322   
 
 323  0
         boolean checkin = true;
 324  0
         int fileStatus = (eachFile.getStatus());
 325   
 
 326   
         // We try to update the status once to give StarTeam
 327   
         // another chance.
 328   
 
 329  0
         if (fileStatus == Status.MERGE || fileStatus == Status.UNKNOWN) {
 330  0
             eachFile.updateStatus(true, true);
 331  0
             fileStatus = (eachFile.getStatus());
 332   
         }
 333   
 
 334  0
         if (fileStatus == Status.MODIFIED) {
 335  0
             log("Checking in: " + describeCheckin(eachFile));
 336   
         } 
 337  0
         else if (fileStatus == Status.MISSING) {
 338  0
             log("Local file missing: " + describeCheckin(eachFile));
 339  0
             checkin = false;
 340   
         }
 341   
         else {
 342  0
             if (isForced()) {
 343  0
                 log("Forced checkin of " + describeCheckin(eachFile) + 
 344   
                     " over status " + Status.name(fileStatus));
 345   
             } else {
 346  0
                 log("Skipping: " + getFullRepositoryPath(eachFile) + 
 347   
                     " - status: " + Status.name(fileStatus));
 348  0
                 checkin = false;
 349   
             }
 350   
         }
 351  0
         if (checkin) {
 352  0
             eachFile.checkin(this.comment, this.lockStatus, 
 353   
                              this.isForced(), true, true);
 354   
         }
 355   
     }
 356   
 
 357   
     /**
 358   
      * handles the deletion of uncontrolled items
 359   
      */
 360   
     private class CheckinMap extends UnmatchedFileMap {
 361  0
         protected boolean isActive() {
 362  0
             return StarTeamCheckin.this.addUncontrolled;
 363   
         }
 364   
 
 365   
     
 366   
         /**
 367   
          * This override adds all its members to the repository.  It is assumed 
 368   
          * that this method will not be called until all the items in the 
 369   
          * corresponding folder have been processed, and that the internal map
 370   
          * will contain only uncontrolled items.
 371   
          */
 372  0
         void processUncontrolledItems() throws BuildException {
 373  0
             if (this.isActive()) {
 374  0
                 Enumeration e = this.keys();
 375  0
                 while (e.hasMoreElements()) {
 376  0
                     java.io.File local = (java.io.File) e.nextElement();
 377  0
                     Item remoteItem = (Item) this.get(local);
 378  0
                     remoteItem.update();
 379   
     
 380   
                     // once we find a folder that isn't in the repository, 
 381   
                     // we know we can add it.
 382  0
                     if (local.isDirectory()) {
 383  0
                         Folder folder = (Folder) remoteItem;
 384  0
                         log("Added uncontrolled folder " 
 385   
                             + folder.getFolderHierarchy()
 386   
                             + " from " + local.getAbsoluteFile());
 387  0
                         if (isRecursive()) {
 388  0
                             UnmatchedFileMap submap = 
 389   
                                 new CheckinMap().init(local, folder);
 390  0
                             submap.processUncontrolledItems();
 391   
                         }
 392   
                     } else {
 393  0
                         com.starbase.starteam.File remoteFile =
 394   
                             (com.starbase.starteam.File) remoteItem;
 395  0
                         log("Added uncontrolled file " 
 396   
                             + TreeBasedTask.getFullRepositoryPath(remoteFile)
 397   
                             + " from " + local.getAbsoluteFile());
 398   
     
 399   
                     }
 400   
                 }
 401   
             }
 402   
         }
 403   
     }
 404   
 
 405   
 }
 406   
 
 407   
 
 408