Path: blob/master/test/functional/VM_Test/src/j9vm/runner/Menu.java
6004 views
/*******************************************************************************1* Copyright (c) 2001, 2020 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package j9vm.runner;22import java.util.*;23import java.io.*;24import com.oti.j9.exclude.*;2526public class Menu {27Vector menuItems;28Vector allClassNames;29String jarFileName;30String exeName;31String bootClassPath;32String userClassPath;33String javaVersion;3435public Menu(String jarFileName, String exeName, String bootClassPath, String userClassPath, String javaVersion) {36this.menuItems = new Vector();37this.allClassNames = new Vector();38this.jarFileName = jarFileName;39this.exeName = exeName;40this.bootClassPath = bootClassPath;41this.userClassPath = userClassPath;42this.javaVersion = javaVersion;43}44public void addElement(MenuItem item) {45menuItems.addElement(item);46}4748public void printMenu() {49System.out.println("---- VMTest Menu ----");50for (int i = 0; i<menuItems.size(); i++) {51MenuItem item = (MenuItem)menuItems.get(i);52String front = Integer.toString(i+1) + ") ";53while (front.length() < 4) front = front + " ";54System.out.print(front);55System.out.println(item.getDisplayString());56}57System.out.println("A) Run ALL tests");58System.out.println("Q) Quit");59}6061public void doCommand(String command) {62if (command.equalsIgnoreCase("A")) {63runTestSet(allClassNames);64return;65}66try {67int n = Integer.parseInt(command);68if (command.equals(String.valueOf(n))) {69n--;70if ((n >= 0) && (n < menuItems.size())) {71runTestSet(((MenuItem)menuItems.get(n)).getClassNames());72}73}74} catch (NumberFormatException e) {75/* Nothing */76}77}7879public void doMenu() {80byte[] buf = new byte[256];81boolean fPrintMenu = true;82for (;;) {83if (fPrintMenu) {84printMenu(); fPrintMenu = false;85}86System.out.print("Enter command(s): ");87System.out.flush();88try {89int result = System.in.read(buf, 0, buf.length);90if (result == 2 && buf[0] == 13) {91/* User just pressed return */92fPrintMenu = true;93continue;94}95} catch (IOException e) {96return;97}98StringTokenizer tokenizer = new StringTokenizer(new String(buf));99while (tokenizer.hasMoreTokens()) {100String command = tokenizer.nextToken();101if (command.equalsIgnoreCase("Q")) {102System.exit(0);103}104doCommand(command);105}106}107}108109public static void usage() {110System.out.println("Usage: j9 [vmOptions] j9vm.runner.Menu [testOptions]");111System.out.println("testOptions:");112System.out.println(" -jar=<jarFileName> Read tests out of jar file <jarFileName>");113System.out.println(" (make sure the tests are in the classpath too, of course!)");114System.out.println(" -exe=<exeFileName> Override default VM executable name");115System.out.println(" -version=<javaVersion> Override default java option");116System.out.println(" -bp=<bootClassPath> Override default VM bp with -bp:<bootClassPath>");117System.out.println(" -cp=<userClassPath> Override default VM bp with -cp:<userClassPath>");118System.out.println(" -xlist=<excludeList> Specify an XML exclude list");119System.out.println(" -xids=<excludeIDS> Specify tags to choose excluded tests");120System.out.println(" -test=<test package> Run all tests in the given package");121System.out.println(" <n> Run menu item <n>");122System.out.println(" A or a Run all tests");123System.out.println("If no tests are specified (i.e. no <n> or A options) an interactive");124System.out.println("menu will be displayed.");125System.exit(1);126}127128public static void main(String[] args) {129Vector commands = new Vector();130String exeName = null;131String javaVersion = null;132String jarName = null;133String bootClassPath = null;134String userClassPath = null;135String excludeFN = null;136String excludeIDs = null;137String packageToTest = null;138139/* Parse all arguments beginning with '-' */140if (args != null) {141for (int i = 0; i<args.length; i++) {142if (!args[i].startsWith("-")) {143commands.addElement(args[i]);144continue;145}146/* TODO: find a cleaner way to do this */147if (args[i].startsWith("-exe=")) {148if (exeName != null) usage();149exeName = args[i].substring(5);150} else if (args[i].startsWith("-version=")) {151if (javaVersion != null) usage();152javaVersion = args[i].substring(9);153} else if (args[i].startsWith("-jar=")) {154if (jarName != null) usage();155jarName = args[i].substring(5);156} else if (args[i].startsWith("-bp=")) {157if (bootClassPath != null) usage();158bootClassPath = args[i].substring(4);159} else if (args[i].startsWith("-cp=")) {160if (userClassPath != null) usage();161userClassPath = args[i].substring(4);162} else if (args[i].startsWith("-xlist=")) {163if (excludeFN != null) usage();164excludeFN = args[i].substring(7);165} else if (args[i].startsWith("-xids=")) {166if (excludeIDs != null) usage();167excludeIDs = args[i].substring(6);168} else if (args[i].startsWith("-test=")) {169packageToTest = args[i].substring(6);170} else {171System.out.println("Unrecognized option: \"" + args[i] + "\"");172usage();173}174}175}176177ExcludeList excludeList = null;178if ( excludeFN!=null && excludeIDs!=null) {179excludeList= ExcludeList.readFrom(excludeFN, excludeIDs);180if (excludeList == null)181System.exit(-1);182excludeList.explain(System.out);183}184185if (exeName == null) {186exeName = System.getProperty("user.dir");187if (!exeName.endsWith(System.getProperty("file.separator"))) {188exeName = exeName + System.getProperty("file.separator");189}190exeName = exeName + "j9"; /* TODO: can we reach for the .exe name? */191}192193if (javaVersion == null) {194/* set default java version to 9 */195javaVersion = "9";196}197198if (jarName == null) {199/* Need to revisit this */200jarName = System.getProperty("java.class.path");201}202if ((bootClassPath == null) && (Integer.parseInt(javaVersion) < 9)) {203bootClassPath = System.getProperty("sun.boot.class.path");204}205if (userClassPath == null) {206userClassPath = System.getProperty("java.class.path");207}208209if ((args == null) || (args.length < 1)) {210}211HashMap testPackageMap = new HashMap();212Enumeration enumeration = null;213try {214enumeration = new AllTestsInJar(jarName);215} catch (IOException e) {216System.out.println("test suite: error reading tests from Jar file \"" + jarName + "\"!");217System.exit(1);218}219System.out.println("test suite: read tests from \"" + jarName + "\"");220while (enumeration.hasMoreElements()) {221String testClassName = (String)enumeration.nextElement();222/* Put tests in bins according to their package. */223String testPackage = testClassName.substring(0, testClassName.lastIndexOf('.'));224Vector v = (Vector)testPackageMap.get(testPackage);225if (v == null) {226/* First test class from this package. */227v = new Vector();228testPackageMap.put(testPackage, v);229}230v.addElement(testClassName);231}232233Iterator iter = testPackageMap.keySet().iterator();234Menu menu = new Menu(jarName, exeName, bootClassPath, userClassPath, javaVersion);235while (iter.hasNext()) {236String testPackage = (String)iter.next();237Vector v = (Vector)testPackageMap.get(testPackage);238if (v.size() == 1) {239/* Package contains only one test. */240String testName = (String)v.get(0);241242if (excludeList==null || excludeList.shouldRun(testName)) {243menu.addElement(new MenuItem(testName, v));244menu.allClassNames.addElement(testName);245}246} else {247/* Package contains multiple tests. */248String testName = testPackage + ".AllTests";249if (excludeList==null || excludeList.shouldRun(testName)) {250menu.addElement(new MenuItem(testName, v));251252for (int i = 0; i<v.size(); i++) {253testName = (String)v.get(i);254if (excludeList==null || excludeList.shouldRun(testName)) {255testName = " " + testName;256menu.addElement(new MenuItem(testName, (String)v.get(i)));257menu.allClassNames.addElement((String)v.get(i));258}259}260}261}262}263264/* Get the index of 'packageToTest' in menu.menuItems vector265* and add it to commands vector.266*/267if (packageToTest != null) {268int index = 0;269for (index = 0; index < menu.menuItems.size(); index++)270{271MenuItem item = (MenuItem)menu.menuItems.get(index);272String packageName = item.getDisplayString();273packageName = packageName.substring(0, packageName.lastIndexOf('.'));274if (packageName.equals(packageToTest)) {275commands.addElement(String.valueOf(index+1));276break;277}278}279}280281if (commands.size() > 0) {282/* Non-interactive mode. */283for (int i = 0; i<commands.size(); i++) {284menu.doCommand((String)commands.get(i));285}286} else {287/* Interactive mode */288menu.doMenu();289}290}291292public Runner newRunnerForTest(String className, String jarFileName) {293try {294Class runnerClass = Class.forName(className + "Runner");295Class[] paramTypes = new Class[5];296Object[] paramValues = new Object[5];297paramTypes[0] = className.getClass(); paramValues[0] = className;298paramTypes[1] = exeName.getClass(); paramValues[1] = exeName;299if (bootClassPath == null) {300paramTypes[2] = String.class;301} else {302paramTypes[2] = bootClassPath.getClass();303}304paramValues[2] = bootClassPath;305paramTypes[3] = userClassPath.getClass(); paramValues[3] = userClassPath;306paramTypes[4] = javaVersion.getClass(); paramValues[4] = javaVersion;307return (Runner)runnerClass.getConstructor(paramTypes).newInstance(paramValues);308} catch (Exception e) {309return new Runner(className, exeName, bootClassPath, userClassPath, javaVersion);310}311}312313public void runTestSet(Vector classNames) {314int numPassed = 0;315int numFailed = 0;316boolean passed;317for (int i = 0; i<classNames.size(); i++) {318String className = (String)classNames.get(i);319System.out.println("+++ " + className + ": +++");320Runner runner = newRunnerForTest(className, jarFileName);321passed = false;322try {323if (runner.run()) {324passed = true;325}326} catch (Throwable e) {327System.out.println("runTestSet() caught exception:");328e.printStackTrace();329}330if (passed) {331numPassed++;332System.out.println("--- Test PASSED ---");333} else {334numFailed++;335System.out.println("*** Test FAILED *** (" + className + ")");336}337System.out.println("");338}339System.out.println(numPassed + " passed / " + numFailed + " failed");340if (numFailed == 0) {341System.out.println("ALL J9VM TESTS PASSED");342}else{343System.exit(-2);344}345System.out.println();346}347348349}350351352