Coverage Report - org.apache.maven.scm.ScmTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
ScmTestCase
0 %
0/111
0 %
0/18
1,48
 
 1  
 package org.apache.maven.scm;
 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.manager.ScmManager;
 23  
 import org.apache.maven.scm.repository.ScmRepository;
 24  
 import org.codehaus.plexus.PlexusTestCase;
 25  
 import org.codehaus.plexus.util.FileUtils;
 26  
 import org.codehaus.plexus.util.IOUtil;
 27  
 import org.codehaus.plexus.util.StringUtils;
 28  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 29  
 import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;
 30  
 import org.codehaus.plexus.util.cli.Commandline;
 31  
 
 32  
 import java.io.File;
 33  
 import java.io.FileWriter;
 34  
 import java.io.IOException;
 35  
 import java.util.Calendar;
 36  
 import java.util.Date;
 37  
 import java.util.TimeZone;
 38  
 
 39  
 /**
 40  
  * Base class for all scm tests. Consumers will typically
 41  
  * extend this class while tck test would extend ScmTckTestCase.
 42  
  * <br>
 43  
  * This class basically defines default locations for the
 44  
  * test environment and implements convenience methods.
 45  
  *
 46  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
 47  
  * @version $Id: ScmTestCase.java 1329106 2012-04-23 07:45:33Z olamy $
 48  
  */
 49  0
 public abstract class ScmTestCase
 50  
     extends PlexusTestCase
 51  
 {
 52  0
     protected static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone( "GMT" );
 53  
 
 54  
     private static boolean debugExecute;
 55  
 
 56  
     private ScmManager scmManager;
 57  
 
 58  
     protected void setUp()
 59  
         throws Exception
 60  
     {
 61  0
         super.setUp();
 62  
 
 63  0
         FileUtils.deleteDirectory( getRepositoryRoot() );
 64  0
         assertFalse( getRepositoryRoot().exists() );
 65  0
         FileUtils.deleteDirectory( getWorkingCopy() );
 66  0
         assertFalse( getWorkingCopy().exists() );
 67  0
         FileUtils.deleteDirectory( getWorkingDirectory() );
 68  0
         assertFalse( getWorkingDirectory().exists() );
 69  0
         FileUtils.deleteDirectory( getAssertionCopy() );
 70  0
         assertFalse( getAssertionCopy().exists() );
 71  0
         FileUtils.deleteDirectory( getUpdatingCopy() );
 72  0
         assertFalse( getUpdatingCopy().exists() );
 73  
 
 74  0
         scmManager = null;
 75  0
     }
 76  
 
 77  
     protected String getModule()
 78  
     {
 79  0
         fail( "getModule() must be overridden." );
 80  
 
 81  0
         return null;
 82  
     }
 83  
 
 84  
     /**
 85  
      * @return default location of the test read/write repository
 86  
      */
 87  
     protected File getRepositoryRoot()
 88  
     {
 89  0
         return PlexusTestCase.getTestFile( "target/scm-test/repository" );
 90  
     }
 91  
 
 92  
     /**
 93  
      * @return Location of the revisioned (read only) repository
 94  
      */
 95  
     protected File getRepository()
 96  
     {
 97  0
         return PlexusTestCase.getTestFile( "/src/test/repository" );
 98  
     }
 99  
 
 100  
     /**
 101  
      * @return location of the working copy (always checkout)
 102  
      */
 103  
     protected File getWorkingCopy()
 104  
     {
 105  0
         return PlexusTestCase.getTestFile( "target/scm-test/working-copy" );
 106  
     }
 107  
 
 108  
     /**
 109  
      * Legacy method - same as getWorkingCopy()
 110  
      *
 111  
      * @return location of the working copy (always checkout)
 112  
      */
 113  
     protected File getWorkingDirectory()
 114  
     {
 115  0
         return getWorkingCopy();
 116  
     }
 117  
 
 118  
     /**
 119  
      * @return default location for doing assertions on a working tree
 120  
      */
 121  
     protected File getAssertionCopy()
 122  
     {
 123  0
         return PlexusTestCase.getTestFile( "target/scm-test/assertion-copy" );
 124  
     }
 125  
 
 126  
     /**
 127  
      * @return default location for doing update operations on a working tree
 128  
      */
 129  
     protected File getUpdatingCopy()
 130  
     {
 131  0
         return PlexusTestCase.getTestFile( "target/scm-test/updating-copy" );
 132  
     }
 133  
 
 134  
     protected ScmManager getScmManager()
 135  
         throws Exception
 136  
     {
 137  0
         if ( scmManager == null )
 138  
         {
 139  0
             scmManager = (ScmManager) lookup( ScmManager.ROLE );
 140  
         }
 141  
 
 142  0
         return scmManager;
 143  
     }
 144  
 
 145  
     protected ScmRepository makeScmRepository( String scmUrl )
 146  
         throws Exception
 147  
     {
 148  0
         return getScmManager().makeScmRepository( scmUrl );
 149  
     }
 150  
 
 151  
     /**
 152  
      * TODO This method is bogus. ActualPatch is not used and if used, it breaks
 153  
      * some unit tests.
 154  
      */
 155  
     public void assertPath( String expectedPath, String actualPath )
 156  
         throws Exception
 157  
     {
 158  0
         assertEquals( StringUtils.replace( expectedPath, "\\", "/" ), StringUtils.replace( expectedPath, "\\", "/" ) );
 159  0
     }
 160  
 
 161  
     protected void assertFile( File root, String fileName )
 162  
         throws Exception
 163  
     {
 164  0
         File file = new File( root, fileName );
 165  
 
 166  0
         assertTrue( "Missing file: '" + file.getAbsolutePath() + "'.", file.exists() );
 167  
 
 168  0
         assertTrue( "File isn't a file: '" + file.getAbsolutePath() + "'.", file.isFile() );
 169  
 
 170  0
         String expected = fileName;
 171  
 
 172  0
         String actual = FileUtils.fileRead( file );
 173  
 
 174  0
         assertEquals( "The file doesn't contain the expected contents. File: " + file.getAbsolutePath(), expected,
 175  
                       actual );
 176  0
     }
 177  
 
 178  
     protected void assertResultIsSuccess( ScmResult result )
 179  
     {
 180  0
         if ( result.isSuccess() )
 181  
         {
 182  0
             return;
 183  
         }
 184  
 
 185  0
         printOutputError( result );
 186  
 
 187  0
         fail( "The check out result success flag was false." );
 188  0
     }
 189  
 
 190  
     protected void printOutputError( ScmResult result )
 191  
     {
 192  0
         System.err.println( "----------------------------------------------------------------------" );
 193  0
         System.err.println( "Provider message" );
 194  0
         System.err.println( "----------------------------------------------------------------------" );
 195  0
         System.err.println( result.getProviderMessage() );
 196  0
         System.err.println( "----------------------------------------------------------------------" );
 197  
 
 198  0
         System.err.println( "----------------------------------------------------------------------" );
 199  0
         System.err.println( "Command output" );
 200  0
         System.err.println( "----------------------------------------------------------------------" );
 201  0
         System.err.println( result.getCommandOutput() );
 202  0
         System.err.println( "----------------------------------------------------------------------" );
 203  0
     }
 204  
 
 205  
     protected ScmFileSet getScmFileSet()
 206  
     {
 207  0
         return new ScmFileSet( getWorkingCopy() );
 208  
     }
 209  
 
 210  
     protected static void setDebugExecute( boolean debugExecute )
 211  
     {
 212  0
         ScmTestCase.debugExecute = debugExecute;
 213  0
     }
 214  
 
 215  
     /**
 216  
      * Execute the command line
 217  
      *
 218  
      * @param workingDirectory not null
 219  
      * @param executable       not null, should be a system command
 220  
      * @param arguments        not null
 221  
      * @throws Exception if any
 222  
      * @see CommandLineUtils#executeCommandLine(Commandline, org.codehaus.plexus.util.cli.StreamConsumer,
 223  
      *      org.codehaus.plexus.util.cli.StreamConsumer)
 224  
      */
 225  
     public static void execute( File workingDirectory, String executable, String arguments )
 226  
         throws Exception
 227  
     {
 228  0
         Commandline cl = new Commandline();
 229  
 
 230  0
         cl.setExecutable( executable );
 231  
 
 232  0
         cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
 233  
 
 234  0
         cl.addArguments( CommandLineUtils.translateCommandline( arguments ) );
 235  
 
 236  0
         StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
 237  
 
 238  0
         StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
 239  
 
 240  0
         System.out.println( "Test command line: " + cl );
 241  
 
 242  0
         int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
 243  
 
 244  0
         if ( debugExecute || exitValue != 0 )
 245  
         {
 246  0
             System.err.println( "-----------------------------------------" );
 247  0
             System.err.println( "Command line: " + cl );
 248  0
             System.err.println( "Working directory: " + cl.getWorkingDirectory() );
 249  0
             System.err.println( "-----------------------------------------" );
 250  0
             System.err.println( "Standard output: " );
 251  0
             System.err.println( "-----------------------------------------" );
 252  0
             System.err.println( stdout.getOutput() );
 253  0
             System.err.println( "-----------------------------------------" );
 254  
 
 255  0
             System.err.println( "Standard error: " );
 256  0
             System.err.println( "-----------------------------------------" );
 257  0
             System.err.println( stderr.getOutput() );
 258  0
             System.err.println( "-----------------------------------------" );
 259  
         }
 260  
 
 261  0
         if ( exitValue != 0 )
 262  
         {
 263  0
             fail( "Exit value wasn't 0, was:" + exitValue );
 264  
         }
 265  0
     }
 266  
 
 267  
     protected static void makeDirectory( File basedir, String fileName )
 268  
     {
 269  0
         File dir = new File( basedir, fileName );
 270  
 
 271  0
         if ( !dir.exists() )
 272  
         {
 273  0
             assertTrue( dir.mkdirs() );
 274  
         }
 275  0
     }
 276  
 
 277  
     protected static void makeFile( File basedir, String fileName )
 278  
         throws IOException
 279  
     {
 280  0
         makeFile( basedir, fileName, fileName );
 281  0
     }
 282  
 
 283  
     public static void makeFile( File basedir, String fileName, String contents )
 284  
         throws IOException
 285  
     {
 286  0
         File file = new File( basedir, fileName );
 287  
 
 288  0
         File parent = file.getParentFile();
 289  
 
 290  0
         if ( !parent.exists() )
 291  
         {
 292  0
             assertTrue( parent.mkdirs() );
 293  
         }
 294  
 
 295  0
         FileWriter writer = new FileWriter( file );
 296  
         try
 297  
         {
 298  0
             writer.write( contents );
 299  
         }
 300  
         finally
 301  
         {
 302  0
             IOUtil.close( writer );
 303  0
         }
 304  0
     }
 305  
 
 306  
     public static Date getDate( int year, int month, int day )
 307  
     {
 308  0
         return getDate( year, month, day, 0, 0, 0, null );
 309  
     }
 310  
 
 311  
     protected static Date getDate( int year, int month, int day, TimeZone tz )
 312  
     {
 313  0
         return getDate( year, month, day, 0, 0, 0, tz );
 314  
     }
 315  
 
 316  
     protected static Date getDate( int year, int month, int day, int hourOfDay, int minute, int second, TimeZone tz )
 317  
     {
 318  0
         Calendar cal = Calendar.getInstance();
 319  
 
 320  0
         if ( tz != null )
 321  
         {
 322  0
             cal.setTimeZone( tz );
 323  
         }
 324  0
         cal.set( year, month, day, hourOfDay, minute, second );
 325  0
         cal.set( Calendar.MILLISECOND, 0 );
 326  
 
 327  0
         return cal.getTime();
 328  
     }
 329  
 
 330  
     public void assertCommandLine( String expectedCommand, File expectedWorkingDirectory, Commandline actualCommand )
 331  
         throws IOException
 332  
     {
 333  0
         Commandline cl = new Commandline( expectedCommand );
 334  0
         if ( expectedWorkingDirectory != null )
 335  
         {
 336  0
             cl.setWorkingDirectory( expectedWorkingDirectory.getAbsolutePath() );
 337  
         }
 338  0
         assertEquals( cl.toString(), actualCommand.toString() );
 339  0
     }
 340  
 
 341  
     /**
 342  
      * @param cmd the executable to run, not null.
 343  
      * @return <code>true</code>
 344  
      */
 345  
     public static boolean isSystemCmd( String cmd )
 346  
     {
 347  
         try
 348  
         {
 349  0
             Runtime.getRuntime().exec( cmd );
 350  
 
 351  0
             return true;
 352  
         }
 353  0
         catch ( IOException e )
 354  
         {
 355  0
             return false;
 356  
         }
 357  
     }
 358  
 }