Skip to content

Commit

Permalink
closes #2 - removed system.exit calls and throw exceptions instead. s…
Browse files Browse the repository at this point in the history
…till print error message to system.error.
  • Loading branch information
CarolineM committed Nov 13, 2012
1 parent a77d63b commit c09a1d8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/main/mavenp2versionmatch/db/SQLiteDBI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions src/main/mavenp2versionmatch/exception/MvnP2Exception.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
67 changes: 37 additions & 30 deletions src/main/mavenp2versionmatch/main/MvnP2Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import mavenp2versionmatch.db.MavenP2Col;
import mavenp2versionmatch.db.MavenP2Version;
import mavenp2versionmatch.db.MySQLDBI;
import mavenp2versionmatch.exception.MvnP2Exception;


public class MvnP2Util {
Expand All @@ -32,7 +33,7 @@ private static boolean isValidAdd(Map<String, String> map) {
* @param args command line input to main
* @return map of db column name and input value
*/
private static Map<String, String> getOptions(String[] args) {
private static Map<String, String> getOptions(String[] args) throws MvnP2Exception {
Map<String, String> map = new HashMap<String, String>();

for (int i = 1; i< args.length; i++) {
Expand All @@ -41,8 +42,9 @@ private static Map<String, String> 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);
Expand All @@ -62,12 +64,13 @@ private static Map<String, String> getOptions(String[] args) {
* Precondition: dbi has been initialized and opened.
* @param true if succeeded, false if failed
*/
private static boolean doAdd(Map<String, String> map) throws SQLException {
private static void doAdd(Map<String, String> 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
Expand All @@ -77,7 +80,6 @@ private static boolean doAdd(Map<String, String> map) throws SQLException {
dbi.addRecord(map);
}
}
return true;
}

/*
Expand Down Expand Up @@ -107,13 +109,17 @@ private static boolean doFind(Map<String, String> 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<String, String> map) throws SQLException {
//TODO caroline change
private static boolean doUpdate(Map<String, String> 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<String, String> mvnMap = filterMap(map,
Expand All @@ -140,9 +146,7 @@ private static boolean doUpdate(Map<String, String> 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;
}
Expand Down Expand Up @@ -174,20 +178,22 @@ private static Map<String, String> filterMap(Map<String, String> 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<String, String> map = getOptions(args);

Expand All @@ -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.");
Expand All @@ -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);
}


Expand Down
38 changes: 26 additions & 12 deletions src/test/java/mavenp2versionmatch/main/test/MvnP2UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.junit.Assert.*;

import mavenp2versionmatch.db.MavenP2Col;
import mavenp2versionmatch.exception.MvnP2Exception;
import mavenp2versionmatch.main.MvnP2Util;

public class MvnP2UtilTest {
Expand Down Expand Up @@ -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(" ");
Expand All @@ -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
Expand Down

0 comments on commit c09a1d8

Please sign in to comment.