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 java.io.IOException;
28  import java.nio.file.Files;
29  import java.nio.file.attribute.FileTime;
30  
31  import org.codehaus.plexus.PlexusTestCase;
32  import org.codehaus.plexus.util.FileUtils;
33  
34  /**
35   * Base abstract class that all the test-cases for different archivers
36   * extend so that they can use its helpful methids.
37   */
38  public abstract class BasePlexusArchiverTest extends PlexusTestCase
39  {
40  
41      /**
42       * Ensure that the last modified timestamp of a file will be greater
43       * than the one specified as a reference.
44       *
45       * @param outputFile the file
46       * @param timestampReference the file will have a newer timestamp
47       *        than this reference timestamp.
48       *
49       * @throws IOException if the timestamp could not be modified
50       */
51      protected void waitUntilNewTimestamp( File outputFile, long timestampReference )
52          throws IOException
53      {
54          long startTime = System.currentTimeMillis();
55          File tmpFile = File.createTempFile(
56              "BasePlexusArchiverTest.waitUntilNewTimestamp", null );
57          long newTimestamp;
58  
59          // We could easily just set the last modified time using
60          // Files.setLastModifiedTime and System.currentTimeMillis(),
61          // but the problem is that tests are using this method to verify that
62          // the force flag is working. To ensure that modified or
63          // newly created files will have timestamp newer than
64          // `timestampReference`, we need to modify a file ourself.
65          // Otherwise the build may fail because when the test overrides
66          // `outputFile` it will have timestamp that is equal
67          // to `timestampReference`.
68          do
69          {
70              FileUtils.fileWrite( tmpFile, "waitUntilNewTimestamp" );
71              newTimestamp = tmpFile.lastModified();
72              Thread.yield();
73          }
74          while ( timestampReference >= newTimestamp
75                  // A simple guard to ensure that we'll not do this forever.
76                  // If the last modified timestamp is not changed to
77                  // a newer value after 10 seconds, probably it never will.
78                  && System.currentTimeMillis() - startTime < 10_000 );
79  
80          tmpFile.delete();
81  
82          if ( timestampReference >= newTimestamp )
83          {
84              throw new IOException("Could not modify the last modified timestamp "
85                  + "to newer than the refence value." );
86          }
87  
88          FileTime newTimestampTime = FileTime.fromMillis( newTimestamp );
89          Files.setLastModifiedTime( outputFile.toPath(), newTimestampTime );
90      }
91  
92      /**
93       * Base method for all the Archivers to create an archiver.
94       *
95       * @param format
96       *
97       * @return
98       *
99       * @throws Exception
100      */
101     protected Archiver createArchiver( String format ) throws Exception
102     {
103 
104         final File pomFile = new File( "pom.xml" );
105         final File rarFile = new File( "target/output/pom.xml." + format );
106 
107         Archiver archiver = (Archiver) lookup( Archiver.ROLE, format );
108         archiver.setDestFile( rarFile );
109         archiver.addFile( pomFile, "pom.xml" );
110 
111         return archiver;
112     }
113 
114 }