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