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

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 554
9d9f26857129
child 809
e63b1f8341ce
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2009, 2010, 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
    27  * @summary Path-based JavaFileManager
    28  * @compile -g 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 = Paths.get("classes." + count);
    88         classes.createDirectory();
    90         Context ctx = new Context();
    91         PathFileManager fm = new JavacPathFileManager(ctx, true, null);
    92         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    93         List<String> options = new ArrayList<String>();
    94         options.addAll(Arrays.asList(opts));
    95         options.addAll(Arrays.asList(
    96                 "-verbose", "-XDverboseCompilePolicy",
    97                 "-d", classes.toString(),
    98                 "-g"
    99         ));
   100         Iterable<? extends JavaFileObject> compilationUnits =
   101                 fm.getJavaFileObjects(testSrcDir.resolve(className + ".java"));
   102         StringWriter sw = new StringWriter();
   103         PrintWriter out = new PrintWriter(sw);
   104         JavaCompiler.CompilationTask t =
   105                 compiler.getTask(out, fm, null, options, null, compilationUnits);
   106         boolean ok = t.call();
   107         System.err.println(sw.toString());
   108         if (!ok) {
   109             throw new Exception("compilation failed");
   110         }
   112         File expect = new File("classes." + count + "/" + className + ".class");
   113         if (!expect.exists())
   114             throw new Exception("expected file not found: " + expect);
   115         // Note that we explicitly specify -g for compiling both the actual class and the expected class.
   116         // This isolates the expected class from javac options that might be given to jtreg.
   117         long expectedSize = new File(testClassesDir.toString(), className + ".class").length();
   118         long actualSize = expect.length();
   119         if (expectedSize != actualSize)
   120             throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize);
   121     }
   123     boolean isJarFileSystemAvailable() {
   124         boolean result = false;
   125         for (FileSystemProvider fsp: FileSystemProvider.installedProviders()) {
   126             String scheme = fsp.getScheme();
   127             System.err.println("Provider: " + scheme + " " + fsp);
   128             if (scheme.equalsIgnoreCase("jar") || scheme.equalsIgnoreCase("zip"))
   129                 result = true;
   130         }
   131         return result;
   132     }
   134     void expand(File jar, File dir) throws IOException {
   135         JarFile jarFile = new JarFile(jar);
   136         try {
   137             Enumeration<JarEntry> entries = jarFile.entries();
   138             while (entries.hasMoreElements()) {
   139                 JarEntry je = entries.nextElement();
   140                 if (!je.isDirectory()) {
   141                     copy(jarFile.getInputStream(je), new File(dir, je.getName()));
   142                 }
   143             }
   144         } finally {
   145             jarFile.close();
   146         }
   147     }
   149     void copy(InputStream in, File dest) throws IOException {
   150         dest.getParentFile().mkdirs();
   151         OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
   152         try {
   153             byte[] data = new byte[8192];
   154             int n;
   155             while ((n = in.read(data, 0, data.length)) > 0)
   156                 out.write(data, 0, n);
   157         } finally {
   158             out.close();
   159             in.close();
   160         }
   161     }
   163     void error(String message) {
   164         System.err.println("Error: " + message);
   165         errors++;
   166     }
   168     int errors;
   169     int count;
   170 }

mercurial