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