jdk/test/java/lang/invoke/lambda/LambdaAccessControlDoPrivilegedTest.java
author rfield
Thu, 06 Dec 2012 21:55:55 -0800
changeset 14762 34956da26ceb
child 16062 c64ef2b01401
permissions -rw-r--r--
8003881: Prevent lambda implementing inner classes from allowing the creation of new instances Summary: Lambda implementing inner classes now has private constructor (thanks Kumar) Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14762
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     1
/*
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     4
 *
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     8
 *
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    13
 * accompanied this code).
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    14
 *
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    18
 *
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    21
 * questions.
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    22
 */
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    23
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    24
/*
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    25
 * @test
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    26
 * @bug 8003881
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    27
 * @summary tests DoPrivileged action (implemented as lambda expressions) by
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    28
 * inserting them into the BootClassPath.
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    29
 * @compile -XDignore.symbol.file LambdaAccessControlDoPrivilegedTest.java
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    30
 * @run main/othervm LambdaAccessControlDoPrivilegedTest
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    31
 */
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    32
import java.io.BufferedReader;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    33
import java.io.File;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    34
import java.io.IOException;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    35
import java.io.InputStreamReader;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    36
import java.io.PrintWriter;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    37
import java.io.StringWriter;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    38
import java.nio.charset.Charset;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    39
import java.nio.file.Files;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    40
import java.util.ArrayList;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    41
import java.util.List;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    42
import java.util.Map;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    43
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    44
public class LambdaAccessControlDoPrivilegedTest extends LUtils {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    45
    public static void main(String... args) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    46
        final List<String> scratch = new ArrayList();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    47
        scratch.clear();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    48
        scratch.add("import java.security.*;");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    49
        scratch.add("public class DoPriv {");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    50
        scratch.add("public static void main(String... args) {");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    51
        scratch.add("String prop = AccessController.doPrivileged((PrivilegedAction<String>) () -> {");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    52
        scratch.add("return System.getProperty(\"user.home\");");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    53
        scratch.add("});");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    54
        scratch.add("}");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    55
        scratch.add("}");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    56
        File doprivJava = new File("DoPriv.java");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    57
        File doprivClass = getClassFile(doprivJava);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    58
        createFile(doprivJava, scratch);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    59
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    60
        scratch.clear();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    61
        scratch.add("public class Bar {");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    62
        scratch.add("public static void main(String... args) {");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    63
        scratch.add("System.out.println(\"sun.boot.class.path\" + \"=\" +");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    64
        scratch.add("    System.getProperty(\"sun.boot.class.path\", \"\"));");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    65
        scratch.add("System.setSecurityManager(new SecurityManager());");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    66
        scratch.add("DoPriv.main();");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    67
        scratch.add("}");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    68
        scratch.add("}");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    69
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    70
        File barJava = new File("Bar.java");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    71
        File barClass = getClassFile(barJava);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    72
        createFile(barJava, scratch);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    73
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    74
        String[] javacArgs = {barJava.getName(), doprivJava.getName()};
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    75
        compile(javacArgs);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    76
        File jarFile = new File("foo.jar");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    77
        String[] jargs = {"cvf", jarFile.getName(), doprivClass.getName()};
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    78
        jarTool.run(jargs);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    79
        doprivJava.delete();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    80
        doprivClass.delete();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    81
        TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    82
                               "-Xbootclasspath/p:foo.jar",
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    83
                               "-cp", ".", "Bar");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    84
        tr.assertZero("testDoPrivileged fails");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    85
        barJava.delete();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    86
        barClass.delete();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    87
        jarFile.delete();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    88
    }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    89
}
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    90
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    91
/*
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    92
 * support infrastructure to invoke a java class from the command line
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    93
 */
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    94
class LUtils {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    95
    static final sun.tools.jar.Main jarTool =
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    96
            new sun.tools.jar.Main(System.out, System.err, "jar-tool");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    97
    static final com.sun.tools.javac.Main javac =
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    98
            new com.sun.tools.javac.Main();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
    99
    static final File cwd = new File(".").getAbsoluteFile();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   100
    static final String JAVAHOME = System.getProperty("java.home");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   101
    static final boolean isWindows =
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   102
            System.getProperty("os.name", "unknown").startsWith("Windows");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   103
    //static final boolean isSDK = JAVAHOME.endsWith("jre");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   104
    static final File JAVA_BIN_FILE = new File(JAVAHOME, "bin");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   105
    static final File JAVA_CMD = new File(JAVA_BIN_FILE,
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   106
            isWindows ? "java.exe" : "java");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   107
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   108
    protected LUtils() {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   109
    }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   110
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   111
    public static void compile(String... args) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   112
        if (javac.compile(args) != 0) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   113
            throw new RuntimeException("compilation fails");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   114
        }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   115
    }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   116
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   117
    static void createFile(File outFile, List<String> content) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   118
        try {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   119
            Files.write(outFile.getAbsoluteFile().toPath(), content,
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   120
                    Charset.defaultCharset());
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   121
        } catch (IOException ex) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   122
            throw new RuntimeException(ex);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   123
        }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   124
    }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   125
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   126
    static File getClassFile(File javaFile) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   127
        return javaFile.getName().endsWith(".java")
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   128
                ? new File(javaFile.getName().replace(".java", ".class"))
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   129
                : null;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   130
    }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   131
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   132
    static String getSimpleName(File inFile) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   133
        String fname = inFile.getName();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   134
        return fname.substring(0, fname.indexOf("."));
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   135
    }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   136
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   137
    static TestResult doExec(String... cmds) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   138
        return doExec(null, null, cmds);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   139
    }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   140
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   141
    /*
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   142
     * A method which executes a java cmd and returns the results in a container
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   143
     */
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   144
    static TestResult doExec(Map<String, String> envToSet,
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   145
            java.util.Set<String> envToRemove, String... cmds) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   146
        String cmdStr = "";
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   147
        for (String x : cmds) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   148
            cmdStr = cmdStr.concat(x + " ");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   149
        }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   150
        ProcessBuilder pb = new ProcessBuilder(cmds);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   151
        Map<String, String> env = pb.environment();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   152
        if (envToRemove != null) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   153
            for (String key : envToRemove) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   154
                env.remove(key);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   155
            }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   156
        }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   157
        if (envToSet != null) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   158
            env.putAll(envToSet);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   159
        }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   160
        BufferedReader rdr = null;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   161
        try {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   162
            List<String> outputList = new ArrayList<>();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   163
            pb.redirectErrorStream(true);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   164
            Process p = pb.start();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   165
            rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   166
            String in = rdr.readLine();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   167
            while (in != null) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   168
                outputList.add(in);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   169
                in = rdr.readLine();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   170
            }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   171
            p.waitFor();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   172
            p.destroy();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   173
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   174
            return new TestResult(cmdStr, p.exitValue(), outputList,
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   175
                    env, new Throwable("current stack of the test"));
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   176
        } catch (Exception ex) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   177
            ex.printStackTrace();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   178
            throw new RuntimeException(ex.getMessage());
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   179
        }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   180
    }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   181
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   182
    static class TestResult {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   183
        String cmd;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   184
        int exitValue;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   185
        List<String> testOutput;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   186
        Map<String, String> env;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   187
        Throwable t;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   188
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   189
        public TestResult(String str, int rv, List<String> oList,
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   190
                Map<String, String> env, Throwable t) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   191
            cmd = str;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   192
            exitValue = rv;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   193
            testOutput = oList;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   194
            this.env = env;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   195
            this.t = t;
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   196
        }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   197
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   198
        void assertZero(String message) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   199
            if (exitValue != 0) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   200
                System.err.println(this);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   201
                throw new RuntimeException(message);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   202
            }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   203
        }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   204
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   205
        @Override
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   206
        public String toString() {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   207
            StringWriter sw = new StringWriter();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   208
            PrintWriter status = new PrintWriter(sw);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   209
            status.println("Cmd: " + cmd);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   210
            status.println("Return code: " + exitValue);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   211
            status.println("Environment variable:");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   212
            for (String x : env.keySet()) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   213
                status.println("\t" + x + "=" + env.get(x));
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   214
            }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   215
            status.println("Output:");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   216
            for (String x : testOutput) {
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   217
                status.println("\t" + x);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   218
            }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   219
            status.println("Exception:");
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   220
            status.println(t.getMessage());
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   221
            t.printStackTrace(status);
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   222
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   223
            return sw.getBuffer().toString();
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   224
        }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   225
    }
34956da26ceb 8003881: Prevent lambda implementing inner classes from allowing the creation of new instances
rfield
parents:
diff changeset
   226
}