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

jjg@71 1 /*
jjg@839 2 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
jjg@71 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@71 4 *
jjg@71 5 * This code is free software; you can redistribute it and/or modify it
jjg@71 6 * under the terms of the GNU General Public License version 2 only, as
jjg@71 7 * published by the Free Software Foundation.
jjg@71 8 *
jjg@71 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@71 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@71 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@71 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@71 13 * accompanied this code).
jjg@71 14 *
jjg@71 15 * You should have received a copy of the GNU General Public License version
jjg@71 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@71 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@71 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
jjg@71 22 */
jjg@71 23
jjg@71 24 /*
jjg@71 25 * @test
jjg@71 26 * @bug 6725036
jjg@71 27 * @summary javac returns incorrect value for lastModifiedTime() when
jjg@71 28 * source is a zip file archive
jjg@71 29 */
jjg@71 30
jjg@71 31 import java.io.File;
jjg@71 32 import java.util.Date;
jjg@71 33 import java.util.jar.JarEntry;
jjg@71 34 import java.util.jar.JarFile;
jjg@71 35 import javax.tools.JavaFileObject;
jjg@71 36
jjg@71 37 import com.sun.tools.javac.file.JavacFileManager;
jjg@103 38 import com.sun.tools.javac.file.RelativePath.RelativeFile;
jjg@71 39 import com.sun.tools.javac.file.ZipFileIndex;
jjg@71 40 import com.sun.tools.javac.file.ZipFileIndexArchive;
jjg@839 41 import com.sun.tools.javac.file.ZipFileIndexCache;
jjg@71 42 import com.sun.tools.javac.util.Context;
jjg@71 43
jjg@71 44 public class T6725036 {
jjg@71 45 public static void main(String... args) throws Exception {
jjg@71 46 new T6725036().run();
jjg@71 47 }
jjg@71 48
jjg@71 49 void run() throws Exception {
jjg@103 50 RelativeFile TEST_ENTRY_NAME = new RelativeFile("java/lang/String.class");
jjg@71 51
jjg@71 52 File f = new File(System.getProperty("java.home"));
jjg@71 53 if (!f.getName().equals("jre"))
jjg@71 54 f = new File(f, "jre");
jjg@71 55 File rt_jar = new File(new File(f, "lib"), "rt.jar");
jjg@71 56
jjg@71 57 JarFile j = new JarFile(rt_jar);
jjg@103 58 JarEntry je = j.getJarEntry(TEST_ENTRY_NAME.getPath());
jjg@71 59 long jarEntryTime = je.getTime();
jjg@71 60
jjg@839 61 ZipFileIndexCache zfic = ZipFileIndexCache.getSharedInstance();
jjg@839 62 ZipFileIndex zfi = zfic.getZipFileIndex(rt_jar, null, false, null, false);
jjg@71 63 long zfiTime = zfi.getLastModified(TEST_ENTRY_NAME);
jjg@71 64
jjg@103 65 check(je, jarEntryTime, zfi + ":" + TEST_ENTRY_NAME.getPath(), zfiTime);
jjg@71 66
jjg@71 67 Context context = new Context();
jjg@71 68 JavacFileManager fm = new JavacFileManager(context, false, null);
jjg@71 69 ZipFileIndexArchive zfia = new ZipFileIndexArchive(fm, zfi);
jjg@71 70 JavaFileObject jfo =
jjg@103 71 zfia.getFileObject(TEST_ENTRY_NAME.dirname(),
jjg@103 72 TEST_ENTRY_NAME.basename());
jjg@71 73 long jfoTime = jfo.getLastModified();
jjg@71 74
jjg@71 75 check(je, jarEntryTime, jfo, jfoTime);
jjg@71 76
jjg@71 77 if (errors > 0)
jjg@71 78 throw new Exception(errors + " occurred");
jjg@71 79 }
jjg@71 80
jjg@71 81 void check(Object ref, long refTime, Object test, long testTime) {
jjg@71 82 if (refTime == testTime)
jjg@71 83 return;
jjg@71 84 System.err.println("Error: ");
jjg@71 85 System.err.println("Expected: " + getText(ref, refTime));
jjg@71 86 System.err.println(" Found: " + getText(test, testTime));
jjg@71 87 errors++;
jjg@71 88 }
jjg@71 89
jjg@71 90 String getText(Object x, long t) {
jjg@71 91 return String.format("%14d", t) + " (" + new Date(t) + ") from " + x;
jjg@71 92 }
jjg@71 93
jjg@71 94 int errors;
jjg@71 95 }

mercurial