< prev index next >

jdk/test/tools/jar/JarEntryTime.java

Print this page




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4225317 6969651
  27  * @modules jdk.jartool/sun.tools.jar
  28  * @summary Check extracted files have date as per those in the .jar file
  29  */
  30 
  31 import java.io.File;
  32 import java.io.PrintWriter;
  33 import java.nio.file.attribute.FileTime;
  34 import java.util.Date;
  35 import java.util.TimeZone;
  36 import sun.tools.jar.Main;
  37 
  38 public class JarEntryTime {





  39 
  40     // ZipEntry's mod date has 2 seconds precision: give extra time to
  41     // allow for e.g. rounding/truncation and networked/samba drives.
  42     static final long PRECISION = 10000L;
  43 
  44     static final TimeZone TZ = TimeZone.getDefault();
  45     static final boolean DST = TZ.inDaylightTime(new Date());
  46 
  47     static boolean cleanup(File dir) throws Throwable {
  48         boolean rc = true;
  49         File[] x = dir.listFiles();
  50         if (x != null) {
  51             for (int i = 0; i < x.length; i++) {
  52                 rc &= x[i].delete();
  53             }
  54         }
  55         return rc & dir.delete();
  56     }
  57 
  58     static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {


  97          */
  98         check(dirOuter.mkdir());
  99         check(dirInner.mkdir());
 100         File fileInner = new File(dirInner, "foo.txt");
 101         try (PrintWriter pw = new PrintWriter(fileInner)) {
 102             pw.println("hello, world");
 103         }
 104 
 105         // Get the "now" from the "last-modified-time" of the last file we
 106         // just created, instead of the "System.currentTimeMillis()", to
 107         // workaround the possible "time difference" due to nfs.
 108         final long now = fileInner.lastModified();
 109         final long earlier = now - (60L * 60L * 6L * 1000L);
 110         final long yesterday = now - (60L * 60L * 24L * 1000L);
 111 
 112         check(dirOuter.setLastModified(now));
 113         check(dirInner.setLastModified(yesterday));
 114         check(fileInner.setLastModified(earlier));
 115 
 116         // Make a jar file from that directory structure
 117         Main jartool = new Main(System.out, System.err, "jar");
 118         check(jartool.run(new String[] {
 119                 "cf",
 120                 jarFile.getName(), dirOuter.getName() } ));
 121         check(jarFile.exists());
 122 
 123         check(cleanup(dirInner));
 124         check(cleanup(dirOuter));
 125 
 126         // Extract and check that the last modified values are those specified
 127         // in the archive
 128         extractJar(jarFile, false);
 129         check(dirOuter.exists());
 130         check(dirInner.exists());
 131         check(fileInner.exists());
 132         checkFileTime(dirOuter.lastModified(), now);
 133         checkFileTime(dirInner.lastModified(), yesterday);
 134         checkFileTime(fileInner.lastModified(), earlier);
 135 
 136         check(cleanup(dirInner));
 137         check(cleanup(dirOuter));
 138 
 139         try (PrintWriter pw = new PrintWriter(testFile)) {
 140             pw.println("hello, world");
 141         }
 142         final long start = testFile.lastModified();
 143 
 144         // Extract and check the last modified values are the current times.
 145         // See sun.tools.jar.Main
 146         extractJar(jarFile, true);
 147 
 148         try (PrintWriter pw = new PrintWriter(testFile)) {
 149             pw.println("hello, world");
 150         }
 151         final long end = testFile.lastModified();
 152 
 153         check(dirOuter.exists());
 154         check(dirInner.exists());
 155         check(fileInner.exists());
 156         checkFileTime(start, dirOuter.lastModified(), end);
 157         checkFileTime(start, dirInner.lastModified(), end);
 158         checkFileTime(start, fileInner.lastModified(), end);
 159 
 160         check(cleanup(dirInner));
 161         check(cleanup(dirOuter));
 162 
 163         check(jarFile.delete());
 164         check(testFile.delete());
 165     }




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4225317 6969651
  27  * @modules jdk.jartool
  28  * @summary Check extracted files have date as per those in the .jar file
  29  */
  30 
  31 import java.io.File;
  32 import java.io.PrintWriter;
  33 import java.nio.file.attribute.FileTime;
  34 import java.util.Date;
  35 import java.util.TimeZone;
  36 import java.util.spi.ToolProvider;
  37 
  38 public class JarEntryTime {
  39     static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
  40         .orElseThrow(() ->
  41             new RuntimeException("jar tool not found")
  42         );
  43 
  44 
  45     // ZipEntry's mod date has 2 seconds precision: give extra time to
  46     // allow for e.g. rounding/truncation and networked/samba drives.
  47     static final long PRECISION = 10000L;
  48 
  49     static final TimeZone TZ = TimeZone.getDefault();
  50     static final boolean DST = TZ.inDaylightTime(new Date());
  51 
  52     static boolean cleanup(File dir) throws Throwable {
  53         boolean rc = true;
  54         File[] x = dir.listFiles();
  55         if (x != null) {
  56             for (int i = 0; i < x.length; i++) {
  57                 rc &= x[i].delete();
  58             }
  59         }
  60         return rc & dir.delete();
  61     }
  62 
  63     static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {


 102          */
 103         check(dirOuter.mkdir());
 104         check(dirInner.mkdir());
 105         File fileInner = new File(dirInner, "foo.txt");
 106         try (PrintWriter pw = new PrintWriter(fileInner)) {
 107             pw.println("hello, world");
 108         }
 109 
 110         // Get the "now" from the "last-modified-time" of the last file we
 111         // just created, instead of the "System.currentTimeMillis()", to
 112         // workaround the possible "time difference" due to nfs.
 113         final long now = fileInner.lastModified();
 114         final long earlier = now - (60L * 60L * 6L * 1000L);
 115         final long yesterday = now - (60L * 60L * 24L * 1000L);
 116 
 117         check(dirOuter.setLastModified(now));
 118         check(dirInner.setLastModified(yesterday));
 119         check(fileInner.setLastModified(earlier));
 120 
 121         // Make a jar file from that directory structure
 122         check(JAR_TOOL.run(System.out, System.err,
 123                            "cf", jarFile.getName(), dirOuter.getName()) == 0);


 124         check(jarFile.exists());
 125 
 126         check(cleanup(dirInner));
 127         check(cleanup(dirOuter));
 128 
 129         // Extract and check that the last modified values are those specified
 130         // in the archive
 131         extractJar(jarFile, false);
 132         check(dirOuter.exists());
 133         check(dirInner.exists());
 134         check(fileInner.exists());
 135         checkFileTime(dirOuter.lastModified(), now);
 136         checkFileTime(dirInner.lastModified(), yesterday);
 137         checkFileTime(fileInner.lastModified(), earlier);
 138 
 139         check(cleanup(dirInner));
 140         check(cleanup(dirOuter));
 141 
 142         try (PrintWriter pw = new PrintWriter(testFile)) {
 143             pw.println("hello, world");
 144         }
 145         final long start = testFile.lastModified();
 146 
 147         // Extract and check the last modified values are the current times.

 148         extractJar(jarFile, true);
 149 
 150         try (PrintWriter pw = new PrintWriter(testFile)) {
 151             pw.println("hello, world");
 152         }
 153         final long end = testFile.lastModified();
 154 
 155         check(dirOuter.exists());
 156         check(dirInner.exists());
 157         check(fileInner.exists());
 158         checkFileTime(start, dirOuter.lastModified(), end);
 159         checkFileTime(start, dirInner.lastModified(), end);
 160         checkFileTime(start, fileInner.lastModified(), end);
 161 
 162         check(cleanup(dirInner));
 163         check(cleanup(dirOuter));
 164 
 165         check(jarFile.delete());
 166         check(testFile.delete());
 167     }


< prev index next >