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