View Javadoc
1   /*
2    * The MIT License
3    *
4    * Copyright (c) 2004, The Codehaus
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package org.codehaus.plexus.archiver;
25  
26  import java.io.File;
27  import org.codehaus.plexus.PlexusTestCase;
28  import org.codehaus.plexus.util.FileUtils;
29  
30  /**
31   * Base abstract class that all the test-cases for different archivers
32   * extend so that they can use its helpful methids.
33   */
34  public abstract class BasePlexusArchiverTest extends PlexusTestCase
35  {
36  
37      /**
38       * Ensure that when a new file is created at the specified location that the timestamp of
39       * that file will be greater than the one specified as a reference.
40       *
41       * Warning: Runs in a busy loop creating a file until the output file is newer than the reference timestamp.
42       * This should be better than sleeping for a race condition time out value.
43       *
44       * @param outputFile the file to be created
45       * @param timestampReference the created file will have a newer timestamp than this reference timestamp.
46       *
47       * @throws Exception failures
48       */
49      protected void waitUntilNewTimestamp( File outputFile, long timestampReference ) throws Exception
50      {
51          File tmpFile = File.createTempFile( "ZipArchiverTest.waitUntilNewTimestamp", null );
52          // slurp the file into a temp file and then copy the temp back over the top until it is newer.
53          FileUtils.copyFile( outputFile, tmpFile );
54  
55          FileUtils.copyFile( tmpFile, outputFile );
56          while ( timestampReference >= outputFile.lastModified() )
57          {
58              FileUtils.copyFile( tmpFile, outputFile );
59              Thread.yield();
60          }
61  
62          tmpFile.delete();
63      }
64  
65      /**
66       * Base method for all the Archivers to create an archiver.
67       *
68       * @param format
69       *
70       * @return
71       *
72       * @throws Exception
73       */
74      protected Archiver createArchiver( String format ) throws Exception
75      {
76  
77          final File pomFile = new File( "pom.xml" );
78          final File rarFile = new File( "target/output/pom.xml." + format );
79  
80          Archiver archiver = (Archiver) lookup( Archiver.ROLE, format );
81          archiver.setDestFile( rarFile );
82          archiver.addFile( pomFile, "pom.xml" );
83  
84          return archiver;
85      }
86  
87  }