From 3e16106f2d7bb1b890b364850977192089117038 Mon Sep 17 00:00:00 2001 From: Winston Li Date: Mon, 3 Nov 2014 14:20:12 +0000 Subject: [PATCH] Handles program arguments and prints usage. --- services/git-bridge/.idea/workspace.xml | 286 +++++++++++------- .../application/WLGitBridgeApplication.java | 46 ++- .../application/WLGitBridgeServer.java | 2 +- .../InvalidProgramArgumentsException.java | 7 + .../WLGitBridgeApplicationTests.java | 20 +- 5 files changed, 244 insertions(+), 117 deletions(-) create mode 100644 services/git-bridge/src/uk/ac/ic/wlgitbridge/application/exception/InvalidProgramArgumentsException.java diff --git a/services/git-bridge/.idea/workspace.xml b/services/git-bridge/.idea/workspace.xml index ce7b45d781..417302fe33 100644 --- a/services/git-bridge/.idea/workspace.xml +++ b/services/git-bridge/.idea/workspace.xml @@ -7,8 +7,10 @@ - + + + @@ -32,36 +34,26 @@ + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - @@ -72,11 +64,21 @@ - - + + - - + + + + + + + + + + + + @@ -92,21 +94,21 @@ - - + + - - + + - - + + - - + + @@ -122,11 +124,11 @@ - + - - + + @@ -179,16 +181,17 @@ @@ -223,7 +226,6 @@ - @@ -247,6 +249,46 @@ + @@ -356,13 +425,16 @@ - - - + + + + + + @@ -415,6 +487,19 @@ + + + - - - - + + - + @@ -590,7 +663,6 @@ - @@ -641,19 +713,10 @@ - - - - - - - - - @@ -661,7 +724,6 @@ - @@ -911,15 +973,6 @@ - - - - - - - - - @@ -927,23 +980,6 @@ - - - - - - - - - - - - - - - - - @@ -955,14 +991,6 @@ - - - - - - - - @@ -995,6 +1023,22 @@ + + + + + + + + + + + + + + + + @@ -1003,10 +1047,26 @@ - + - - + + + + + + + + + + + + + + + + + + @@ -1019,10 +1079,18 @@ - + - - + + + + + + + + + + diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeApplication.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeApplication.java index 8aeead6268..7f3d3b023d 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeApplication.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeApplication.java @@ -1,27 +1,63 @@ package uk.ac.ic.wlgitbridge.application; +import uk.ac.ic.wlgitbridge.application.exception.InvalidProgramArgumentsException; + import javax.servlet.ServletException; -import java.util.Arrays; /** * Created by Winston on 02/11/14. */ public class WLGitBridgeApplication { + public static final int EXIT_CODE_FAILED = 1; + private static final String USAGE_MESSAGE = "usage: writelatex-git-bridge port root_git_directory_path"; + + private int port; + private String rootGitDirectoryPath; + public WLGitBridgeApplication(String[] args) { - parseArguments(args); + try { + parseArguments(args); + } catch (InvalidProgramArgumentsException e) { + printUsage(); + System.exit(EXIT_CODE_FAILED); + } } public void run() { try { - new WLGitBridgeServer(8080).start(); + new WLGitBridgeServer(port, rootGitDirectoryPath).start(); } catch (ServletException e) { e.printStackTrace(); } } - private void parseArguments(String[] args) { - System.out.println(Arrays.toString(args)); + private void parseArguments(String[] args) throws InvalidProgramArgumentsException { + checkArgumentsLength(args); + parsePortNumber(args); + parseRootGitDirectoryPath(args); + } + + private void checkArgumentsLength(String[] args) throws InvalidProgramArgumentsException { + if (args.length < 2) { + throw new InvalidProgramArgumentsException(); + } + } + + private void parsePortNumber(String[] args) throws InvalidProgramArgumentsException { + try { + port = Integer.parseInt(args[0]); + } catch (NumberFormatException e) { + throw new InvalidProgramArgumentsException(); + } + } + + private void parseRootGitDirectoryPath(String[] args) { + rootGitDirectoryPath = args[1]; + } + + private void printUsage() { + System.out.println(USAGE_MESSAGE); } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java index 5f533e504d..d77f2c9bff 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/WLGitBridgeServer.java @@ -16,7 +16,7 @@ public class WLGitBridgeServer { private final Server jettyServer; private final int port; - public WLGitBridgeServer(final int port) throws ServletException { + public WLGitBridgeServer(final int port, String rootGitDirectoryPath) throws ServletException { this.port = port; jettyServer = new Server(port); configureJettyServer(); diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/exception/InvalidProgramArgumentsException.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/exception/InvalidProgramArgumentsException.java new file mode 100644 index 0000000000..6885a5f2b0 --- /dev/null +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/application/exception/InvalidProgramArgumentsException.java @@ -0,0 +1,7 @@ +package uk.ac.ic.wlgitbridge.application.exception; + +/** + * Created by Winston on 03/11/14. + */ +public class InvalidProgramArgumentsException extends Throwable { +} diff --git a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/application/WLGitBridgeApplicationTests.java b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/application/WLGitBridgeApplicationTests.java index c67d317549..456ef3bb87 100644 --- a/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/application/WLGitBridgeApplicationTests.java +++ b/services/git-bridge/test/uk/ac/ic/wlgitbridge/test/application/WLGitBridgeApplicationTests.java @@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.test.application; import org.junit.Test; import org.junit.Assert; +import uk.ac.ic.wlgitbridge.application.WLGitBridgeApplication; /** * Created by Winston on 02/11/14. @@ -9,8 +10,23 @@ import org.junit.Assert; public class WLGitBridgeApplicationTests { @Test - public void nothingToTest() { - Assert.assertTrue(true); + public void diesIfLessThanTwoArguments() { + + } + + @Test + public void diesIfFirstArgumentIsInvalidNumber() { + + } + + @Test + public void setsPortToFirstArgument() { + + } + + @Test + public void setsRootGitDirectoryPathToSecondArgument() { + } }