test/tools/javap/InvalidOptions.java

Mon, 04 Nov 2013 18:04:34 -0800

author
cl
date
Mon, 04 Nov 2013 18:04:34 -0800
changeset 2181
658861d1b36e
child 2289
3fbda1dca565
permissions
-rw-r--r--

8027411: javap tonga tests cleanup: write a java program to test invalid options -h and -b
Reviewed-by: jjg

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
cl@2181 27 * @summary test invalid options -h and -b
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, "-h", "Error: -h is no longer available - use the javah program");
cl@2181 43 test(2, "-b", "Error: unknown option: -b",
cl@2181 44 "Usage: javap <options> <classes>",
cl@2181 45 "use -help for a list of possible options");
cl@2181 46 if (errorCount > 0)
cl@2181 47 throw new Exception(errorCount + " errors received");
cl@2181 48 }
cl@2181 49
cl@2181 50 void test(int expect, String option, String ... expectedOutput) {
cl@2181 51 String output = runJavap(expect, option);
cl@2181 52 verify(output, expectedOutput);
cl@2181 53 }
cl@2181 54
cl@2181 55 String runJavap(int expect, String... option) {
cl@2181 56 StringWriter sw = new StringWriter();
cl@2181 57 PrintWriter pw = new PrintWriter(sw);
cl@2181 58 int rc = com.sun.tools.javap.Main.run(option, pw);
cl@2181 59 pw.close();
cl@2181 60 System.out.println("javap prints:");
cl@2181 61 System.out.println(sw);
cl@2181 62 if (rc != expect)
cl@2181 63 throw new Error("Expect to return " + expect + ", but return " + rc);
cl@2181 64 return sw.toString();
cl@2181 65 }
cl@2181 66
cl@2181 67 void verify(String output, String... expects) {
cl@2181 68 for (String expect: expects) {
cl@2181 69 if (!output.contains(expect))
cl@2181 70 error(expect + " not found");
cl@2181 71 }
cl@2181 72 }
cl@2181 73
cl@2181 74 void error(String msg) {
cl@2181 75 System.err.println(msg);
cl@2181 76 errorCount++;
cl@2181 77 }
cl@2181 78 }

mercurial