test/langtools/tools/javah/T7126832/T7126832.java
branchhttp-client-branch
changeset 55982 b6ff245c0db6
parent 55981 907bddce488c
parent 48332 651a95f30dfb
child 55983 e4a1f0c9d4c6
equal deleted inserted replaced
55981:907bddce488c 55982:b6ff245c0db6
     1 /*
       
     2  * Copyright (c) 2012, 2016, 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  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 7126832
       
    27  * @modules jdk.compiler/com.sun.tools.javah
       
    28  * @compile java.java
       
    29  * @summary com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
       
    30  * @run main T7126832
       
    31  */
       
    32 
       
    33 import java.io.*;
       
    34 import java.util.*;
       
    35 
       
    36 public class T7126832 {
       
    37     public static void main(String... args) throws Exception {
       
    38         new T7126832().run();
       
    39     }
       
    40 
       
    41     void run() throws Exception {
       
    42         Locale prev = Locale.getDefault();
       
    43         Locale.setDefault(Locale.ENGLISH);
       
    44         try {
       
    45             // Verify that a .java file is correctly diagnosed
       
    46             File ff = writeFile(new File("JavahTest.java"), "class JavahTest {}");
       
    47             test(Arrays.asList(ff.getPath()), 1, "Could not find class file for 'JavahTest.java'.");
       
    48 
       
    49             // Verify that a class named 'xx.java' is accepted.
       
    50             // Note that ./xx/java.class exists, so this should work ok
       
    51             test(Arrays.asList("xx.java"), 0, null);
       
    52 
       
    53             if (errors > 0) {
       
    54                 throw new Exception(errors + " errors occurred");
       
    55             }
       
    56         } finally {
       
    57            Locale.setDefault(prev);
       
    58         }
       
    59     }
       
    60 
       
    61     void test(List<String> args, int expectRC, String expectOut) {
       
    62         System.err.println("Test: " + args
       
    63                 + " rc:" + expectRC
       
    64                 + ((expectOut != null) ? " out:" + expectOut : ""));
       
    65 
       
    66         StringWriter sw = new StringWriter();
       
    67         PrintWriter pw = new PrintWriter(sw);
       
    68         int rc = 0;
       
    69         String out = null;
       
    70         try {
       
    71             rc = com.sun.tools.javah.Main.run(args.toArray(new String[args.size()]), pw);
       
    72             out = sw.toString();
       
    73         } catch(Exception ee) {
       
    74             rc = 1;
       
    75             out = ee.toString();;
       
    76         }
       
    77         pw.close();
       
    78         if (!out.isEmpty()) {
       
    79             System.err.println(out);
       
    80         }
       
    81         if (rc != expectRC) {
       
    82             error("Unexpected exit code: " + rc + "; expected: " + expectRC);
       
    83         }
       
    84         if (expectOut != null && !out.contains(expectOut)) {
       
    85             error("Expected string not found: " + expectOut);
       
    86         }
       
    87 
       
    88         System.err.println();
       
    89     }
       
    90 
       
    91     File writeFile(File ff, String ss) throws IOException {
       
    92         if (ff.getParentFile() != null)
       
    93             ff.getParentFile().mkdirs();
       
    94 
       
    95         try (FileWriter out = new FileWriter(ff)) {
       
    96             out.write(ss);
       
    97         }
       
    98         return ff;
       
    99     }
       
   100 
       
   101     void error(String msg) {
       
   102         System.err.println(msg);
       
   103         errors++;
       
   104     }
       
   105 
       
   106     int errors;
       
   107 }
       
   108