1 /*
   2  * Copyright (c) 2015, 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  * @summary unit test for SAGetopt function
  27  * @modules jdk.hotspot.agent/sun.jvm.hotspot
  28  * @compile -XDignore.symbol.file SAGetoptTest.java
  29  * @run main SAGetoptTest
  30  */
  31 
  32 import sun.jvm.hotspot.SAGetopt;
  33 
  34 public class SAGetoptTest {
  35 
  36     private static boolean a_opt;
  37     private static boolean b_opt;
  38     private static boolean c_opt;
  39     private static boolean e_opt;
  40     private static boolean mixed_opt;
  41 
  42     private static String  d_value;
  43     private static String  exe_value;
  44     private static String  core_value;
  45 
  46     private static void initArgValues() {
  47         a_opt = false;
  48         b_opt = false;
  49         c_opt = false;
  50         e_opt = false;
  51         mixed_opt = false;
  52 
  53         d_value = "";
  54         exe_value = "";
  55         core_value = "";
  56     }
  57 
  58 
  59     private static void optionsTest(String[] args) {
  60         initArgValues();
  61 
  62         SAGetopt sg = new SAGetopt(args);
  63 
  64         String[] longOpts = {"exe=","core=","mixed"};
  65         String shortOpts = "abcd:e";
  66         String s;
  67 
  68         while((s = sg.next(shortOpts, longOpts)) != null) {
  69             if (s.equals("a")) {
  70                 a_opt = true;
  71                 continue;
  72             }
  73 
  74             if (s.equals("b")) {
  75                 b_opt = true;
  76                 continue;
  77             }
  78 
  79             if (s.equals("c")) {
  80                 c_opt = true;
  81                 continue;
  82             }
  83 
  84             if (s.equals("e")) {
  85                 e_opt = true;
  86                 continue;
  87             }
  88 
  89             if (s.equals("mixed")) {
  90                 mixed_opt = true;
  91                 continue;
  92             }
  93 
  94             if (s.equals("d")) {
  95                 d_value = sg.getOptarg();
  96                 continue;
  97             }
  98 
  99             if (s.equals("exe")) {
 100                 exe_value = sg.getOptarg();
 101                 continue;
 102             }
 103 
 104             if (s.equals("core")) {
 105                 core_value = sg.getOptarg();
 106                 continue;
 107             }
 108         }
 109     }
 110 
 111     private static void badOptionsTest(int setNumber, String[] args, String expectedMessage) {
 112         String msg = null;
 113         try {
 114             optionsTest(args);
 115         } catch(RuntimeException ex) {
 116             msg = ex.getMessage();
 117         }
 118 
 119         if (msg == null || !msg.equals(expectedMessage)) {
 120             if (msg != null) {
 121                 System.err.println("Unexpected error '" + msg + "'");
 122             }
 123             throw new RuntimeException("Bad option test " + setNumber + " failed");
 124         }
 125     }
 126 
 127     public static void main(String[] args) {
 128         String[] optionSet1 = {"-abd", "bla", "-c"};
 129         optionsTest(optionSet1);
 130         if (!a_opt || !b_opt || !d_value.equals("bla") || !c_opt) {
 131             throw new RuntimeException("Good optionSet 1 failed");
 132         }
 133 
 134         String[] optionSet2 = {"-e", "--mixed"};
 135         optionsTest(optionSet2);
 136         if (!e_opt || !mixed_opt) {
 137             throw new RuntimeException("Good optionSet 2 failed");
 138         }
 139 
 140         String[] optionSet3 = {"--exe=bla", "--core", "bla_core", "--mixed"};
 141         optionsTest(optionSet3);
 142         if (!exe_value.equals("bla") || !core_value.equals("bla_core") || !mixed_opt) {
 143             throw new RuntimeException("Good optionSet 3 failed");
 144         }
 145 
 146         // Bad options test
 147         String[] optionSet4 = {"-abd", "-c"};
 148         badOptionsTest(4, optionSet4, "Argument is expected for 'd'");
 149 
 150         String[] optionSet5 = {"-exe", "bla", "--core"};
 151         badOptionsTest(5, optionSet5, "Invalid option 'x'");
 152 
 153         String[] optionSet6 = {"--exe", "--core", "bla_core"};
 154         badOptionsTest(6, optionSet6, "Argument is expected for 'exe'");
 155 
 156         String[] optionSet7 = {"--exe"};
 157         badOptionsTest(7, optionSet7, "Argument is expected for 'exe'");
 158     }
 159   }