Path: blob/master/test/jdk/sun/tools/jhsdb/SAGetoptTest.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test26* @summary unit test for SAGetopt function27* @modules jdk.hotspot.agent/sun.jvm.hotspot28* @compile -XDignore.symbol.file SAGetoptTest.java29* @run main SAGetoptTest30*/3132import sun.jvm.hotspot.SAGetopt;3334public class SAGetoptTest {3536private static boolean a_opt;37private static boolean b_opt;38private static boolean c_opt;39private static boolean e_opt;40private static boolean mixed_opt;4142private static String d_value;43private static String exe_value;44private static String core_value;4546private static void initArgValues() {47a_opt = false;48b_opt = false;49c_opt = false;50e_opt = false;51mixed_opt = false;5253d_value = "";54exe_value = "";55core_value = "";56}575859private static void optionsTest(String[] args) {60initArgValues();6162SAGetopt sg = new SAGetopt(args);6364String[] longOpts = {"exe=","core=","mixed"};65String shortOpts = "abcd:e";66String s;6768while((s = sg.next(shortOpts, longOpts)) != null) {69if (s.equals("a")) {70a_opt = true;71continue;72}7374if (s.equals("b")) {75b_opt = true;76continue;77}7879if (s.equals("c")) {80c_opt = true;81continue;82}8384if (s.equals("e")) {85e_opt = true;86continue;87}8889if (s.equals("mixed")) {90mixed_opt = true;91continue;92}9394if (s.equals("d")) {95d_value = sg.getOptarg();96continue;97}9899if (s.equals("exe")) {100exe_value = sg.getOptarg();101continue;102}103104if (s.equals("core")) {105core_value = sg.getOptarg();106continue;107}108}109}110111private static void badOptionsTest(int setNumber, String[] args, String expectedMessage) {112String msg = null;113try {114optionsTest(args);115} catch(RuntimeException ex) {116msg = ex.getMessage();117}118119if (msg == null || !msg.equals(expectedMessage)) {120if (msg != null) {121System.err.println("Unexpected error '" + msg + "'");122}123throw new RuntimeException("Bad option test " + setNumber + " failed");124}125}126127public static void main(String[] args) {128String[] optionSet1 = {"-abd", "bla", "-c"};129optionsTest(optionSet1);130if (!a_opt || !b_opt || !d_value.equals("bla") || !c_opt) {131throw new RuntimeException("Good optionSet 1 failed");132}133134String[] optionSet2 = {"-e", "--mixed"};135optionsTest(optionSet2);136if (!e_opt || !mixed_opt) {137throw new RuntimeException("Good optionSet 2 failed");138}139140String[] optionSet3 = {"--exe=bla", "--core", "bla_core", "--mixed"};141optionsTest(optionSet3);142if (!exe_value.equals("bla") || !core_value.equals("bla_core") || !mixed_opt) {143throw new RuntimeException("Good optionSet 3 failed");144}145146// Bad options test147String[] optionSet4 = {"-abd", "-c"};148badOptionsTest(4, optionSet4, "Argument is expected for 'd'");149150String[] optionSet5 = {"-exe", "bla", "--core"};151badOptionsTest(5, optionSet5, "Invalid option 'x'");152153String[] optionSet6 = {"--exe", "--core", "bla_core"};154badOptionsTest(6, optionSet6, "Argument is expected for 'exe'");155156String[] optionSet7 = {"--exe"};157badOptionsTest(7, optionSet7, "Argument is expected for 'exe'");158}159}160161162