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