Path: blob/master/test/langtools/tools/javap/InvalidOptions.java
41144 views
/*1* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8027411 803286926* @summary test an invalid option27* @modules jdk.jdeps/com.sun.tools.javap28*/2930import java.io.*;31import java.util.zip.*;3233public class InvalidOptions {34int errorCount;35String log;3637public static void main(String[] args) throws Exception {38new InvalidOptions().run();39}4041void run() throws Exception {42test(2, "-b", "Error: unknown option: -b",43"Usage: javap <options> <classes>",44"use --help for a list of possible options");45if (errorCount > 0)46throw new Exception(errorCount + " errors received");47}4849void test(int expect, String option, String ... expectedOutput) {50String output = runJavap(expect, option);51verify(output, expectedOutput);52}5354String runJavap(int expect, String... option) {55StringWriter sw = new StringWriter();56PrintWriter pw = new PrintWriter(sw);57int rc = com.sun.tools.javap.Main.run(option, pw);58pw.close();59System.out.println("javap prints:");60System.out.println(sw);61if (rc != expect)62throw new Error("Expect to return " + expect + ", but return " + rc);63return sw.toString();64}6566void verify(String output, String... expects) {67for (String expect: expects) {68if (!output.contains(expect))69error(expect + " not found");70}71}7273void error(String msg) {74System.err.println(msg);75errorCount++;76}77}787980