Coverage report

  %line %branch
nl.toolforge.karma.core.cmd.impl.UpdateModuleCommand
0% 
0% 

 1  
 /*
 2  
 Karma core - Core of the Karma application
 3  
 Copyright (C) 2004  Toolforge <www.toolforge.nl>
 4  
 
 5  
 This library is free software; you can redistribute it and/or
 6  
 modify it under the terms of the GNU Lesser General Public
 7  
 License as published by the Free Software Foundation; either
 8  
 version 2.1 of the License, or (at your option) any later version.
 9  
 
 10  
 This library is distributed in the hope that it will be useful,
 11  
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 Lesser General Public License for more details.
 14  
 
 15  
 You should have received a copy of the GNU Lesser General Public
 16  
 License along with this library; if not, write to the Free Software
 17  
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 */
 19  
 package nl.toolforge.karma.core.cmd.impl;
 20  
 
 21  
 import nl.toolforge.karma.core.Patch;
 22  
 import nl.toolforge.karma.core.Version;
 23  
 import nl.toolforge.karma.core.cmd.CommandDescriptor;
 24  
 import nl.toolforge.karma.core.cmd.CommandException;
 25  
 import nl.toolforge.karma.core.cmd.CommandResponse;
 26  
 import nl.toolforge.karma.core.cmd.DefaultCommand;
 27  
 import nl.toolforge.karma.core.cmd.event.MessageEvent;
 28  
 import nl.toolforge.karma.core.cmd.event.SimpleMessage;
 29  
 import nl.toolforge.karma.core.manifest.Manifest;
 30  
 import nl.toolforge.karma.core.manifest.ManifestException;
 31  
 import nl.toolforge.karma.core.manifest.ReleaseManifest;
 32  
 import nl.toolforge.karma.core.module.Module;
 33  
 import nl.toolforge.karma.core.vc.AuthenticationException;
 34  
 import nl.toolforge.karma.core.vc.RunnerFactory;
 35  
 import nl.toolforge.karma.core.vc.VersionControlException;
 36  
 import nl.toolforge.karma.core.vc.cvsimpl.CVSRunner;
 37  
 import nl.toolforge.karma.core.vc.cvsimpl.Utils;
 38  
 
 39  
 import java.util.regex.PatternSyntaxException;
 40  
 
 41  
 /**
 42  
  * <p>This command updates a module on a developers' local system. When the module has not been updated before, the
 43  
  * module will be checked out (this is transparent for the user). The syntax for this command is:
 44  
  * <p/>
 45  
  * <pre>update-module -m, --module-name &lt;module-name&gt; [ -v &lt;version-number&gt; ]</pre
 46  
  * <p/>
 47  
  * <p>The <code>m</code> option specifies the module that should be updated. The <code>v</code> option specifies a
 48  
  * specific version that should be fetched. The <code>v</code> option implies that the module is updated to
 49  
  * <code>STATIC</code> state, regardless of the configuration for the module in the manifest. This state can be revoked
 50  
  * by updating the module without specifying the <code>v</code> option. Karma will apply pattern rules to determine the
 51  
  * actual symbolic name that is used in the version control system for the version.
 52  
  *
 53  
  * @author D.A. Smedes
 54  
  * @version $Id: UpdateModuleCommand.java,v 1.56 2004/11/10 23:53:09 asmedes Exp $
 55  
  */
 56  
 public class UpdateModuleCommand extends DefaultCommand {
 57  
 
 58  0
   protected CommandResponse response = new CommandResponse();
 59  
 
 60  
   /**
 61  
    * Creates a <code>UpdateModuleCommand</code> for module <code>module</code> that should be updated. This module
 62  
    * requires an <code>Option</code>
 63  
    *
 64  
    * @param descriptor The command descriptor for this command.
 65  
    */
 66  
   public UpdateModuleCommand(CommandDescriptor descriptor) {
 67  0
     super(descriptor);
 68  0
   }
 69  
 
 70  
   /**
 71  
    * This command will update the module from the version control system. An update is done when
 72  
    * the module is already present, otherwise a checkout will be performed. The checkout directory for the module
 73  
    * is relative to the root directory of the <code>active</code> manifest.
 74  
    */
 75  
   public void execute() throws CommandException {
 76  
 
 77  0
     String moduleName = "";
 78  0
     Module module = null;
 79  0
     Manifest manifest = null;
 80  
 
 81  
     // A manifest must be present for this command
 82  
     //
 83  0
     if (!getContext().isManclass="keyword">ifestLoaded()) {
 84  0
       throw new CommandException(ManifestException.NO_ACTIVE_MANIFEST);
 85  
     }
 86  
 
 87  0
     moduleName = getCommandLine().getOptionValue("m");
 88  
     try {
 89  0
       manifest = getContext().getCurrentManifest();
 90  0
       module = manifest.getModule(moduleName);
 91  0
     } catch (ManifestException e) {
 92  0
       throw new CommandException(e.getErrorCode(),e.getMessageArguments());
 93  0
     }
 94  
 
 95  
     // If the module is in working state, we don't need to determine a version.
 96  
     //
 97  0
     Version version = null;
 98  
 
 99  0
     if (!manclass="keyword">ifest.getState(module).equals(Module.WORKING)) { // Saves time ...
 100  
 
 101  0
       if (getCommandLine().getOptionValue("v") != null) {
 102  
         // The module should be updated to a specific version.
 103  
         //
 104  
         try {
 105  
 
 106  
           // Are we requesting a patch or a normal version ?
 107  
           //
 108  0
           String manualVersion = getCommandLine().getOptionValue("v");
 109  0
           if (manualVersion.matches(Patch.VERSION_PATTERN_STRING)) {
 110  0
             version = new Patch(manualVersion);
 111  
           } else {
 112  0
             version = new Version(manualVersion);
 113  
           }
 114  0
         } catch (PatternSyntaxException pse) {
 115  0
           throw new CommandException(CommandException.INVALID_ARGUMENT,
 116  
               new Object[]{getCommandLine().getOptionValue("v"),
 117  
                            "Version has to be <number>-<number>[-<number>], e.g. '0-0'"});
 118  0
         }
 119  0
       } else if (manclass="keyword">ifest.getState(module).equals(Module.STATIC)) {
 120  0
         version = module.getVersion();
 121  0
       } else if (manclass="keyword">ifest.getState(module).equals(Module.DYNAMIC)) {
 122  
         // todo CVSVersionExtractor should be retrieved through a Factory.
 123  
         //
 124  
         try {
 125  0
           version = Utils.getLastVersion(module);
 126  0
         } catch (VersionControlException e) {
 127  0
           throw new CommandException(e.getErrorCode(), e.getMessageArguments());
 128  0
         }
 129  
       }
 130  
     }
 131  
 
 132  
     try {
 133  0
       if (version != null && !manclass="keyword">ifest.getState(module).equals(Module.WORKING) && version.equals(Utils.getLocalVersion(module))) {
 134  
         // todo message to be internationalized.
 135  
         //
 136  
 
 137  
         // No need to update.
 138  
         //
 139  0
         response.addEvent(
 140  
             new MessageEvent(this, class="keyword">new SimpleMessage("Module `" + module.getName() + "` is already up-to-date with version `" + version.toString() + "`.")));
 141  
 
 142  
       } else {
 143  
 
 144  0
         CVSRunner runner = (CVSRunner) RunnerFactory.getRunner(module.getLocation());
 145  0
         runner.setCommandResponse(response);
 146  
 
 147  0
         if (!Utils.existsInRepository(module)) {
 148  0
           throw new CommandException(VersionControlException.MODULE_NOT_IN_REPOSITORY, class="keyword">new Object[]{module.getName(), module.getLocation().getId()});
 149  
         }
 150  
 
 151  
         //todo check whether the requested version does exist for the module.
 152  
 
 153  0
         if (manclass="keyword">ifest.getState(module).equals(Module.WORKING) && manclass="keyword">ifest instanceof ReleaseManclass="keyword">ifest) {
 154  0
           runner.checkout(module, module.getPatchLine(), null);
 155  
         } else {
 156  0
           runner.checkout(module, version);
 157  
         }
 158  
 
 159  0
         SimpleMessage message = null;
 160  
         // todo message to be internationalized.
 161  0
         if (version == null) {
 162  
           // No state change.
 163  
           //
 164  0
           message = new SimpleMessage("Module `" + module.getName() + "` updated.");
 165  
         } else {
 166  0
           if (manclass="keyword">ifest instanceof ReleaseManclass="keyword">ifest) {
 167  0
             manifest.setState(module, Module.STATIC);
 168  0
             message = new SimpleMessage("Module `" + module.getName() + "` updated with version `" + version.toString() + "`; state set to STATIC.");
 169  
           } else {
 170  0
             if (manclass="keyword">ifest.getState(module).equals(Module.STATIC)) {
 171  
               // The module was static.
 172  
               //
 173  0
               message = new SimpleMessage("Module `" + module.getName() + "` updated.");
 174  
             } else {
 175  0
               manifest.setState(module, Module.DYNAMIC);
 176  0
               message = new SimpleMessage("Module `" + module.getName() + "` updated with version `" + version.toString() + "`; state set to DYNAMIC.");
 177  
             }
 178  
           }
 179  
         }
 180  0
         response.addEvent(new MessageEvent(this, message));
 181  
       }
 182  0
     } catch (VersionControlException e) {
 183  0
       throw new CommandException(e.getErrorCode(), e.getMessageArguments());
 184  0
     } catch (AuthenticationException e) {
 185  0
       throw new CommandException(e.getErrorCode(), e.getMessageArguments());
 186  0
     }
 187  0
   }
 188  
 
 189  
   public CommandResponse getCommandResponse() {
 190  0
     return response;
 191  
   }
 192  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.