langtools/test/tools/javap/InvalidOptions.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 22698 818555741cd5
child 30730 d3ce7619db2c
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
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
/*
22698
818555741cd5 8032869: remove support for legacy options in javap
jjg
parents: 21706
diff changeset
     2
 * Copyright (c) 2013, 2014, 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
21706
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    28
 */
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
import java.io.*;
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    31
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
    32
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    33
public class InvalidOptions {
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    34
    int errorCount;
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    35
    String log;
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    36
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    37
    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
    38
        new InvalidOptions().run();
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    39
    }
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
    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
    42
        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
    43
                      "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
    44
                      "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
    45
        if (errorCount > 0)
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    46
            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
    47
    }
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
    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
    50
        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
    51
        verify(output, expectedOutput);
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    52
    }
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
    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
    55
        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
    56
        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
    57
        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
    58
        pw.close();
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    59
        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
    60
        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
    61
        if (rc != expect)
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    62
           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
    63
        return sw.toString();
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    64
    }
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
    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
    67
        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
    68
            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
    69
                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
    70
        }
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
    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
    74
        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
    75
        errorCount++;
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    76
    }
5b69542c19b3 8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
cl
parents:
diff changeset
    77
}