langtools/test/tools/javac/lib/ToolBox.java
author vromero
Mon, 16 Dec 2013 15:07:13 +0000
changeset 22149 c7e024d637bf
parent 16549 1bdeedb5446c
child 23810 b92eb80925f0
permissions -rw-r--r--
8028708: TEST_BUG, Tests should pass through VM options, langtools tests Reviewed-by: jjg, vromero Contributed-by: andrey.x.nazarov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     1
/*
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     4
 *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     7
 * published by the Free Software Foundation.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     8
 *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    13
 * accompanied this code).
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    14
 *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    18
 *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    21
 * questions.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    22
 */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    23
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    24
import java.io.BufferedReader;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    25
import java.io.File;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    26
import java.io.FileNotFoundException;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    27
import java.io.FileWriter;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    28
import java.io.IOException;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    29
import java.io.InputStreamReader;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    30
import java.io.PrintWriter;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    31
import java.io.StringWriter;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    32
import java.net.URI;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    33
import java.nio.charset.Charset;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    34
import java.nio.file.Files;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    35
import java.nio.file.Path;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    36
import java.nio.file.Paths;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    37
import java.nio.file.StandardOpenOption;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    38
import java.util.ArrayList;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    39
import java.util.Arrays;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    40
import java.util.Collection;
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    41
import java.util.Collections;
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    42
import java.util.EnumSet;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    43
import java.util.List;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    44
import java.util.Map;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    45
import java.util.Set;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    46
import java.util.regex.Matcher;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    47
import java.util.regex.Pattern;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    48
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    49
import javax.tools.JavaCompiler;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    50
import javax.tools.JavaFileObject;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    51
import javax.tools.SimpleJavaFileObject;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    52
import javax.tools.ToolProvider;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    53
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    54
import com.sun.source.util.JavacTask;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    55
import com.sun.tools.javac.api.JavacTaskImpl;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    56
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    57
import sun.tools.jar.Main;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    58
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    59
import static java.nio.file.StandardCopyOption.*;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    60
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    61
/**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    62
 * Toolbox for jtreg tests.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    63
 */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    64
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    65
public class ToolBox {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    66
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    67
    public static final String lineSeparator = System.getProperty("line.separator");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    68
    public static final String jdkUnderTest = System.getProperty("test.jdk");
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    69
    public static final Path javaBinary = Paths.get(jdkUnderTest, "bin", "java");
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    70
    public static final Path javacBinary = Paths.get(jdkUnderTest, "bin", "javac");
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    71
22149
c7e024d637bf 8028708: TEST_BUG, Tests should pass through VM options, langtools tests
vromero
parents: 16549
diff changeset
    72
    public static final List<String> testVMOpts = readOptions("test.vm.opts");
c7e024d637bf 8028708: TEST_BUG, Tests should pass through VM options, langtools tests
vromero
parents: 16549
diff changeset
    73
    public static final List<String> testToolVMOpts = readOptions("test.tool.vm.opts");
c7e024d637bf 8028708: TEST_BUG, Tests should pass through VM options, langtools tests
vromero
parents: 16549
diff changeset
    74
    public static final List<String> testJavaOpts = readOptions("test.java.opts");
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    75
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    76
    private static final Charset defaultCharset = Charset.defaultCharset();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    77
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    78
    static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    79
22149
c7e024d637bf 8028708: TEST_BUG, Tests should pass through VM options, langtools tests
vromero
parents: 16549
diff changeset
    80
    private static List<String> readOptions(String property) {
c7e024d637bf 8028708: TEST_BUG, Tests should pass through VM options, langtools tests
vromero
parents: 16549
diff changeset
    81
        String options = System.getProperty(property, "");
c7e024d637bf 8028708: TEST_BUG, Tests should pass through VM options, langtools tests
vromero
parents: 16549
diff changeset
    82
        return options.length() > 0 ? Arrays.asList(options.split("\\s+")) : Collections.<String>emptyList();
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    83
    }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    84
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    85
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    86
     * The expected result of command-like method execution.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    87
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    88
    public enum Expect {SUCCESS, FAIL}
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    89
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    90
    enum AcceptedParams {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    91
        EXPECT,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    92
        SOURCES,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    93
        OPTIONS,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    94
        STD_OUTPUT,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    95
        ERR_OUTPUT,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    96
        EXTRA_ENV,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    97
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    98
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    99
    enum OutputKind {STD, ERR}
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   100
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   101
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   102
     * Helper class to abstract the processing of command's output.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   103
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   104
    static abstract class WriterHelper {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   105
        OutputKind kind;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   106
        public abstract void pipeOutput(ProcessBuilder pb);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   107
        public abstract void readFromStream(Process p) throws IOException;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   108
        public abstract void addAll(Collection<? extends String> c) throws IOException;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   109
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   110
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   111
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   112
     * Helper class for redirecting command's output to a file.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   113
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   114
    static class FileWriterHelper extends WriterHelper {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   115
        File file;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   116
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   117
        FileWriterHelper(File file, OutputKind kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   118
            this.file = file;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   119
            this.kind = kind;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   120
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   121
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   122
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   123
        public void pipeOutput(ProcessBuilder pb) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   124
            if (file != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   125
                switch (kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   126
                    case STD:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   127
                        pb.redirectInput(file);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   128
                        break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   129
                    case ERR:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   130
                        pb.redirectError(file);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   131
                        break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   132
                }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   133
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   134
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   135
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   136
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   137
        public void readFromStream(Process p) throws IOException {}
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   138
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   139
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   140
        public void addAll(Collection<? extends String> c) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   141
            if (file.exists())
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   142
                Files.write(file.toPath(), c, defaultCharset,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   143
                        StandardOpenOption.WRITE, StandardOpenOption.APPEND);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   144
            else
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   145
                Files.write(file.toPath(), c, defaultCharset);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   146
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   147
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   148
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   149
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   150
     * Helper class for redirecting command's output to a String list.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   151
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   152
    static class ListWriterHelper extends WriterHelper {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   153
        List<String> list;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   154
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   155
        public ListWriterHelper(List<String> list, OutputKind kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   156
            this.kind = kind;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   157
            this.list = list;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   158
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   159
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   160
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   161
        public void pipeOutput(ProcessBuilder pb) {}
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   162
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   163
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   164
        public void readFromStream(Process p) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   165
            BufferedReader br = null;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   166
            switch (kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   167
                case STD:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   168
                    br = new BufferedReader(new InputStreamReader(p.getInputStream()));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   169
                    break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   170
                case ERR:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   171
                    br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   172
                    break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   173
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   174
            String line;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   175
            while ((line = br.readLine()) != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   176
                list.add(line);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   177
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   178
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   179
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   180
        public void addAll(Collection<? extends String> c) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   181
            list.addAll(c);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   182
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   183
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   184
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   185
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   186
     * Simple factory class for creating a WriterHelper instance.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   187
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   188
    static class WriterHelperFactory {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   189
        static WriterHelper make(File file, OutputKind kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   190
            return new FileWriterHelper(file, kind);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   191
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   192
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   193
        static WriterHelper make(List<String> list, OutputKind kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   194
            return new ListWriterHelper(list, kind);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   195
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   196
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   197
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   198
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   199
     * A generic class for holding command's arguments.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   200
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   201
    public static abstract class GenericArgs <T extends GenericArgs> {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   202
        protected static List<Set<AcceptedParams>> minAcceptedParams;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   203
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   204
        protected Set<AcceptedParams> currentParams =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   205
                EnumSet.<AcceptedParams>noneOf(AcceptedParams.class);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   206
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   207
        protected Expect whatToExpect;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   208
        protected WriterHelper stdOutput;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   209
        protected WriterHelper errOutput;
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   210
        protected List<String> args = new ArrayList<>();
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   211
        protected String[] argsArr;
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   212
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   213
        protected GenericArgs() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   214
            set(Expect.SUCCESS);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   215
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   216
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   217
        public T set(Expect whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   218
            currentParams.add(AcceptedParams.EXPECT);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   219
            this.whatToExpect = whatToExpt;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   220
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   221
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   222
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   223
        public T setStdOutput(List<String> stdOutput) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   224
            currentParams.add(AcceptedParams.STD_OUTPUT);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   225
            this.stdOutput = WriterHelperFactory.make(stdOutput, OutputKind.STD);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   226
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   227
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   228
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   229
        public T setStdOutput(File output) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   230
            currentParams.add(AcceptedParams.STD_OUTPUT);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   231
            this.stdOutput = WriterHelperFactory.make(output, OutputKind.STD);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   232
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   233
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   234
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   235
        public T setErrOutput(List<String> errOutput) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   236
            currentParams.add(AcceptedParams.ERR_OUTPUT);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   237
            this.errOutput = WriterHelperFactory.make(errOutput, OutputKind.ERR);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   238
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   239
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   240
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   241
        public T setErrOutput(File errOutput) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   242
            currentParams.add(AcceptedParams.ERR_OUTPUT);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   243
            this.errOutput = WriterHelperFactory.make(errOutput, OutputKind.ERR);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   244
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   245
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   246
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   247
        public T setAllArgs(String... args) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   248
            currentParams.add(AcceptedParams.OPTIONS);
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   249
            this.argsArr = args;
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   250
            return (T) this;
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   251
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   252
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   253
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   254
        public T appendArgs(String... args) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   255
            appendArgs(Arrays.asList(args));
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   256
            return (T)this;
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   257
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   258
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   259
        public T appendArgs(Path... args) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   260
            if (args != null) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   261
                List<String> list = new ArrayList<>();
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   262
                for (int i = 0; i < args.length; i++) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   263
                    if (args[i] != null) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   264
                        list.add(args[i].toString());
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   265
                    }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   266
                }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   267
                appendArgs(list);
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   268
            }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   269
            return (T)this;
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   270
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   271
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   272
        public T appendArgs(List<String> args) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   273
            if (args != null && args.size() > 0) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   274
                currentParams.add(AcceptedParams.OPTIONS);
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   275
                for (int i = 0; i < args.size(); i++) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   276
                    if (args.get(i) != null) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   277
                        this.args.add(args.get(i));
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   278
                    }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   279
                }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   280
            }
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   281
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   282
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   283
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   284
        public T setOptions(List<String> options) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   285
            currentParams.add(AcceptedParams.OPTIONS);
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   286
            this.args = options;
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   287
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   288
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   289
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   290
        public T setOptions(String... options) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   291
            currentParams.add(AcceptedParams.OPTIONS);
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   292
            this.args = Arrays.asList(options);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   293
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   294
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   295
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   296
        public boolean hasMinParams() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   297
            for (Set<AcceptedParams> minSet : minAcceptedParams) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   298
                if (currentParams.containsAll(minSet)) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   299
                    return true;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   300
                }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   301
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   302
            return false;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   303
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   304
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   305
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   306
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   307
     * A more specific class for holding javac-like command's arguments.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   308
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   309
    public static class JavaToolArgs extends GenericArgs<JavaToolArgs> {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   310
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   311
        static {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   312
            minAcceptedParams = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   313
            minAcceptedParams.add(EnumSet.<AcceptedParams>of(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   314
                    AcceptedParams.EXPECT, AcceptedParams.OPTIONS));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   315
            minAcceptedParams.add(EnumSet.<AcceptedParams>of(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   316
                    AcceptedParams.EXPECT, AcceptedParams.SOURCES));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   317
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   318
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   319
        protected List<? extends JavaFileObject> sources;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   320
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   321
        public JavaToolArgs() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   322
            super();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   323
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   324
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   325
        public JavaToolArgs(Expect whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   326
            super.set(whatToExpt);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   327
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   328
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   329
        public JavaToolArgs setSources(List<? extends JavaFileObject> sources) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   330
            currentParams.add(AcceptedParams.SOURCES);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   331
            this.sources = sources;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   332
            return this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   333
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   334
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   335
        public JavaToolArgs setSources(JavaSource... sources) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   336
            return setSources(Arrays.asList(sources));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   337
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   338
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   339
        public JavaToolArgs setSources(String... sources) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   340
            List<JavaSource> javaSrcs = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   341
            for (String source : sources) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   342
                javaSrcs.add(new JavaSource(source));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   343
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   344
            return setSources(javaSrcs);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   345
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   346
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   347
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   348
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   349
     * A more specific class for holding any command's arguments.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   350
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   351
    public static class AnyToolArgs extends GenericArgs<AnyToolArgs> {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   352
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   353
        static {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   354
            minAcceptedParams = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   355
            minAcceptedParams.add(EnumSet.<AcceptedParams>of(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   356
                    AcceptedParams.EXPECT, AcceptedParams.OPTIONS));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   357
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   358
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   359
        Map<String, String> extraEnv;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   360
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   361
        public AnyToolArgs() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   362
            super();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   363
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   364
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   365
        public AnyToolArgs(Expect whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   366
            set(whatToExpt);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   367
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   368
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   369
        public AnyToolArgs set(Map<String, String> extraEnv) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   370
            currentParams.add(AcceptedParams.EXTRA_ENV);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   371
            this.extraEnv = extraEnv;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   372
            return this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   373
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   374
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   375
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   376
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   377
     * Custom exception for bad command execution.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   378
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   379
    public static class CommandExecutionException extends Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   380
        CommandExecutionException(List<String> command, Expect whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   381
            super(createMessage(command, whatToExpt));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   382
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   383
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   384
        CommandExecutionException(Expect whatToExpt, String... command) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   385
            this(Arrays.asList(command), whatToExpt);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   386
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   387
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   388
        private static String createMessage(List<String> command, Expect whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   389
            StringBuilder sb = new StringBuilder().append("Command : ");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   390
            sb.append(command.toString()).append(lineSeparator);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   391
            switch (whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   392
                case SUCCESS:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   393
                    sb.append("    has unexpectedly failed");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   394
                    break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   395
                case FAIL:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   396
                    sb.append("    has been unexpectedly successful");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   397
                    break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   398
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   399
            return sb.toString();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   400
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   401
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   402
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   403
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   404
     * Custom exception for not equal resources.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   405
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   406
    public static class ResourcesNotEqualException extends Exception {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   407
        public ResourcesNotEqualException(List<String> res1, List<String> res2) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   408
            super(createMessage(res1, res2));
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   409
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   410
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   411
        public ResourcesNotEqualException(String line1, String line2) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   412
            super(createMessage(line1, line2));
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   413
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   414
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   415
        public ResourcesNotEqualException(Path path1, Path path2) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   416
            super(createMessage(path1, path2));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   417
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   418
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   419
        private static String createMessage(Path path1, Path path2) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   420
            return new StringBuilder()
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   421
                    .append("The resources provided for comparison in paths \n")
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   422
                    .append(path1.toString()).append(" and \n")
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   423
                    .append(path2.toString()).append("are different").toString();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   424
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   425
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   426
        private static String createMessage(String line1, String line2) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   427
            return new StringBuilder()
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   428
                    .append("The resources provided for comparison are different at lines: \n")
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   429
                    .append(line1).append(" and \n")
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   430
                    .append(line2).toString();
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   431
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   432
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   433
        private static String createMessage(List<String> res1, List<String> res2) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   434
            return new StringBuilder()
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   435
                    .append("The resources provided for comparison are different: \n")
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   436
                    .append("Resource 1 is: ").append(res1).append("\n and \n")
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   437
                    .append("Resource 2 is: ").append(res2).append("\n").toString();
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   438
        }
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   439
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   440
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   441
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   442
     * A javac compiler caller method.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   443
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   444
    public static int javac(JavaToolArgs params)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   445
            throws CommandExecutionException, IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   446
        if (params.hasMinParams()) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   447
            if (params.argsArr != null) {
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   448
                return genericJavaCMD(JavaCMD.JAVAC, params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   449
            } else {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   450
                return genericJavaCMD(JavaCMD.JAVAC_API, params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   451
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   452
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   453
        throw new AssertionError("javac command has been invoked with less parameters than needed");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   454
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   455
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   456
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   457
     * A javap calling method.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   458
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   459
    public static String javap(JavaToolArgs params)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   460
            throws CommandExecutionException, IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   461
        if (params.hasMinParams()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   462
            List<String> list = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   463
            params.setErrOutput(list);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   464
            genericJavaCMD(JavaCMD.JAVAP, params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   465
            return listToString(list);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   466
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   467
        throw new AssertionError("javap command has been invoked with less parameters than needed");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   468
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   469
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   470
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   471
     * A javah calling method.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   472
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   473
    public static int javah(JavaToolArgs params)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   474
            throws CommandExecutionException, IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   475
        if (params.hasMinParams()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   476
            return genericJavaCMD(JavaCMD.JAVAH, params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   477
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   478
        throw new AssertionError("javah command has been invoked with less parameters than needed");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   479
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   480
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   481
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   482
     * A enum class for langtools commands.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   483
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   484
    enum JavaCMD {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   485
        JAVAC {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   486
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   487
            int run(JavaToolArgs params, PrintWriter pw) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   488
                return com.sun.tools.javac.Main.compile(params.argsArr, pw);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   489
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   490
        },
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   491
        JAVAC_API {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   492
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   493
            int run(JavaToolArgs params, PrintWriter pw) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   494
                JavacTask ct = (JavacTask)comp.getTask(pw, null, null,
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   495
                        params.args, null, params.sources);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   496
                return ((JavacTaskImpl)ct).doCall().exitCode;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   497
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   498
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   499
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   500
            String getName() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   501
                return "javac";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   502
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   503
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   504
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   505
            List<String> getExceptionMsgContent(JavaToolArgs params) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   506
                List<String> result = super.getExceptionMsgContent(params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   507
                for (JavaFileObject source : params.sources) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   508
                    if (source instanceof JavaSource) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   509
                        result.add(((JavaSource)source).name);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   510
                    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   511
                }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   512
                return result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   513
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   514
        },
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   515
        JAVAH {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   516
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   517
            int run(JavaToolArgs params, PrintWriter pw) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   518
                return com.sun.tools.javah.Main.run(params.argsArr, pw);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   519
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   520
        },
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   521
        JAVAP {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   522
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   523
            int run(JavaToolArgs params, PrintWriter pw) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   524
                return com.sun.tools.javap.Main.run(params.argsArr, pw);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   525
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   526
        };
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   527
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   528
        abstract int run(JavaToolArgs params, PrintWriter pw);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   529
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   530
        String getName() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   531
            return this.name().toLowerCase();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   532
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   533
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   534
        List<String> getExceptionMsgContent(JavaToolArgs params) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   535
            List<String> result = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   536
            result.add(getName());
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   537
            result.addAll(params.argsArr != null ?
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   538
                    Arrays.asList(params.argsArr) :
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   539
                    params.args);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   540
            return result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   541
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   542
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   543
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   544
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   545
     * A helper method for executing langtools commands.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   546
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   547
    private static int genericJavaCMD(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   548
            JavaCMD cmd,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   549
            JavaToolArgs params)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   550
            throws CommandExecutionException, IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   551
        int rc = 0;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   552
        StringWriter sw = null;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   553
        try (PrintWriter pw = (params.errOutput == null) ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   554
                null : new PrintWriter(sw = new StringWriter())) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   555
            rc = cmd.run(params, pw);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   556
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   557
        String out = (sw == null) ? null : sw.toString();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   558
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   559
        if (params.errOutput != null && (out != null) && !out.isEmpty()) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   560
            params.errOutput.addAll(splitLines(out, lineSeparator));
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   561
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   562
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   563
        if ( (rc == 0 && params.whatToExpect == Expect.SUCCESS) ||
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   564
             (rc != 0 && params.whatToExpect == Expect.FAIL) ) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   565
            return rc;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   566
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   567
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   568
        throw new CommandExecutionException(cmd.getExceptionMsgContent(params),
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   569
                params.whatToExpect);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   570
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   571
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   572
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   573
     * A jar calling method.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   574
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   575
    public static boolean jar(String... params) throws CommandExecutionException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   576
        Main jarGenerator = new Main(System.out, System.err, "jar");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   577
        boolean result = jarGenerator.run(params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   578
        if (!result) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   579
            List<String> command = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   580
            command.add("jar");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   581
            command.addAll(Arrays.asList(params));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   582
            throw new CommandExecutionException(command, Expect.SUCCESS);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   583
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   584
        return result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   585
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   586
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   587
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   588
     * A general command calling method.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   589
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   590
    public static int executeCommand(AnyToolArgs params)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   591
            throws CommandExecutionException, IOException, InterruptedException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   592
        if (params.hasMinParams()) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   593
            List<String> cmd = (params.args != null) ?
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   594
                    params.args :
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   595
                    Arrays.asList(params.argsArr);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   596
            return executeCommand(cmd, params.extraEnv, params.stdOutput,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   597
                    params.errOutput, params.whatToExpect);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   598
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   599
        throw new AssertionError("command has been invoked with less parameters than needed");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   600
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   601
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   602
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   603
     * A helper method for calling a general command.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   604
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   605
    private static int executeCommand(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   606
            List<String> command,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   607
            Map<String, String> extraEnv,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   608
            WriterHelper stdOutput,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   609
            WriterHelper errOutput,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   610
            Expect whatToExpt)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   611
            throws IOException, InterruptedException, CommandExecutionException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   612
        ProcessBuilder pb = new ProcessBuilder(command);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   613
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   614
        if (stdOutput != null) stdOutput.pipeOutput(pb);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   615
        if (errOutput != null) errOutput.pipeOutput(pb);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   616
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   617
        if (extraEnv != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   618
            pb.environment().putAll(extraEnv);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   619
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   620
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   621
        Process p = pb.start();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   622
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   623
        if (stdOutput != null) stdOutput.readFromStream(p);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   624
        if (errOutput != null) errOutput.readFromStream(p);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   625
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   626
        int result = p.waitFor();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   627
        if ( (result == 0 && whatToExpt == Expect.SUCCESS) ||
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   628
             (result != 0 && whatToExpt == Expect.FAIL) ) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   629
            return result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   630
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   631
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   632
        throw new CommandExecutionException(command, whatToExpt);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   633
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   634
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   635
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   636
     * This set of methods can be used instead of diff when the only needed
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   637
     * result is the equality or inequality of the two given resources.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   638
     *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   639
     * A resource can be a file or a String list.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   640
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   641
    public static void compareLines(Path aPath, Path otherPath, String encoding)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   642
            throws FileNotFoundException, IOException, ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   643
        compareLines(aPath, otherPath, encoding, false);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   644
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   645
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   646
    public static void compareLines(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   647
            Path aPath, Path otherPath, String encoding, boolean trim)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   648
            throws FileNotFoundException, IOException, ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   649
        Charset charset = encoding != null ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   650
                Charset.forName(encoding) :
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   651
                defaultCharset;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   652
        List<String> list1 = Files.readAllLines(aPath, charset);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   653
        List<String> list2 = Files.readAllLines(otherPath, charset);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   654
        compareLines(list1, list2, trim);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   655
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   656
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   657
    public static void compareLines(Path path, List<String> strings, String encoding)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   658
            throws FileNotFoundException, IOException, ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   659
        compareLines(path, strings, encoding, false);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   660
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   661
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   662
    public static void compareLines(Path path, List<String> strings,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   663
            String encoding, boolean trim)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   664
            throws FileNotFoundException, IOException, ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   665
        Charset charset = encoding != null ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   666
                Charset.forName(encoding) :
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   667
                defaultCharset;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   668
        List<String> list = Files.readAllLines(path, charset);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   669
        compareLines(list, strings, trim);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   670
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   671
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   672
    public static void compareLines(List<String> list1, List<String> list2)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   673
            throws ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   674
        compareLines(list1, list2, false);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   675
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   676
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   677
    public static void compareLines(List<String> list1,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   678
            List<String> list2, boolean trim) throws ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   679
        if ((list1 == list2) || (list1 == null && list2 == null)) return;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   680
        if (list1.size() != list2.size())
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   681
            throw new ResourcesNotEqualException(list1, list2);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   682
        int i = 0;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   683
        int j = 0;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   684
        while (i < list1.size() &&
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   685
               j < list2.size() &&
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   686
               equals(list1.get(i), list2.get(j), trim)) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   687
            i++; j++;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   688
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   689
        if (!(i == list1.size() && j == list2.size()))
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   690
            throw new ResourcesNotEqualException(list1, list2);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   691
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   692
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   693
    private static boolean equals(String s1, String s2, boolean trim) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   694
        return (trim ? s1.trim().equals(s2.trim()) : s1.equals(s2));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   695
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   696
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   697
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   698
     * A set of simple grep-like methods, looks for regExpr in text.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   699
     * The content of text is split using the new line character as a pattern
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   700
     * and later the regExpr is seek in every split line. If a match is found,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   701
     * the whole line is added to the result.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   702
     */
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   703
    public static List<String> grep(String regExpr, String text, String sep) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   704
        return grep(regExpr, splitLines(text, sep));
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   705
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   706
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   707
    public static List<String> grep(String regExpr, List<String> text) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   708
        List<String> result = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   709
        Pattern pattern = Pattern.compile(regExpr);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   710
        for (String s : text) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   711
            if (pattern.matcher(s).find()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   712
                result.add(s);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   713
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   714
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   715
        return result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   716
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   717
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   718
    public static List<String> grep(String regExpr, File f)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   719
            throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   720
        List<String> lines = Files.readAllLines(f.toPath(), defaultCharset);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   721
        return grep(regExpr, lines);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   722
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   723
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   724
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   725
     * A touch-like method.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   726
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   727
    public static boolean touch(String fileName) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   728
        File file = new File(fileName);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   729
        return touch(file);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   730
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   731
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   732
    public static boolean touch(File file) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   733
        if (file.exists()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   734
            file.setLastModified(System.currentTimeMillis());
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   735
            return true;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   736
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   737
        return false;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   738
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   739
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   740
    public static void createJavaFile(File outFile) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   741
        createJavaFile(outFile, null);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   742
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   743
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   744
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   745
     * A method for creating a valid but very simple java file.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   746
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   747
    public static void createJavaFile(File outFile, File superClass)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   748
            throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   749
        String srcStr = "public class " + getSimpleName(outFile) + " ";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   750
        if (superClass != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   751
            srcStr = srcStr.concat("extends " + getSimpleName(superClass) + " ");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   752
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   753
        srcStr = srcStr.concat("{}");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   754
        try (PrintWriter ps = new PrintWriter(new FileWriter(outFile))) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   755
            ps.println(srcStr);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   756
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   757
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   758
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   759
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   760
     * Creates a java file name given its source.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   761
     * The file is created in the working directory, creating a directory
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   762
     * tree if there is a package declaration.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   763
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   764
    public static void createJavaFileFromSource(String source) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   765
        createJavaFileFromSource(null, source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   766
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   767
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   768
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   769
     * Creates a java file name given its source.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   770
     * The file is created in the working directory, creating a directory
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   771
     * tree if there is a package declaration or the argument initialPath
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   772
     * has a valid path.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   773
     *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   774
     * e.i. if initialPath is foo/ and the source is:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   775
     * package bar;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   776
     *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   777
     * public class bazz {}
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   778
     *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   779
     * this method will create the file foo/bar/bazz.java in the working
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   780
     * directory.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   781
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   782
    public static void createJavaFileFromSource(Path initialPath,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   783
            String source) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   784
        String fileName = getJavaFileNameFromSource(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   785
        String dirTree = getDirTreeFromSource(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   786
        Path path = (dirTree != null) ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   787
                Paths.get(dirTree, fileName) :
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   788
                Paths.get(fileName);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   789
        path = (initialPath != null) ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   790
                initialPath.resolve(path):
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   791
                path;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   792
        writeFile(path, source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   793
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   794
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   795
    static Pattern publicClassPattern =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   796
            Pattern.compile("public\\s+(?:class|enum|interface){1}\\s+(\\w+)");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   797
    static Pattern packageClassPattern =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   798
            Pattern.compile("(?:class|enum|interface){1}\\s+(\\w+)");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   799
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   800
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   801
     * Extracts the java file name from the class declaration.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   802
     * This method is intended for simple files and uses regular expressions,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   803
     * so comments matching the pattern can make the method fail.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   804
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   805
    private static String getJavaFileNameFromSource(String source) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   806
        String className = null;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   807
        Matcher matcher = publicClassPattern.matcher(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   808
        if (matcher.find()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   809
            className = matcher.group(1) + ".java";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   810
        } else {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   811
            matcher = packageClassPattern.matcher(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   812
            if (matcher.find()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   813
                className = matcher.group(1) + ".java";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   814
            } else {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   815
                throw new AssertionError("Could not extract the java class " +
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   816
                        "name from the provided source");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   817
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   818
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   819
        return className;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   820
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   821
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   822
    static Pattern packagePattern =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   823
            Pattern.compile("package\\s+(((?:\\w+\\.)*)(?:\\w+))");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   824
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   825
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   826
     * Extracts the path from the package declaration if present.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   827
     * This method is intended for simple files and uses regular expressions,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   828
     * so comments matching the pattern can make the method fail.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   829
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   830
    private static String getDirTreeFromSource(String source) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   831
        Matcher matcher = packagePattern.matcher(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   832
        return matcher.find() ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   833
            matcher.group(1).replace(".", File.separator) :
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   834
            null;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   835
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   836
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   837
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   838
     * A method for creating a jar's manifest file with supplied data.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   839
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   840
    public static void mkManifestWithClassPath(String mainClass,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   841
            String... classes) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   842
        List <String> lines = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   843
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   844
        StringBuilder sb = new StringBuilder("Class-Path: ".length() +
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   845
                classes[0].length()).append("Class-Path: ").append(classes[0]);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   846
        for (int i = 1; i < classes.length; i++) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   847
            sb.append(" ").append(classes[i]);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   848
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   849
        lines.add(sb.toString());
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   850
        if (mainClass != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   851
            lines.add(new StringBuilder("Main-Class: ".length() +
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   852
                      mainClass.length())
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   853
                      .append("Main-Class: ")
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   854
                      .append(mainClass).toString());
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   855
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   856
        Files.write(Paths.get("MANIFEST.MF"), lines, null);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   857
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   858
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   859
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   860
     * A utility method to obtain the file name.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   861
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   862
    static String getSimpleName(File inFile) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   863
        return inFile.toPath().getFileName().toString();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   864
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   865
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   866
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   867
     * A method to write to a file, the directory tree is created if needed.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   868
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   869
    public static File writeFile(Path path, String body) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   870
        File result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   871
        if (path.getParent() != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   872
            Files.createDirectories(path.getParent());
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   873
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   874
        try (FileWriter out = new FileWriter(result = path.toAbsolutePath().toFile())) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   875
            out.write(body);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   876
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   877
        return result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   878
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   879
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   880
    public static File writeFile(String path, String body) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   881
        return writeFile(Paths.get(path), body);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   882
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   883
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   884
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   885
     * A rm-like method, the file is deleted only if it exists.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   886
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   887
    public static void rm(Path path) throws Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   888
        Files.deleteIfExists(path);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   889
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   890
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   891
    public static void rm(String filename) throws Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   892
        rm(Paths.get(filename));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   893
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   894
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   895
    public static void rm(File f) throws Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   896
        rm(f.toPath());
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   897
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   898
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   899
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   900
     * Copy source file to destination file.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   901
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   902
    public static void copyFile(File destfile, File srcfile)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   903
        throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   904
        copyFile(destfile.toPath(), srcfile.toPath());
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   905
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   906
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   907
    public static void copyFile(Path destPath, Path srcPath)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   908
        throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   909
        Files.createDirectories(destPath);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   910
        Files.copy(srcPath, destPath, REPLACE_EXISTING);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   911
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   912
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   913
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   914
     * Splits a String using the System's line separator character as splitting point.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   915
     */
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   916
    public static List<String> splitLines(String lines, String sep) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   917
        return Arrays.asList(lines.split(sep));
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   918
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   919
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   920
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   921
     * Converts a String list into one String by appending the System's line separator
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   922
     * character after each component.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   923
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   924
    private static String listToString(List<String> lines) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   925
        StringBuilder sb = new StringBuilder();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   926
        for (String s : lines) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   927
            sb.append(s).append(lineSeparator);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   928
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   929
        return sb.toString();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   930
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   931
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   932
    /**
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   933
     * Returns true if the OS is a Windows version.
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   934
     */
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   935
    public static boolean isWindows() {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   936
        String osName = System.getProperty("os.name");
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   937
        return osName.toUpperCase().startsWith("WINDOWS");
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   938
    }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   939
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   940
    /**
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   941
     * Class representing an in-memory java source file. It is able to extract
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   942
     * the file name from simple source codes using regular expressions.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   943
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   944
    public static class JavaSource extends SimpleJavaFileObject {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   945
        String source;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   946
        String name;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   947
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   948
        public JavaSource(String className, String source) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   949
            super(URI.create(className),
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   950
                    JavaFileObject.Kind.SOURCE);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   951
            this.name = className;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   952
            this.source = source;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   953
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   954
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   955
        public JavaSource(String source) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   956
            super(URI.create(getJavaFileNameFromSource(source)),
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   957
                    JavaFileObject.Kind.SOURCE);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   958
            this.name = getJavaFileNameFromSource(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   959
            this.source = source;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   960
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   961
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   962
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   963
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   964
            return source;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   965
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   966
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   967
}