Skip to content

Commit

Permalink
Implement Derby database implementation for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
palych-piter committed Jul 9, 2017
1 parent 6b3bad9 commit d7620d8
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 99 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@

# Idea files #
################
/.idea/


# bat files #
################
/bin/

# Gradle files #
################
.gradle


# Files in the build folder #
#############################
/build/


# Compiled source #
###################
*.com
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ dependencies {

}


jar {

manifest {
Expand Down
45 changes: 22 additions & 23 deletions src/main/java/excel2db/excel2db.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import java.util.HashSet;
import java.util.concurrent.TimeUnit;

import excel2db.ApplicationException;
import excel2db.service.CreateTable;
import excel2db.service.DBConnection;
import excel2db.service.GenerateFileList;
import excel2db.service.InitConstants;
import excel2db.service.InitInputFiles;
import excel2db.service.PopulateTable;
import excel2db.Excel2dbTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -85,31 +83,32 @@ public static void main(String[] args) {
//this way we call methods for objects that is already initialized by the Spring
app.dbConnection.establishDBConnection();

// iterate through file name values in properties file,
// for each file start a new thread
for (String fileNameValue : fileList) {

File fileName = new File(fileNameValue);
//check if the file really exists
if (fileName.exists() == false) {
logger.error("The file " + fileNameValue + " doesn't exist");
} else {
taskExecutor.execute(new Excel2dbTask(app, fileName));
// in case the connection established
// iterate through file name values in properties file, for each file start a new thread
if (connection != null) {
for (String fileNameValue : fileList) {

File fileName = new File(fileNameValue);
//check if the file really exists
if (fileName.exists() == false) {
logger.error("The file " + fileNameValue + " doesn't exist");
} else {
taskExecutor.execute(new Excel2dbTask(app, fileName));
}
}

}

//TODO : This is just as a workaround to wait till the thread has been really started
//TODO : Need to use the ScheduledThreadPoolExecutor to customize delays on starting threads
TimeUnit.SECONDS.sleep(1);
// Wait until all threads are finished
logger.info("Waiting until active threads exist");
while (taskExecutor.getActiveCount() != 0) {
}
logger.info("All threads are inactive");

app.closeConnections();
//TODO : This is just as a workaround to wait till the thread has been really started
//TODO : Need to use the ScheduledThreadPoolExecutor to customize delays on starting threads
TimeUnit.SECONDS.sleep(1);
// Wait until all threads are finished
logger.info("Waiting until active threads exist");
while (taskExecutor.getActiveCount() != 0) {
}
logger.info("All threads are inactive");

app.closeConnections();
}
} catch (Exception e) {
logger.error("An exception occurred while running application", e);
System.exit(1);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/excel2db/service/DBConnection.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package excel2db.service;

import java.sql.Connection;
import java.sql.SQLException;

public interface DBConnection {

public void establishDBConnection();
public void establishDBConnection() throws SQLException;

}
56 changes: 51 additions & 5 deletions src/main/java/excel2db/service/impl/CreateTableDerbyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
* Created by Andrey on 6/21/2017.
*/

import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

import excel2db.excel2db;
import excel2db.service.CreateTable;
import excel2db.service.PopulateTable;
import org.apache.poi.ss.usermodel.Sheet;
Expand All @@ -15,15 +21,55 @@

public class CreateTableDerbyImpl implements CreateTable {

public static final Logger logger = LoggerFactory.getLogger(PopulateTableOracleImpl.class);
public static final Logger logger = LoggerFactory.getLogger(CreateTableDerbyImpl.class);

private Integer numOfProcessedRows = 0;
public void createTable(Sheet sheet, String tableName) throws SQLException {

public void createTable(Sheet sheet, String tableName)
throws SQLException {
Map<String, Integer> header = new LinkedHashMap<>();
header = InitInputFilesImpl.readSheetHeader(sheet);

//dropping statement
String sqlTableDropStatement = "DROP TABLE \"" +
tableName + "\"";

//creating statement
StringBuilder sqlTableCreateStatement = new StringBuilder();
sqlTableCreateStatement.append("CREATE TABLE \"" +
tableName + "\"(");
for (String headerColumnName : header.keySet()) {
sqlTableCreateStatement.append("\"" + headerColumnName + "\"" + " VARCHAR(100), ");
}

sqlTableCreateStatement.setLength(sqlTableCreateStatement.length() - 2);
sqlTableCreateStatement.append(")");


//Derby doesn't support the "DROP IF EXISTS" SQL statement
//Thus, checking the table and dropping if the table exists
logger.info("Checking if the table " + tableName + " already exists");
DatabaseMetaData dbm = excel2db.connection.getMetaData();
ResultSet tables = dbm.getTables(null, null, tableName, null);
if (tables.next()) {
// Table exists, dropping
logger.info("The table " + tableName + " exists, dropping ...");
PreparedStatement pstmtDrop = excel2db.connection.prepareStatement(sqlTableDropStatement);
pstmtDrop.execute();
logger.info("The table " + tableName + " has been dropped");
}
else {
// Table does not exist, skipp dropping
logger.info("The table " + tableName + " doesn't exist, skip dropping");
}


//creating
logger.info("The table " + tableName + " is being created ...");
PreparedStatement pstmtCreate = excel2db.connection.prepareStatement(sqlTableCreateStatement.toString());
pstmtCreate.execute();
logger.info("The table " + tableName + " has been created");

logger.info("Create table method : Derby implementation is not covered yet");

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void createTable(Sheet sheet, String tableName) throws SQLException {
sqlTableCreateStatement.append("CREATE TABLE \"" +
tableName + "\"(");
for (String headerColumnName : header.keySet()) {
sqlTableCreateStatement.append("\"" + headerColumnName + "\"" + " VARCHAR2(20), ");
sqlTableCreateStatement.append("\"" + headerColumnName + "\"" + " VARCHAR2(100), ");
}

sqlTableCreateStatement.setLength(sqlTableCreateStatement.length() - 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class CreateTablePostgresImpl implements CreateTable {

public void createTable(Sheet sheet, String tableName) throws SQLException {

//sheetEntityManager = new SheetEntityManager(sheet);
//initializing var for a header
Map<String, Integer> header = new LinkedHashMap<>();
header = InitInputFilesImpl.readSheetHeader(sheet);
Expand Down
42 changes: 7 additions & 35 deletions src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,34 @@
* Created by Andrey on 6/21/2017.
*/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import excel2db.excel2db;
import excel2db.service.DBConnection;
import org.apache.derby.jdbc.EmbeddedDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;





public class DBConnectionDerbyImpl implements DBConnection {

// connection parameters
@Value("${db.derby.server}")
String dbServer;

@Value("${db.derby.user}")
String dbUser;

@Value("${db.derby.password}")
String dbPassword;

@Value("${db.derby.port}")
String dbPort;

@Value("${db.derby.database}")
String dbName;


public static final Logger logger = LoggerFactory.getLogger(DBConnectionDerbyImpl.class);

public void establishDBConnection() {
{

try {
excel2db.connection = DriverManager.getConnection(
//"jdbc:derby//" + dbServer + ":" + dbPort + "/" + dbName + ";create=true;user=" + dbUser + ";password=" + dbPassword
"jdbc:derby://localhost:1527/testDB;create=true"
);
//initialize an In-Memory Derby database
EmbeddedDriver driver = new EmbeddedDriver();
excel2db.connection = driver.connect("jdbc:derby:memory:testdb;create=true", new Properties());
logger.info("Derby connection is established");

} catch (SQLException e) {
logger.error("Derby Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (excel2db.connection != null) {
logger.info("Derby connection is established");
} else {
logger.error("Failed to make the Derby connection!");
}

}

}

}
13 changes: 2 additions & 11 deletions src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,11 @@ public void establishDBConnection() {

try {
excel2db.connection = DriverManager.getConnection(
"jdbc:oracle:thin:@//" + dbServer + ":" + dbPort + "/" + dbSid, dbUser, dbPassword)
;

"jdbc:oracle:thin:@//" + dbServer + ":" + dbPort + "/" + dbSid, dbUser, dbPassword);
logger.info("Oracle connection is established");
} catch (SQLException e) {
logger.error("Oracle Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (excel2db.connection != null) {
logger.info("Oracle connection is established");
} else {
logger.error("Failed to make the Oracle connection!");
}

}

}
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package excel2db.service.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

Expand Down Expand Up @@ -29,21 +30,18 @@ public class DBConnectionPostgresImpl implements DBConnection {

public static final Logger logger = LoggerFactory.getLogger(DBConnectionPostgresImpl.class);

public void establishDBConnection() {

public void establishDBConnection() throws SQLException {

//TODO : try to use the try-catch-resource construction, connection object should be closed as soon as the program stops working with this object
try {
excel2db.connection = DriverManager.getConnection(
"jdbc:postgresql://" + dbServer + ":" + dbPort + "/" + dbDatabase, dbUser, dbPassword)
;

} catch (SQLException e) {
logger.error("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (excel2db.connection != null) {
"jdbc:postgresql://" + dbServer + ":" + dbPort + "/" + dbDatabase, dbUser, dbPassword);
logger.info("Postgres connection is established");
} else {
logger.error("Failed to make connection!");
} catch (SQLException e) {
logger.error("Postgres Connection Failed! Check output console.");
}

}

}
3 changes: 3 additions & 0 deletions src/main/java/excel2db/service/impl/InitInputFilesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public void setInputSheetFile(File inputSheetFile) {

public static final Logger logger = LoggerFactory.getLogger(InitInputFilesImpl.class);

//fabric method for
//fabric method pattern, we don't know which object should be created
//in advance and define the object type based on an extension
@Override
public Sheet initInputFiles(File inputFile)
throws ApplicationException, IOException {
Expand Down
14 changes: 5 additions & 9 deletions src/main/resources/excel2db.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#db implementation: Postgres, Oracle
db.implementation=Postgres
#db implementation: Postgres, Oracle, Derby
#use Derby if you run Unit tests and have no Postgres and Oracle installed on you laptop
#in this case the Derby In-Memory database will be created and used during
#the UnitTest execution
db.implementation=Derby

# names of input tables
input.files=test.xlsx,test1.xlsx
Expand All @@ -21,15 +24,8 @@ db.oracle.password=oracle


# DON'T TOUCH :

# set db implementation classes based on the db.implementation
dbConnectionImplenentation=excel2db.service.impl.DBConnection${db.implementation}Impl
createTableImplenentation=excel2db.service.impl.CreateTable${db.implementation}Impl

# derby connection parameters
db.derby.server=localhost
db.derby.database=excel2db
db.derby.port=5433
db.derby.user=postgres
db.derby.password=postgres

0 comments on commit d7620d8

Please sign in to comment.