Wed, 16 Mar 2011 09:41:30 -0700
7026359: (langtools) fix big jar test
Reviewed-by: jjg
test/tools/javac/file/zip/T6836682.java | file | annotate | diff | comparison | revisions |
1.1 --- a/test/tools/javac/file/zip/T6836682.java Wed Mar 16 11:12:26 2011 +0000 1.2 +++ b/test/tools/javac/file/zip/T6836682.java Wed Mar 16 09:41:30 2011 -0700 1.3 @@ -23,58 +23,122 @@ 1.4 1.5 /* 1.6 * @test 1.7 - * @ignore 1.8 * @bug 6836682 7025988 1.9 - * @summary JavacFileManager handles zip64 archives (64K+ entries and large file support) 1.10 + * @summary JavacFileManager handling of zip64 archives (Scenario A and B) 1.11 * @compile -XDignore.symbol.file T6836682.java Utils.java 1.12 * @run main T6836682 1.13 */ 1.14 +/* 1.15 + * This test consists of two scenarios: 1.16 + * 1.17 + * Scenario A: create a jar with entries exceeding 64K, and see if the javac 1.18 + * can handle this large jar on the classpath. Generally this test completes 1.19 + * within a minute 1.20 + * 1.21 + * Scenario B: create a jar with a large enough file exceeding 4GB, and 1.22 + * similarly test javac. This test is known to be slow and problematic on 1.23 + * certain operating systems, thus this test can be selected by passing a 1.24 + * property through jtreg as follows: 1.25 + * -javaoptions=-DT6836682.testScenarioB=true. 1.26 + * Note this test will only run iff all the disk requirements are met at runtime. 1.27 + */ 1.28 +import java.io.BufferedInputStream; 1.29 import java.io.BufferedOutputStream; 1.30 import java.io.File; 1.31 import java.io.FileInputStream; 1.32 import java.io.FileOutputStream; 1.33 import java.io.IOException; 1.34 +import java.io.OutputStream; 1.35 import java.nio.file.Files; 1.36 import java.nio.file.Path; 1.37 -import java.util.jar.JarOutputStream; 1.38 +import java.util.zip.CRC32; 1.39 import java.util.zip.ZipEntry; 1.40 +import java.util.zip.ZipOutputStream; 1.41 1.42 public class T6836682 { 1.43 1.44 private static final long GIGA = 1024 * 1024 * 1024; 1.45 + private static final int BUFFER_LEN = Short.MAX_VALUE * 2; 1.46 1.47 - static void createLargeFile(File outFile, long minlength) throws IOException { 1.48 - FileOutputStream fos = null; 1.49 - BufferedOutputStream bos = null; 1.50 - byte[] buffer = new byte[Short.MAX_VALUE * 2]; 1.51 + static long getCount(long minlength) { 1.52 + return (minlength / BUFFER_LEN) + 1; 1.53 + } 1.54 + 1.55 + static long computeCRC(long minlength) { 1.56 + CRC32 crc = new CRC32(); 1.57 + byte[] buffer = new byte[BUFFER_LEN]; 1.58 + long count = getCount(minlength); 1.59 + for (long i = 0; i < count; i++) { 1.60 + crc.update(buffer); 1.61 + } 1.62 + return crc.getValue(); 1.63 + } 1.64 + 1.65 + static long computeCRC(File inFile) throws IOException { 1.66 + byte[] buffer = new byte[8192]; 1.67 + CRC32 crc = new CRC32(); 1.68 + FileInputStream fis = null; 1.69 + BufferedInputStream bis = null; 1.70 try { 1.71 - fos = new FileOutputStream(outFile); 1.72 - bos = new BufferedOutputStream(fos); 1.73 - long count = minlength / ( Short.MAX_VALUE * 2) + 1; 1.74 - for (long i = 0 ; i < count ; i++) { 1.75 - bos.write(buffer); 1.76 + fis = new FileInputStream(inFile); 1.77 + bis = new BufferedInputStream(fis); 1.78 + int n = bis.read(buffer); 1.79 + while (n > 0) { 1.80 + crc.update(buffer, 0, n); 1.81 + n = bis.read(buffer); 1.82 } 1.83 } finally { 1.84 - Utils.close(bos); 1.85 - Utils.close(fos); 1.86 + Utils.close(bis); 1.87 + Utils.close(fis); 1.88 } 1.89 - if (outFile.length() < minlength) { 1.90 - throw new RuntimeException("could not create large file " + outFile.getAbsolutePath()); 1.91 + return crc.getValue(); 1.92 + } 1.93 + 1.94 + static void createLargeFile(OutputStream os, long minlength) throws IOException { 1.95 + byte[] buffer = new byte[BUFFER_LEN]; 1.96 + long count = getCount(minlength); 1.97 + for (long i = 0; i < count; i++) { 1.98 + os.write(buffer); 1.99 } 1.100 + os.flush(); 1.101 } 1.102 1.103 static void createJarWithLargeFile(File jarFile, File javaFile, 1.104 long minlength) throws IOException { 1.105 Utils.createClassFile(javaFile, null, true); 1.106 - File largeFile = new File("large.data"); 1.107 - createLargeFile(largeFile, minlength); 1.108 - String[] jarArgs = { 1.109 - "0cvf", 1.110 - jarFile.getAbsolutePath(), 1.111 - largeFile.getName(), 1.112 - Utils.getClassFileName(javaFile) 1.113 - }; 1.114 - Utils.jarTool.run(jarArgs); 1.115 + File classFile = new File(Utils.getClassFileName(javaFile)); 1.116 + ZipOutputStream zos = null; 1.117 + BufferedOutputStream bos = null; 1.118 + FileInputStream fis = null; 1.119 + try { 1.120 + zos = new ZipOutputStream(new FileOutputStream(jarFile)); 1.121 + zos.setLevel(ZipOutputStream.STORED); 1.122 + zos.setMethod(0); 1.123 + bos = new BufferedOutputStream(zos); 1.124 + 1.125 + ZipEntry ze = new ZipEntry("large.data"); 1.126 + ze.setCompressedSize(getCount(minlength) * BUFFER_LEN); 1.127 + ze.setSize(getCount(minlength) * BUFFER_LEN); 1.128 + ze.setCrc(computeCRC(minlength)); 1.129 + ze.setMethod(ZipEntry.STORED); 1.130 + zos.putNextEntry(ze); 1.131 + createLargeFile(bos, minlength); 1.132 + 1.133 + ze = new ZipEntry(classFile.getName()); 1.134 + ze.setCompressedSize(classFile.length()); 1.135 + ze.setSize(classFile.length()); 1.136 + ze.setCrc(computeCRC(classFile)); 1.137 + ze.setMethod(ZipEntry.STORED); 1.138 + zos.putNextEntry(ze); 1.139 + fis = new FileInputStream(classFile); 1.140 + Utils.copyStream(fis, bos); 1.141 + bos.flush(); 1.142 + zos.closeEntry(); 1.143 + } finally { 1.144 + Utils.close(bos); 1.145 + Utils.close(zos); 1.146 + Utils.close(fis); 1.147 + } 1.148 // deleted to prevent accidental linkage 1.149 new File(Utils.getClassFileName(javaFile)).delete(); 1.150 } 1.151 @@ -82,27 +146,40 @@ 1.152 static void createLargeJar(File jarFile, File javaFile) throws IOException { 1.153 File classFile = new File(Utils.getClassFileName(javaFile)); 1.154 Utils.createClassFile(javaFile, null, true); 1.155 - JarOutputStream jos = null; 1.156 + ZipOutputStream zos = null; 1.157 FileInputStream fis = null; 1.158 + final int MAX = Short.MAX_VALUE * 2 + 10; 1.159 + ZipEntry ze = null; 1.160 try { 1.161 - jos = new JarOutputStream(new FileOutputStream(jarFile)); 1.162 + zos = new ZipOutputStream(new FileOutputStream(jarFile)); 1.163 + zos.setLevel(ZipOutputStream.STORED); 1.164 + zos.setMethod(ZipOutputStream.STORED); 1.165 + for (int i = 0; i < MAX ; i++) { 1.166 + ze = new ZipEntry("X" + i + ".txt"); 1.167 + ze.setSize(0); 1.168 + ze.setCompressedSize(0); 1.169 + ze.setCrc(0); 1.170 + zos.putNextEntry(ze); 1.171 + } 1.172 1.173 - for (int i = 0; i < Short.MAX_VALUE * 2 + 10; i++) { 1.174 - jos.putNextEntry(new ZipEntry("X" + i + ".txt")); 1.175 - } 1.176 - jos.putNextEntry(new ZipEntry(classFile.getName())); 1.177 + // add a class file 1.178 + ze = new ZipEntry(classFile.getName()); 1.179 + ze.setCompressedSize(classFile.length()); 1.180 + ze.setSize(classFile.length()); 1.181 + ze.setCrc(computeCRC(classFile)); 1.182 + zos.putNextEntry(ze); 1.183 fis = new FileInputStream(classFile); 1.184 - Utils.copyStream(fis, jos); 1.185 + Utils.copyStream(fis, zos); 1.186 } finally { 1.187 - Utils.close(jos); 1.188 + Utils.close(zos); 1.189 Utils.close(fis); 1.190 - } 1.191 // deleted to prevent accidental linkage 1.192 new File(Utils.getClassFileName(javaFile)).delete(); 1.193 } 1.194 + } 1.195 1.196 // a jar with entries exceeding 64k + a class file for the existential test 1.197 - public static void testLargeJar(String... args) throws IOException { 1.198 + public static void testScenarioA(String... args) throws IOException { 1.199 File largeJar = new File("large.jar"); 1.200 File javaFile = new File("Foo.java"); 1.201 createLargeJar(largeJar, javaFile); 1.202 @@ -120,7 +197,7 @@ 1.203 } 1.204 1.205 // a jar with an enormous file + a class file for the existential test 1.206 - public static void testHugeJar(String... args) throws IOException { 1.207 + public static void testScenarioB(String... args) throws IOException { 1.208 final File largeJar = new File("huge.jar"); 1.209 final File javaFile = new File("Foo.java"); 1.210 1.211 @@ -131,7 +208,7 @@ 1.212 final long absolute = MAX_VALUE + 1L; 1.213 final long required = (long)(absolute * 1.1); // pad for sundries 1.214 System.out.println("\tavailable: " + available / GIGA + " GB"); 1.215 - System.out.println("\required: " + required / GIGA + " GB"); 1.216 + System.out.println("\trequired: " + required / GIGA + " GB"); 1.217 1.218 if (available > required) { 1.219 createJarWithLargeFile(largeJar, javaFile, absolute); 1.220 @@ -146,12 +223,19 @@ 1.221 Utils.deleteFile(largeJar); 1.222 } 1.223 } else { 1.224 - System.out.println("Warning: test passes vacuously, requirements exceeds available space"); 1.225 + System.out.println("Warning: testScenarioB passes vacuously," + 1.226 + " requirements exceeds available space"); 1.227 } 1.228 } 1.229 1.230 public static void main(String... args) throws IOException { 1.231 - testLargeJar(); 1.232 - testHugeJar(); 1.233 + testScenarioA(); 1.234 + System.out.println("testScenarioA: PASS"); 1.235 + if (Boolean.getBoolean("T6836682.testScenarioB")) { 1.236 + testScenarioB(); 1.237 + System.out.println("testScenarioB: PASS"); 1.238 + } else { 1.239 + System.out.println("Warning: testScenarioB, large file test skipped"); 1.240 + } 1.241 } 1.242 }