diff --git a/MySQL Setup Instructions.txt b/MySQL Setup Instructions.txt index 4bda130..3a32aea 100644 --- a/MySQL Setup Instructions.txt +++ b/MySQL Setup Instructions.txt @@ -1,15 +1,15 @@ Installing MySQL: - download and install MySQL (ensure Connector/J is installed too) - - set the username to 'root' and password to 'Excellence' - if you've already installed MySQL with different credentials, need to set these in db project + - set the username to 'root' and password to 'Excellence' + if you've already installed MySQL with different credentials, need to set these in db project Setting up our database: - open the MySQL workbench (at least, if you're like me and like easy-to-use GUIs!) - create schema 'eclipse' with default collation - - set newly created schema as the default schema + - set newly created schema as the default schema - execute 'create_tables_mysql.sql' script (found in the same folder as this document) Getting it working with our project: - add the J Connector jar file to your system's classpath - - J Connector should have been installed with MySQL; if not, download and extract locally - - instead of adding to classpath, can copy and past jar file to your JRE lib\ext folder (ex. C:\Program Files\Java\jre7\lib\ext) \ No newline at end of file + - J Connector should have been installed with MySQL; if not, download and extract locally + - instead of adding to classpath, can copy and past jar file to your JRE lib\ext folder (ex. C:\Program Files\Java\jre7\lib\ext) \ No newline at end of file diff --git a/create_tables_mysql.sql b/create_tables_mysql.sql index 5d9c1e0..9243a71 100644 --- a/create_tables_mysql.sql +++ b/create_tables_mysql.sql @@ -1,3 +1,5 @@ +DROP TABLE IF EXISTS maven_p2; + CREATE TABLE maven_p2 ( id SERIAL, diff --git a/pom.xml b/pom.xml index b7044b0..924bf87 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,11 @@ 4.8.1 test + + mysql + mysql-connector-java + 5.1.21 + diff --git a/src/main/mavenp2versionmatch/db/MySQLDBI.java b/src/main/mavenp2versionmatch/db/MySQLDBI.java index ec86277..4255dd3 100644 --- a/src/main/mavenp2versionmatch/db/MySQLDBI.java +++ b/src/main/mavenp2versionmatch/db/MySQLDBI.java @@ -1,6 +1,5 @@ package mavenp2versionmatch.db; -import java.io.File; import java.sql.*; import java.util.Iterator; import java.util.List; diff --git a/src/main/mavenp2versionmatch/db/SQLiteDBI.java b/src/main/mavenp2versionmatch/db/SQLiteDBI.java index 863f2a7..e180b34 100644 --- a/src/main/mavenp2versionmatch/db/SQLiteDBI.java +++ b/src/main/mavenp2versionmatch/db/SQLiteDBI.java @@ -19,8 +19,8 @@ public class SQLiteDBI implements DBI{ private static final String DEFAULT_TABLENAME = "maven_p2"; public SQLiteDBI(String dbName, String tableName) throws SQLException { - this.dbName = dbName; - this.tableName = tableName; + SQLiteDBI.dbName = dbName; + SQLiteDBI.tableName = tableName; //make sure driver is loaded try { Class.forName("org.sqlite.JDBC"); @@ -48,7 +48,7 @@ public void closeDB() throws SQLException { conn.close(); } } - //TODO duplicate records are added + public void addRecord(Map colMap) throws SQLException{ if(conn.isClosed()) throw new SQLException("Connection is closed, cannot add record"); diff --git a/src/main/mavenp2versionmatch/main/MvnP2Util.java b/src/main/mavenp2versionmatch/main/MvnP2Util.java index 2912430..9da19f6 100644 --- a/src/main/mavenp2versionmatch/main/MvnP2Util.java +++ b/src/main/mavenp2versionmatch/main/MvnP2Util.java @@ -2,19 +2,14 @@ import java.sql.SQLException; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; - import javax.swing.JOptionPane; - - import mavenp2versionmatch.db.DBI; import mavenp2versionmatch.db.MavenP2Col; import mavenp2versionmatch.db.MavenP2Version; import mavenp2versionmatch.db.MySQLDBI; -import mavenp2versionmatch.db.SQLiteDBI; + public class MvnP2Util { private static DBI dbi; @@ -67,8 +62,9 @@ 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) { + private static boolean doAdd(Map map) throws SQLException { 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 @@ -77,14 +73,9 @@ private static boolean doAdd(Map map) { // check if a matching entry already exists, updating instead of adding if match found if (!doUpdate(map)){ // if no match found, add the new record - try { - dbi.addRecord(map); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } - } + + dbi.addRecord(map); + } } return true; } @@ -93,21 +84,19 @@ private static boolean doAdd(Map map) { * searches a record and prints to standard output. * Precondition: dbi has been initialized and opened. * @param map of db column name and input value - * @return true if find succeeded, false if find failed + * @return true if one or more item is found, false otherwise */ - private static boolean doFind(Map map) { - try { + private static boolean doFind(Map map) throws SQLException { List mpvList = dbi.find(map); for(MavenP2Version v: mpvList) { System.out.println(v); } - } catch (SQLException e) { - e.printStackTrace(); + + if(mpvList.size() > 0) { + return true; + } return false; - } - return true; - } /** @@ -119,39 +108,43 @@ private static boolean doFind(Map map) { * @param map of db column name and input value * @return true if a matching record was found to update, false otherwise or on failure. */ - private static boolean doUpdate(Map map) { + private static boolean doUpdate(Map map) throws SQLException { + //TODO caroline change if (!isValidAdd(map)) { System.err.println("Invalid input. Must include git repo, branch, " + "commit and one of maven version and p2 version."); return false; } else{ - try { - Map mvnMap = filterMap(map, MavenP2Col.MAVEN_VERSION); - Map p2Map = filterMap(map, MavenP2Col.P2_VERSION); - List mvnMatch = dbi.find(mvnMap); - List p2Match = dbi.find(p2Map); - if (mvnMatch.size() > 0 || p2Match.size() > 0){ - System.out.println("Matching record found in the database."); - // matching record found - ask the user if they would like to update - if(JOptionPane.showConfirmDialog(null, "Matching record found in the database.\nUpdate the existing record?") == JOptionPane.OK_OPTION){ - if (mvnMatch.size() > 0){ - for (String key : mvnMap.keySet()) map.remove(key); - dbi.updateRecord(mvnMap, map); - } - else{ - for (String key : p2Map.keySet()) map.remove(key); - dbi.updateRecord(p2Map, map); - } - } - else{ - System.out.println("Nothing updated in database - user cancellation."); + Map mvnMap = filterMap(map, + MavenP2Col.MAVEN_VERSION); + Map p2Map = filterMap(map, MavenP2Col.P2_VERSION); + List mvnMatch = dbi.find(mvnMap); + List p2Match = dbi.find(p2Map); + + if (mvnMatch.size() > 0 || p2Match.size() > 0) { + System.out.println("Matching record found in the database."); + // matching record found - ask the user if they would like to + // update + if (JOptionPane + .showConfirmDialog(null, + "Matching record found in the database." + + "\nUpdate the existing record?") == JOptionPane.OK_OPTION) { + if (mvnMatch.size() > 0) { + for (String key : mvnMap.keySet()) + map.remove(key); + dbi.updateRecord(mvnMap, map); + } else { + for (String key : p2Map.keySet()) + map.remove(key); + dbi.updateRecord(p2Map, map); } - return true; + } else { + //TODO caroline change? + System.out + .println("Nothing updated in database - user cancellation."); } - } catch (SQLException e) { - e.printStackTrace(); - return false; + return true; } } return false; @@ -182,8 +175,9 @@ private static Map filterMap(Map map, MavenP2Col * main method * @param args */ - public static void main(String[] args) { + public static void main(String[] args) throws SQLException { if (args.length < 1) { + //TODO caroline change System.err.println("Arguments not found. Must specify one of: " + "add, find, update."); System.exit(-1); @@ -191,18 +185,14 @@ public static void main(String[] args) { Command command = Command.findByStr(args[0]); if (command == null) { + //TODO caroline change System.err.println("Command not found: " + args[0]); System.exit(-1); } Map map = getOptions(args); - - try{ - dbi = new MySQLDBI(); // initialize database interface. - dbi.openDB(); - }catch(SQLException e){ - System.err.println("Couldn't initialize database interface."); - System.exit(-1); - } + + dbi = new MySQLDBI(); // initialize database interface. + dbi.openDB(); switch (command) { case ADD: diff --git a/src/test/java/mavenp2versionmatch/db/test/MavenP2ColTest.java b/src/test/java/mavenp2versionmatch/db/test/MavenP2ColTest.java index 3b048b3..cbba7bd 100644 --- a/src/test/java/mavenp2versionmatch/db/test/MavenP2ColTest.java +++ b/src/test/java/mavenp2versionmatch/db/test/MavenP2ColTest.java @@ -7,7 +7,7 @@ public class MavenP2ColTest { @Test - public void testFindByStrKnown() throws Exception{ + public void testFindByStrKnown() { // test argument names ArgColPair[] args = { new ArgColPair("-repo", "git_repo"), @@ -25,7 +25,7 @@ public void testFindByStrKnown() throws Exception{ } @Test - public void testFindByStrUnknown() throws Exception{ + public void testFindByStrUnknown() { // test for failure when passing in unknown argument names. MavenP2Col col = MavenP2Col.findByStr("-notarg"); assertEquals(null, col); diff --git a/src/test/java/mavenp2versionmatch/db/test/MvnP2UtilSqliteTest.java b/src/test/java/mavenp2versionmatch/db/test/MvnP2UtilSqliteTest.java index 1fa1980..5dd8fad 100644 --- a/src/test/java/mavenp2versionmatch/db/test/MvnP2UtilSqliteTest.java +++ b/src/test/java/mavenp2versionmatch/db/test/MvnP2UtilSqliteTest.java @@ -5,7 +5,6 @@ import java.util.HashMap; import java.sql.*; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import org.junit.*; @@ -48,7 +47,7 @@ public static void tearDown() throws SQLException { /* note: use different filenames for each test's database to avoid any unexpected interactions between different unit tests. */ @Test - public void testSqliteDoAddAndFind() throws SQLException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{ + public void testSqliteDoAddAndFind() throws Exception { MapmAdd = new HashMap(); mAdd.put("git_repo", "GITREPO"); @@ -58,8 +57,6 @@ public void testSqliteDoAddAndFind() throws SQLException, IllegalArgumentExcepti Object args[] = new Object[1]; args[0] = mAdd; - - Integer oldsize = dbi.findAll().size(); doAdd.invoke(util, args); @@ -68,7 +65,9 @@ public void testSqliteDoAddAndFind() throws SQLException, IllegalArgumentExcepti List mpvList = dbi.find(mFind); - assertEquals("added one entry to database, should contain " + oldsize + 1 + "instead, contains " + mpvList.size(), mpvList.size(), oldsize + 1); + assertEquals("added one entry to database, " + + "there should be one matching, but there is " + + mpvList.size(), mpvList.size(), 1); MavenP2Version mpv = mpvList.get(0); assertEquals(mpv.getGitRepo(), "GITREPO"); assertEquals(mpv.getGitCommit(), "GITCOMMIT"); diff --git a/src/test/java/mavenp2versionmatch/main/test/MvnP2UtilTest.java b/src/test/java/mavenp2versionmatch/main/test/MvnP2UtilTest.java index 2c7867d..c46352d 100644 --- a/src/test/java/mavenp2versionmatch/main/test/MvnP2UtilTest.java +++ b/src/test/java/mavenp2versionmatch/main/test/MvnP2UtilTest.java @@ -21,7 +21,7 @@ public class MvnP2UtilTest { * what if you pass something like -project -git_commit asdf * */ @BeforeClass - public static void setup() throws SecurityException, NoSuchMethodException { + public static void setup() throws Exception { util = new MvnP2Util(); goParams = new Class[1]; @@ -65,7 +65,7 @@ public void testValidAddMaven() throws Exception { } @Test - public void testValidAddP2() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ + public void testValidAddP2() throws Exception { // test with a valid dummy string for a p2 add String[] args = new String("add -repo dummyrepo -cmt dummycommit -br dummybranch -p2v 0.0-DUMMY").split(" "); int numargs = (args.length - 1) / 2; @@ -93,7 +93,7 @@ public void testValidAddP2() throws IllegalArgumentException, IllegalAccessExcep } @Test - public void testValidAddMissingArgs() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ + public void testValidAddMissingArgs() throws Exception { // test with an invalid dummy string, missing version info String[] args = new String("add -repo dummyrepo -cmt dummycommit -br dummybranch").split(" "); int numargs = (args.length - 1) / 2; @@ -120,7 +120,7 @@ public void testValidAddMissingArgs() throws IllegalArgumentException, IllegalAc } @Test - public void testValidAddInvalidArgument() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ + public void testValidAddInvalidArgument() throws Exception { // 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(" "); @@ -158,7 +158,7 @@ public void testValidAddMissingValue(){ */ @Test - public void testValidAddSameArgumentTwice() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ + public void testValidAddSameArgumentTwice() throws Exception { // Specify the same argument twice // Expected: receive the last specified argument. // Maybe we can warn the user that the same argument was passed twice.