Path: blob/master/test/functional/cmdLineTests/shareClassTests/SCCommandLineOptionTests/SimpleGrep.java
6004 views
/*******************************************************************************1* Copyright (c) 2004, 2004 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*******************************************************************************/21import java.io.BufferedReader;22import java.io.FileInputStream;23import java.io.FileNotFoundException;24import java.io.FileReader;25import java.io.IOException;26import java.io.InputStream;27import java.io.InputStreamReader;28//import java.util.regex.Pattern;2930/**31* Check for presence of text in specified file or stdin32*/33public class SimpleGrep34{35static String sSCCSid="%Z%%M% %I% %W% %G% %U%";3637public static void main(String[] args) throws IOException38{39boolean result = false;40switch (args.length)41{42case 2 :43result = searchForStringInFile(args[0], args[1]);44break;45case 3 :46result = searchForStringInFile(args[0], args[1], args[2]);47break;48case 0 :49default :50System.err.println("usage: SimpleGrep <string> <filename> [<count>]");51System.err.println(" search for <string> in <filename>");52System.err.println(" if <min> is specified, search for at <count> matching lines");53break;54}55printResult(result);56}57public static boolean searchForStringInFile(String searchText, String filename, String count) throws IOException, FileNotFoundException58{59boolean result;60//System.out.println("Searching for " + count + " occurences of \"" + searchText + "\" in file \"" + filename + "\"");61int actualCount = countLinesContainingString(searchText, new FileInputStream(filename));62int expectedCount = Integer.parseInt(count);63//System.out.println("Found " + actualCount + " matching line" + (actualCount == 1 ? "." : "s."));64result = expectedCount == actualCount;65if (!result)66displayFile(filename);67return result;68}69public static boolean searchForStringInFile(String searchText, String filename) throws IOException, FileNotFoundException70{71boolean result;72//System.out.println("Searching for \"" + searchText + "\" in file \"" + filename + "\"");73result = searchForString(searchText, new FileInputStream(filename));74//System.out.println("Found" + (result ? " " : " no ") + "match.");75if (!result)76displayFile(filename);77return result;78}79public static boolean searchForString(String searchString, InputStream in) throws IOException80{81// create a buffered reader with a 1Mb buffer82BufferedReader br = new BufferedReader(new InputStreamReader(in));8384for (String line = br.readLine(); line != null; line = br.readLine())85{86if (line.indexOf(searchString) != -1)87{88return true;89}90}91br.close();92return false;93}9495public static int countLinesContainingString(String searchString, InputStream in) throws IOException96{97int count = 0;98// create a buffered reader with a 1Mb buffer99BufferedReader br = new BufferedReader(new InputStreamReader(in));100101for (String line = br.readLine(); line != null; line = br.readLine())102{103if (line.indexOf(searchString) != -1)104{105count++;106}107}108br.close();109return count;110}111112public static void displayFile(String filename) throws IOException, FileNotFoundException113{114BufferedReader br = new BufferedReader(new FileReader(filename));115116for (String line = br.readLine(); line != null; line = br.readLine())117{118System.out.println(filename + ": " + line);119}120}121public static void printResult(boolean result)122{123System.out.println("TEST " + (result ? "PASSED" : "FAILED"));124}125}126127128