Wed, 24 Jun 2015 15:43:06 -0700
Merge
cl@2181 | 1 | /* |
cl@2181 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
cl@2181 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
cl@2181 | 4 | * |
cl@2181 | 5 | * This code is free software; you can redistribute it and/or modify it |
cl@2181 | 6 | * under the terms of the GNU General Public License version 2 only, as |
cl@2181 | 7 | * published by the Free Software Foundation. |
cl@2181 | 8 | * |
cl@2181 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
cl@2181 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
cl@2181 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
cl@2181 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
cl@2181 | 13 | * accompanied this code). |
cl@2181 | 14 | * |
cl@2181 | 15 | * You should have received a copy of the GNU General Public License version |
cl@2181 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
cl@2181 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
cl@2181 | 18 | * |
cl@2181 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
cl@2181 | 20 | * or visit www.oracle.com if you need additional information or have any |
cl@2181 | 21 | * questions. |
cl@2181 | 22 | */ |
cl@2181 | 23 | |
cl@2181 | 24 | /* |
cl@2181 | 25 | * @test |
cl@2181 | 26 | * @bug 8027411 |
kizune@2289 | 27 | * @summary test an invalid option |
cl@2181 | 28 | */ |
cl@2181 | 29 | |
cl@2181 | 30 | import java.io.*; |
cl@2181 | 31 | import java.util.zip.*; |
cl@2181 | 32 | |
cl@2181 | 33 | public class InvalidOptions { |
cl@2181 | 34 | int errorCount; |
cl@2181 | 35 | String log; |
cl@2181 | 36 | |
cl@2181 | 37 | public static void main(String[] args) throws Exception { |
cl@2181 | 38 | new InvalidOptions().run(); |
cl@2181 | 39 | } |
cl@2181 | 40 | |
cl@2181 | 41 | void run() throws Exception { |
cl@2181 | 42 | test(2, "-b", "Error: unknown option: -b", |
cl@2181 | 43 | "Usage: javap <options> <classes>", |
cl@2181 | 44 | "use -help for a list of possible options"); |
cl@2181 | 45 | if (errorCount > 0) |
cl@2181 | 46 | throw new Exception(errorCount + " errors received"); |
cl@2181 | 47 | } |
cl@2181 | 48 | |
cl@2181 | 49 | void test(int expect, String option, String ... expectedOutput) { |
cl@2181 | 50 | String output = runJavap(expect, option); |
cl@2181 | 51 | verify(output, expectedOutput); |
cl@2181 | 52 | } |
cl@2181 | 53 | |
cl@2181 | 54 | String runJavap(int expect, String... option) { |
cl@2181 | 55 | StringWriter sw = new StringWriter(); |
cl@2181 | 56 | PrintWriter pw = new PrintWriter(sw); |
cl@2181 | 57 | int rc = com.sun.tools.javap.Main.run(option, pw); |
cl@2181 | 58 | pw.close(); |
cl@2181 | 59 | System.out.println("javap prints:"); |
cl@2181 | 60 | System.out.println(sw); |
cl@2181 | 61 | if (rc != expect) |
cl@2181 | 62 | throw new Error("Expect to return " + expect + ", but return " + rc); |
cl@2181 | 63 | return sw.toString(); |
cl@2181 | 64 | } |
cl@2181 | 65 | |
cl@2181 | 66 | void verify(String output, String... expects) { |
cl@2181 | 67 | for (String expect: expects) { |
cl@2181 | 68 | if (!output.contains(expect)) |
cl@2181 | 69 | error(expect + " not found"); |
cl@2181 | 70 | } |
cl@2181 | 71 | } |
cl@2181 | 72 | |
cl@2181 | 73 | void error(String msg) { |
cl@2181 | 74 | System.err.println(msg); |
cl@2181 | 75 | errorCount++; |
cl@2181 | 76 | } |
cl@2181 | 77 | } |