Skip to content
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
66 changes: 40 additions & 26 deletions graphwalker-cli/src/main/java/org/graphwalker/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,20 @@ enum Command {
public static void main(String[] args) {
CLI cli = new CLI();
try {
cli.run(args);
int status = cli.run(args);
System.exit(status);
} catch (Exception e) {
// We should have caught all exceptions up until here, but there
// might have been problems with the command parser for instance...
System.err.println(e + System.lineSeparator());
logger.error("An error occurred when running command: " + StringUtils.join(args, " "), e);
logger.error("An unexpected error occurred when running command: " + StringUtils.join(args, " "), e);
}
}

/**
* Parses the command line.
*/
private void run(String[] args) {
public int run(String[] args) {
Options options = new Options();
JCommander jc = new JCommander(options);
jc.setProgramName("graphwalker");
Expand Down Expand Up @@ -145,12 +146,12 @@ private void run(String[] args) {

if (options.help) {
jc.usage();
return;
return 0;
} else if (options.version) {
System.out.println(printVersionInformation());
return;
return 0;
} else if (jc.getParsedCommand() == null) {
throw new MissingCommandException("Missing a command. Add '--help'");
throw new MissingCommandException("Missing a command. Use the '--help' option to list all available commands.");
}

// Parse for commands
Expand All @@ -174,19 +175,24 @@ private void run(String[] args) {
runCommandSource();
} else if (jc.getParsedCommand().equalsIgnoreCase("check")) {
command = Command.CHECK;
runCommandCheck();
return runCommandCheck();
}
} catch (UnsupportedFileFormat | MissingCommandException e) {
System.err.println(e.getMessage() + System.lineSeparator());
return 1;
} catch (ParameterException e) {
System.err.println("An error occurred when running command: " + StringUtils.join(args, " "));
System.err.println("An error occurred when running command: '" + StringUtils.join(args, " ") + "'.");
System.err.println(e.getMessage() + System.lineSeparator());
jc.usage();
return 1;
} catch (Exception e) {
System.err.println("An error occurred when running command: " + StringUtils.join(args, " "));
System.err.println(e.getMessage() + System.lineSeparator());
logger.error("An error occurred when running command: " + StringUtils.join(args, " "), e);
System.err.println("An error occurred when running command: '" + StringUtils.join(args, " ") + "'.");
System.err.println(e.getMessage() + System.lineSeparator());
return 2;
}

return 0;
}

private void setLogLevel(Options options) {
Expand All @@ -210,7 +216,7 @@ private void setLogLevel(Options options) {
}
}

private void runCommandCheck() throws Exception, UnsupportedFileFormat {
private int runCommandCheck() throws Exception, UnsupportedFileFormat {
List<Context> contexts = getContextsWithPathGenerators(check.model.iterator());
if (check.blocked) {
org.graphwalker.io.common.Util.filterBlockedElements(contexts);
Expand All @@ -221,8 +227,10 @@ private void runCommandCheck() throws Exception, UnsupportedFileFormat {
for (String issue : issues) {
System.out.println(issue);
}
return 1;
} else {
System.out.println("No issues found with the model(s).");
return 0;
}
}

Expand Down Expand Up @@ -252,7 +260,7 @@ private void runCommandMethods() throws Exception, UnsupportedFileFormat {

for (Context context : contexts) {
for (Vertex.RuntimeVertex vertex : context.getModel().getVertices()) {
if (null != vertex.getName()) {
if (vertex.getName() != null) {
names.add(vertex.getName());
}
}
Expand Down Expand Up @@ -296,6 +304,7 @@ private void runCommandOnline() throws Exception, UnsupportedFileFormat {
+ online.port
+ "/graphwalker/getNext");
System.out.println("Press Control+C to end...");

try {
server.start();
Thread.currentThread().join();
Expand All @@ -308,7 +317,7 @@ private void runCommandOnline() throws Exception, UnsupportedFileFormat {
server.stop();
}
} else {
throw new ParameterException("--service expected either WEBSOCKET or RESTFUL");
throw new ParameterException("--service expected either WEBSOCKET or RESTFUL.");
}
}

Expand All @@ -320,8 +329,9 @@ private void runCommandConvert() throws Exception, UnsupportedFileFormat {
try {
contexts = inputFactory.create(Paths.get(inputFileName));
} catch (DslException e) {
System.err.println("When parsing model: '" + inputFileName + "' " + e.getMessage() + System.lineSeparator());
throw new Exception("Model syntax error");
throw new Exception("The following syntax error occurred when parsing: '" + inputFileName + "'."
+ System.lineSeparator()
+ "Syntax Error: " + e.getMessage() + System.lineSeparator());
}

if (convert.blocked) {
Expand All @@ -343,11 +353,12 @@ private void runCommandSource() throws Exception, UnsupportedFileFormat {
contexts = inputFactory.create(Paths.get(modelFileName));
if (isNullOrEmpty(contexts)) {
logger.error("No valid models found in: " + modelFileName);
throw new RuntimeException("No valid models found in: " + modelFileName);
throw new RuntimeException("No valid models found in: '" + modelFileName + "'.");
}
} catch (DslException e) {
System.err.println("When parsing model: '" + modelFileName + "' " + e.getMessage() + System.lineSeparator());
throw new Exception("Model syntax error");
throw new Exception("The following syntax error occurred when parsing: '" + modelFileName + "'."
+ System.lineSeparator()
+ "Syntax Error: " + e.getMessage());
}

if (source.blocked) {
Expand Down Expand Up @@ -376,7 +387,7 @@ private void runCommandSource() throws Exception, UnsupportedFileFormat {
}
} catch (IOException e) {
logger.error(e.getMessage());
throw new RuntimeException("Could not read the file: " + templateFileName);
throw new RuntimeException("Could not read the file: '" + templateFileName + "'.");
}
String templateStr = templateStrBuilder.toString();

Expand Down Expand Up @@ -446,10 +457,12 @@ public List<Context> getContextsWithPathGenerators(Iterator itr) throws Exceptio
try {
contexts = factory.create(Paths.get(modelFileName));
} catch (DslException e) {
System.err.println("When parsing model: '" + modelFileName + "' " + e.getMessage() + System.lineSeparator());
throw new Exception("Model syntax error");
throw new Exception("The following syntax error occurred when parsing: '" + modelFileName + "'."
+ System.lineSeparator()
+ "Syntax Error: " + e.getMessage());
}
// TODO fix all occurences of get(0) is not safe

// TODO fix all occurrences of get(0) is not safe
contexts.get(0).setPathGenerator(GeneratorFactory.parse((String) itr.next()));

if (triggerOnce &&
Expand All @@ -464,9 +477,9 @@ public List<Context> getContextsWithPathGenerators(Iterator itr) throws Exceptio
}

if (elements == null) {
throw new ParameterException("--start-element Did not find matching element in the model: " + modelFileName);
throw new ParameterException("--start-element Did not find matching element in the model: '" + modelFileName + "'.");
} else if (elements.size() > 1) {
throw new ParameterException("--start-element There are more than one matching element in the model: " + modelFileName);
throw new ParameterException("--start-element There are more than one matching element in the model: '" + modelFileName + "'.");
}
contexts.get(0).setNextElement(elements.get(0));
}
Expand Down Expand Up @@ -501,8 +514,9 @@ private List<Context> getContexts(Iterator itr) throws Exception, UnsupportedFil
try {
contexts = factory.create(Paths.get(modelFileName));
} catch (DslException e) {
System.err.println("When parsing model: '" + modelFileName + "' " + e.getMessage() + System.lineSeparator());
throw new Exception("Model syntax error");
throw new Exception("The following syntax error occurred when parsing: '" + modelFileName + "'."
+ System.lineSeparator()
+ "Syntax Error: " + e.getMessage());
}
executionContexts.addAll(contexts);
}
Expand Down
22 changes: 17 additions & 5 deletions graphwalker-cli/src/test/java/org/graphwalker/cli/CLITestRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* 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
Expand Down Expand Up @@ -49,17 +49,23 @@ protected Result runCommand(String args[]) {
System.setOut(outStream);
System.setErr(errStream);

int status = -1;

try {
CLI.main(args);
CLI cli = new CLI();
status = cli.run(args);
} finally {
System.setOut(oldOutStream);
System.setErr(oldErrStream);

String outMsg = stdOutput.toString();
String errMsg = errOutput.toString();

logger.info("stdout: " + outMsg);
logger.info("stderr: " + errMsg);
return new Result(outMsg, errMsg);
logger.info("status: " + status);

return new Result(outMsg, errMsg, status);
}
}

Expand All @@ -82,10 +88,12 @@ public class Result {

private final String output;
private final String error;
private final int status;

public Result(String output, String error) {
public Result(String output, String error, int status) {
this.output = output;
this.error = error;
this.status = status;
}

public String getOutput() {
Expand All @@ -95,5 +103,9 @@ public String getOutput() {
public String getError() {
return error;
}

public int getStatus() {
return status;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
* 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
Expand Down Expand Up @@ -41,6 +41,7 @@ public class CheckTest extends CLITestRoot {
public void check() throws IOException {
String args[] = {"check", "-m", "graphml/online/ShoppingCart.graphml", "random(edge_coverage(100))"};
Result result = runCommand(args);
assertThat(result.getStatus(), is(0));
assertThat(result.getError(), is(""));
assertThat(result.getOutput(), containsString("No issues found with the model(s)."));
}
Expand All @@ -49,6 +50,7 @@ public void check() throws IOException {
public void checkWithBlocked() throws IOException {
String args[] = {"check", "-m", "graphml/online/ShoppingCart.graphml", "random(edge_coverage(100))"};
Result result = runCommand(args);
assertThat(result.getStatus(), is(0));
assertThat(result.getError(), is(""));
assertThat(result.getOutput(), containsString("No issues found with the model(s)."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
* 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
Expand Down Expand Up @@ -46,8 +46,9 @@ public class ConvertFilesTest extends CLITestRoot {
public void convertGraphmlToJava() throws IOException {
String args[] = {"convert", "--input", "graphml/UC01_GW2.graphml", "--format", "java"};
Result result = runCommand(args);
assertThat(result.getStatus(), is(0));
assertThat(result.getError(), is(""));
//TODO:Fix test
// Assert.assertTrue(tempFile.length() > 0);
// TODO: Fix test
// assertTrue(tempFile.length() > 0);
}
}
Loading