test/tools/javac/T6725036.java

Thu, 16 Oct 2008 07:39:53 -0700

author
jjg
date
Thu, 16 Oct 2008 07:39:53 -0700
changeset 145
2c1ef6ec9413
parent 103
e571266ae14f
child 554
9d9f26857129
permissions
-rw-r--r--

6759810: bad regression test causes source file to be deleted
Reviewed-by: mcimadamore

jjg@71 1 /*
jjg@71 2 * Copyright 2008 Sun Microsystems, Inc. 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 *
jjg@71 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@71 20 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@71 21 * have any 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@71 41 import com.sun.tools.javac.util.Context;
jjg@71 42
jjg@71 43 public class T6725036 {
jjg@71 44 public static void main(String... args) throws Exception {
jjg@71 45 new T6725036().run();
jjg@71 46 }
jjg@71 47
jjg@71 48 void run() throws Exception {
jjg@103 49 RelativeFile TEST_ENTRY_NAME = new RelativeFile("java/lang/String.class");
jjg@71 50
jjg@71 51 File f = new File(System.getProperty("java.home"));
jjg@71 52 if (!f.getName().equals("jre"))
jjg@71 53 f = new File(f, "jre");
jjg@71 54 File rt_jar = new File(new File(f, "lib"), "rt.jar");
jjg@71 55
jjg@71 56 JarFile j = new JarFile(rt_jar);
jjg@103 57 JarEntry je = j.getJarEntry(TEST_ENTRY_NAME.getPath());
jjg@71 58 long jarEntryTime = je.getTime();
jjg@71 59
jjg@71 60 ZipFileIndex zfi =
jjg@71 61 ZipFileIndex.getZipFileIndex(rt_jar, null, false, null, false);
jjg@71 62 long zfiTime = zfi.getLastModified(TEST_ENTRY_NAME);
jjg@71 63
jjg@103 64 check(je, jarEntryTime, zfi + ":" + TEST_ENTRY_NAME.getPath(), zfiTime);
jjg@71 65
jjg@71 66 Context context = new Context();
jjg@71 67 JavacFileManager fm = new JavacFileManager(context, false, null);
jjg@71 68 ZipFileIndexArchive zfia = new ZipFileIndexArchive(fm, zfi);
jjg@71 69 JavaFileObject jfo =
jjg@103 70 zfia.getFileObject(TEST_ENTRY_NAME.dirname(),
jjg@103 71 TEST_ENTRY_NAME.basename());
jjg@71 72 long jfoTime = jfo.getLastModified();
jjg@71 73
jjg@71 74 check(je, jarEntryTime, jfo, jfoTime);
jjg@71 75
jjg@71 76 if (errors > 0)
jjg@71 77 throw new Exception(errors + " occurred");
jjg@71 78 }
jjg@71 79
jjg@71 80 void check(Object ref, long refTime, Object test, long testTime) {
jjg@71 81 if (refTime == testTime)
jjg@71 82 return;
jjg@71 83 System.err.println("Error: ");
jjg@71 84 System.err.println("Expected: " + getText(ref, refTime));
jjg@71 85 System.err.println(" Found: " + getText(test, testTime));
jjg@71 86 errors++;
jjg@71 87 }
jjg@71 88
jjg@71 89 String getText(Object x, long t) {
jjg@71 90 return String.format("%14d", t) + " (" + new Date(t) + ") from " + x;
jjg@71 91 }
jjg@71 92
jjg@71 93 int errors;
jjg@71 94 }

mercurial