Path: blob/master/test/functional/cmdline_options_tester/src/SaveOutput.java
6004 views
/*******************************************************************************1* Copyright (c) 2004, 2018 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*******************************************************************************/2122/**23* Saveoutput matches and stores desired output from test suite in a variable24*25* Example:26* <saveoutput regex="no" type="success" saveName="moduleAddr" splitIndex="1" splitBy="!j9module ">!j9module 0x</saveoutput>27* This matches output containing "!j9module 0x" and split the line by "!j9module " then take the second substring (Indexed "1").28*/29class SaveOutput extends Output {30private String _saveName;31private String _splitIndex;32private String _splitBy;3334/**35* Pass the arguments to local variables.36* @param saveName Name of the variable the output is put into37* @param splitIndex Index of the substring after split38* @param splitBy The string that the matching line is split by39*/40public SaveOutput( String matchRegex, String matchJavaUtilPattern, String showRegexMatch, String matchCase, String type, String saveName, String splitIndex, String splitBy ) {41super(matchRegex, matchJavaUtilPattern, showRegexMatch, matchCase, type);42_saveName = saveName;43_splitIndex = splitIndex;44_splitBy = splitBy;45}4647@Override48boolean match( Object o ) {49boolean result = super.match(o);50if (result && (getType() == TestCondition.REQUIRED || getType() == TestCondition.SUCCESS)) {51String line = (String) o;52if (_splitBy == null || _splitBy.equals("")) {53System.err.println("String split failure: splitBy cannot be empty");54result = false;55} else if (!line.contains(_splitBy)) {56System.err.printf(57"String split failure: String does not contain splitBy %n" + "String: %s%n" + "splitBy: %s%n",58line, _splitBy);59result = false;60} else {61String[] splitArray = line.split(_splitBy);62int arraySize = splitArray.length;63try {64int splitIndexInt = Integer.parseInt(_splitIndex);65if (splitIndexInt >= arraySize || splitIndexInt < 0) {66System.err.printf("String split failure: splitIndex out of bound %n"67+ "Size of splitArray: %s%n" + "splitIndex: %d%n", arraySize, splitIndexInt);68result = false;69} else {70TestSuite.putVariable(_saveName, splitArray[splitIndexInt]);71}72} catch (NumberFormatException e) {73System.err.println("String split failure: splitIndex is not a number");74result = false;75}76}77}78return result;79}8081}82838485