test/tools/javac/T6725036.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 839
a8437c34fdc7
child 1870
6101e52ce9e3
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

     1 /*
     2  * Copyright (c) 2008, 2011, 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  */
    24 /*
    25  * @test
    26  * @bug 6725036
    27  * @summary javac returns incorrect value for lastModifiedTime() when
    28  *          source is a zip file archive
    29  */
    31 import java.io.File;
    32 import java.util.Date;
    33 import java.util.jar.JarEntry;
    34 import java.util.jar.JarFile;
    35 import javax.tools.JavaFileObject;
    37 import com.sun.tools.javac.file.JavacFileManager;
    38 import com.sun.tools.javac.file.RelativePath.RelativeFile;
    39 import com.sun.tools.javac.file.ZipFileIndex;
    40 import com.sun.tools.javac.file.ZipFileIndexArchive;
    41 import com.sun.tools.javac.file.ZipFileIndexCache;
    42 import com.sun.tools.javac.util.Context;
    44 public class T6725036 {
    45     public static void main(String... args) throws Exception {
    46         new T6725036().run();
    47     }
    49     void run() throws Exception {
    50         RelativeFile TEST_ENTRY_NAME = new RelativeFile("java/lang/String.class");
    52         File f = new File(System.getProperty("java.home"));
    53         if (!f.getName().equals("jre"))
    54             f = new File(f, "jre");
    55         File rt_jar = new File(new File(f, "lib"), "rt.jar");
    57         JarFile j = new JarFile(rt_jar);
    58         JarEntry je = j.getJarEntry(TEST_ENTRY_NAME.getPath());
    59         long jarEntryTime = je.getTime();
    61         ZipFileIndexCache zfic = ZipFileIndexCache.getSharedInstance();
    62         ZipFileIndex zfi = zfic.getZipFileIndex(rt_jar, null, false, null, false);
    63         long zfiTime = zfi.getLastModified(TEST_ENTRY_NAME);
    65         check(je, jarEntryTime, zfi + ":" + TEST_ENTRY_NAME.getPath(), zfiTime);
    67         Context context = new Context();
    68         JavacFileManager fm = new JavacFileManager(context, false, null);
    69         ZipFileIndexArchive zfia = new ZipFileIndexArchive(fm, zfi);
    70         JavaFileObject jfo =
    71             zfia.getFileObject(TEST_ENTRY_NAME.dirname(),
    72                                    TEST_ENTRY_NAME.basename());
    73         long jfoTime = jfo.getLastModified();
    75         check(je, jarEntryTime, jfo, jfoTime);
    77         if (errors > 0)
    78             throw new Exception(errors + " occurred");
    79     }
    81     void check(Object ref, long refTime, Object test, long testTime) {
    82         if (refTime == testTime)
    83             return;
    84         System.err.println("Error: ");
    85         System.err.println("Expected: " + getText(ref, refTime));
    86         System.err.println("   Found: " + getText(test, testTime));
    87         errors++;
    88     }
    90     String getText(Object x, long t) {
    91         return String.format("%14d", t) + " (" + new Date(t) + ") from " + x;
    92     }
    94     int errors;
    95 }

mercurial