langtools/test/tools/javap/InvalidOptions.java
author lana
Thu, 21 Jan 2016 09:46:01 -0800
changeset 35340 38f7386ed942
parent 30846 2b3f379840f0
child 36526 3b41f1c69604
permissions -rw-r--r--
Added tag jdk-9+102 for changeset 7e3feb2ba5b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21706
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 22698
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
21706
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
     4
 *
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
     8
 *
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    13
 * accompanied this code).
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    14
 *
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    18
 *
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    21
 * questions.
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    22
 */
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    23
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    24
/*
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    25
 * @test
22698
818555741cd5 8032869: remove support for legacy options in javap
jjg
parents: 21706
diff changeset
    26
 * @bug 8027411 8032869
818555741cd5 8032869: remove support for legacy options in javap
jjg
parents: 21706
diff changeset
    27
 * @summary test an invalid option
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    28
 * @modules jdk.jdeps
21706
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    29
 */
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    30
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    31
import java.io.*;
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    32
import java.util.zip.*;
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    33
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    34
public class InvalidOptions {
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    35
    int errorCount;
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    36
    String log;
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    37
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    38
    public static void main(String[] args) throws Exception {
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    39
        new InvalidOptions().run();
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    40
    }
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    41
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    42
    void run() throws Exception {
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    43
        test(2, "-b", "Error: unknown option: -b",
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    44
                      "Usage: javap <options> <classes>",
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    45
                      "use -help for a list of possible options");
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    46
        if (errorCount > 0)
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    47
            throw new Exception(errorCount + " errors received");
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    48
    }
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    49
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    50
    void test(int expect, String option, String ... expectedOutput) {
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    51
        String output = runJavap(expect, option);
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    52
        verify(output, expectedOutput);
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    53
    }
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    54
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    55
    String runJavap(int expect, String... option) {
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    56
        StringWriter sw = new StringWriter();
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    57
        PrintWriter pw = new PrintWriter(sw);
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    58
        int rc = com.sun.tools.javap.Main.run(option, pw);
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    59
        pw.close();
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    60
        System.out.println("javap prints:");
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    61
        System.out.println(sw);
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    62
        if (rc != expect)
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    63
           throw new Error("Expect to return " + expect + ", but return " + rc);
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    64
        return sw.toString();
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    65
    }
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    66
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    67
    void verify(String output, String... expects) {
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    68
        for (String expect: expects) {
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    69
            if (!output.contains(expect))
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    70
                error(expect + " not found");
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    71
        }
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    72
    }
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    73
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    74
    void error(String msg) {
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    75
        System.err.println(msg);
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    76
        errorCount++;
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    77
    }
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    78
}