From 3f6c19ed282df6b08e481bc08491bf4702f02624 Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Sun, 1 Jun 2025 00:39:00 +0800 Subject: [PATCH] Add black box tests for queryBridgeWords. --- app/build.gradle.kts | 2 + app/src/main/java/fun/youthlic/GraphCLI.java | 4 - .../java/fun/youthlic/GraphCLIHelperTest.java | 80 +++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 app/src/test/java/fun/youthlic/GraphCLIHelperTest.java diff --git a/app/build.gradle.kts b/app/build.gradle.kts index aeb7ee9..bec3688 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -34,6 +34,8 @@ dependencies { implementation(libs.guava) implementation("org.jline:jline:3.29.0") + + compileOnly("com.github.spotbugs:spotbugs-annotations:3.1.3") } // Apply a specific Java toolchain to ease working on different environments. diff --git a/app/src/main/java/fun/youthlic/GraphCLI.java b/app/src/main/java/fun/youthlic/GraphCLI.java index a8aff4e..73a16c5 100644 --- a/app/src/main/java/fun/youthlic/GraphCLI.java +++ b/app/src/main/java/fun/youthlic/GraphCLI.java @@ -1,6 +1,5 @@ package fun.youthlic; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; @@ -26,7 +25,6 @@ import org.jline.terminal.TerminalBuilder; */ @SuppressWarnings("ALL") public class GraphCLI { - @SuppressFBWarnings({"DM_DEFAULT_ENCODING", "PATH_TRAVERSAL_IN"}) static String readFile(final String filename) { String text = null; try (BufferedReader inputReader = new BufferedReader(new FileReader(filename))) { @@ -255,7 +253,6 @@ class GraphCLIHelper { return output.toString(); } - @SuppressFBWarnings({"PREDICTABLE_RANDOM", "DMI_RANDOM_USED_ONLY_ONCE"}) String generateNewText(final String inputText) { final var output = new StringBuilder(); final var words = inputText.split("\\s+"); @@ -332,7 +329,6 @@ class GraphCLIHelper { return graph.computePageRank(word); } - @SuppressFBWarnings("DM_DEFAULT_ENCODING") String randomWalk() { final var text = String.join(" ", graph.randomWalk()); final Path path = Paths.get("output.txt"); diff --git a/app/src/test/java/fun/youthlic/GraphCLIHelperTest.java b/app/src/test/java/fun/youthlic/GraphCLIHelperTest.java new file mode 100644 index 0000000..7b46438 --- /dev/null +++ b/app/src/test/java/fun/youthlic/GraphCLIHelperTest.java @@ -0,0 +1,80 @@ +package fun.youthlic; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class GraphCLIHelperTest { + private final GraphCLIHelper graph = new GraphCLIHelper("B D E H A C D H A F D H A B G H A C G H A D G H"); + + @Test + void testWord1NotInGraph() { + var result = graph.queryBridgeWords("x", "a"); + assertEquals("No x or a in the graph!", result); + } + @Test + void testWord2NotInGraph() { + String result = graph.queryBridgeWords("a", "x"); + assertEquals("No a or x in the graph!", result); + } + + @Test + void testBothWordsNotInGraph() { + String result = graph.queryBridgeWords("x", "y"); + assertEquals("No x or y in the graph!", result); + } + + @Test + void testNoBridgeWords() { + String result = graph.queryBridgeWords("a", "b"); + assertEquals("No bridge words from a to b!", result); + } + + @Test + void testSingleBridgeWord() { + String result = graph.queryBridgeWords("b", "e"); + assertEquals("The bridge words from b to e are: d.", result); + } + + @Test + void testTwoBridgeWords() { + String result = graph.queryBridgeWords("c", "h"); + assertEquals("The bridge words from c to h are: g, and d.", result); + } + + @Test + void testThreeBridgeWords() { + String result = graph.queryBridgeWords("a", "g"); + assertEquals("The bridge words from a to g are: c, b, and d.", result); + } + + @Test + void testSameWordInput() { + String result = graph.queryBridgeWords("a", "a"); + assertEquals("No bridge words from a to a!", result); + } + + @Test + void testWord1Empty() { + String result = graph.queryBridgeWords("", "a"); + assertEquals("No or a in the graph!", result); + } + + @Test + void testWord2Empty() { + String result = graph.queryBridgeWords("a", ""); + assertEquals("No a or in the graph!", result); + } + + @Test + void testBothWordsEmpty() { + String result = graph.queryBridgeWords("", ""); + assertEquals("No or in the graph!", result); + } + + @Test + void testCaseMismatch() { + String result = graph.queryBridgeWords("B", "E"); + assertEquals("The bridge words from B to E are: d.", result); + } +}