/** * MIT License * * Copyright (c) 2021 Exercism * Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.lang.reflect.Method; import java.util.stream.Collectors; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.annotation.ElementType; public class GrepToolMain { @Test(name = "One file, one match, no flags") public void oneFileOneMatchNoFlags() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "Of Atreus, Agamemnon, King of men."; String actual = grepTool.grep("Agamemnon", Collections.emptyList(), Collections.singletonList("iliad.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, one match, print line numbers flag") @Disabled public void oneFileOneMatchPrintLineNumbersFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "2:Of that Forbidden Tree, whose mortal tast"; String actual = grepTool.grep("Forbidden", Collections.singletonList("-n"), Collections.singletonList("paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, one match, case-insensitive flag") @Disabled public void oneFileOneMatchCaseInsensitiveFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "Of that Forbidden Tree, whose mortal tast"; String actual = grepTool.grep("FORBIDDEN", Collections.singletonList("-i"), Collections.singletonList("paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, one match, print file names flag") @Disabled public void oneFileOneMatchPrintFileNamesFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "paradise-lost.txt"; String actual = grepTool.grep("Forbidden", Collections.singletonList("-l"), Collections.singletonList("paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, one match, match entire lines flag") @Disabled public void oneFileOneMatchEntireLinesFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "With loss of Eden, till one greater Man"; String actual = grepTool.grep("With loss of Eden, till one greater Man", Collections.singletonList("-x"), Collections.singletonList("paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, one match, multiple flags") @Disabled public void oneFileOneMatchMultipleFlags() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "9:Of Atreus, Agamemnon, King of men."; String actual = grepTool.grep("OF ATREUS, Agamemnon, KIng of MEN.", Arrays.asList("-n", "-i", "-x"), Collections.singletonList("iliad.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, several matches, no flags") @Disabled public void oneFileSeveralMatchesNoFlags() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "Nor how it may concern my modesty,\n" + "But I beseech your grace that I may know\n" + "The worst that may befall me in this case,"; String actual = grepTool.grep("may", Collections.emptyList(), Collections.singletonList("midsummer-night.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, several matches, print line numbers flag") @Disabled public void oneFileSeveralMatchesPrintLineNumbersFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "3:Nor how it may concern my modesty,\n" + "5:But I beseech your grace that I may know\n" + "6:The worst that may befall me in this case,"; String actual = grepTool.grep("may", Collections.singletonList("-n"), Collections.singletonList("midsummer-night.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, several matches, match entire lines flag") @Disabled public void oneFileSeveralMatchesMatchEntireLineFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = ""; String actual = grepTool.grep("may", Collections.singletonList("-x"), Collections.singletonList("midsummer-night.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, several matches, case-insensitive flag") @Disabled public void oneFileSeveralMatchesCaseInsensitiveFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "Achilles sing, O Goddess! Peleus' son;\n" + "The noble Chief Achilles from the son"; String actual = grepTool.grep("ACHILLES", Collections.singletonList("-i"), Collections.singletonList("iliad.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, several matches, inverted flag") @Disabled public void oneFileSeveralMatchesInvertedFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "Brought Death into the World, and all our woe,\n" + "With loss of Eden, till one greater Man\n" + "Restore us, and regain the blissful Seat,\n" + "Sing Heav'nly Muse, that on the secret top\n" + "That Shepherd, who first taught the chosen Seed"; String actual = grepTool.grep("Of", Collections.singletonList("-v"), Collections.singletonList("paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, no matches, various flags") @Disabled public void oneFileNoMatchesVariousFlags() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = ""; String actual = grepTool.grep("Gandalf", Arrays.asList("-n", "-l", "-x", "-i"), Collections.singletonList("iliad.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, one match, file flag takes precedence over line flag") @Disabled public void oneFileOneMatchFileFlagTakesPrecedenceOverLineFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "iliad.txt"; String actual = grepTool.grep("ten", Arrays.asList("-n", "-l"), Collections.singletonList("iliad.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "One file, several matches, inverted and match entire lines flags") @Disabled public void oneFileSeveralMatchesInvertedAndMatchEntireLinesFlags() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "Achilles sing, O Goddess! Peleus' son;\n" + "His wrath pernicious, who ten thousand woes\n" + "Caused to Achaia's host, sent many a soul\n" + "And Heroes gave (so stood the will of Jove)\n" + "To dogs and to all ravening fowls a prey,\n" + "When fierce dispute had separated once\n" + "The noble Chief Achilles from the son\n" + "Of Atreus, Agamemnon, King of men."; String actual = grepTool.grep("Illustrious into Ades premature,", Arrays.asList("-x", "-v"), Collections.singletonList("iliad.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, one match, no flags") @Disabled public void multipleFilesOneMatchNoFlags() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "iliad.txt:Of Atreus, Agamemnon, King of men."; String actual = grepTool.grep("Agamemnon", Collections.emptyList(), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, several matches, no flags") @Disabled public void multipleFilesSeveralMatchesNoFlags() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "midsummer-night.txt:Nor how it may concern my modesty,\n" + "midsummer-night.txt:But I beseech your grace that I may know\n" + "midsummer-night.txt:The worst that may befall me in this case,"; String actual = grepTool.grep("may", Collections.emptyList(), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, several matches, print line numbers flag") @Disabled public void multipleFilesSeveralMatchesPrintLineNumbersFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "midsummer-night.txt:5:But I beseech your grace that I may know\n" + "midsummer-night.txt:6:The worst that may befall me in this case,\n" + "paradise-lost.txt:2:Of that Forbidden Tree, whose mortal tast\n" + "paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top"; String actual = grepTool.grep("that", Collections.singletonList("-n"), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, one match, print file names flag") @Disabled public void multipleFilesOneMatchPrintFileNamesFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "iliad.txt\n" + "paradise-lost.txt"; String actual = grepTool.grep("who", Collections.singletonList("-l"), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, several matches, case-insensitive flag") @Disabled public void multipleFilesSeveralMatchesCaseInsensitiveFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "iliad.txt:Caused to Achaia's host, sent many a soul\n" + "iliad.txt:Illustrious into Ades premature,\n" + "iliad.txt:And Heroes gave (so stood the will of Jove)\n" + "iliad.txt:To dogs and to all ravening fowls a prey,\n" + "midsummer-night.txt:I do entreat your grace to pardon me.\n" + "midsummer-night.txt:In such a presence here to plead my thoughts;\n" + "midsummer-night.txt:If I refuse to wed Demetrius.\n" + "paradise-lost.txt:Brought Death into the World, and all our woe,\n" + "paradise-lost.txt:Restore us, and regain the blissful Seat,\n" + "paradise-lost.txt:Sing Heav'nly Muse, that on the secret top"; String actual = grepTool.grep("TO", Collections.singletonList("-i"), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, several matches, inverted flag") @Disabled public void multipleFilesSeveralMatchesInvertedFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "iliad.txt:Achilles sing, O Goddess! Peleus' son;\n" + "iliad.txt:The noble Chief Achilles from the son\n" + "midsummer-night.txt:If I refuse to wed Demetrius."; String actual = grepTool.grep("a", Collections.singletonList("-v"), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, one match, match entire lines flag") @Disabled public void multipleFilesOneMatchEntireLinesFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "midsummer-night.txt:But I beseech your grace that I may know"; String actual = grepTool.grep("But I beseech your grace that I may know", Collections.singletonList("-x"), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, one match, multiple flags") @Disabled public void multipleFilesOneMatchMultipleFlags() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "paradise-lost.txt:4:With loss of Eden, till one greater Man"; String actual = grepTool.grep("WITH LOSS OF EDEN, TILL ONE GREATER MAN", Arrays.asList("-n", "-i", "-x"), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, no matches, various flags") @Disabled public void multipleFilesNoMatchesVariousFlags() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = ""; String actual = grepTool.grep("Frodo", Arrays.asList("-n", "-l", "-x", "-i"), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, several matches, file flag takes precedence over line number flag") @Disabled public void multipleFilesSeveralMatchesFileFlagTakesPrecedenceOverLineNumberFlag() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "iliad.txt\n" + "paradise-lost.txt"; String actual = grepTool.grep("who", Arrays.asList("-n", "-l"), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } @Test(name = "Multiple files, several matches, inverted and match entire lines flags") @Disabled public void multipleFilesSeveralMatchesInvertedAndMatchEntireLinesFlags() { runTest(() -> { try { setUp(); GrepTool grepTool = new GrepTool(); String expected = "iliad.txt:Achilles sing, O Goddess! Peleus' son;\n" + "iliad.txt:His wrath pernicious, who ten thousand woes\n" + "iliad.txt:Caused to Achaia's host, sent many a soul\n" + "iliad.txt:And Heroes gave (so stood the will of Jove)\n" + "iliad.txt:To dogs and to all ravening fowls a prey,\n" + "iliad.txt:When fierce dispute had separated once\n" + "iliad.txt:The noble Chief Achilles from the son\n" + "iliad.txt:Of Atreus, Agamemnon, King of men.\n" + "midsummer-night.txt:I do entreat your grace to pardon me.\n" + "midsummer-night.txt:I know not by what power I am made bold,\n" + "midsummer-night.txt:Nor how it may concern my modesty,\n" + "midsummer-night.txt:In such a presence here to plead my thoughts;\n" + "midsummer-night.txt:But I beseech your grace that I may know\n" + "midsummer-night.txt:The worst that may befall me in this case,\n" + "midsummer-night.txt:If I refuse to wed Demetrius.\n" + "paradise-lost.txt:Of Mans First Disobedience, and the Fruit\n" + "paradise-lost.txt:Of that Forbidden Tree, whose mortal tast\n" + "paradise-lost.txt:Brought Death into the World, and all our woe,\n" + "paradise-lost.txt:With loss of Eden, till one greater Man\n" + "paradise-lost.txt:Restore us, and regain the blissful Seat,\n" + "paradise-lost.txt:Sing Heav'nly Muse, that on the secret top\n" + "paradise-lost.txt:Of Oreb, or of Sinai, didst inspire\n" + "paradise-lost.txt:That Shepherd, who first taught the chosen Seed"; String actual = grepTool.grep("Illustrious into Ades premature,", Arrays.asList("-x", "-v"), Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")); assertStringEquals(expected, actual); tearDown(); } catch (IOException e) { throw new RuntimeException(e); } }); } public void setUp() throws IOException { List iliadText = Arrays.asList("Achilles sing, O Goddess! Peleus' son;", "His wrath pernicious, who ten thousand woes", "Caused to Achaia's host, sent many a soul", "Illustrious into Ades premature,", "And Heroes gave (so stood the will of Jove)", "To dogs and to all ravening fowls a prey,", "When fierce dispute had separated once", "The noble Chief Achilles from the son", "Of Atreus, Agamemnon, King of men."); writeToFile("iliad.txt", iliadText); List midsummerNightText = Arrays.asList("I do entreat your grace to pardon me.", "I know not by what power I am made bold,", "Nor how it may concern my modesty,", "In such a presence here to plead my thoughts;", "But I beseech your grace that I may know", "The worst that may befall me in this case,", "If I refuse to wed Demetrius."); writeToFile("midsummer-night.txt", midsummerNightText); List paradiseLostText = Arrays.asList("Of Mans First Disobedience, and the Fruit", "Of that Forbidden Tree, whose mortal tast", "Brought Death into the World, and all our woe,", "With loss of Eden, till one greater Man", "Restore us, and regain the blissful Seat,", "Sing Heav'nly Muse, that on the secret top", "Of Oreb, or of Sinai, didst inspire", "That Shepherd, who first taught the chosen Seed"); writeToFile("paradise-lost.txt", paradiseLostText); } public void tearDown() throws IOException { deleteFile("iliad.txt"); deleteFile("midsummer-night.txt"); deleteFile("paradise-lost.txt"); } private void writeToFile(String filename, List contents) throws IOException { Path file = Paths.get(filename); Files.write(file, contents, Charset.forName("UTF-8")); } private void deleteFile(String filename) throws IOException { Files.deleteIfExists(Paths.get(filename)); } private void runTest(Runnable body) { try { body.run(); System.out.println("TEST PASSED"); } catch (Exception | AssertionError t) { System.out.println("TEST FAILED: " + t.getMessage()); } } private void assertStringEquals(String expected, String actual) { if (!expected.equals(actual)) { throw new AssertionError("Expected:\n" + expected + "\nbut got:\n" + actual); } } public static void main(String[] args) { new GrepToolMain().runTests(); } /** * Annotation to mark test methods. The name parameter is used to identify the * test in the output. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) private @interface Test { String name(); } /** * Annotation to mark test methods as disabled. Disabled tests will not be * executed when running the tests. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) private @interface Disabled { } private void runTests() { Method[] methods = this.getClass().getDeclaredMethods(); List allTestMethods = Arrays.asList(methods).stream().filter(m -> m.isAnnotationPresent(Test.class)) .collect(Collectors.toList()); List enabledTestMethods = allTestMethods.stream().filter(m -> !m.isAnnotationPresent(Disabled.class)) .collect(Collectors.toList()); System.out .println("Running " + enabledTestMethods.size() + " out of " + allTestMethods.size() + " tests... \n"); for (Method method : enabledTestMethods) { try { System.out.println("Running test: " + method.getAnnotation(Test.class).name()); method.invoke(this); } catch (Exception e) { System.out.println("Test " + method.getAnnotation(Test.class).name() + " failed with an unexpected exception: " + e.getCause()); } } System.out.println("Finished running tests."); if (allTestMethods.size() != enabledTestMethods.size()) { System.out.println("\nThe following tests are disabled and were not executed:"); for (Method method : allTestMethods) { if (method.isAnnotationPresent(Disabled.class)) { System.out.println("- " + method.getAnnotation(Test.class).name()); } } System.out.println( "To enable these tests, remove the @Disabled annotation from the corresponding test methods."); } } }