From 3b8a6106f340e77240157a3f42461ce633356dcb Mon Sep 17 00:00:00 2001 From: Navin Singh Date: Wed, 22 Dec 2021 13:08:45 +0530 Subject: [PATCH 1/2] graceful message and exit for wrong value of phase #81 --- client/src/main/java/zingg/client/Client.java | 7 +++ .../test/java/zingg/client/TestClient.java | 62 +++++++++++++++++-- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/zingg/client/Client.java b/client/src/main/java/zingg/client/Client.java index fd741cc3d..cd584afea 100644 --- a/client/src/main/java/zingg/client/Client.java +++ b/client/src/main/java/zingg/client/Client.java @@ -8,6 +8,7 @@ import zingg.client.util.Email; import zingg.client.util.EmailBody; +import zingg.client.util.Util; /** * This is the main point of interface with the Zingg matching product. @@ -21,6 +22,7 @@ public class Client implements Serializable { private ClientOptions options; public static final Log LOG = LogFactory.getLog(Client.class); + public static final int EXIT_STATUS_ILLEGAL_CMD = 127; /** @@ -131,6 +133,11 @@ public static void main(String... args) { System.exit(0); } String phase = options.get(ClientOptions.PHASE).value.trim(); + if (ZinggOptions.getByValue(phase) == null) { + LOG.error("'" + phase + "' is not a valid phase"); + LOG.error("Valid phases are: " + Util.join(ZinggOptions.getAllZinggOptions(), "|")); + System.exit(EXIT_STATUS_ILLEGAL_CMD); + } Arguments arguments = null; if (options.get(ClientOptions.CONF).value.endsWith("json")) { arguments = Arguments.createArgumentsFromJSON(options.get(ClientOptions.CONF).value, phase); diff --git a/client/src/test/java/zingg/client/TestClient.java b/client/src/test/java/zingg/client/TestClient.java index 3a8bf334f..d181a84a1 100644 --- a/client/src/test/java/zingg/client/TestClient.java +++ b/client/src/test/java/zingg/client/TestClient.java @@ -1,12 +1,66 @@ package zingg.client; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; + import org.junit.Test; public class TestClient { - + @Test - public void testInvalidArgs() { - + public void testInvalidPhaseArg() { + String s = null; + int exitStatusExpected = 127; // error code for an illegal command e.g. typo + try { + String zinggInvalidPhaseCmd = "scripts/zingg.sh --phase findTrainingDat --conf examples/febrl/config.json"; + Process p = Runtime.getRuntime().exec(zinggInvalidPhaseCmd, null, new File(System.getenv("ZINGG_HOME"))); + BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); + BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); + // read the output from the command + System.out.println("Here is the standard output of the command:\n"); + while ((s = stdInput.readLine()) != null) { + System.out.println(s); + } + // read any errors from the attempted command + System.out.println("Here is the standard error of the command (if any):\n"); + while ((s = stdError.readLine()) != null) { + System.out.println(s); + } + p.waitFor(); + assertTrue(exitStatusExpected == p.exitValue()); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } } -} + @Test + public void testValidPhaseArg() { + String s = null; + int exitStatusExpected = 0; //default SUCCESS code + try { + String zinggHome = System.getenv("ZINGG_HOME"); + String zinggCmd = "scripts/zingg.sh --phase findTrainingData --conf examples/febrl/config.json"; + Process p = Runtime.getRuntime().exec(zinggCmd, null, new File(zinggHome)); + BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); + BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); + // read the output from the command + System.out.println("Here is the standard output of the command:\n"); + while ((s = stdInput.readLine()) != null) { + System.out.println(s); + } + // read any errors from the attempted command + System.out.println("Here is the standard error of the command (if any):\n"); + while ((s = stdError.readLine()) != null) { + System.out.println(s); + } + p.waitFor(); + assertTrue(exitStatusExpected == p.exitValue()); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file From eb53368ee7d8ffafbd9ba742f232c6335248e820 Mon Sep 17 00:00:00 2001 From: Navin Singh Date: Wed, 22 Dec 2021 23:24:28 +0530 Subject: [PATCH 2/2] refactored the verification check into a new ZinggOptions function #81 --- client/src/main/java/zingg/client/Client.java | 8 +-- .../main/java/zingg/client/ZinggOptions.java | 10 ++- .../test/java/zingg/client/TestClient.java | 61 ++++--------------- 3 files changed, 22 insertions(+), 57 deletions(-) diff --git a/client/src/main/java/zingg/client/Client.java b/client/src/main/java/zingg/client/Client.java index cd584afea..933cbe9d7 100644 --- a/client/src/main/java/zingg/client/Client.java +++ b/client/src/main/java/zingg/client/Client.java @@ -8,7 +8,6 @@ import zingg.client.util.Email; import zingg.client.util.EmailBody; -import zingg.client.util.Util; /** * This is the main point of interface with the Zingg matching product. @@ -22,7 +21,6 @@ public class Client implements Serializable { private ClientOptions options; public static final Log LOG = LogFactory.getLog(Client.class); - public static final int EXIT_STATUS_ILLEGAL_CMD = 127; /** @@ -133,11 +131,7 @@ public static void main(String... args) { System.exit(0); } String phase = options.get(ClientOptions.PHASE).value.trim(); - if (ZinggOptions.getByValue(phase) == null) { - LOG.error("'" + phase + "' is not a valid phase"); - LOG.error("Valid phases are: " + Util.join(ZinggOptions.getAllZinggOptions(), "|")); - System.exit(EXIT_STATUS_ILLEGAL_CMD); - } + ZinggOptions.verifyPhase(phase); Arguments arguments = null; if (options.get(ClientOptions.CONF).value.endsWith("json")) { arguments = Arguments.createArgumentsFromJSON(options.get(ClientOptions.CONF).value, phase); diff --git a/client/src/main/java/zingg/client/ZinggOptions.java b/client/src/main/java/zingg/client/ZinggOptions.java index 4884d8c5b..40972f6d0 100644 --- a/client/src/main/java/zingg/client/ZinggOptions.java +++ b/client/src/main/java/zingg/client/ZinggOptions.java @@ -2,6 +2,8 @@ import java.util.Arrays; +import zingg.client.util.Util; + public enum ZinggOptions { TRAIN("train"), @@ -38,5 +40,11 @@ public static final ZinggOptions getByValue(String value){ return null; } - + public static void verifyPhase(String phase) throws ZinggClientException { + if (getByValue(phase) == null) { + String message = "'" + phase + "' is not a valid phase. " + + "Valid phases are: " + Util.join(getAllZinggOptions(), "|"); + throw new ZinggClientException(message); + } + } } \ No newline at end of file diff --git a/client/src/test/java/zingg/client/TestClient.java b/client/src/test/java/zingg/client/TestClient.java index d181a84a1..5056246bf 100644 --- a/client/src/test/java/zingg/client/TestClient.java +++ b/client/src/test/java/zingg/client/TestClient.java @@ -1,66 +1,29 @@ package zingg.client; -import static org.junit.Assert.assertTrue; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; +import static org.junit.Assert.fail; import org.junit.Test; public class TestClient { @Test - public void testInvalidPhaseArg() { - String s = null; - int exitStatusExpected = 127; // error code for an illegal command e.g. typo + public void testValidPhase() { + String phase = "train"; try { - String zinggInvalidPhaseCmd = "scripts/zingg.sh --phase findTrainingDat --conf examples/febrl/config.json"; - Process p = Runtime.getRuntime().exec(zinggInvalidPhaseCmd, null, new File(System.getenv("ZINGG_HOME"))); - BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); - BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); - // read the output from the command - System.out.println("Here is the standard output of the command:\n"); - while ((s = stdInput.readLine()) != null) { - System.out.println(s); - } - // read any errors from the attempted command - System.out.println("Here is the standard error of the command (if any):\n"); - while ((s = stdError.readLine()) != null) { - System.out.println(s); - } - p.waitFor(); - assertTrue(exitStatusExpected == p.exitValue()); - } catch (IOException | InterruptedException e) { - e.printStackTrace(); + ZinggOptions.verifyPhase(phase); + } catch (ZinggClientException e1) { + fail("No exception was expected as it is a valid phase: " + phase); } } @Test - public void testValidPhaseArg() { - String s = null; - int exitStatusExpected = 0; //default SUCCESS code + public void testInvalidPhase() { + String phase = "tain"; try { - String zinggHome = System.getenv("ZINGG_HOME"); - String zinggCmd = "scripts/zingg.sh --phase findTrainingData --conf examples/febrl/config.json"; - Process p = Runtime.getRuntime().exec(zinggCmd, null, new File(zinggHome)); - BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); - BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); - // read the output from the command - System.out.println("Here is the standard output of the command:\n"); - while ((s = stdInput.readLine()) != null) { - System.out.println(s); - } - // read any errors from the attempted command - System.out.println("Here is the standard error of the command (if any):\n"); - while ((s = stdError.readLine()) != null) { - System.out.println(s); - } - p.waitFor(); - assertTrue(exitStatusExpected == p.exitValue()); - } catch (IOException | InterruptedException e) { - e.printStackTrace(); + ZinggOptions.verifyPhase(phase); + fail("An exception should have been thrown for an invalid phase"); + } catch (ZinggClientException e1) { + System.out.println("Expected exception as it is an invalid phase: " + phase); } } } \ No newline at end of file