InvalidOptions.java revision 3294:9adfb22ff08f
1132720Skan/*
2132720Skan * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
3169691Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4132720Skan *
5132720Skan * This code is free software; you can redistribute it and/or modify it
6132720Skan * under the terms of the GNU General Public License version 2 only, as
7132720Skan * published by the Free Software Foundation.
8132720Skan *
9132720Skan * This code is distributed in the hope that it will be useful, but WITHOUT
10132720Skan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11132720Skan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12132720Skan * version 2 for more details (a copy is included in the LICENSE file that
13132720Skan * accompanied this code).
14132720Skan *
15132720Skan * You should have received a copy of the GNU General Public License version
16132720Skan * 2 along with this work; if not, write to the Free Software Foundation,
17132720Skan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18132720Skan *
19169691Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20132720Skan * or visit www.oracle.com if you need additional information or have any
21132720Skan * questions.
22132720Skan */
23132720Skan
24132720Skan/*
25132720Skan * @test
26132720Skan * @bug 8027411 8032869
27132720Skan * @summary test an invalid option
28132720Skan * @modules jdk.jdeps/com.sun.tools.javap
29132720Skan */
30132720Skan
31169691Skanimport java.io.*;
32169691Skanimport java.util.zip.*;
33169691Skan
34169691Skanpublic class InvalidOptions {
35132720Skan    int errorCount;
36132720Skan    String log;
37132720Skan
38132720Skan    public static void main(String[] args) throws Exception {
39132720Skan        new InvalidOptions().run();
40132720Skan    }
41132720Skan
42169691Skan    void run() throws Exception {
43132720Skan        test(2, "-b", "Error: unknown option: -b",
44169691Skan                      "Usage: javap <options> <classes>",
45169691Skan                      "use -help for a list of possible options");
46132720Skan        if (errorCount > 0)
47132720Skan            throw new Exception(errorCount + " errors received");
48132720Skan    }
49132720Skan
50132720Skan    void test(int expect, String option, String ... expectedOutput) {
51132720Skan        String output = runJavap(expect, option);
52132720Skan        verify(output, expectedOutput);
53132720Skan    }
54132720Skan
55132720Skan    String runJavap(int expect, String... option) {
56132720Skan        StringWriter sw = new StringWriter();
57132720Skan        PrintWriter pw = new PrintWriter(sw);
58132720Skan        int rc = com.sun.tools.javap.Main.run(option, pw);
59132720Skan        pw.close();
60132720Skan        System.out.println("javap prints:");
61132720Skan        System.out.println(sw);
62169691Skan        if (rc != expect)
63169691Skan           throw new Error("Expect to return " + expect + ", but return " + rc);
64132720Skan        return sw.toString();
65132720Skan    }
66132720Skan
67132720Skan    void verify(String output, String... expects) {
68132720Skan        for (String expect: expects) {
69132720Skan            if (!output.contains(expect))
70132720Skan                error(expect + " not found");
71132720Skan        }
72169691Skan    }
73169691Skan
74132720Skan    void error(String msg) {
75132720Skan        System.err.println(msg);
76132720Skan        errorCount++;
77132720Skan    }
78132720Skan}
79132720Skan