Coverage report

  %line %branch
nl.toolforge.karma.core.cmd.impl.TestModule
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 java.io.File;
 22  
 
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 import org.apache.tools.ant.BuildException;
 26  
 import org.apache.tools.ant.DirectoryScanner;
 27  
 import org.apache.tools.ant.Project;
 28  
 
 29  
 import nl.toolforge.karma.core.cmd.Command;
 30  
 import nl.toolforge.karma.core.cmd.CommandDescriptor;
 31  
 import nl.toolforge.karma.core.cmd.CommandException;
 32  
 import nl.toolforge.karma.core.cmd.CommandFactory;
 33  
 import nl.toolforge.karma.core.cmd.CommandLoadException;
 34  
 import nl.toolforge.karma.core.cmd.CommandResponse;
 35  
 import nl.toolforge.karma.core.cmd.event.ErrorEvent;
 36  
 import nl.toolforge.karma.core.cmd.event.MessageEvent;
 37  
 import nl.toolforge.karma.core.cmd.event.SimpleMessage;
 38  
 import nl.toolforge.karma.core.cmd.util.DependencyException;
 39  
 import nl.toolforge.karma.core.cmd.util.DependencyHelper;
 40  
 import nl.toolforge.karma.core.module.ModuleTypeException;
 41  
 
 42  
 /**
 43  
  * Run the unit tests of a given module.
 44  
  * <p>
 45  
  * At this moment this class only supports Java/JUnit in combination with Ant.
 46  
  * </p>
 47  
  *
 48  
  * @author W.H. Schraal
 49  
  * @version $Id: TestModule.java,v 1.32 2004/11/16 22:28:02 hippe Exp $
 50  
  */
 51  
 public class TestModule extends AbstractBuildCommand {
 52  
 
 53  0
   private static final Log logger = LogFactory.getLog(TestModule.class);
 54  
 
 55  0
   private CommandResponse commandResponse = new CommandResponse();
 56  
 
 57  
   public TestModule(CommandDescriptor descriptor) {
 58  0
     super(descriptor);
 59  0
   }
 60  
 
 61  
   public void execute() throws CommandException {
 62  
 
 63  0
     super.execute();
 64  
 
 65  0
     Command command = null;
 66  
     try {
 67  
       String commandLineString;
 68  0
       if (!getCommandLine().hasOption("n")) {
 69  0
         commandLineString = "bm -m " + module.getName();
 70  
       } else {
 71  0
         commandLineString = "bm -n -m " + module.getName();
 72  
       }
 73  0
       logger.debug("Going to: "+commandLineString);
 74  0
       command = CommandFactory.getInstance().getCommand(commandLineString);
 75  0
       command.setContext(getContext());
 76  0
       command.registerCommandResponseListener(getResponseListener());
 77  0
       command.execute();
 78  0
     } catch (CommandException ce) {
 79  0
       if (ce.getErrorCode().equals(CommandException.DEPENDENCY_DOES_NOT_EXIST) ||
 80  
           ce.getErrorCode().equals(CommandException.BUILD_FAILED) ||
 81  
           ce.getErrorCode().equals(DependencyException.DEPENDENCY_NOT_FOUND) ) {
 82  0
         commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments()));
 83  0
         throw new CommandException(ce, CommandException.TEST_FAILED, class="keyword">new Object[]{module.getName()});
 84  0
       } else if (ce.getErrorCode().equals(CommandException.NO_SRC_DIR)) {
 85  
         //do not log anything this has already been done.
 86  
         //commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments()));
 87  
       } else {
 88  0
         commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments()));
 89  
       }
 90  0
     } catch (CommandLoadException e) {
 91  0
       throw new CommandException(e.getErrorCode(), e.getMessageArguments());
 92  
     } finally {
 93  0
       if ( command != null ) {
 94  0
         command.deregisterCommandResponseListener(getResponseListener());
 95  
       }
 96  0
     }
 97  
 
 98  
     // Define the location where junit source files are stored for a module (the default location in the context of
 99  
     // a manifest).
 100  
     //
 101  0
     if (!getBuildEnvironment().getModuleTestSourceDirectory().exists()) {
 102  
       // No point in building a module, if no test/java is available.
 103  
       //
 104  0
       throw new CommandException(CommandException.NO_TEST_DIR, class="keyword">new Object[] {getCurrentModule().getName()});
 105  
     }
 106  0
     DirectoryScanner scanner = new DirectoryScanner();
 107  0
     scanner.setBasedir(getBuildEnvironment().getModuleTestSourceDirectory());
 108  0
     scanner.setIncludes(new String[]{"**/*.java"});
 109  0
     scanner.scan();
 110  0
     if (scanner.getIncludedFiles().length == 0) {
 111  
       // No point in building a module, if no sources available.
 112  
       //
 113  0
       throw new CommandException(CommandException.NO_TEST_DIR, class="keyword">new Object[] {getCurrentModule().getName(), "test/java"});
 114  
     }
 115  
 
 116  
     // Configure the Ant project
 117  
     //
 118  0
     Project project = getAntProject("test-module.xml");
 119  
 
 120  0
     logger.debug("Setting 'module-source-dir' to: "+getBuildEnvironment().getModuleTestSourceDirectory().getPath());
 121  0
     logger.debug("Setting 'module-test-dir' to: "+getBuildEnvironment().getModuleTestBuildDirectory().getPath());
 122  0
     project.setProperty("module-source-dir", getBuildEnvironment().getModuleTestSourceDirectory().getPath());
 123  0
     project.setProperty("module-test-dir", getBuildEnvironment().getModuleTestBuildDirectory().getPath());
 124  
 
 125  
     try {
 126  
 
 127  0
       logger.debug("Setting 'module-compile-dir' to: "+getCompileDirectory().getPath());
 128  0
       project.setProperty("module-compile-dir", getCompileDirectory().getPath());
 129  
 
 130  0
       String deps = "";
 131  
 
 132  0
       DependencyHelper helper = new DependencyHelper(getCurrentManifest());
 133  
 
 134  0
       if (getCurrentModule().getDependencies().size() > 0) {
 135  0
         deps = helper.getTestClassPath(getCurrentModule()) + ";";
 136  
       }
 137  
 
 138  0
       File f = getCurrentManifest().getBuildBaseDirectory();
 139  0
       f = new File(f, getCurrentModule().getName());
 140  0
       f = new File(f, "build");
 141  
 
 142  0
       deps += f.getPath();
 143  0
       logger.debug("Setting 'module-classpath' to: "+deps);
 144  0
       project.setProperty("module-classpath", deps);
 145  0
     } catch (DependencyException d) {
 146  0
       throw new CommandException(d.getErrorCode(), d.getMessageArguments());
 147  0
     } catch (ModuleTypeException d) {
 148  0
       throw new CommandException(d.getErrorCode(), d.getMessageArguments());
 149  0
     }
 150  
 
 151  
     try {
 152  0
       project.executeTarget("run");
 153  0
     } catch (BuildException e) {
 154  0
       logger.info(e.getMessage(), e);
 155  0
       throw new CommandException(CommandException.TEST_FAILED, class="keyword">new Object[] {getCurrentModule().getName()});
 156  0
     }
 157  
 
 158  
     // todo: localize message
 159  0
     commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Module " + getCurrentModule().getName() + " tested succesfully.")));
 160  0
   }
 161  
 
 162  
   /**
 163  
    * Gets the commands' response object.
 164  
    *
 165  
    * @return The commands' response object.
 166  
    */
 167  
   public CommandResponse getCommandResponse() {
 168  0
     return this.commandResponse;
 169  
   }
 170  
 }

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