Path: blob/master/test/hotspot/jtreg/compiler/lib/ir_framework/Scenario.java
64507 views
/*1* Copyright (c) 2021, 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*/2223package compiler.lib.ir_framework;2425import compiler.lib.ir_framework.shared.TestRunException;2627import java.util.*;28import java.util.stream.Collectors;2930/**31* This class represents a scenario that can be executed by the {@link TestFramework}.32* <p>33* A JTreg test should use the test framework with {@code @run driver} (without directly specify any additional flags).34* If a test should run with additional flags, use {@link TestFramework#runWithFlags(String...)} or35* {@link TestFramework#addFlags(String...)}. If, however, the test should be run with different settings (equivalent36* to having multiple {@code @run} entries in a normal JTreg test), use scenarios. A scenario will be run with the37* scenario specific VM flags, if any, and optionally specified VM flags with {@link TestFramework#addFlags(String...)}38* whereas scenario VM flags will have precedence.39* <p>40* There is also the possibility to specify additional VM flags for all scenarios by using {@code DScenarioFlags}.41*42* @see TestFramework43*/44public class Scenario {45private static final String ADDITIONAL_SCENARIO_FLAGS_PROPERTY = System.getProperty("ScenarioFlags", "");46private static final String SCENARIOS_PROPERTY = System.getProperty("Scenarios", "");47private static final List<String> ADDITIONAL_SCENARIO_FLAGS;48private static final Set<Integer> ENABLED_SCENARIOS;4950private final List<String> flags;51private final int index;52private final boolean enabled;53private String testVMOutput;5455static {56if (!SCENARIOS_PROPERTY.isEmpty()) {57var split = SCENARIOS_PROPERTY.split("\\s*,\\s*");58try {59ENABLED_SCENARIOS = Arrays.stream(split).map(Integer::parseInt).collect(Collectors.toSet());60} catch (NumberFormatException e) {61throw new TestRunException("Provided a scenario index in the -DScenario comma-separated list which is not "62+ "a number: " + SCENARIOS_PROPERTY);63}64} else {65ENABLED_SCENARIOS = Collections.emptySet();66}6768ADDITIONAL_SCENARIO_FLAGS = ADDITIONAL_SCENARIO_FLAGS_PROPERTY.isEmpty() ? Collections.emptyList() :69Arrays.asList(ADDITIONAL_SCENARIO_FLAGS_PROPERTY.split("\\s*,\\s*"));70}7172/**73* Create a scenario with {@code index} that will be run with the additional VM flags specified in {@code flags}74* (or without any if null or parameter not provided).75* <p>76* The scenario {@code index} must be unique to be distinguishable in the stdout and stderr output and when specifying77* {@code -DScenarios} (see {@link Scenario}).78*79* @param index the unique scenario index.80* @param flags the scenario flags or null (i.e. no parameter specified) if no flags should be used.81*/82public Scenario(int index, String... flags) {83this.index = index;84if (flags != null) {85this.flags = new ArrayList<>(Arrays.asList(flags));86this.flags.addAll(ADDITIONAL_SCENARIO_FLAGS);87} else {88this.flags = new ArrayList<>();89}90this.enabled = ENABLED_SCENARIOS.isEmpty() || ENABLED_SCENARIOS.contains(index);91}9293/**94* Add additional VM flags to this scenario.95*96* @param flags the additional scenario VM flags.97*/98public void addFlags(String... flags) {99if (flags != null) {100this.flags.addAll(Arrays.asList(flags));101}102}103104/**105* Get all scenario specific VM flags as defined in {@link #Scenario(int, String...)}.106*107* @return the scenario VM flags.108*/109public List<String> getFlags() {110return flags;111}112113/**114* Get the unique scenario index as defined in {@link #Scenario(int, String...)}.115*116* @return the scenario index.117*/118public int getIndex() {119return index;120}121122/**123* Get the test VM output (stdout + stderr) of this scenario from the last execution of the framework.124*125* @return the test VM output.126*/127public String getTestVMOutput() {128return testVMOutput;129}130131/**132* Set the test VM output, called by the framework.133*/134void setTestVMOutput(String testVMOutput) {135this.testVMOutput = testVMOutput;136}137138/**139* Returns a boolean indicating if this scenario will be executed by the test framework. This only depends on140* the property flag {@code -DScenarios} (see {@link Scenario}). This is only used by the framework internally.141*142* @return {@code true} if {@code -DScenarios} is either not set or if {@code -DScenarios} specifies the scenario143* index set by {@link #Scenario(int, String...)}.144* {@code false} otherwise.145*/146boolean isEnabled() {147return enabled;148}149}150151152