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