Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful message and exit for wrong value of phase #84

Merged
merged 3 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/src/main/java/zingg/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public static void main(String... args) {
System.exit(0);
}
String phase = options.get(ClientOptions.PHASE).value.trim();
ZinggOptions.verifyPhase(phase);
Arguments arguments = null;
if (options.get(ClientOptions.CONF).value.endsWith("json")) {
arguments = Arguments.createArgumentsFromJSON(options.get(ClientOptions.CONF).value, phase);
Expand Down
10 changes: 9 additions & 1 deletion client/src/main/java/zingg/client/ZinggOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Arrays;

import zingg.client.util.Util;

public enum ZinggOptions {

TRAIN("train"),
Expand Down Expand Up @@ -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);
}
}
}
25 changes: 21 additions & 4 deletions client/src/test/java/zingg/client/TestClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package zingg.client;

import static org.junit.Assert.fail;

import org.junit.Test;

public class TestClient {

@Test
public void testInvalidArgs() {

public void testValidPhase() {
String phase = "train";
try {
ZinggOptions.verifyPhase(phase);
} catch (ZinggClientException e1) {
fail("No exception was expected as it is a valid phase: " + phase);
}
}

}
@Test
public void testInvalidPhase() {
String phase = "tain";
try {
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);
}
}
}