Coverage report

  %line %branch
nl.toolforge.karma.core.test.LocalCVSInitializer
83% 
100% 

 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.test;
 20  
 
 21  
 import nl.toolforge.core.util.file.MyFileUtils;
 22  
 import nl.toolforge.karma.core.Version;
 23  
 import nl.toolforge.karma.core.cmd.CommandResponse;
 24  
 import nl.toolforge.karma.core.module.Module;
 25  
 import nl.toolforge.karma.core.module.SourceModule;
 26  
 import nl.toolforge.karma.core.vc.AuthenticationException;
 27  
 import nl.toolforge.karma.core.vc.Runner;
 28  
 import nl.toolforge.karma.core.vc.cvsimpl.CVSException;
 29  
 import nl.toolforge.karma.core.vc.cvsimpl.CVSRepository;
 30  
 import nl.toolforge.karma.core.vc.cvsimpl.CVSRunner;
 31  
 import org.apache.commons.io.FileUtils;
 32  
 import org.apache.commons.logging.Log;
 33  
 import org.apache.commons.logging.LogFactory;
 34  
 import org.apache.tools.ant.Project;
 35  
 import org.apache.tools.ant.taskdefs.Copy;
 36  
 import org.apache.tools.ant.types.FileSet;
 37  
 
 38  
 import java.io.File;
 39  
 import java.io.IOException;
 40  
 import java.io.InputStream;
 41  
 import java.util.Properties;
 42  
 import java.util.Random;
 43  
 
 44  
 /**
 45  
  * <p>Initializes tests that need a local CVS repository. This local repository can be found as a <code>zip</code>-file
 46  
  * and/or <code>tar</code> file in the <code>resources/test</code> directory of this packages' sources. It contains a
 47  
  * CVS repository with predefined modules that can be used for testing purposes. If the CVS repository cannot be found
 48  
  * (i.e. the user has not prepared his/her local environment with the CVSROOT), and you don't want your CVS stuff to
 49  
  * be tested, make sure you ignore these testcases in your test-configuration.
 50  
  *
 51  
  * <p>Note the dependency of this class with {@link nl.toolforge.karma.core.location.Location} and
 52  
  * {@link nl.toolforge.karma.core.vc.cvsimpl.CVSRepository}.
 53  
  *
 54  
  * <p>When performing operations on managed files, a randomize function is used to be able to repeatedly perform
 55  
  * tests. Filenames and modulenames in the repository could be named like <code>bla_036548290.56437</code>.
 56  
  *
 57  
  * @author D.A. Smedes
 58  
  * @version $Id:
 59  
  */
 60  390
 public class LocalCVSInitializer extends BaseTest {
 61  
 
 62  208
   private static Log logger = LogFactory.getLog(LocalCVSInitializer.class);
 63  
 
 64  
   /**
 65  
    * The name of the a test module in the test repository
 66  
    */
 67  
   protected static final String DEFAULT_MODULE_1 = "CORE-test-module-1";
 68  
 
 69  104
   private static CVSRepository location = null;
 70  390
   private File localCVSRepository = null;
 71  
 
 72  
   /**
 73  
    * Can be used to access random <code>int</code>s.
 74  
    */
 75  104
   protected static Random randomizer = new Random();
 76  
 
 77  
   public void setUp() throws InitializationException {
 78  
 
 79  390
     super.setUp(); // ...
 80  
 
 81  390
     String localPath = null; // Reference to test CVSROOT directory.
 82  
 
 83  
     try {
 84  390
       Properties props = new Properties();
 85  390
       InputStream stream = getClass().getClassLoader().getResourceAsStream("test/test-cvs.properties");
 86  390
       if (stream == null) {
 87  
         //properties file does not exist
 88  
         //log an error. Nullpointer will follow... ;)
 89  0
         logger.error("could not find properties file: test-cvs.properties");
 90  
       }
 91  390
       props.load(stream);
 92  390
       localPath = props.getProperty("cvs.local.path");
 93  
 
 94  
       // To always have a clean set of a cvs repository, copy the structure in test-CVSROOT.tgz to
 95  
       // the same directory, but with "-tmp" added to it.
 96  
 
 97  390
       Copy copy = new Copy();
 98  390
       copy.setProject(new Project());
 99  
 
 100  390
       FileSet set = new FileSet();
 101  390
       set.setDir(new File(localPath));
 102  390
       set.setIncludes("**/*");
 103  
 
 104  390
       copy.addFileset(set);
 105  
 
 106  390
       localCVSRepository = MyFileUtils.createTempDirectory();
 107  
 
 108  390
       copy.setTodir(localCVSRepository);
 109  390
       copy.execute();
 110  
 
 111  390
       logger.debug("cvs.local.path = " + localPath);
 112  
 
 113  390
       location = new CVSRepository("test-id-1");
 114  390
       location.setProtocol(CVSRepository.LOCAL);
 115  390
       location.setRepository(localCVSRepository.getPath());
 116  
 
 117  390
       location.setWorkingContext(getWorkingContext());
 118  
 
 119  0
     } catch (Exception e) {
 120  0
       throw new InitializationException("Local CVS repository could not be initialized. Trying to initialize repository at : ".concat(localPath));
 121  390
     }
 122  
 
 123  390
   }
 124  
 
 125  
   /**
 126  
    * Deletes the temporary directory.
 127  
    */
 128  
   public void tearDown() {
 129  
 
 130  390
     super.tearDown();
 131  
 
 132  
     try {
 133  390
       FileUtils.deleteDirectory(localCVSRepository);
 134  0
     } catch (IOException e) {
 135  0
       fail(e.getMessage());
 136  390
     }
 137  390
   }
 138  
 
 139  
   /**
 140  
    * Gets the <code>CVSRepository</code> that can be used for junit testing.
 141  
    */
 142  
   protected CVSRepository getTestLocation() {
 143  806
     return location;
 144  
   }
 145  
 
 146  
   /**
 147  
    * Creates a randomly named test filename in the module directory (<code>TestModule</code>) for the test cvs
 148  
    * repository. Names are of the form <code>test_&lt;random-int&gt;</code>.
 149  
    */
 150  
   protected String getTestFileName() throws InitializationException {
 151  
 
 152  52
     int someInt = randomizer.nextInt();
 153  52
     someInt = (someInt < 0 ? someInt * -1 : someInt); // > 0
 154  
 
 155  52
     return "test_" + someInt + ".nobody.txt";
 156  
   }
 157  
 
 158  
   /**
 159  
    * <p>Checks out {@link #DEFAULT_MODULE_1}, which can then be used to test against.
 160  
    */
 161  
   public final Module checkoutDefaultModule1() {
 162  
 
 163  
     try {
 164  182
       Runner runner = getTestRunner();
 165  
 
 166  182
       Module module = new SourceModule(DEFAULT_MODULE_1, location);
 167  182
       module.setBaseDir(new File(getWorkingContext().getProjectBaseDirectory(), module.getName()));
 168  70
 //      module.setCheckoutDir(getWorkingContext().getProjectBaseDirectory());
 169  
 
 170  182
       runner.checkout(module);
 171  
 
 172  182
       return module;
 173  
 
 174  0
     } catch (Exception e) {
 175  0
       fail(e.getMessage());
 176  
     }
 177  0
     return null;
 178  
   }
 179  
 
 180  
   public final Module checkoutDefaultModuleWithVersion() {
 181  
 
 182  
     try {
 183  78
       Runner runner = getTestRunner();
 184  
 
 185  78
       Module module = new SourceModule(DEFAULT_MODULE_1, location, class="keyword">new Version("0-0"));
 186  78
       module.setBaseDir(new File(getWorkingContext().getProjectBaseDirectory(), module.getName()));
 187  30
 //      module.setCheckoutDir(getWorkingContext().getProjectBaseDirectory());
 188  
 
 189  78
       runner.checkout(module);
 190  
 
 191  78
       return module;
 192  
 
 193  0
     } catch (Exception e) {
 194  0
       fail(e.getMessage());
 195  
     }
 196  0
     return null;
 197  
   }
 198  
 
 199  
   /**
 200  
    * Initializes a Runner for test purposes.
 201  
    *
 202  
    * @return A Runner instance.
 203  
    * @throws CVSException When initializing the runner failed.
 204  
    */
 205  
   protected final Runner getTestRunner() throws CVSException, AuthenticationException {
 206  390
     return getTestRunner(new CommandResponseFaker(null));
 207  
   }
 208  
 
 209  
   /**
 210  
    * Initializes a Runner for test purposes, with an optional CommandResponse.
 211  
    *
 212  
    * @param response
 213  
    * @return
 214  
    * @throws CVSException When initializing the runner failed.
 215  
    */
 216  
   protected final Runner getTestRunner(CommandResponse response) throws CVSException, AuthenticationException {
 217  
 
 218  442
     CVSRunner runner = new CVSRunner(getTestLocation());
 219  
 
 220  442
     if (response != null) {
 221  442
       runner.setCommandResponse(response);
 222  
     }
 223  
 
 224  442
     return runner;
 225  
   }
 226  
 
 227  
   /**
 228  
    * When this class is run (it is a test class), it won't bother you with 'no tests found'.
 229  
    */
 230  
   public void testNothing() {
 231  104
     assertTrue(true);
 232  104
   }
 233  
 }

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