test/tools/javac/nio/compileTest/CompileTest.java

Wed, 09 Feb 2011 14:04:53 -0800

author
jjg
date
Wed, 09 Feb 2011 14:04:53 -0800
changeset 869
c6cb387190ee
parent 847
babf86a1ac92
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7016750: tools/javac/nio/CompileTest failing in nightly test
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright (c) 2009, 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 6906175 6915476 6915497 7006564
    27  * @summary Path-based JavaFileManager
    28  * @compile -g CompileTest.java HelloPathWorld.java
    29  * @run main CompileTest
    30  */
    32 import java.io.*;
    33 import java.nio.file.*;
    34 import java.util.*;
    35 import java.util.jar.*;
    36 import javax.tools.*;
    38 import com.sun.tools.javac.nio.*;
    39 import com.sun.tools.javac.util.Context;
    40 import java.nio.file.spi.FileSystemProvider;
    43 public class CompileTest {
    44     public static void main(String[] args) throws Exception {
    45         new CompileTest().run();
    46     }
    48     public void run() throws Exception {
    49         File rtDir = new File("rt.dir");
    50         File javaHome = new File(System.getProperty("java.home"));
    51         if (javaHome.getName().equals("jre"))
    52             javaHome = javaHome.getParentFile();
    53         File rtJar = new File(new File(new File(javaHome, "jre"), "lib"), "rt.jar");
    54         expand(rtJar, rtDir);
    56         String[] rtDir_opts = {
    57             "-bootclasspath", rtDir.toString(),
    58             "-classpath", "",
    59             "-sourcepath", "",
    60             "-extdirs", ""
    61         };
    62         test(rtDir_opts, "HelloPathWorld");
    64         if (isJarFileSystemAvailable()) {
    65             String[] rtJar_opts = {
    66                 "-bootclasspath", rtJar.toString(),
    67                 "-classpath", "",
    68                 "-sourcepath", "",
    69                 "-extdirs", ""
    70             };
    71             test(rtJar_opts, "HelloPathWorld");
    73             String[] default_opts = { };
    74             test(default_opts, "HelloPathWorld");
    76             // finally, a non-trivial program
    77             test(default_opts, "CompileTest");
    78         } else
    79             System.err.println("jar file system not available: test skipped");
    80     }
    82     void test(String[] opts, String className) throws Exception {
    83         count++;
    84         System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className);
    85         Path testSrcDir = Paths.get(System.getProperty("test.src"));
    86         Path testClassesDir = Paths.get(System.getProperty("test.classes"));
    87         Path classes = Files.createDirectory(Paths.get("classes." + count));
    89         Context ctx = new Context();
    90         PathFileManager fm = new JavacPathFileManager(ctx, true, null);
    91         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    92         List<String> options = new ArrayList<String>();
    93         options.addAll(Arrays.asList(opts));
    94         options.addAll(Arrays.asList(
    95                 "-verbose", "-XDverboseCompilePolicy",
    96                 "-d", classes.toString(),
    97                 "-g"
    98         ));
    99         Iterable<? extends JavaFileObject> compilationUnits =
   100                 fm.getJavaFileObjects(testSrcDir.resolve(className + ".java"));
   101         StringWriter sw = new StringWriter();
   102         PrintWriter out = new PrintWriter(sw);
   103         JavaCompiler.CompilationTask t =
   104                 compiler.getTask(out, fm, null, options, null, compilationUnits);
   105         boolean ok = t.call();
   106         System.err.println(sw.toString());
   107         if (!ok) {
   108             throw new Exception("compilation failed");
   109         }
   111         File expect = new File("classes." + count + "/" + className + ".class");
   112         if (!expect.exists())
   113             throw new Exception("expected file not found: " + expect);
   114         // Note that we explicitly specify -g for compiling both the actual class and the expected class.
   115         // This isolates the expected class from javac options that might be given to jtreg.
   116         long expectedSize = new File(testClassesDir.toString(), className + ".class").length();
   117         long actualSize = expect.length();
   118         if (expectedSize != actualSize)
   119             throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize);
   120     }
   122     boolean isJarFileSystemAvailable() {
   123         boolean result = false;
   124         for (FileSystemProvider fsp: FileSystemProvider.installedProviders()) {
   125             String scheme = fsp.getScheme();
   126             System.err.println("Provider: " + scheme + " " + fsp);
   127             if (scheme.equalsIgnoreCase("jar") || scheme.equalsIgnoreCase("zip"))
   128                 result = true;
   129         }
   130         return result;
   131     }
   133     void expand(File jar, File dir) throws IOException {
   134         JarFile jarFile = new JarFile(jar);
   135         try {
   136             Enumeration<JarEntry> entries = jarFile.entries();
   137             while (entries.hasMoreElements()) {
   138                 JarEntry je = entries.nextElement();
   139                 if (!je.isDirectory()) {
   140                     copy(jarFile.getInputStream(je), new File(dir, je.getName()));
   141                 }
   142             }
   143         } finally {
   144             jarFile.close();
   145         }
   146     }
   148     void copy(InputStream in, File dest) throws IOException {
   149         dest.getParentFile().mkdirs();
   150         OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
   151         try {
   152             byte[] data = new byte[8192];
   153             int n;
   154             while ((n = in.read(data, 0, data.length)) > 0)
   155                 out.write(data, 0, n);
   156         } finally {
   157             out.close();
   158             in.close();
   159         }
   160     }
   162     void error(String message) {
   163         System.err.println("Error: " + message);
   164         errors++;
   165     }
   167     int errors;
   168     int count;
   169 }

mercurial