1 /*
   2  * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 6356642
  27  * @summary Verify that extcheck exits appropriately when invalid args are given.
  28  * @run shell TestExtcheckArgs.sh
  29  * @author Dave Bristor
  30  */
  31 
  32 import java.io.File;
  33 import com.sun.tools.extcheck.Main;
  34 
  35 /*
  36  * Test extcheck by using Runtime.exec instead of invoking
  37  * com.sun.tools.extcheck.Main.main, since the latter does its own
  38  * System.exit under the conditions checked here.
  39  */
  40 public class TestExtcheckArgs {
  41     public static void realMain(String[] args) throws Throwable {
  42         String testJar = System.getenv("TESTJAVA") + File.separator
  43             + "lib" + File.separator + "jconsole.jar";
  44 
  45         verify(new String[] {
  46                }, Main.INSUFFICIENT);
  47         verify(new String[] {
  48                    "-verbose"
  49                }, Main.MISSING);
  50         verify(new String[] {
  51                    "-verbose",
  52                    "foo"
  53                }, Main.DOES_NOT_EXIST);
  54         verify(new String[] {
  55                    testJar,
  56                    "bar"
  57                }, Main.EXTRA);
  58         verify(new String[] {
  59                    "-verbose",
  60                    testJar,
  61                    "bar"
  62                }, Main.EXTRA);
  63     }
  64 
  65     static void verify(String[] args, String expected) throws Throwable {
  66         try {
  67             Main.realMain(args);
  68             fail();
  69         } catch (Exception ex) {
  70             if (ex.getMessage().startsWith(expected)) {
  71                 pass();
  72             } else {
  73                 fail("Unexpected message: " + ex.getMessage());
  74             }
  75         }
  76     }
  77 
  78     //--------------------- Infrastructure ---------------------------
  79     static volatile int passed = 0, failed = 0;
  80     static boolean pass() {passed++; return true;}
  81     static boolean fail() {failed++; Thread.dumpStack(); return false;}
  82     static boolean fail(String msg) {System.out.println(msg); return fail();}
  83     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
  84     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
  85     static boolean equal(Object x, Object y) {
  86         if (x == null ? y == null : x.equals(y)) return pass();
  87         else return fail(x + " not equal to " + y);}
  88     public static void main(String[] args) throws Throwable {
  89         try {realMain(args);} catch (Throwable t) {unexpected(t);}
  90         System.out.println("\nPassed = " + passed + " failed = " + failed);
  91         if (failed > 0) throw new AssertionError("Some tests failed");}
  92 }