diff --git a/src/main/mavenp2versionmatch/db/SQLiteDBI.java b/src/main/mavenp2versionmatch/db/SQLiteDBI.java index e180b34..7a12f1b 100644 --- a/src/main/mavenp2versionmatch/db/SQLiteDBI.java +++ b/src/main/mavenp2versionmatch/db/SQLiteDBI.java @@ -8,7 +8,7 @@ public class SQLiteDBI implements DBI{ //TODO: we should be opening and closing the connection in each method rather - // than making the called open it and then closing it ourselves + // than making the caller open it and then closing it ourselves private Connection conn; //TODO: standardize the dbName and tableName or load them from a config file private static String dbName; diff --git a/src/main/mavenp2versionmatch/exception/MvnP2Exception.java b/src/main/mavenp2versionmatch/exception/MvnP2Exception.java new file mode 100644 index 0000000..8b5b7cd --- /dev/null +++ b/src/main/mavenp2versionmatch/exception/MvnP2Exception.java @@ -0,0 +1,21 @@ +package mavenp2versionmatch.exception; + +public class MvnP2Exception extends Exception { + + public MvnP2Exception() { + super(); + } + + public MvnP2Exception(String message) { + super(message); + } + + public MvnP2Exception(Throwable cause) { + super(cause); + } + + public MvnP2Exception(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/mavenp2versionmatch/main/MvnP2Util.java b/src/main/mavenp2versionmatch/main/MvnP2Util.java index 9da19f6..456a817 100644 --- a/src/main/mavenp2versionmatch/main/MvnP2Util.java +++ b/src/main/mavenp2versionmatch/main/MvnP2Util.java @@ -9,6 +9,7 @@ import mavenp2versionmatch.db.MavenP2Col; import mavenp2versionmatch.db.MavenP2Version; import mavenp2versionmatch.db.MySQLDBI; +import mavenp2versionmatch.exception.MvnP2Exception; public class MvnP2Util { @@ -32,7 +33,7 @@ private static boolean isValidAdd(Map map) { * @param args command line input to main * @return map of db column name and input value */ - private static Map getOptions(String[] args) { + private static Map getOptions(String[] args) throws MvnP2Exception { Map map = new HashMap(); for (int i = 1; i< args.length; i++) { @@ -41,8 +42,9 @@ private static Map getOptions(String[] args) { MavenP2Col e = MavenP2Col.findByStr(key); if (e == null) { - System.err.println("Invalid argument. No entry for " + key); - /* mjl: removed a call to System.exit(-1) here. want to handle gracefully, accept any remaining arguments.*/ + String errormsg = "Invalid argument. No entry for " + key; + System.err.println(errormsg); + throw new MvnP2Exception(errormsg); } else { map.put(e.getColName(), val); @@ -62,12 +64,13 @@ private static Map getOptions(String[] args) { * Precondition: dbi has been initialized and opened. * @param true if succeeded, false if failed */ - private static boolean doAdd(Map map) throws SQLException { + private static void doAdd(Map map) throws SQLException, MvnP2Exception { if (!isValidAdd(map)) { - //TODO caroline change - System.err.println("Invalid input. Must include git repo, branch, " + - "commit and one of maven version and p2 version."); - return false; // a failure + String errormsg = "Invalid input. Must include git repo, branch, " + + "commit and one of maven version and p2 version."; + + System.err.println(errormsg); + throw new MvnP2Exception(errormsg); } else { // check if a matching entry already exists, updating instead of adding if match found @@ -77,7 +80,6 @@ private static boolean doAdd(Map map) throws SQLException { dbi.addRecord(map); } } - return true; } /* @@ -107,13 +109,17 @@ private static boolean doFind(Map map) throws SQLException { * Precondition: dbi has been initialized and opened. * @param map of db column name and input value * @return true if a matching record was found to update, false otherwise or on failure. + * @throws SQLException, MvnP2Exception */ - private static boolean doUpdate(Map map) throws SQLException { - //TODO caroline change + private static boolean doUpdate(Map map) throws SQLException, MvnP2Exception { + if (!isValidAdd(map)) { - System.err.println("Invalid input. Must include git repo, branch, " + - "commit and one of maven version and p2 version."); - return false; + String errormsg = "Invalid input. Must include git repo, branch, " + + "commit and one of maven version and p2 version."; + + System.err.println(errormsg); + + throw new MvnP2Exception(errormsg); } else{ Map mvnMap = filterMap(map, @@ -140,9 +146,7 @@ private static boolean doUpdate(Map map) throws SQLException { dbi.updateRecord(p2Map, map); } } else { - //TODO caroline change? - System.out - .println("Nothing updated in database - user cancellation."); + System.out.println("Nothing updated in database - user cancellation."); } return true; } @@ -174,20 +178,22 @@ private static Map filterMap(Map map, MavenP2Col /** * main method * @param args + * @throws MvnP2Exception */ - public static void main(String[] args) throws SQLException { + public static void main(String[] args) throws SQLException, MvnP2Exception { if (args.length < 1) { - //TODO caroline change - System.err.println("Arguments not found. Must specify one of: " + - "add, find, update."); - System.exit(-1); + String errormsg = "Arguments not found. Must specify one of: " + + "add, find, update."; + System.err.println(errormsg); + throw new MvnP2Exception(errormsg); } Command command = Command.findByStr(args[0]); if (command == null) { - //TODO caroline change - System.err.println("Command not found: " + args[0]); - System.exit(-1); + String errormsg = "Command not found: " + args[0]; + + System.err.println(errormsg); + throw new MvnP2Exception(errormsg); } Map map = getOptions(args); @@ -196,7 +202,7 @@ public static void main(String[] args) throws SQLException { switch (command) { case ADD: - if (!doAdd(map)) System.out.println("Failed to complete add successfully."); + doAdd(map); break; case FIND: if (!doFind(map)) System.out.println("No matching record found in the database, or other database failure."); @@ -207,11 +213,12 @@ public static void main(String[] args) throws SQLException { default: System.err.println("Unexpected command. Should never get here."); - try{ + try { dbi.closeDB(); // done with DBI, close it. - }catch(SQLException e){ - System.err.println("Couldn't close database interface."); - System.exit(-1); + } catch(SQLException e) { + String errormsg = "Couldn't close database interface."; + System.err.println(errormsg); + throw new MvnP2Exception(errormsg); } diff --git a/src/test/java/mavenp2versionmatch/main/test/MvnP2UtilTest.java b/src/test/java/mavenp2versionmatch/main/test/MvnP2UtilTest.java index c46352d..6cc0191 100644 --- a/src/test/java/mavenp2versionmatch/main/test/MvnP2UtilTest.java +++ b/src/test/java/mavenp2versionmatch/main/test/MvnP2UtilTest.java @@ -7,6 +7,7 @@ import static org.junit.Assert.*; import mavenp2versionmatch.db.MavenP2Col; +import mavenp2versionmatch.exception.MvnP2Exception; import mavenp2versionmatch.main.MvnP2Util; public class MvnP2UtilTest { @@ -120,7 +121,7 @@ public void testValidAddMissingArgs() throws Exception { } @Test - public void testValidAddInvalidArgument() throws Exception { + public void testValidAddInvalidArgument() { // test with a made up argument // expected: string will be accepted, but the bad argument will not be included in our Map. String[] args = new String("add -repo dummyrepo -cmt dummycommit -br dummybranch -p2v 0.0-DUMMY -invalidarg abcd").split(" "); @@ -129,18 +130,31 @@ public void testValidAddInvalidArgument() throws Exception { Object goargcontainer[] = new Object[1]; goargcontainer[0] = args; - Object result = getOptions.invoke(util, goargcontainer); - Map m = null; - - if (result instanceof Map) { - m = (Map)result; - } - else { - fail("getOptions returned an unexpected type"); + Object result; + try { + result = getOptions.invoke(util, goargcontainer); + Map m = null; + + if (result instanceof Map) { + m = (Map) result; + } else { + fail("getOptions returned an unexpected type"); + } + + // assertEquals("Should return false, test arguments contain made up argument -invalidarg being accepted", + // false, util.isValidAdd(m)); + assertEquals(numargs, m.size()); + + } catch (InvocationTargetException e) { + if (!(e.getCause() instanceof MvnP2Exception)) { + e.printStackTrace(); + fail("unexpected exception"); + } + + } catch (Exception e) { + e.printStackTrace(); + fail("unexpected exception"); } - - //assertEquals("Should return false, test arguments contain made up argument -invalidarg being accepted", false, util.isValidAdd(m)); - assertEquals(numargs, m.size()); } /* rethink this