Do not catch exceptions in tests; add database groups

The groups let us enable / disable tests for a specific db,
depending on which one is available on the machine.
This commit is contained in:
Antonin Delpeuch 2018-01-27 16:20:07 +00:00
parent 197dc47f37
commit b2b9a4bc9a
16 changed files with 474 additions and 769 deletions

View File

@ -50,8 +50,11 @@ public class DBExtensionTestUtils {
* Create Test Table with one row of Data * Create Test Table with one row of Data
* @param dbConfig DatabaseConfiguration to test * @param dbConfig DatabaseConfiguration to test
* @param tableName * @param tableName
* @throws DatabaseServiceException
* @throws SQLException
*/ */
public static void initTestData(DatabaseConfiguration dbConfig, String tableName) { public static void initTestData(DatabaseConfiguration dbConfig, String tableName)
throws DatabaseServiceException, SQLException {
Statement stmt = null; Statement stmt = null;
Connection conn = null; Connection conn = null;
@ -87,16 +90,11 @@ public class DBExtensionTestUtils {
logger.info("Database Test Init Data Created!!!"); logger.info("Database Test Init Data Created!!!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally { } finally {
if(stmt != null) { if(stmt != null) {
try { try {
stmt.close(); stmt.close();
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -104,7 +102,6 @@ public class DBExtensionTestUtils {
try { try {
conn.close(); conn.close();
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -113,7 +110,8 @@ public class DBExtensionTestUtils {
} }
public static void initTestData(DatabaseConfiguration dbConfig) { public static void initTestData(DatabaseConfiguration dbConfig)
throws DatabaseServiceException, SQLException {
Statement stmt = null; Statement stmt = null;
Connection conn = null; Connection conn = null;
@ -148,11 +146,7 @@ public class DBExtensionTestUtils {
// System.out.println("Insert Data Result::" + insertResult); // System.out.println("Insert Data Result::" + insertResult);
logger.info("Database Test Init Data Created!!!"); logger.info("Database Test Init Data Created!!!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally { } finally {
if(stmt != null) { if(stmt != null) {
try { try {
@ -179,8 +173,11 @@ public class DBExtensionTestUtils {
* Table name: test_data * Table name: test_data
* @param sampleSize * @param sampleSize
* @param batchSize * @param batchSize
* @throws DatabaseServiceException
* @throws SQLException
*/ */
public void generateMySQLTestData(int sampleSize, int batchSize) { public void generateMySQLTestData(int sampleSize, int batchSize)
throws DatabaseServiceException, SQLException {
mncMap = new HashMap<Integer, Integer>(); mncMap = new HashMap<Integer, Integer>();
mccMap = new HashMap<Integer, Integer>(); mccMap = new HashMap<Integer, Integer>();
mccMap.put(0, 302); mccMap.put(0, 302);
@ -208,65 +205,56 @@ public class DBExtensionTestUtils {
+ "id, ue_id, start_time, end_date, bytes_upload, bytes_download, cell_id, mcc, mnc, lac, imei)" + "id, ue_id, start_time, end_date, bytes_upload, bytes_download, cell_id, mcc, mnc, lac, imei)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
Connection conn; Connection conn = DatabaseService.get(MYSQL_DB_NAME).getConnection(dc);
try {
conn = DatabaseService.get(MYSQL_DB_NAME).getConnection(dc);
Statement truncateStmt = conn.createStatement(); Statement truncateStmt = conn.createStatement();
int result = truncateStmt.executeUpdate(truncateTableSQL); int result = truncateStmt.executeUpdate(truncateTableSQL);
System.out.println("Truncate Table Result::" + result); System.out.println("Truncate Table Result::" + result);
truncateStmt.close(); truncateStmt.close();
conn.setAutoCommit(false); conn.setAutoCommit(false);
PreparedStatement stmt = conn.prepareStatement(insertTableSQL); PreparedStatement stmt = conn.prepareStatement(insertTableSQL);
int counter=1; int counter=1;
for (int i = 0; i < sampleSize; i++) { for (int i = 0; i < sampleSize; i++) {
stmt.setLong(1, i); stmt.setLong(1, i);
stmt.setString(2, getNextUeId()); stmt.setString(2, getNextUeId());
stmt.setDate(3, getNextStartDate()); stmt.setDate(3, getNextStartDate());
stmt.setDate(4, getNextEndDate()); stmt.setDate(4, getNextEndDate());
stmt.setInt(5, rand.nextInt()); stmt.setInt(5, rand.nextInt());
stmt.setInt(6, rand.nextInt()); stmt.setInt(6, rand.nextInt());
stmt.setInt(7, rand.nextInt(10)); stmt.setInt(7, rand.nextInt(10));
stmt.setInt(8, getMCC()); stmt.setInt(8, getMCC());
stmt.setInt(9, getMNC()); stmt.setInt(9, getMNC());
stmt.setInt(10, rand.nextInt(100)); stmt.setInt(10, rand.nextInt(100));
stmt.setString(11, getNextIMEI()); stmt.setString(11, getNextIMEI());
stmt.addBatch();
//Execute batch of 1000 records
if(i%batchSize==0){
stmt.executeBatch();
conn.commit();
System.out.println("Batch "+(counter++)+" executed successfully");
}
}
//execute final batch
stmt.executeBatch();
System.out.println("Final Batch Executed "+(counter++)+" executed successfully");
conn.commit();
conn.close();
} catch (DatabaseServiceException e) { stmt.addBatch();
// TODO Auto-generated catch block
e.printStackTrace(); //Execute batch of 1000 records
} catch (SQLException e) { if(i%batchSize==0){
// TODO Auto-generated catch block stmt.executeBatch();
e.printStackTrace(); conn.commit();
System.out.println("Batch "+(counter++)+" executed successfully");
}
} }
//execute final batch
stmt.executeBatch();
System.out.println("Final Batch Executed "+(counter++)+" executed successfully");
conn.commit();
conn.close();
} }
/** /**
* *
* @param sampleSize * @param sampleSize
* @param batchSize * @param batchSize
* @throws DatabaseServiceException
* @throws SQLException
*/ */
public void generatePgSQLTestData(int sampleSize, int batchSize) { public void generatePgSQLTestData(int sampleSize, int batchSize) throws DatabaseServiceException, SQLException {
mncMap = new HashMap<Integer, Integer>(); mncMap = new HashMap<Integer, Integer>();
mccMap = new HashMap<Integer, Integer>(); mccMap = new HashMap<Integer, Integer>();
mccMap.put(0, 302); mccMap.put(0, 302);
@ -294,56 +282,46 @@ public class DBExtensionTestUtils {
+ "id, ue_id, start_time, end_date, bytes_upload, bytes_download, cell_id, mcc, mnc, lac, imei)" + "id, ue_id, start_time, end_date, bytes_upload, bytes_download, cell_id, mcc, mnc, lac, imei)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
Connection conn; Connection conn = DatabaseService.get(PGSQL_DB_NAME).getConnection(dc);
try {
conn = DatabaseService.get(PGSQL_DB_NAME).getConnection(dc);
Statement truncateStmt = conn.createStatement(); Statement truncateStmt = conn.createStatement();
int result = truncateStmt.executeUpdate(truncateTableSQL); int result = truncateStmt.executeUpdate(truncateTableSQL);
System.out.println("Truncate Table Result::" + result); System.out.println("Truncate Table Result::" + result);
truncateStmt.close(); truncateStmt.close();
conn.setAutoCommit(false); conn.setAutoCommit(false);
PreparedStatement stmt = conn.prepareStatement(insertTableSQL); PreparedStatement stmt = conn.prepareStatement(insertTableSQL);
int counter=1; int counter=1;
for (int i = 0; i < sampleSize; i++) { for (int i = 0; i < sampleSize; i++) {
stmt.setLong(1, i); stmt.setLong(1, i);
stmt.setString(2, getNextUeId()); stmt.setString(2, getNextUeId());
stmt.setDate(3, getNextStartDate()); stmt.setDate(3, getNextStartDate());
stmt.setDate(4, getNextEndDate()); stmt.setDate(4, getNextEndDate());
stmt.setInt(5, rand.nextInt()); stmt.setInt(5, rand.nextInt());
stmt.setInt(6, rand.nextInt()); stmt.setInt(6, rand.nextInt());
stmt.setInt(7, rand.nextInt(10)); stmt.setInt(7, rand.nextInt(10));
stmt.setInt(8, getMCC()); stmt.setInt(8, getMCC());
stmt.setInt(9, getMNC()); stmt.setInt(9, getMNC());
stmt.setInt(10, rand.nextInt(100)); stmt.setInt(10, rand.nextInt(100));
stmt.setString(11, getNextIMEI()); stmt.setString(11, getNextIMEI());
stmt.addBatch();
//Execute batch of 1000 records
if(i%batchSize==0){
stmt.executeBatch();
conn.commit();
System.out.println("Batch "+(counter++)+" executed successfully");
}
}
//execute final batch
stmt.executeBatch();
System.out.println("Final Batch Executed "+(counter++)+" executed successfully");
conn.commit();
conn.close();
} catch (DatabaseServiceException e) { stmt.addBatch();
// TODO Auto-generated catch block
e.printStackTrace(); //Execute batch of 1000 records
} catch (SQLException e) { if(i%batchSize==0){
// TODO Auto-generated catch block stmt.executeBatch();
e.printStackTrace(); conn.commit();
System.out.println("Batch "+(counter++)+" executed successfully");
}
} }
//execute final batch
stmt.executeBatch();
System.out.println("Final Batch Executed "+(counter++)+" executed successfully");
conn.commit();
conn.close();
} }
@ -381,7 +359,7 @@ public class DBExtensionTestUtils {
return "" + n; return "" + n;
} }
public static void main(String[] args) { public static void main(String[] args) throws DatabaseServiceException, SQLException {
DBExtensionTestUtils testUtil = new DBExtensionTestUtils(); DBExtensionTestUtils testUtil = new DBExtensionTestUtils();
testUtil.generatePgSQLTestData(SAMPLE_SIZE, BATCH_SIZE); testUtil.generatePgSQLTestData(SAMPLE_SIZE, BATCH_SIZE);
// testUtil.generateMySQLTestData(); // testUtil.generateMySQLTestData();
@ -404,7 +382,7 @@ public class DBExtensionTestUtils {
//System.out.println("Drop Table Result::" + dropResult); //System.out.println("Drop Table Result::" + dropResult);
} }
logger.info("Database Test Cleanup Done!!!"); logger.info("Database Test Cleanup Done");
} catch (DatabaseServiceException e) { } catch (DatabaseServiceException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block

View File

@ -33,6 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.extension.database; package com.google.refine.extension.database;
import java.util.Properties;
import org.slf4j.Logger; import org.slf4j.Logger;
public class DBExtensionTests { public class DBExtensionTests {
@ -60,14 +62,9 @@ public class DBExtensionTests {
protected final String DEFAULT_MARIADB_NAME = "testdb"; protected final String DEFAULT_MARIADB_NAME = "testdb";
protected final String DEFAULT_TEST_TABLE = "test_data"; protected final String DEFAULT_TEST_TABLE = "test_data";
protected Properties properties;
protected Logger logger; protected Logger logger;
// @BeforeSuite
// public void init() {
// System.out.println("Log4j init...");
// System.setProperty("log4j.configuration", "log4j-test.properties");
// }
} }

View File

@ -8,6 +8,7 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -35,7 +36,7 @@ import com.google.refine.model.Project;
@Test(groups = { "requiresMySQL" })
public class DatabaseImportControllerTest extends DBExtensionTests{ public class DatabaseImportControllerTest extends DBExtensionTests{
@Mock @Mock
@ -118,141 +119,114 @@ public class DatabaseImportControllerTest extends DBExtensionTests{
} }
@Test @Test
public void testDoPostInvalidSubCommand() { public void testDoPostInvalidSubCommand() throws IOException, ServletException, JSONException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try { when(request.getQueryString()).thenReturn(
when(request.getQueryString()).thenReturn( "http://127.0.0.1:3333/command/core/importing-controller?controller=database/database-import-controller&subCommand=invalid-sub-command");
"http://127.0.0.1:3333/command/core/importing-controller?controller=database/database-import-controller&subCommand=invalid-sub-command");
when(response.getWriter()).thenReturn(pw);
//test
SUT.doPost(request, response);
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
String code = json.getString("status");
String message = json.getString("message");
Assert.assertNotNull(code);
Assert.assertNotNull(message);
Assert.assertEquals(code, "error");
Assert.assertEquals(message, "No such sub command");
} catch (Exception e) { when(response.getWriter()).thenReturn(pw);
// TODO Auto-generated catch block //test
e.printStackTrace(); SUT.doPost(request, response);
}
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
String code = json.getString("status");
String message = json.getString("message");
Assert.assertNotNull(code);
Assert.assertNotNull(message);
Assert.assertEquals(code, "error");
Assert.assertEquals(message, "No such sub command");
} }
@Test @Test
public void testDoPostInitializeParser() { public void testDoPostInitializeParser() throws ServletException, IOException, JSONException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try { when(request.getQueryString()).thenReturn(
when(request.getQueryString()).thenReturn( "http://127.0.0.1:3333/command/core/importing-controller?controller=database/database-import-controller&subCommand=initialize-parser-ui");
"http://127.0.0.1:3333/command/core/importing-controller?controller=database/database-import-controller&subCommand=initialize-parser-ui"); when(response.getWriter()).thenReturn(pw);
when(response.getWriter()).thenReturn(pw);
SUT.doPost(request, response);
SUT.doPost(request, response);
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
String status = json.getString("status"); String result = sw.getBuffer().toString().trim();
//System.out.println("json::" + json); JSONObject json = new JSONObject(result);
Assert.assertEquals(status, "ok");
} catch (Exception e) { String status = json.getString("status");
// TODO Auto-generated catch block //System.out.println("json::" + json);
e.printStackTrace(); Assert.assertEquals(status, "ok");
}
} }
@Test @Test
public void testDoPostParsePreview() { public void testDoPostParsePreview() throws IOException, ServletException, JSONException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try {
long jobId = job.id; long jobId = job.id;
when(request.getQueryString()).thenReturn( when(request.getQueryString()).thenReturn(
"http://127.0.0.1:3333/command/core/importing-controller?controller=database%2Fdatabase-import-controller&jobID=" "http://127.0.0.1:3333/command/core/importing-controller?controller=database%2Fdatabase-import-controller&jobID="
+ jobId + "&subCommand=parse-preview"); + jobId + "&subCommand=parse-preview");
when(response.getWriter()).thenReturn(pw); when(response.getWriter()).thenReturn(pw);
when(request.getParameter("databaseType")).thenReturn(testDbConfig.getDatabaseType()); when(request.getParameter("databaseType")).thenReturn(testDbConfig.getDatabaseType());
when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost()); when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost());
when(request.getParameter("databasePort")).thenReturn("" + testDbConfig.getDatabasePort()); when(request.getParameter("databasePort")).thenReturn("" + testDbConfig.getDatabasePort());
when(request.getParameter("databaseUser")).thenReturn(testDbConfig.getDatabaseUser()); when(request.getParameter("databaseUser")).thenReturn(testDbConfig.getDatabaseUser());
when(request.getParameter("databasePassword")).thenReturn(testDbConfig.getDatabasePassword()); when(request.getParameter("databasePassword")).thenReturn(testDbConfig.getDatabasePassword());
when(request.getParameter("initialDatabase")).thenReturn(testDbConfig.getDatabaseName()); when(request.getParameter("initialDatabase")).thenReturn(testDbConfig.getDatabaseName());
when(request.getParameter("query")).thenReturn(query); when(request.getParameter("query")).thenReturn(query);
when(request.getParameter("options")).thenReturn(JSON_OPTION); when(request.getParameter("options")).thenReturn(JSON_OPTION);
SUT.doPost(request, response);
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
String status = json.getString("status");
//System.out.println("json::" + json);
Assert.assertEquals(status, "ok");
} catch (Exception e) { SUT.doPost(request, response);
// TODO Auto-generated catch block
e.printStackTrace(); String result = sw.getBuffer().toString().trim();
} JSONObject json = new JSONObject(result);
String status = json.getString("status");
//System.out.println("json::" + json);
Assert.assertEquals(status, "ok");
} }
@Test @Test
public void testDoPostCreateProject() { public void testDoPostCreateProject() throws IOException, ServletException, JSONException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try {
long jobId = job.id; long jobId = job.id;
when(request.getQueryString()).thenReturn( when(request.getQueryString()).thenReturn(
"http://127.0.0.1:3333/command/core/importing-controller?controller=database%2Fdatabase-import-controller&jobID=" "http://127.0.0.1:3333/command/core/importing-controller?controller=database%2Fdatabase-import-controller&jobID="
+ jobId + "&subCommand=create-project"); + jobId + "&subCommand=create-project");
when(response.getWriter()).thenReturn(pw); when(response.getWriter()).thenReturn(pw);
when(request.getParameter("databaseType")).thenReturn(testDbConfig.getDatabaseType()); when(request.getParameter("databaseType")).thenReturn(testDbConfig.getDatabaseType());
when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost()); when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost());
when(request.getParameter("databasePort")).thenReturn("" + testDbConfig.getDatabasePort()); when(request.getParameter("databasePort")).thenReturn("" + testDbConfig.getDatabasePort());
when(request.getParameter("databaseUser")).thenReturn(testDbConfig.getDatabaseUser()); when(request.getParameter("databaseUser")).thenReturn(testDbConfig.getDatabaseUser());
when(request.getParameter("databasePassword")).thenReturn(testDbConfig.getDatabasePassword()); when(request.getParameter("databasePassword")).thenReturn(testDbConfig.getDatabasePassword());
when(request.getParameter("initialDatabase")).thenReturn(testDbConfig.getDatabaseName()); when(request.getParameter("initialDatabase")).thenReturn(testDbConfig.getDatabaseName());
when(request.getParameter("query")).thenReturn(query); when(request.getParameter("query")).thenReturn(query);
when(request.getParameter("options")).thenReturn(JSON_OPTION); when(request.getParameter("options")).thenReturn(JSON_OPTION);
SUT.doPost(request, response);
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
String status = json.getString("status"); SUT.doPost(request, response);
//System.out.println("json::" + json);
Assert.assertEquals(status, "ok"); String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
} catch (Exception e) { String status = json.getString("status");
// TODO Auto-generated catch block //System.out.println("json::" + json);
e.printStackTrace(); Assert.assertEquals(status, "ok");
}
} }
@BeforeTest @BeforeTest

View File

@ -62,16 +62,14 @@ public class DatabaseServiceTest extends DBExtensionTests{
} }
@Test(groups = {"requiresPgSQL"})
@Test
public void testGetPgSQLDBService() { public void testGetPgSQLDBService() {
DatabaseService dbService = DatabaseService.get(PgSQLDatabaseService.DB_NAME); DatabaseService dbService = DatabaseService.get(PgSQLDatabaseService.DB_NAME);
Assert.assertNotNull(dbService); Assert.assertNotNull(dbService);
Assert.assertEquals(dbService.getClass(), PgSQLDatabaseService.class); Assert.assertEquals(dbService.getClass(), PgSQLDatabaseService.class);
} }
@Test @Test(groups = {"requiresMySQL"})
public void testGetMySQLDBService() { public void testGetMySQLDBService() {
DatabaseService dbService = DatabaseService.get(MySQLDatabaseService.DB_NAME); DatabaseService dbService = DatabaseService.get(MySQLDatabaseService.DB_NAME);
@ -79,7 +77,7 @@ public class DatabaseServiceTest extends DBExtensionTests{
Assert.assertEquals(dbService.getClass(), MySQLDatabaseService.class); Assert.assertEquals(dbService.getClass(), MySQLDatabaseService.class);
} }
@Test @Test(groups = {"requiresMariaDB"})
public void testGetMariaDBSQLDBService() { public void testGetMariaDBSQLDBService() {
DatabaseService dbService = DatabaseService.get(MariaDBDatabaseService.DB_NAME); DatabaseService dbService = DatabaseService.get(MariaDBDatabaseService.DB_NAME);
@ -87,64 +85,34 @@ public class DatabaseServiceTest extends DBExtensionTests{
Assert.assertEquals(dbService.getClass(), MariaDBDatabaseService.class); Assert.assertEquals(dbService.getClass(), MariaDBDatabaseService.class);
} }
@Test @Test(groups = {"requiresMySQL"})
public void testGetConnection() { public void testGetConnection() throws DatabaseServiceException {
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType());
try { Connection conn = dbService.getConnection(testDbConfig);
Assert.assertNotNull(conn);
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType());
Connection conn = dbService.getConnection(testDbConfig);
Assert.assertNotNull(conn);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test(groups = {"requiresMySQL"})
public void testTestConnection() { public void testTestConnection() throws DatabaseServiceException {
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType());
try { boolean result = dbService.testConnection(testDbConfig);
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType()); Assert.assertEquals(result, true);
boolean result = dbService.testConnection(testDbConfig);
Assert.assertEquals(result, true);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test(groups = {"requiresMySQL"})
public void testConnect() { public void testConnect() throws DatabaseServiceException {
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType());
try { DatabaseInfo databaseInfo = dbService.connect(testDbConfig);
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType()); Assert.assertNotNull(databaseInfo);
DatabaseInfo databaseInfo = dbService.connect(testDbConfig);
Assert.assertNotNull(databaseInfo);
} catch (DatabaseServiceException e) {
e.printStackTrace();
}
} }
@Test @Test(groups = {"requiresMySQL"})
public void testExecuteQuery() { public void testExecuteQuery() throws DatabaseServiceException {
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType());
DatabaseInfo databaseInfo = dbService.testQuery(testDbConfig,
"SELECT * FROM " + testTable);
try { Assert.assertNotNull(databaseInfo);
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType());
DatabaseInfo databaseInfo = dbService.testQuery(testDbConfig,
"SELECT * FROM " + testTable);
Assert.assertNotNull(databaseInfo);
} catch (DatabaseServiceException e) {
e.printStackTrace();
}
} }
@Test @Test
@ -157,44 +125,26 @@ public class DatabaseServiceTest extends DBExtensionTests{
Assert.assertEquals(limitQuery, "SELECT * FROM " + testTable + " LIMIT " + 100 + " OFFSET " + 0 + ";"); Assert.assertEquals(limitQuery, "SELECT * FROM " + testTable + " LIMIT " + 100 + " OFFSET " + 0 + ";");
} }
@Test @Test(groups = {"requiresMySQL"})
public void testGetColumns() { public void testGetColumns() throws DatabaseServiceException {
List<DatabaseColumn> dbColumns; List<DatabaseColumn> dbColumns;
try { DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType());
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType()); dbColumns = dbService.getColumns(testDbConfig,"SELECT * FROM " + testTable);
dbColumns = dbService.getColumns(testDbConfig,"SELECT * FROM " + testTable); Assert.assertNotNull(dbColumns);
Assert.assertNotNull(dbColumns);
int cols = dbColumns.size();
Assert.assertEquals(cols, 10);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int cols = dbColumns.size();
Assert.assertEquals(cols, 10);
} }
@Test @Test(groups = {"requiresMySQL"})
public void testGetRows() { public void testGetRows() throws DatabaseServiceException {
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType());
List<DatabaseRow> dbRows = dbService.getRows(testDbConfig,
"SELECT * FROM " + testTable);
try { Assert.assertNotNull(dbRows);
Assert.assertEquals(dbRows.size(), 1);
DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType());
List<DatabaseRow> dbRows = dbService.getRows(testDbConfig,
"SELECT * FROM " + testTable);
Assert.assertNotNull(dbRows);
Assert.assertEquals(dbRows.size(), 1);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
} }

View File

@ -1,5 +1,7 @@
package com.google.refine.extension.database; package com.google.refine.extension.database;
import java.sql.SQLException;
import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Optional; import org.testng.annotations.Optional;
@ -30,7 +32,8 @@ public class DatabaseTestConfig extends DBExtensionTests {
@Optional(DEFAULT_MARIADB_NAME) String mariadbDbName, @Optional(DEFAULT_MARIADB_HOST) String mariadbDbHost, @Optional(DEFAULT_MARIADB_NAME) String mariadbDbName, @Optional(DEFAULT_MARIADB_HOST) String mariadbDbHost,
@Optional(DEFAULT_MARIADB_PORT) String mariadbDbPort, @Optional(DEFAULT_MARIADB_USER) String mariadbyDbUser, @Optional(DEFAULT_MARIADB_PORT) String mariadbDbPort, @Optional(DEFAULT_MARIADB_USER) String mariadbyDbUser,
@Optional(DEFAULT_MARIADB_PASSWORD) String mariadbDbPassword, @Optional(DEFAULT_TEST_TABLE) String mariadbTestTable) { @Optional(DEFAULT_MARIADB_PASSWORD) String mariadbDbPassword, @Optional(DEFAULT_TEST_TABLE) String mariadbTestTable)
throws DatabaseServiceException, SQLException {
//System.out.println("@BeforeSuite\n"); //System.out.println("@BeforeSuite\n");
mysqlDbConfig = new DatabaseConfiguration(); mysqlDbConfig = new DatabaseConfiguration();

View File

@ -3,12 +3,15 @@ package com.google.refine.extension.database.cmd;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@ -24,6 +27,7 @@ import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.mysql.MySQLDatabaseService; import com.google.refine.extension.database.mysql.MySQLDatabaseService;
@Test(groups = { "requiresMySQL" })
public class ConnectCommandTest extends DBExtensionTests { public class ConnectCommandTest extends DBExtensionTests {
@ -63,7 +67,7 @@ public class ConnectCommandTest extends DBExtensionTests {
@Test @Test
public void testDoPost() { public void testDoPost() throws IOException, ServletException, JSONException {
when(request.getParameter("databaseType")).thenReturn(MySQLDatabaseService.DB_NAME); when(request.getParameter("databaseType")).thenReturn(MySQLDatabaseService.DB_NAME);
when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost()); when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost());
@ -75,26 +79,19 @@ public class ConnectCommandTest extends DBExtensionTests {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try { when(response.getWriter()).thenReturn(pw);
when(response.getWriter()).thenReturn(pw); ConnectCommand connectCommand = new ConnectCommand();
ConnectCommand connectCommand = new ConnectCommand();
connectCommand.doPost(request, response);
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
String code = json.getString("code"); connectCommand.doPost(request, response);
Assert.assertEquals(code, "ok");
String databaseInfo = json.getString("databaseInfo");
Assert.assertNotNull(databaseInfo);
} catch (Exception e) { String result = sw.getBuffer().toString().trim();
// TODO Auto-generated catch block JSONObject json = new JSONObject(result);
e.printStackTrace();
} String code = json.getString("code");
Assert.assertEquals(code, "ok");
String databaseInfo = json.getString("databaseInfo");
Assert.assertNotNull(databaseInfo);
} }

View File

@ -2,12 +2,15 @@ package com.google.refine.extension.database.cmd;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@ -22,7 +25,7 @@ import com.google.refine.extension.database.DatabaseConfiguration;
import com.google.refine.extension.database.DatabaseService; import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.mysql.MySQLDatabaseService; import com.google.refine.extension.database.mysql.MySQLDatabaseService;
@Test(groups = { "requiresMySQL" })
public class ExecuteQueryCommandTest extends DBExtensionTests { public class ExecuteQueryCommandTest extends DBExtensionTests {
@Mock @Mock
@ -60,7 +63,7 @@ public class ExecuteQueryCommandTest extends DBExtensionTests {
} }
@Test @Test
public void testDoPost() { public void testDoPost() throws IOException, ServletException, JSONException {
when(request.getParameter("databaseType")).thenReturn(testDbConfig.getDatabaseType()); when(request.getParameter("databaseType")).thenReturn(testDbConfig.getDatabaseType());
when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost()); when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost());
@ -74,27 +77,20 @@ public class ExecuteQueryCommandTest extends DBExtensionTests {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try {
when(response.getWriter()).thenReturn(pw);
ExecuteQueryCommand executeQueryCommand = new ExecuteQueryCommand();
executeQueryCommand.doPost(request, response);
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
String code = json.getString("code");
Assert.assertEquals(code, "ok");
String queryResult = json.getString("QueryResult");
Assert.assertNotNull(queryResult);
} catch (Exception e) { when(response.getWriter()).thenReturn(pw);
// TODO Auto-generated catch block ExecuteQueryCommand executeQueryCommand = new ExecuteQueryCommand();
e.printStackTrace();
} executeQueryCommand.doPost(request, response);
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
String code = json.getString("code");
Assert.assertEquals(code, "ok");
String queryResult = json.getString("QueryResult");
Assert.assertNotNull(queryResult);
} }
} }

View File

@ -7,6 +7,7 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -135,7 +136,7 @@ public class SavedConnectionCommandTest extends DBExtensionTests{
} }
@Test @Test
public void testDoPost() { public void testDoPost() throws IOException, ServletException, JSONException {
when(request.getParameter("connectionName")).thenReturn("test-db-name"); when(request.getParameter("connectionName")).thenReturn("test-db-name");
when(request.getParameter("databaseType")).thenReturn(MySQLDatabaseService.DB_NAME); when(request.getParameter("databaseType")).thenReturn(MySQLDatabaseService.DB_NAME);
@ -148,33 +149,24 @@ public class SavedConnectionCommandTest extends DBExtensionTests{
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try { when(response.getWriter()).thenReturn(pw);
when(response.getWriter()).thenReturn(pw);
SUT.doPost(request, response);
SUT.doPost(request, response);
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
JSONArray savedConnections = json.getJSONArray("savedConnections");
Assert.assertNotNull(savedConnections);
int len = savedConnections.length();
Assert.assertEquals(len, 1);
} catch (Exception e) { String result = sw.getBuffer().toString().trim();
// TODO Auto-generated catch block
e.printStackTrace(); JSONObject json = new JSONObject(result);
}
JSONArray savedConnections = json.getJSONArray("savedConnections");
Assert.assertNotNull(savedConnections);
int len = savedConnections.length();
Assert.assertEquals(len, 1);
} }
@Test @Test
public void testDoGet() { public void testDoGet() throws IOException, ServletException, JSONException {
String testDbName = "testLocalDb"; String testDbName = "testLocalDb";
//add saved connection //add saved connection
saveDatabaseConfiguration(testDbName); saveDatabaseConfiguration(testDbName);
@ -191,32 +183,25 @@ public class SavedConnectionCommandTest extends DBExtensionTests{
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try { when(response.getWriter()).thenReturn(pw);
when(response.getWriter()).thenReturn(pw);
SUT.doGet(request, response);
SUT.doGet(request, response);
JSONObject json = new JSONObject(sw.getBuffer().toString().trim());
JSONObject json = new JSONObject(sw.getBuffer().toString().trim());
JSONArray savedConnections = json.getJSONArray("savedConnections");
JSONArray savedConnections = json.getJSONArray("savedConnections"); Assert.assertNotNull(savedConnections);
Assert.assertNotNull(savedConnections);
Assert.assertEquals(savedConnections.length(), 1);
Assert.assertEquals(savedConnections.length(), 1);
JSONObject sc = (JSONObject)savedConnections.get(0);
JSONObject sc = (JSONObject)savedConnections.get(0); // System.out.println("sc" + sc);
// System.out.println("sc" + sc); String connName = sc.getString("connectionName");
String connName = sc.getString("connectionName"); Assert.assertEquals(connName, testDbName);
Assert.assertEquals(connName, testDbName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testDoPut() { public void testDoPut() throws IOException, ServletException, JSONException {
String testDbName = "testLocalDb"; String testDbName = "testLocalDb";
saveDatabaseConfiguration(testDbName); saveDatabaseConfiguration(testDbName);
@ -224,37 +209,30 @@ public class SavedConnectionCommandTest extends DBExtensionTests{
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try { when(response.getWriter()).thenReturn(pw);
when(response.getWriter()).thenReturn(pw);
//modify database config
//modify database config String newHost = "localhost";
String newHost = "localhost"; when(request.getParameter("connectionName")).thenReturn(testDbName);
when(request.getParameter("connectionName")).thenReturn(testDbName); when(request.getParameter("databaseType")).thenReturn(MySQLDatabaseService.DB_NAME);
when(request.getParameter("databaseType")).thenReturn(MySQLDatabaseService.DB_NAME); when(request.getParameter("databaseServer")).thenReturn(newHost);
when(request.getParameter("databaseServer")).thenReturn(newHost); when(request.getParameter("databasePort")).thenReturn("" + testDbConfig.getDatabasePort());
when(request.getParameter("databasePort")).thenReturn("" + testDbConfig.getDatabasePort()); when(request.getParameter("databaseUser")).thenReturn(testDbConfig.getDatabaseUser());
when(request.getParameter("databaseUser")).thenReturn(testDbConfig.getDatabaseUser()); when(request.getParameter("databasePassword")).thenReturn(testDbConfig.getDatabasePassword());
when(request.getParameter("databasePassword")).thenReturn(testDbConfig.getDatabasePassword()); when(request.getParameter("initialDatabase")).thenReturn(testDbConfig.getDatabaseName());
when(request.getParameter("initialDatabase")).thenReturn(testDbConfig.getDatabaseName());
SUT.doPut(request, response);
SUT.doPut(request, response);
JSONObject json = new JSONObject(sw.getBuffer().toString().trim());
JSONObject json = new JSONObject(sw.getBuffer().toString().trim()); JSONArray savedConnections = json.getJSONArray("savedConnections");
JSONArray savedConnections = json.getJSONArray("savedConnections"); Assert.assertNotNull(savedConnections);
Assert.assertNotNull(savedConnections);
Assert.assertEquals(savedConnections.length(), 1);
Assert.assertEquals(savedConnections.length(), 1);
JSONObject sc = (JSONObject)savedConnections.get(0);
JSONObject sc = (JSONObject)savedConnections.get(0); System.out.println("sc" + sc);
System.out.println("sc" + sc); String newDbHost = sc.getString("databaseHost");
String newDbHost = sc.getString("databaseHost"); Assert.assertEquals(newDbHost, newHost);
Assert.assertEquals(newDbHost, newHost);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test

View File

@ -2,12 +2,15 @@ package com.google.refine.extension.database.cmd;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@ -23,7 +26,7 @@ import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.mysql.MySQLDatabaseService; import com.google.refine.extension.database.mysql.MySQLDatabaseService;
@Test(groups = { "requiresMySQL" })
public class TestConnectCommandTest extends DBExtensionTests{ public class TestConnectCommandTest extends DBExtensionTests{
@Mock @Mock
@ -63,7 +66,7 @@ public class TestConnectCommandTest extends DBExtensionTests{
@Test @Test
public void testDoPost() { public void testDoPost() throws IOException, ServletException, JSONException {
when(request.getParameter("databaseType")).thenReturn(MySQLDatabaseService.DB_NAME); when(request.getParameter("databaseType")).thenReturn(MySQLDatabaseService.DB_NAME);
when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost()); when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost());
@ -77,23 +80,16 @@ public class TestConnectCommandTest extends DBExtensionTests{
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try { when(response.getWriter()).thenReturn(pw);
when(response.getWriter()).thenReturn(pw); TestConnectCommand connectCommand = new TestConnectCommand();
TestConnectCommand connectCommand = new TestConnectCommand();
connectCommand.doPost(request, response);
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
String code = json.getString("code"); connectCommand.doPost(request, response);
Assert.assertEquals(code, "ok");
} catch (Exception e) { String result = sw.getBuffer().toString().trim();
// TODO Auto-generated catch block JSONObject json = new JSONObject(result);
e.printStackTrace();
} String code = json.getString("code");
Assert.assertEquals(code, "ok");
} }

View File

@ -2,12 +2,15 @@ package com.google.refine.extension.database.cmd;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@ -22,7 +25,7 @@ import com.google.refine.extension.database.DatabaseConfiguration;
import com.google.refine.extension.database.DatabaseService; import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.mysql.MySQLDatabaseService; import com.google.refine.extension.database.mysql.MySQLDatabaseService;
@Test(groups = { "requiresMySQL" })
public class TestQueryCommandTest extends DBExtensionTests { public class TestQueryCommandTest extends DBExtensionTests {
@Mock @Mock
@ -61,7 +64,7 @@ public class TestQueryCommandTest extends DBExtensionTests {
@Test @Test
public void testDoPost() { public void testDoPost() throws IOException, ServletException, JSONException {
when(request.getParameter("databaseType")).thenReturn(testDbConfig.getDatabaseType()); when(request.getParameter("databaseType")).thenReturn(testDbConfig.getDatabaseType());
when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost()); when(request.getParameter("databaseServer")).thenReturn(testDbConfig.getDatabaseHost());
@ -76,25 +79,19 @@ public class TestQueryCommandTest extends DBExtensionTests {
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
try { when(response.getWriter()).thenReturn(pw);
when(response.getWriter()).thenReturn(pw); TestQueryCommand executeQueryCommand = new TestQueryCommand();
TestQueryCommand executeQueryCommand = new TestQueryCommand();
executeQueryCommand.doPost(request, response);
String result = sw.getBuffer().toString().trim();
JSONObject json = new JSONObject(result);
String code = json.getString("code"); executeQueryCommand.doPost(request, response);
Assert.assertEquals(code, "ok");
String queryResult = json.getString("QueryResult");
Assert.assertNotNull(queryResult);
} catch (Exception e) { String result = sw.getBuffer().toString().trim();
// TODO Auto-generated catch block JSONObject json = new JSONObject(result);
e.printStackTrace();
} String code = json.getString("code");
Assert.assertEquals(code, "ok");
String queryResult = json.getString("QueryResult");
Assert.assertNotNull(queryResult);
} }

View File

@ -1,6 +1,7 @@
package com.google.refine.extension.database.mariadb; package com.google.refine.extension.database.mariadb;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.testng.Assert; import org.testng.Assert;
@ -14,7 +15,7 @@ import com.google.refine.extension.database.DatabaseConfiguration;
import com.google.refine.extension.database.DatabaseService; import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.DatabaseServiceException; import com.google.refine.extension.database.DatabaseServiceException;
@Test(groups = { "requiresMariaDB" })
public class MariaDBConnectionManagerTest extends DBExtensionTests { public class MariaDBConnectionManagerTest extends DBExtensionTests {
@ -48,48 +49,27 @@ public class MariaDBConnectionManagerTest extends DBExtensionTests {
@Test @Test
public void testTestConnection() { public void testTestConnection() throws DatabaseServiceException {
boolean conn = MariaDBConnectionManager.getInstance().testConnection(testDbConfig);
try { Assert.assertEquals(conn, true);
boolean conn = MariaDBConnectionManager.getInstance().testConnection(testDbConfig);
Assert.assertEquals(conn, true);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testGetConnection() { public void testGetConnection() throws DatabaseServiceException {
Connection conn = MariaDBConnectionManager.getInstance().getConnection(testDbConfig, true);
try { Assert.assertNotNull(conn);
Connection conn = MariaDBConnectionManager.getInstance().getConnection(testDbConfig, true);
Assert.assertNotNull(conn);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testShutdown() { public void testShutdown() throws DatabaseServiceException, SQLException {
Connection conn = MariaDBConnectionManager.getInstance().getConnection(testDbConfig, true);
try { Assert.assertNotNull(conn);
Connection conn = MariaDBConnectionManager.getInstance().getConnection(testDbConfig, true);
Assert.assertNotNull(conn); MariaDBConnectionManager.getInstance().shutdown();
MariaDBConnectionManager.getInstance().shutdown(); if(conn != null) {
Assert.assertEquals(conn.isClosed(), true);
if(conn != null) { }
Assert.assertEquals(conn.isClosed(), true);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

View File

@ -19,7 +19,7 @@ import com.google.refine.extension.database.model.DatabaseColumn;
import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseInfo;
import com.google.refine.extension.database.model.DatabaseRow; import com.google.refine.extension.database.model.DatabaseRow;
@Test(groups = { "requiresMariaDB" })
public class MariaDBDatabaseServiceTest extends DBExtensionTests{ public class MariaDBDatabaseServiceTest extends DBExtensionTests{
@ -63,60 +63,35 @@ public class MariaDBDatabaseServiceTest extends DBExtensionTests{
} }
@Test @Test
public void testGetConnection() { public void testGetConnection() throws DatabaseServiceException {
try { MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService)DatabaseService.get(MariaDBDatabaseService.DB_NAME);
Connection conn = pgSqlService.getConnection(testDbConfig);
MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService)DatabaseService.get(MariaDBDatabaseService.DB_NAME);
Connection conn = pgSqlService.getConnection(testDbConfig); Assert.assertNotNull(conn);
Assert.assertNotNull(conn);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testTestConnection() { public void testTestConnection() throws DatabaseServiceException {
try { MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService)DatabaseService.get(MariaDBDatabaseService.DB_NAME);
MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService)DatabaseService.get(MariaDBDatabaseService.DB_NAME);
boolean result = pgSqlService.testConnection(testDbConfig);
boolean result = pgSqlService.testConnection(testDbConfig); Assert.assertEquals(result, true);
Assert.assertEquals(result, true);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testConnect() { public void testConnect() throws DatabaseServiceException {
try { MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService)DatabaseService.get(MariaDBDatabaseService.DB_NAME);
MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService)DatabaseService.get(MariaDBDatabaseService.DB_NAME); DatabaseInfo databaseInfo = pgSqlService.connect(testDbConfig);
DatabaseInfo databaseInfo = pgSqlService.connect(testDbConfig); Assert.assertNotNull(databaseInfo);
Assert.assertNotNull(databaseInfo);
} catch (DatabaseServiceException e) {
e.printStackTrace();
}
} }
@Test @Test
public void testExecuteQuery() { public void testExecuteQuery() throws DatabaseServiceException {
MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService) DatabaseService
.get(MariaDBDatabaseService.DB_NAME);
DatabaseInfo databaseInfo = pgSqlService.testQuery(testDbConfig, "SELECT * FROM " + testTable);
try { Assert.assertNotNull(databaseInfo);
MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService) DatabaseService
.get(MariaDBDatabaseService.DB_NAME);
DatabaseInfo databaseInfo = pgSqlService.testQuery(testDbConfig, "SELECT * FROM " + testTable);
Assert.assertNotNull(databaseInfo);
} catch (DatabaseServiceException e) {
e.printStackTrace();
}
} }
@Test @Test
@ -130,18 +105,12 @@ public class MariaDBDatabaseServiceTest extends DBExtensionTests{
} }
@Test @Test
public void testGetRows() { public void testGetRows() throws DatabaseServiceException {
try { MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService) DatabaseService
MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService) DatabaseService
.get(MariaDBDatabaseService.DB_NAME); .get(MariaDBDatabaseService.DB_NAME);
List<DatabaseRow> dbRows = pgSqlService.getRows(testDbConfig, "SELECT * FROM " + testTable); List<DatabaseRow> dbRows = pgSqlService.getRows(testDbConfig, "SELECT * FROM " + testTable);
Assert.assertNotNull(dbRows); Assert.assertNotNull(dbRows);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
@ -151,21 +120,15 @@ public class MariaDBDatabaseServiceTest extends DBExtensionTests{
} }
@Test @Test
public void testGetColumns() { public void testGetColumns() throws DatabaseServiceException {
List<DatabaseColumn> dbColumns; List<DatabaseColumn> dbColumns;
try { MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService) DatabaseService
MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService) DatabaseService .get(MariaDBDatabaseService.DB_NAME);
.get(MariaDBDatabaseService.DB_NAME);
dbColumns = pgSqlService.getColumns(testDbConfig, "SELECT * FROM " + testTable); dbColumns = pgSqlService.getColumns(testDbConfig, "SELECT * FROM " + testTable);
Assert.assertNotNull(dbColumns); Assert.assertNotNull(dbColumns);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

View File

@ -1,6 +1,7 @@
package com.google.refine.extension.database.mysql; package com.google.refine.extension.database.mysql;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.testng.Assert; import org.testng.Assert;
@ -14,6 +15,7 @@ import com.google.refine.extension.database.DatabaseConfiguration;
import com.google.refine.extension.database.DatabaseService; import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.DatabaseServiceException; import com.google.refine.extension.database.DatabaseServiceException;
@Test(groups = { "requiresMySQL" })
public class MySQLConnectionManagerTest extends DBExtensionTests { public class MySQLConnectionManagerTest extends DBExtensionTests {
private DatabaseConfiguration testDbConfig; private DatabaseConfiguration testDbConfig;
@ -45,49 +47,30 @@ public class MySQLConnectionManagerTest extends DBExtensionTests {
@Test @Test
public void testTestConnection() { public void testTestConnection() throws DatabaseServiceException {
try { boolean conn = MySQLConnectionManager.getInstance().testConnection(testDbConfig);
boolean conn = MySQLConnectionManager.getInstance().testConnection(testDbConfig); Assert.assertEquals(conn, true);
Assert.assertEquals(conn, true);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testGetConnection() { public void testGetConnection() throws DatabaseServiceException {
try { Connection conn = MySQLConnectionManager.getInstance().getConnection(testDbConfig, true);
Connection conn = MySQLConnectionManager.getInstance().getConnection(testDbConfig, true); Assert.assertNotNull(conn);
Assert.assertNotNull(conn);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testShutdown() { public void testShutdown() throws DatabaseServiceException, SQLException {
try { Connection conn = MySQLConnectionManager.getInstance().getConnection(testDbConfig, true);
Connection conn = MySQLConnectionManager.getInstance().getConnection(testDbConfig, true); Assert.assertNotNull(conn);
Assert.assertNotNull(conn);
MySQLConnectionManager.getInstance().shutdown();
MySQLConnectionManager.getInstance().shutdown();
if(conn != null) {
if(conn != null) { Assert.assertEquals(conn.isClosed(), true);
Assert.assertEquals(conn.isClosed(), true); }
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
} }

View File

@ -20,7 +20,7 @@ import com.google.refine.extension.database.model.DatabaseInfo;
import com.google.refine.extension.database.model.DatabaseRow; import com.google.refine.extension.database.model.DatabaseRow;
@Test(groups = { "requiresMySQL" })
public class MySQLDatabaseServiceTest extends DBExtensionTests{ public class MySQLDatabaseServiceTest extends DBExtensionTests{
private DatabaseConfiguration testDbConfig; private DatabaseConfiguration testDbConfig;
@ -62,60 +62,36 @@ public class MySQLDatabaseServiceTest extends DBExtensionTests{
@Test @Test
public void testGetConnection() { public void testGetConnection() throws DatabaseServiceException {
try { MySQLDatabaseService pgSqlService = (MySQLDatabaseService)DatabaseService.get(MySQLDatabaseService.DB_NAME);
Connection conn = pgSqlService.getConnection(testDbConfig);
MySQLDatabaseService pgSqlService = (MySQLDatabaseService)DatabaseService.get(MySQLDatabaseService.DB_NAME);
Connection conn = pgSqlService.getConnection(testDbConfig); Assert.assertNotNull(conn);
Assert.assertNotNull(conn);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testTestConnection() { public void testTestConnection() throws DatabaseServiceException {
try { MySQLDatabaseService pgSqlService = (MySQLDatabaseService)DatabaseService.get(MySQLDatabaseService.DB_NAME);
MySQLDatabaseService pgSqlService = (MySQLDatabaseService)DatabaseService.get(MySQLDatabaseService.DB_NAME);
boolean result = pgSqlService.testConnection(testDbConfig);
boolean result = pgSqlService.testConnection(testDbConfig); Assert.assertEquals(result, true);
Assert.assertEquals(result, true);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testConnect() { public void testConnect() throws DatabaseServiceException {
try { MySQLDatabaseService pgSqlService = (MySQLDatabaseService)DatabaseService.get(MySQLDatabaseService.DB_NAME);
MySQLDatabaseService pgSqlService = (MySQLDatabaseService)DatabaseService.get(MySQLDatabaseService.DB_NAME); DatabaseInfo databaseInfo = pgSqlService.connect(testDbConfig);
DatabaseInfo databaseInfo = pgSqlService.connect(testDbConfig); Assert.assertNotNull(databaseInfo);
Assert.assertNotNull(databaseInfo);
} catch (DatabaseServiceException e) {
e.printStackTrace();
}
} }
@Test @Test
public void testExecuteQuery() { public void testExecuteQuery() throws DatabaseServiceException {
try { MySQLDatabaseService pgSqlService = (MySQLDatabaseService) DatabaseService
.get(MySQLDatabaseService.DB_NAME);
DatabaseInfo databaseInfo = pgSqlService.testQuery(testDbConfig, "SELECT * FROM " + testTable);
MySQLDatabaseService pgSqlService = (MySQLDatabaseService) DatabaseService Assert.assertNotNull(databaseInfo);
.get(MySQLDatabaseService.DB_NAME);
DatabaseInfo databaseInfo = pgSqlService.testQuery(testDbConfig, "SELECT * FROM " + testTable);
Assert.assertNotNull(databaseInfo);
} catch (DatabaseServiceException e) {
e.printStackTrace();
}
} }
@Test @Test
@ -129,18 +105,12 @@ public class MySQLDatabaseServiceTest extends DBExtensionTests{
} }
@Test @Test
public void testGetRows() { public void testGetRows() throws DatabaseServiceException {
try { MySQLDatabaseService pgSqlService = (MySQLDatabaseService) DatabaseService
MySQLDatabaseService pgSqlService = (MySQLDatabaseService) DatabaseService .get(MySQLDatabaseService.DB_NAME);
.get(MySQLDatabaseService.DB_NAME); List<DatabaseRow> dbRows = pgSqlService.getRows(testDbConfig, "SELECT * FROM " + testTable);
List<DatabaseRow> dbRows = pgSqlService.getRows(testDbConfig, "SELECT * FROM " + testTable);
Assert.assertNotNull(dbRows); Assert.assertNotNull(dbRows);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
@ -150,21 +120,14 @@ public class MySQLDatabaseServiceTest extends DBExtensionTests{
} }
@Test @Test
public void testGetColumns() { public void testGetColumns() throws DatabaseServiceException {
List<DatabaseColumn> dbColumns; List<DatabaseColumn> dbColumns;
MySQLDatabaseService pgSqlService = (MySQLDatabaseService) DatabaseService
.get(MySQLDatabaseService.DB_NAME);
try { dbColumns = pgSqlService.getColumns(testDbConfig, "SELECT * FROM " + testTable);
MySQLDatabaseService pgSqlService = (MySQLDatabaseService) DatabaseService
.get(MySQLDatabaseService.DB_NAME);
dbColumns = pgSqlService.getColumns(testDbConfig, "SELECT * FROM " + testTable); Assert.assertNotNull(dbColumns);
Assert.assertNotNull(dbColumns);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

View File

@ -1,6 +1,7 @@
package com.google.refine.extension.database.pgsql; package com.google.refine.extension.database.pgsql;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.testng.Assert; import org.testng.Assert;
@ -15,7 +16,7 @@ import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.DatabaseServiceException; import com.google.refine.extension.database.DatabaseServiceException;
@Test(groups = { "requiresPgSQL" })
public class PgSQLConnectionManagerTest extends DBExtensionTests { public class PgSQLConnectionManagerTest extends DBExtensionTests {
private DatabaseConfiguration testDbConfig; private DatabaseConfiguration testDbConfig;
@ -48,48 +49,30 @@ public class PgSQLConnectionManagerTest extends DBExtensionTests {
@Test @Test
public void testTestConnection() { public void testTestConnection() throws DatabaseServiceException {
try { boolean isConnected = PgSQLConnectionManager.getInstance().testConnection(testDbConfig);
boolean isConnected = PgSQLConnectionManager.getInstance().testConnection(testDbConfig); Assert.assertEquals(isConnected, true);
Assert.assertEquals(isConnected, true);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testGetConnection() { public void testGetConnection() throws DatabaseServiceException {
try { Connection conn = PgSQLConnectionManager.getInstance().getConnection(testDbConfig, true);
Connection conn = PgSQLConnectionManager.getInstance().getConnection(testDbConfig, true); Assert.assertNotNull(conn);
Assert.assertNotNull(conn);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testShutdown() { public void testShutdown() throws DatabaseServiceException, SQLException {
try { Connection conn = PgSQLConnectionManager.getInstance().getConnection(testDbConfig, true);
Connection conn = PgSQLConnectionManager.getInstance().getConnection(testDbConfig, true); Assert.assertNotNull(conn);
Assert.assertNotNull(conn);
PgSQLConnectionManager.getInstance().shutdown();
PgSQLConnectionManager.getInstance().shutdown();
if(conn != null) {
if(conn != null) { Assert.assertEquals(conn.isClosed(), true);
Assert.assertEquals(conn.isClosed(), true); }
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

View File

@ -19,7 +19,7 @@ import com.google.refine.extension.database.model.DatabaseColumn;
import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseInfo;
import com.google.refine.extension.database.model.DatabaseRow; import com.google.refine.extension.database.model.DatabaseRow;
@Test(groups = { "requiresPgSQL" })
public class PgSQLDatabaseServiceTest extends DBExtensionTests{ public class PgSQLDatabaseServiceTest extends DBExtensionTests{
private DatabaseConfiguration testDbConfig; private DatabaseConfiguration testDbConfig;
@ -61,60 +61,38 @@ public class PgSQLDatabaseServiceTest extends DBExtensionTests{
@Test @Test
public void testGetConnection() { public void testGetConnection() throws DatabaseServiceException {
try {
PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService)DatabaseService.get(PgSQLDatabaseService.DB_NAME);
PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService)DatabaseService.get(PgSQLDatabaseService.DB_NAME); Connection conn = pgSqlService.getConnection(testDbConfig);
Connection conn = pgSqlService.getConnection(testDbConfig);
Assert.assertNotNull(conn);
Assert.assertNotNull(conn);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testTestConnection() { public void testTestConnection() throws DatabaseServiceException {
try { PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService)DatabaseService.get(PgSQLDatabaseService.DB_NAME);
PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService)DatabaseService.get(PgSQLDatabaseService.DB_NAME);
boolean result = pgSqlService.testConnection(testDbConfig);
boolean result = pgSqlService.testConnection(testDbConfig); Assert.assertEquals(result, true);
Assert.assertEquals(result, true);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
public void testConnect() { public void testConnect() throws DatabaseServiceException {
try {
PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService)DatabaseService.get(PgSQLDatabaseService.DB_NAME); PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService)DatabaseService.get(PgSQLDatabaseService.DB_NAME);
DatabaseInfo databaseInfo = pgSqlService.connect(testDbConfig); DatabaseInfo databaseInfo = pgSqlService.connect(testDbConfig);
Assert.assertNotNull(databaseInfo); Assert.assertNotNull(databaseInfo);
} catch (DatabaseServiceException e) {
e.printStackTrace();
}
} }
@Test @Test
public void testExecuteQuery() { public void testExecuteQuery() throws DatabaseServiceException {
try { PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService) DatabaseService
.get(PgSQLDatabaseService.DB_NAME);
DatabaseInfo databaseInfo = pgSqlService.testQuery(testDbConfig, "SELECT * FROM " + testTable);
PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService) DatabaseService Assert.assertNotNull(databaseInfo);
.get(PgSQLDatabaseService.DB_NAME);
DatabaseInfo databaseInfo = pgSqlService.testQuery(testDbConfig, "SELECT * FROM " + testTable);
Assert.assertNotNull(databaseInfo);
} catch (DatabaseServiceException e) {
e.printStackTrace();
}
} }
@Test @Test
@ -128,18 +106,12 @@ public class PgSQLDatabaseServiceTest extends DBExtensionTests{
} }
@Test @Test
public void testGetRows() { public void testGetRows() throws DatabaseServiceException {
try { PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService) DatabaseService
PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService) DatabaseService .get(PgSQLDatabaseService.DB_NAME);
.get(PgSQLDatabaseService.DB_NAME); List<DatabaseRow> dbRows = pgSqlService.getRows(testDbConfig, "SELECT * FROM " + testTable);
List<DatabaseRow> dbRows = pgSqlService.getRows(testDbConfig, "SELECT * FROM " + testTable);
Assert.assertNotNull(dbRows); Assert.assertNotNull(dbRows);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
@Test @Test
@ -149,21 +121,16 @@ public class PgSQLDatabaseServiceTest extends DBExtensionTests{
} }
@Test @Test
public void testGetColumns() { public void testGetColumns() throws DatabaseServiceException {
List<DatabaseColumn> dbColumns; List<DatabaseColumn> dbColumns;
try { PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService) DatabaseService
PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService) DatabaseService .get(PgSQLDatabaseService.DB_NAME);
.get(PgSQLDatabaseService.DB_NAME);
dbColumns = pgSqlService.getColumns(testDbConfig, "SELECT * FROM " + testTable); dbColumns = pgSqlService.getColumns(testDbConfig, "SELECT * FROM " + testTable);
Assert.assertNotNull(dbColumns); Assert.assertNotNull(dbColumns);
} catch (DatabaseServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }