Skip to content

Commit

Permalink
closes #7 - added MySQL driver to dependencies and changed exception …
Browse files Browse the repository at this point in the history
…flow so that the main method throws exceptions. This will allow the maven plugin to fail gracefully.
  • Loading branch information
CarolineM committed Nov 13, 2012
1 parent e8a8a26 commit a77d63b
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 78 deletions.
10 changes: 5 additions & 5 deletions MySQL Setup Instructions.txt
Original file line number Diff line number Diff line change
@@ -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)
- 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)
2 changes: 2 additions & 0 deletions create_tables_mysql.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DROP TABLE IF EXISTS maven_p2;

CREATE TABLE maven_p2
(
id SERIAL,
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
1 change: 0 additions & 1 deletion src/main/mavenp2versionmatch/db/MySQLDBI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mavenp2versionmatch.db;

import java.io.File;
import java.sql.*;
import java.util.Iterator;
import java.util.List;
Expand Down
6 changes: 3 additions & 3 deletions src/main/mavenp2versionmatch/db/SQLiteDBI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -48,7 +48,7 @@ public void closeDB() throws SQLException {
conn.close();
}
}
//TODO duplicate records are added

public void addRecord(Map<String, String> colMap) throws SQLException{
if(conn.isClosed())
throw new SQLException("Connection is closed, cannot add record");
Expand Down
104 changes: 47 additions & 57 deletions src/main/mavenp2versionmatch/main/MvnP2Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,8 +62,9 @@ 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) {
private static boolean doAdd(Map<String, String> 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
Expand All @@ -77,14 +73,9 @@ private static boolean doAdd(Map<String, String> 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;
}
Expand All @@ -93,21 +84,19 @@ private static boolean doAdd(Map<String, String> 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<String, String> map) {
try {
private static boolean doFind(Map<String, String> map) throws SQLException {
List<MavenP2Version> 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;

}

/**
Expand All @@ -119,39 +108,43 @@ private static boolean doFind(Map<String, String> 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<String, String> map) {
private static boolean doUpdate(Map<String, String> 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<String, String> mvnMap = filterMap(map, MavenP2Col.MAVEN_VERSION);
Map<String, String> p2Map = filterMap(map, MavenP2Col.P2_VERSION);
List<MavenP2Version> mvnMatch = dbi.find(mvnMap);
List<MavenP2Version> 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<String, String> mvnMap = filterMap(map,
MavenP2Col.MAVEN_VERSION);
Map<String, String> p2Map = filterMap(map, MavenP2Col.P2_VERSION);
List<MavenP2Version> mvnMatch = dbi.find(mvnMap);
List<MavenP2Version> 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;
Expand Down Expand Up @@ -182,27 +175,24 @@ private static Map<String, String> filterMap(Map<String, String> 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);
}

Command command = Command.findByStr(args[0]);
if (command == null) {
//TODO caroline change
System.err.println("Command not found: " + args[0]);
System.exit(-1);
}
Map<String, String> 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:
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/mavenp2versionmatch/db/test/MavenP2ColTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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 {

Map<String, String>mAdd = new HashMap<String, String>();
mAdd.put("git_repo", "GITREPO");
Expand All @@ -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);

Expand All @@ -68,7 +65,9 @@ public void testSqliteDoAddAndFind() throws SQLException, IllegalArgumentExcepti

List<MavenP2Version> 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");
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/mavenp2versionmatch/main/test/MvnP2UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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(" ");
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a77d63b

Please sign in to comment.