T6725036.java revision 3155:30e288cb2d22
1/* 2 * Copyright (c) 2008, 2015, 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 * @modules jdk.compiler/com.sun.tools.javac.api 31 * jdk.compiler/com.sun.tools.javac.file 32 * jdk.compiler/com.sun.tools.javac.main 33 * jdk.compiler/com.sun.tools.javac.util 34 * @build ToolBox 35 * @run main T6725036 36 */ 37 38import java.io.File; 39import java.io.IOException; 40import java.util.Collections; 41import java.util.Date; 42import java.util.jar.JarEntry; 43import java.util.jar.JarFile; 44 45import javax.tools.*; 46 47import com.sun.tools.javac.file.JavacFileManager; 48import com.sun.tools.javac.file.RelativePath.RelativeFile; 49import com.sun.tools.javac.util.Context; 50 51public class T6725036 { 52 public static void main(String... args) throws Exception { 53 new T6725036().run(); 54 } 55 56 void run() throws Exception { 57 RelativeFile TEST_ENTRY_NAME = new RelativeFile("java/lang/String.class"); 58 59 File testJar = createJar("test.jar", "java.lang.*"); 60 61 try (JarFile j = new JarFile(testJar)) { 62 JarEntry je = j.getJarEntry(TEST_ENTRY_NAME.getPath()); 63 long jarEntryTime = je.getTime(); 64 65 Context context = new Context(); 66 JavacFileManager fm = new JavacFileManager(context, false, null); 67 fm.setLocation(StandardLocation.CLASS_PATH, Collections.singletonList(testJar)); 68 FileObject fo = 69 fm.getFileForInput(StandardLocation.CLASS_PATH, "", TEST_ENTRY_NAME.getPath()); 70 long jfoTime = fo.getLastModified(); 71 72 check(je, jarEntryTime, fo, jfoTime); 73 74 if (errors > 0) 75 throw new Exception(errors + " occurred"); 76 } 77 } 78 79 File createJar(String name, String... paths) throws IOException { 80 JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 81 try (JavaFileManager fm = comp.getStandardFileManager(null, null, null)) { 82 File f = new File(name); 83 ToolBox tb = new ToolBox(); 84 tb.new JarTask(name) 85 .files(fm, StandardLocation.PLATFORM_CLASS_PATH, paths) 86 .run(); 87 return f; 88 } 89 } 90 91 void check(Object ref, long refTime, Object test, long testTime) { 92 if (refTime == testTime) 93 return; 94 System.err.println("Error: "); 95 System.err.println("Expected: " + getText(ref, refTime)); 96 System.err.println(" Found: " + getText(test, testTime)); 97 errors++; 98 } 99 100 String getText(Object x, long t) { 101 return String.format("%14d", t) + " (" + new Date(t) + ") from " + x; 102 } 103 104 int errors; 105} 106