langtools/test/tools/javac/lib/ToolBox.java
author vromero
Thu, 14 Mar 2013 08:30:16 +0000
changeset 16549 1bdeedb5446c
parent 16304 475504933a2d
child 22149 c7e024d637bf
permissions -rw-r--r--
8008582: jtreg failures after conversion of shell tests to Java Reviewed-by: jjg
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
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    72
    public static final List<String> testToolVMOpts;
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    73
    public static final List<String> testVMOpts;
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    74
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    75
    private static final Charset defaultCharset = Charset.defaultCharset();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    76
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    77
    static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    78
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    79
    static {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    80
        String sysProp = System.getProperty("test.tool.vm.opts");
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    81
        if (sysProp != null && sysProp.length() > 0) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    82
            testToolVMOpts = Arrays.asList(sysProp.split("\\s+"));
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    83
        } else {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    84
            testToolVMOpts = Collections.<String>emptyList();
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    85
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    86
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    87
        sysProp = System.getProperty("test.vm.opts");
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    88
        if (sysProp != null && sysProp.length() > 0) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    89
            testVMOpts = Arrays.asList(sysProp.split("\\s+"));
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    90
        } else {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    91
            testVMOpts = Collections.<String>emptyList();
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    92
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    93
    }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
    94
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    95
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    96
     * The expected result of command-like method execution.
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
    public enum Expect {SUCCESS, FAIL}
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
    99
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   100
    enum AcceptedParams {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   101
        EXPECT,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   102
        SOURCES,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   103
        OPTIONS,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   104
        STD_OUTPUT,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   105
        ERR_OUTPUT,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   106
        EXTRA_ENV,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   107
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   108
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   109
    enum OutputKind {STD, ERR}
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 to abstract the processing of command's output.
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 abstract class WriterHelper {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   115
        OutputKind kind;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   116
        public abstract void pipeOutput(ProcessBuilder pb);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   117
        public abstract void readFromStream(Process p) throws IOException;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   118
        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
   119
    }
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
     * 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
   123
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   124
    static class FileWriterHelper extends WriterHelper {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   125
        File file;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   126
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   127
        FileWriterHelper(File file, OutputKind kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   128
            this.file = file;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   129
            this.kind = kind;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   130
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   131
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   132
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   133
        public void pipeOutput(ProcessBuilder pb) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   134
            if (file != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   135
                switch (kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   136
                    case STD:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   137
                        pb.redirectInput(file);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   138
                        break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   139
                    case ERR:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   140
                        pb.redirectError(file);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   141
                        break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   142
                }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   143
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   144
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   145
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   146
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   147
        public void readFromStream(Process p) throws IOException {}
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
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   150
        public void addAll(Collection<? extends String> c) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   151
            if (file.exists())
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   152
                Files.write(file.toPath(), c, defaultCharset,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   153
                        StandardOpenOption.WRITE, StandardOpenOption.APPEND);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   154
            else
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   155
                Files.write(file.toPath(), c, defaultCharset);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   156
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   157
    }
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
     * 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
   161
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   162
    static class ListWriterHelper extends WriterHelper {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   163
        List<String> list;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   164
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   165
        public ListWriterHelper(List<String> list, OutputKind kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   166
            this.kind = kind;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   167
            this.list = list;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   168
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   169
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   170
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   171
        public void pipeOutput(ProcessBuilder pb) {}
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   172
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   173
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   174
        public void readFromStream(Process p) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   175
            BufferedReader br = null;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   176
            switch (kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   177
                case STD:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   178
                    br = new BufferedReader(new InputStreamReader(p.getInputStream()));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   179
                    break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   180
                case ERR:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   181
                    br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   182
                    break;
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
            String line;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   185
            while ((line = br.readLine()) != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   186
                list.add(line);
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
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   189
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   190
        public void addAll(Collection<? extends String> c) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   191
            list.addAll(c);
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
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   194
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
     * Simple factory class for creating a WriterHelper instance.
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
    static class WriterHelperFactory {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   199
        static WriterHelper make(File file, OutputKind kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   200
            return new FileWriterHelper(file, kind);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   201
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   202
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   203
        static WriterHelper make(List<String> list, OutputKind kind) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   204
            return new ListWriterHelper(list, kind);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   205
        }
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
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   208
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   209
     * A generic class for holding command's arguments.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   210
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   211
    public static abstract class GenericArgs <T extends GenericArgs> {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   212
        protected static List<Set<AcceptedParams>> minAcceptedParams;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   213
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   214
        protected Set<AcceptedParams> currentParams =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   215
                EnumSet.<AcceptedParams>noneOf(AcceptedParams.class);
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
        protected Expect whatToExpect;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   218
        protected WriterHelper stdOutput;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   219
        protected WriterHelper errOutput;
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   220
        protected List<String> args = new ArrayList<>();
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   221
        protected String[] argsArr;
16304
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
        protected GenericArgs() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   224
            set(Expect.SUCCESS);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   225
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   226
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   227
        public T set(Expect whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   228
            currentParams.add(AcceptedParams.EXPECT);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   229
            this.whatToExpect = whatToExpt;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   230
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   231
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   232
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   233
        public T setStdOutput(List<String> stdOutput) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   234
            currentParams.add(AcceptedParams.STD_OUTPUT);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   235
            this.stdOutput = WriterHelperFactory.make(stdOutput, OutputKind.STD);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   236
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   237
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   238
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   239
        public T setStdOutput(File output) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   240
            currentParams.add(AcceptedParams.STD_OUTPUT);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   241
            this.stdOutput = WriterHelperFactory.make(output, OutputKind.STD);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   242
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   243
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   244
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   245
        public T setErrOutput(List<String> errOutput) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   246
            currentParams.add(AcceptedParams.ERR_OUTPUT);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   247
            this.errOutput = WriterHelperFactory.make(errOutput, OutputKind.ERR);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   248
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   249
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   250
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   251
        public T setErrOutput(File errOutput) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   252
            currentParams.add(AcceptedParams.ERR_OUTPUT);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   253
            this.errOutput = WriterHelperFactory.make(errOutput, OutputKind.ERR);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   254
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   255
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   256
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   257
        public T setAllArgs(String... args) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   258
            currentParams.add(AcceptedParams.OPTIONS);
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   259
            this.argsArr = args;
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   260
            return (T) this;
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   261
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   262
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   263
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   264
        public T appendArgs(String... args) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   265
            appendArgs(Arrays.asList(args));
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   266
            return (T)this;
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   267
        }
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
        public T appendArgs(Path... args) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   270
            if (args != null) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   271
                List<String> list = new ArrayList<>();
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   272
                for (int i = 0; i < args.length; i++) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   273
                    if (args[i] != null) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   274
                        list.add(args[i].toString());
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   275
                    }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   276
                }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   277
                appendArgs(list);
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
            return (T)this;
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   280
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   281
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   282
        public T appendArgs(List<String> args) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   283
            if (args != null && args.size() > 0) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   284
                currentParams.add(AcceptedParams.OPTIONS);
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   285
                for (int i = 0; i < args.size(); i++) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   286
                    if (args.get(i) != null) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   287
                        this.args.add(args.get(i));
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   288
                    }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   289
                }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   290
            }
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   291
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   292
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   293
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   294
        public T setOptions(List<String> options) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   295
            currentParams.add(AcceptedParams.OPTIONS);
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   296
            this.args = options;
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   297
            return (T)this;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   298
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   299
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   300
        public T setOptions(String... options) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   301
            currentParams.add(AcceptedParams.OPTIONS);
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   302
            this.args = Arrays.asList(options);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   303
            return (T)this;
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
        public boolean hasMinParams() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   307
            for (Set<AcceptedParams> minSet : minAcceptedParams) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   308
                if (currentParams.containsAll(minSet)) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   309
                    return true;
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
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   312
            return false;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   313
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   314
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   315
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   316
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   317
     * 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
   318
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   319
    public static class JavaToolArgs extends GenericArgs<JavaToolArgs> {
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
        static {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   322
            minAcceptedParams = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   323
            minAcceptedParams.add(EnumSet.<AcceptedParams>of(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   324
                    AcceptedParams.EXPECT, AcceptedParams.OPTIONS));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   325
            minAcceptedParams.add(EnumSet.<AcceptedParams>of(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   326
                    AcceptedParams.EXPECT, AcceptedParams.SOURCES));
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
        protected List<? extends JavaFileObject> sources;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   330
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   331
        public JavaToolArgs() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   332
            super();
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(Expect whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   336
            super.set(whatToExpt);
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(List<? extends JavaFileObject> sources) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   340
            currentParams.add(AcceptedParams.SOURCES);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   341
            this.sources = sources;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   342
            return this;
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
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   345
        public JavaToolArgs setSources(JavaSource... sources) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   346
            return setSources(Arrays.asList(sources));
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
        public JavaToolArgs setSources(String... sources) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   350
            List<JavaSource> javaSrcs = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   351
            for (String source : sources) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   352
                javaSrcs.add(new JavaSource(source));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   353
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   354
            return setSources(javaSrcs);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   355
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   356
    }
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
     * 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
   360
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   361
    public static class AnyToolArgs extends GenericArgs<AnyToolArgs> {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   362
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   363
        static {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   364
            minAcceptedParams = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   365
            minAcceptedParams.add(EnumSet.<AcceptedParams>of(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   366
                    AcceptedParams.EXPECT, AcceptedParams.OPTIONS));
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
        Map<String, String> extraEnv;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   370
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   371
        public AnyToolArgs() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   372
            super();
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
        public AnyToolArgs(Expect whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   376
            set(whatToExpt);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   377
        }
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 AnyToolArgs set(Map<String, String> extraEnv) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   380
            currentParams.add(AcceptedParams.EXTRA_ENV);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   381
            this.extraEnv = extraEnv;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   382
            return this;
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
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   385
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
     * Custom exception for bad command execution.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   388
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   389
    public static class CommandExecutionException extends Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   390
        CommandExecutionException(List<String> command, Expect whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   391
            super(createMessage(command, whatToExpt));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   392
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   393
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   394
        CommandExecutionException(Expect whatToExpt, String... command) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   395
            this(Arrays.asList(command), whatToExpt);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   396
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   397
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   398
        private static String createMessage(List<String> command, Expect whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   399
            StringBuilder sb = new StringBuilder().append("Command : ");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   400
            sb.append(command.toString()).append(lineSeparator);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   401
            switch (whatToExpt) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   402
                case SUCCESS:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   403
                    sb.append("    has unexpectedly failed");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   404
                    break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   405
                case FAIL:
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   406
                    sb.append("    has been unexpectedly successful");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   407
                    break;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   408
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   409
            return sb.toString();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   410
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   411
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   412
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
     * Custom exception for not equal resources.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   415
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   416
    public static class ResourcesNotEqualException extends Exception {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   417
        public ResourcesNotEqualException(List<String> res1, List<String> res2) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   418
            super(createMessage(res1, res2));
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   419
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   420
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   421
        public ResourcesNotEqualException(String line1, String line2) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   422
            super(createMessage(line1, line2));
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   423
        }
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
        public ResourcesNotEqualException(Path path1, Path path2) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   426
            super(createMessage(path1, path2));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   427
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   428
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   429
        private static String createMessage(Path path1, Path path2) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   430
            return new StringBuilder()
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   431
                    .append("The resources provided for comparison in paths \n")
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   432
                    .append(path1.toString()).append(" and \n")
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   433
                    .append(path2.toString()).append("are different").toString();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   434
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   435
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   436
        private static String createMessage(String line1, String line2) {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   437
            return new StringBuilder()
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   438
                    .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
   439
                    .append(line1).append(" and \n")
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   440
                    .append(line2).toString();
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   441
        }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   442
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   443
        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
   444
            return new StringBuilder()
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   445
                    .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
   446
                    .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
   447
                    .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
   448
        }
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   449
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   450
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
     * A javac compiler caller method.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   453
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   454
    public static int javac(JavaToolArgs params)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   455
            throws CommandExecutionException, IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   456
        if (params.hasMinParams()) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   457
            if (params.argsArr != null) {
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   458
                return genericJavaCMD(JavaCMD.JAVAC, params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   459
            } else {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   460
                return genericJavaCMD(JavaCMD.JAVAC_API, params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   461
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   462
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   463
        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
   464
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   465
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
     * A javap calling method.
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
    public static String javap(JavaToolArgs params)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   470
            throws CommandExecutionException, IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   471
        if (params.hasMinParams()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   472
            List<String> list = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   473
            params.setErrOutput(list);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   474
            genericJavaCMD(JavaCMD.JAVAP, params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   475
            return listToString(list);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   476
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   477
        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
   478
    }
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
     * A javah calling method.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   482
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   483
    public static int javah(JavaToolArgs params)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   484
            throws CommandExecutionException, IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   485
        if (params.hasMinParams()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   486
            return genericJavaCMD(JavaCMD.JAVAH, params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   487
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   488
        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
   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
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   492
     * A enum class for langtools commands.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   493
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   494
    enum JavaCMD {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   495
        JAVAC {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   496
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   497
            int run(JavaToolArgs params, PrintWriter pw) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   498
                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
   499
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   500
        },
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   501
        JAVAC_API {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   502
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   503
            int run(JavaToolArgs params, PrintWriter pw) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   504
                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
   505
                        params.args, null, params.sources);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   506
                return ((JavacTaskImpl)ct).doCall().exitCode;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   507
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   508
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   509
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   510
            String getName() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   511
                return "javac";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   512
            }
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
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   515
            List<String> getExceptionMsgContent(JavaToolArgs params) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   516
                List<String> result = super.getExceptionMsgContent(params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   517
                for (JavaFileObject source : params.sources) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   518
                    if (source instanceof JavaSource) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   519
                        result.add(((JavaSource)source).name);
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
                }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   522
                return result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   523
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   524
        },
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   525
        JAVAH {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   526
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   527
            int run(JavaToolArgs params, PrintWriter pw) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   528
                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
   529
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   530
        },
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   531
        JAVAP {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   532
            @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   533
            int run(JavaToolArgs params, PrintWriter pw) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   534
                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
   535
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   536
        };
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   537
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   538
        abstract int run(JavaToolArgs params, PrintWriter pw);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   539
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   540
        String getName() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   541
            return this.name().toLowerCase();
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
        List<String> getExceptionMsgContent(JavaToolArgs params) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   545
            List<String> result = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   546
            result.add(getName());
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   547
            result.addAll(params.argsArr != null ?
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   548
                    Arrays.asList(params.argsArr) :
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   549
                    params.args);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   550
            return result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   551
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   552
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   553
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   554
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   555
     * A helper method for executing langtools commands.
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
    private static int genericJavaCMD(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   558
            JavaCMD cmd,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   559
            JavaToolArgs params)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   560
            throws CommandExecutionException, IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   561
        int rc = 0;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   562
        StringWriter sw = null;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   563
        try (PrintWriter pw = (params.errOutput == null) ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   564
                null : new PrintWriter(sw = new StringWriter())) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   565
            rc = cmd.run(params, pw);
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
        String out = (sw == null) ? null : sw.toString();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   568
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   569
        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
   570
            params.errOutput.addAll(splitLines(out, lineSeparator));
16304
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
        if ( (rc == 0 && params.whatToExpect == Expect.SUCCESS) ||
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   574
             (rc != 0 && params.whatToExpect == Expect.FAIL) ) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   575
            return rc;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   576
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   577
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   578
        throw new CommandExecutionException(cmd.getExceptionMsgContent(params),
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   579
                params.whatToExpect);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   580
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   581
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   582
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   583
     * A jar calling method.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   584
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   585
    public static boolean jar(String... params) throws CommandExecutionException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   586
        Main jarGenerator = new Main(System.out, System.err, "jar");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   587
        boolean result = jarGenerator.run(params);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   588
        if (!result) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   589
            List<String> command = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   590
            command.add("jar");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   591
            command.addAll(Arrays.asList(params));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   592
            throw new CommandExecutionException(command, Expect.SUCCESS);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   593
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   594
        return result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   595
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   596
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   597
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   598
     * A general command calling method.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   599
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   600
    public static int executeCommand(AnyToolArgs params)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   601
            throws CommandExecutionException, IOException, InterruptedException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   602
        if (params.hasMinParams()) {
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   603
            List<String> cmd = (params.args != null) ?
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   604
                    params.args :
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   605
                    Arrays.asList(params.argsArr);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   606
            return executeCommand(cmd, params.extraEnv, params.stdOutput,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   607
                    params.errOutput, params.whatToExpect);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   608
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   609
        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
   610
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   611
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   612
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   613
     * A helper method for calling a general command.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   614
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   615
    private static int executeCommand(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   616
            List<String> command,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   617
            Map<String, String> extraEnv,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   618
            WriterHelper stdOutput,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   619
            WriterHelper errOutput,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   620
            Expect whatToExpt)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   621
            throws IOException, InterruptedException, CommandExecutionException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   622
        ProcessBuilder pb = new ProcessBuilder(command);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   623
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   624
        if (stdOutput != null) stdOutput.pipeOutput(pb);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   625
        if (errOutput != null) errOutput.pipeOutput(pb);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   626
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   627
        if (extraEnv != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   628
            pb.environment().putAll(extraEnv);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   629
        }
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
        Process p = pb.start();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   632
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   633
        if (stdOutput != null) stdOutput.readFromStream(p);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   634
        if (errOutput != null) errOutput.readFromStream(p);
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
        int result = p.waitFor();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   637
        if ( (result == 0 && whatToExpt == Expect.SUCCESS) ||
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   638
             (result != 0 && whatToExpt == Expect.FAIL) ) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   639
            return result;
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
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   642
        throw new CommandExecutionException(command, whatToExpt);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   643
    }
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
     * 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
   647
     * 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
   648
     *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   649
     * 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
   650
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   651
    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
   652
            throws FileNotFoundException, IOException, ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   653
        compareLines(aPath, otherPath, encoding, false);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   654
    }
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
    public static void compareLines(
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   657
            Path aPath, Path otherPath, String encoding, boolean trim)
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
        Charset charset = encoding != null ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   660
                Charset.forName(encoding) :
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   661
                defaultCharset;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   662
        List<String> list1 = Files.readAllLines(aPath, charset);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   663
        List<String> list2 = Files.readAllLines(otherPath, charset);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   664
        compareLines(list1, list2, trim);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   665
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   666
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   667
    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
   668
            throws FileNotFoundException, IOException, ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   669
        compareLines(path, strings, encoding, false);
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(Path path, List<String> strings,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   673
            String encoding, boolean trim)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   674
            throws FileNotFoundException, IOException, ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   675
        Charset charset = encoding != null ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   676
                Charset.forName(encoding) :
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   677
                defaultCharset;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   678
        List<String> list = Files.readAllLines(path, charset);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   679
        compareLines(list, strings, trim);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   680
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   681
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   682
    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
   683
            throws ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   684
        compareLines(list1, list2, false);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   685
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   686
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   687
    public static void compareLines(List<String> list1,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   688
            List<String> list2, boolean trim) throws ResourcesNotEqualException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   689
        if ((list1 == list2) || (list1 == null && list2 == null)) return;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   690
        if (list1.size() != list2.size())
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   691
            throw new ResourcesNotEqualException(list1, list2);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   692
        int i = 0;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   693
        int j = 0;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   694
        while (i < list1.size() &&
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   695
               j < list2.size() &&
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   696
               equals(list1.get(i), list2.get(j), trim)) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   697
            i++; j++;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   698
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   699
        if (!(i == list1.size() && j == list2.size()))
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   700
            throw new ResourcesNotEqualException(list1, list2);
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   701
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   702
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   703
    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
   704
        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
   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
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   708
     * 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
   709
     * 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
   710
     * 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
   711
     * the whole line is added to the result.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   712
     */
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   713
    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
   714
        return grep(regExpr, splitLines(text, sep));
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   715
    }
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
    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
   718
        List<String> result = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   719
        Pattern pattern = Pattern.compile(regExpr);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   720
        for (String s : text) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   721
            if (pattern.matcher(s).find()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   722
                result.add(s);
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
        return result;
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
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   728
    public static List<String> grep(String regExpr, File f)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   729
            throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   730
        List<String> lines = Files.readAllLines(f.toPath(), defaultCharset);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   731
        return grep(regExpr, lines);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   732
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   733
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   734
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   735
     * A touch-like method.
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
    public static boolean touch(String fileName) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   738
        File file = new File(fileName);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   739
        return touch(file);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   740
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   741
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   742
    public static boolean touch(File file) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   743
        if (file.exists()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   744
            file.setLastModified(System.currentTimeMillis());
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   745
            return true;
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
        return false;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   748
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   749
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   750
    public static void createJavaFile(File outFile) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   751
        createJavaFile(outFile, null);
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
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   754
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   755
     * 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
   756
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   757
    public static void createJavaFile(File outFile, File superClass)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   758
            throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   759
        String srcStr = "public class " + getSimpleName(outFile) + " ";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   760
        if (superClass != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   761
            srcStr = srcStr.concat("extends " + getSimpleName(superClass) + " ");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   762
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   763
        srcStr = srcStr.concat("{}");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   764
        try (PrintWriter ps = new PrintWriter(new FileWriter(outFile))) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   765
            ps.println(srcStr);
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
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   770
     * Creates a java file name given its source.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   771
     * 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
   772
     * tree if there is a package declaration.
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
    public static void createJavaFileFromSource(String source) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   775
        createJavaFileFromSource(null, source);
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
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
     * Creates a java file name given its source.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   780
     * 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
   781
     * 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
   782
     * has a valid path.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   783
     *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   784
     * 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
   785
     * package bar;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   786
     *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   787
     * public class bazz {}
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   788
     *
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   789
     * 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
   790
     * directory.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   791
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   792
    public static void createJavaFileFromSource(Path initialPath,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   793
            String source) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   794
        String fileName = getJavaFileNameFromSource(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   795
        String dirTree = getDirTreeFromSource(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   796
        Path path = (dirTree != null) ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   797
                Paths.get(dirTree, fileName) :
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   798
                Paths.get(fileName);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   799
        path = (initialPath != null) ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   800
                initialPath.resolve(path):
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   801
                path;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   802
        writeFile(path, source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   803
    }
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
    static Pattern publicClassPattern =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   806
            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
   807
    static Pattern packageClassPattern =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   808
            Pattern.compile("(?:class|enum|interface){1}\\s+(\\w+)");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   809
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   810
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   811
     * Extracts the java file name from the class declaration.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   812
     * 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
   813
     * 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
   814
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   815
    private static String getJavaFileNameFromSource(String source) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   816
        String className = null;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   817
        Matcher matcher = publicClassPattern.matcher(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   818
        if (matcher.find()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   819
            className = matcher.group(1) + ".java";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   820
        } else {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   821
            matcher = packageClassPattern.matcher(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   822
            if (matcher.find()) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   823
                className = matcher.group(1) + ".java";
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   824
            } else {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   825
                throw new AssertionError("Could not extract the java class " +
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   826
                        "name from the provided source");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   827
            }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   828
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   829
        return className;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   830
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   831
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   832
    static Pattern packagePattern =
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   833
            Pattern.compile("package\\s+(((?:\\w+\\.)*)(?:\\w+))");
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   834
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
     * Extracts the path from the package declaration if present.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   837
     * 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
   838
     * 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
   839
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   840
    private static String getDirTreeFromSource(String source) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   841
        Matcher matcher = packagePattern.matcher(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   842
        return matcher.find() ?
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   843
            matcher.group(1).replace(".", File.separator) :
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   844
            null;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   845
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   846
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   847
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   848
     * 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
   849
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   850
    public static void mkManifestWithClassPath(String mainClass,
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   851
            String... classes) throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   852
        List <String> lines = new ArrayList<>();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   853
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   854
        StringBuilder sb = new StringBuilder("Class-Path: ".length() +
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   855
                classes[0].length()).append("Class-Path: ").append(classes[0]);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   856
        for (int i = 1; i < classes.length; i++) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   857
            sb.append(" ").append(classes[i]);
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
        lines.add(sb.toString());
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   860
        if (mainClass != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   861
            lines.add(new StringBuilder("Main-Class: ".length() +
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   862
                      mainClass.length())
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   863
                      .append("Main-Class: ")
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   864
                      .append(mainClass).toString());
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
        Files.write(Paths.get("MANIFEST.MF"), lines, null);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   867
    }
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
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   870
     * A utility method to obtain the file name.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   871
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   872
    static String getSimpleName(File inFile) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   873
        return inFile.toPath().getFileName().toString();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   874
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   875
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
     * 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
   878
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   879
    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
   880
        File result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   881
        if (path.getParent() != null) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   882
            Files.createDirectories(path.getParent());
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
        try (FileWriter out = new FileWriter(result = path.toAbsolutePath().toFile())) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   885
            out.write(body);
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
        return result;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   888
    }
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
    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
   891
        return writeFile(Paths.get(path), body);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   892
    }
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
     * 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
   896
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   897
    public static void rm(Path path) throws Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   898
        Files.deleteIfExists(path);
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
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   901
    public static void rm(String filename) throws Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   902
        rm(Paths.get(filename));
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   903
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   904
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   905
    public static void rm(File f) throws Exception {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   906
        rm(f.toPath());
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   907
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   908
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   909
    /**
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   910
     * Copy source file to destination file.
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
    public static void copyFile(File destfile, File srcfile)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   913
        throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   914
        copyFile(destfile.toPath(), srcfile.toPath());
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   915
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   916
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   917
    public static void copyFile(Path destPath, Path srcPath)
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   918
        throws IOException {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   919
        Files.createDirectories(destPath);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   920
        Files.copy(srcPath, destPath, REPLACE_EXISTING);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   921
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   922
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
     * 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
   925
     */
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   926
    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
   927
        return Arrays.asList(lines.split(sep));
16304
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
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
     * 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
   932
     * character after each component.
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   933
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   934
    private static String listToString(List<String> lines) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   935
        StringBuilder sb = new StringBuilder();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   936
        for (String s : lines) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   937
            sb.append(s).append(lineSeparator);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   938
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   939
        return sb.toString();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   940
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   941
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   942
    /**
16549
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   943
     * 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
   944
     */
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   945
    public static boolean isWindows() {
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   946
        String osName = System.getProperty("os.name");
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   947
        return osName.toUpperCase().startsWith("WINDOWS");
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   948
    }
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   949
1bdeedb5446c 8008582: jtreg failures after conversion of shell tests to Java
vromero
parents: 16304
diff changeset
   950
    /**
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   951
     * 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
   952
     * 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
   953
     */
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   954
    public static class JavaSource extends SimpleJavaFileObject {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   955
        String source;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   956
        String name;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   957
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   958
        public JavaSource(String className, String source) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   959
            super(URI.create(className),
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   960
                    JavaFileObject.Kind.SOURCE);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   961
            this.name = className;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   962
            this.source = source;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   963
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   964
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   965
        public JavaSource(String source) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   966
            super(URI.create(getJavaFileNameFromSource(source)),
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   967
                    JavaFileObject.Kind.SOURCE);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   968
            this.name = getJavaFileNameFromSource(source);
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   969
            this.source = source;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   970
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   971
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   972
        @Override
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   973
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   974
            return source;
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   975
        }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   976
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
diff changeset
   977
}