T6725036.java revision 2721:f7ce2cfa4cdb
1/* 2 * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 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 6725036 8016760 27 * @summary javac returns incorrect value for lastModifiedTime() when 28 * source is a zip file archive 29 * @library /tools/lib 30 * @build ToolBox 31 * @run main T6725036 32 */ 33 34import java.io.File; 35import java.io.IOException; 36import java.util.Date; 37import java.util.jar.JarEntry; 38import java.util.jar.JarFile; 39import javax.tools.*; 40 41import com.sun.tools.javac.file.JavacFileManager; 42import com.sun.tools.javac.file.RelativePath.RelativeFile; 43import com.sun.tools.javac.file.ZipFileIndex; 44import com.sun.tools.javac.file.ZipFileIndexArchive; 45import com.sun.tools.javac.file.ZipFileIndexCache; 46import com.sun.tools.javac.util.Context; 47 48public class T6725036 { 49 public static void main(String... args) throws Exception { 50 new T6725036().run(); 51 } 52 53 void run() throws Exception { 54 RelativeFile TEST_ENTRY_NAME = new RelativeFile("java/lang/String.class"); 55 56 File testJar = createJar("test.jar", "java.lang.*"); 57 58 try (JarFile j = new JarFile(testJar)) { 59 JarEntry je = j.getJarEntry(TEST_ENTRY_NAME.getPath()); 60 long jarEntryTime = je.getTime(); 61 62 ZipFileIndexCache zfic = ZipFileIndexCache.getSharedInstance(); 63 ZipFileIndex zfi = zfic.getZipFileIndex(testJar, null, false, null, false); 64 long zfiTime = zfi.getLastModified(TEST_ENTRY_NAME); 65 66 check(je, jarEntryTime, zfi + ":" + TEST_ENTRY_NAME.getPath(), zfiTime); 67 68 Context context = new Context(); 69 JavacFileManager fm = new JavacFileManager(context, false, null); 70 ZipFileIndexArchive zfia = new ZipFileIndexArchive(fm, zfi); 71 JavaFileObject jfo = 72 zfia.getFileObject(TEST_ENTRY_NAME.dirname(), 73 TEST_ENTRY_NAME.basename()); 74 long jfoTime = jfo.getLastModified(); 75 76 check(je, jarEntryTime, jfo, jfoTime); 77 78 if (errors > 0) 79 throw new Exception(errors + " occurred"); 80 } 81 } 82 83 File createJar(String name, String... paths) throws IOException { 84 JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 85 try (JavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 86 File f = new File(name); 87 ToolBox tb = new ToolBox(); 88 tb.new JarTask(f.getPath()) 89 .files(fm, StandardLocation.PLATFORM_CLASS_PATH, paths) 90 .run(); 91 return f; 92 } 93 } 94 95 void check(Object ref, long refTime, Object test, long testTime) { 96 if (refTime == testTime) 97 return; 98 System.err.println("Error: "); 99 System.err.println("Expected: " + getText(ref, refTime)); 100 System.err.println(" Found: " + getText(test, testTime)); 101 errors++; 102 } 103 104 String getText(Object x, long t) { 105 return String.format("%14d", t) + " (" + new Date(t) + ") from " + x; 106 } 107 108 int errors; 109} 110