Path: blob/master/test/functional/RasapiTest/src/com/ibm/dump/tests/StringUtils.java
6007 views
/*******************************************************************************1* Copyright (c) 2016, 2021 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 com.ibm.dump.tests;2223import java.util.ArrayList;24import java.util.NoSuchElementException;25import java.util.StringTokenizer;262728public class StringUtils {2930/**31* Check that the line contains a given value at a given position.32* Whether it does or not, write a line to System.err to trace progress.33*34* @param line the line to check35* @param whichWord which word in the line to check36* @param requiredValue the number to look for37* @return true or false depending on whether the line does indeed contain the given number at the given word38*/39static boolean linePassesCheckForStringAtWord(String line, int whichWord, String requiredValue) {40StringTokenizer st = new StringTokenizer(line);41String word = "";42try {43for (int i = 0;i < whichWord; i++) {44word = st.nextToken();45}46} catch (NoSuchElementException e) {47System.err.printf("***Failed to find the value %s at word %d in line \"%s\", no such element\n", requiredValue, whichWord, line, word) ;48return false;49}50if (word.equals(requiredValue)) {51System.err.printf("Successfully found the value %s at word %d in line \"%s\"\n", requiredValue, whichWord, line) ;52return true;53} else {54System.err.printf("***Failed to find the value %s at word %d in line \"%s\", found %s instead\n", requiredValue, whichWord, line, word) ;55return false;56}5758}5960/**61* Check that the line contains one of a number of given values at a given position.62* Whether it does or not, write a line to System.err to trace progress.63*64* @param line the line to check65* @param whichWord which word in the line to check66* @param requiredValue the number to look for67* @return true or false depending on whether the line does indeed contain the given number at the given word68*/69static boolean linePassesCheckForStringsAtWord(String line, int whichWord, String[] requiredValues) {70StringTokenizer st = new StringTokenizer(line);71String word = "";72try {73for (int i = 0;i < whichWord; i++) {74word = st.nextToken();75}76} catch (NoSuchElementException e) {77System.err.printf("***Failed to find any value at word %d in line \"%s\", no such element\n", whichWord, line, word) ;78return false;79}80for (String requiredValue : requiredValues) {81if (word.equals(requiredValue)) {82System.err.printf("Successfully found the value %s at word %d in line \"%s\"\n", requiredValue, whichWord, line) ;83return true;84}85}86System.err.printf("***Failed to find any of the required values at word %d in line \"%s\", found %s instead. ", whichWord, line, word) ;87System.err.printf("The required values were:") ;88for (String requiredValue : requiredValues) {89System.err.printf(requiredValue + " ");90}91System.err.printf("\n");92return false;93}949596/**97* Given a line break it into an array of tokens (words)98* @param line99* @return arraylist of tokens (words)100*/101public static ArrayList<String> extractTokensFromLine(String line) {102ArrayList<String> tokens = new ArrayList<String>();103StringTokenizer st = new StringTokenizer(line);104while (st.hasMoreTokens()) {105String word = st.nextToken();106tokens.add(word);107}108return tokens;109}110111112}113114115