Skip to content

Commit

Permalink
Adding Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
palych-piter committed May 18, 2017
1 parent df8723c commit 2077eae
Show file tree
Hide file tree
Showing 19 changed files with 389 additions and 161 deletions.
40 changes: 40 additions & 0 deletions src/main/java/excel2db/Excel2dbTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package excel2db;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import com.ge.mdm.tools.common.ApplicationException;
import org.apache.commons.io.FilenameUtils;

public class Excel2dbTask extends Thread {

public Excel2dbTask(excel2db app, File fileName) {
this.app = app;
this.fileName = fileName;
}

public excel2db app;
public File fileName;

String tableName;

@Override
public void run() {
try {
app.initInputFiles.initInputFiles(fileName);

tableName = FilenameUtils.removeExtension(fileName.getName());
app.createTable.createTable(tableName);
app.populateTable.populateTable(tableName);

} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ApplicationException e) {
e.printStackTrace();
}

}
}
35 changes: 35 additions & 0 deletions src/main/java/excel2db/InsertDBRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package excel2db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.PreparedStatement;
import java.sql.SQLException;


public class InsertDBRecord extends Thread {

public InsertDBRecord(PreparedStatement insertDBRecordStatement) {
this.insertDBRecordStatement = insertDBRecordStatement;
}

public PreparedStatement insertDBRecordStatement;

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

@Override
public void run() {
try {
logger.info("Thread " + getName() + " started");
insertDBRecordStatement.execute();
logger.info("Thread " + getName() + " finished");
} catch (SQLException e) {
//Log the error somehow
}
// catch (InterruptedException e) {
// //Log the error somehow
// }

}

}
172 changes: 50 additions & 122 deletions src/main/java/excel2db/excel2db.java
Original file line number Diff line number Diff line change
@@ -1,186 +1,114 @@

package excel2db;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;

import com.ge.mdm.tools.common.ApplicationException;
import com.ge.mdm.tools.common.SheetEntityManager;
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 org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;


public class excel2db

{

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

// declaring the constructor to meet to the Spring framework convention
// constructor injection method (CI))
public excel2db (CreateTable createTable, PopulateTable populateTable) {
public excel2db(CreateTable createTable, PopulateTable populateTable) {
this.createTable = createTable;
this.populateTable = populateTable;
}

private Workbook workbook;

private File inputSheetFile;
private File outputSheetFile;
private String fileExtension;

//vars are used in interface implementations
public static Connection connection = null;
public static SheetEntityManager sheetEntityManager;
public static Sheet sheet;
public static Short numberProcessedRecords = Short.valueOf((short)0);
public static Short numberProcessedRecords = Short.valueOf((short) 0);


// declaring variables to meet to the Spring framework convention
public DBConnection dbConnection;
public InitInputFiles initInputFiles;
public GenerateFileList generateFileList;
public CreateTable createTable;
public PopulateTable populateTable;
public InitConstants initConstants;


public File getInputSheetFile() { return inputSheetFile; }

public void setInputSheetFile(File inputSheetFile)
{
this.inputSheetFile = inputSheetFile;
}

public File getOutputSheetFile() {
return outputSheetFile;
}

public void setOutputSheetFile(File outputSheetFile) {
this.outputSheetFile = outputSheetFile;
}

public static ThreadPoolTaskExecutor taskExecutor;

// declaring setters to meet to the Spring framework convention.
// Setter injection method (SI))
public void setDbConnection(DBConnection dbConnection)
{
public void setDbConnection(DBConnection dbConnection) {
this.dbConnection = dbConnection;
}
public void setInitInputFiles(InitInputFiles initInputFiles) {
this.initInputFiles = initInputFiles;
}
public void setGenerateFileList(GenerateFileList generateFileList) {
this.generateFileList = generateFileList;
}
public void setInitConstants(InitConstants initConstants) {
this.initConstants = initConstants;
}


public static void main(String[] args)
{
if (args.length < 1) {
logger.error("Need file name as an argument.");
System.exit(1);
}

logger.info("excel2db : Passing file name as an argument ; " + String.join(" ", args));

File inputFile = new File(args[0]);
public static void main(String[] args) {

try
{
try {

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
excel2db app = (excel2db)context.getBean("excel2db");
excel2db app = (excel2db) context.getBean("excel2db");

app.setInputSheetFile(inputFile);
app.initAndValidate();
taskExecutor = (ThreadPoolTaskExecutor) context.getBean("taskExecutor");

//this way we call methods for objects that is already initialized by the Spring
app.dbConnection.establishDBConnection();
app.createTable.createTable();
app.populateTable.populateTable();

// iterate through file name values inn properties file
for (String fileNameValue : app.generateFileList.generateFileList()) {
File fileName = new File(fileNameValue);
taskExecutor.execute(new Excel2dbTask(app, fileName));
}

// Wait until all threads are finished
logger.info("Waiting until active threads exist");
while (excel2db.taskExecutor.getActiveCount() != 0) {
}
logger.info("All threads are inactive");

app.closeConnections();

}
catch (Exception e) {
} catch (Exception e) {
logger.error("An exception occurred while running application", e);
System.exit(1);
}

}


public void initAndValidate()
throws ApplicationException, IOException
{
logger.info("Reading input file '{}'...", inputSheetFile.getAbsolutePath());


fileExtension = FilenameUtils.getExtension(inputSheetFile.getName());
if (fileExtension.equalsIgnoreCase("xlsx")) {
workbook = createWorkbook(openOPCPackage(inputSheetFile));
} else {
workbook = createWorkbook(openNPOIFSFileSystemPackage(inputSheetFile));
}

sheet = workbook.getSheetAt(0);
if (sheet == null) {
throw new ApplicationException("Workbook does not contain a sheet with index 0");
}

sheetEntityManager = new SheetEntityManager(sheet);
}


private static OPCPackage openOPCPackage(File file)
throws ApplicationException
{
try
{
return OPCPackage.open(file);
} catch (InvalidFormatException e) {
throw new ApplicationException("Unable to open OPC package from " + file.getAbsolutePath(), e);
}
}

private static NPOIFSFileSystem openNPOIFSFileSystemPackage(File file) throws ApplicationException
{
try {
return new NPOIFSFileSystem(file);
} catch (IOException e) {
throw new ApplicationException("Unable to open NPOIFSFileSystem package from " + file.getAbsolutePath(), e);
}
}

private static Workbook createWorkbook(OPCPackage pkg) throws ApplicationException
{
try {
return new XSSFWorkbook(pkg);
} catch (IOException e) {
throw new ApplicationException("Unable to create workbook from OPC package", e);
}
}

private static Workbook createWorkbook(NPOIFSFileSystem pkg) throws ApplicationException
{
try {
return new HSSFWorkbook(pkg);
} catch (IOException e) {
throw new ApplicationException("Unable to create workbook from NPOIFSFileSystem package", e);
}
}

public void closeConnections() throws ApplicationException, SQLException {
logger.info("Closing connections");
connection.close();
logger.info("The Process is Completed");
logger.info("Number of Processed Records: " + numberProcessedRecords.toString());

logger.info("Closing task executor ");
taskExecutor.shutdown();
logger.info("Task executor is stopped");

logger.info("The process is completed");
logger.info("Number of processed records: " + numberProcessedRecords.toString());

}

}
2 changes: 1 addition & 1 deletion src/main/java/excel2db/service/CreateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface CreateTable {

public void createTable () throws SQLException;
public void createTable(String tableName) throws SQLException;

}
3 changes: 1 addition & 2 deletions src/main/java/excel2db/service/DBConnection.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

package excel2db.service;

public interface DBConnection {
public interface DBConnection {

public void establishDBConnection();

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/excel2db/service/GenerateFileList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package excel2db.service;

import java.util.HashSet;

public interface GenerateFileList {
public HashSet<String> generateFileList();
}

7 changes: 7 additions & 0 deletions src/main/java/excel2db/service/InitConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package excel2db.service;

public class InitConstants {

public final static String workingDir = System.getProperty("user.dir");

}
12 changes: 12 additions & 0 deletions src/main/java/excel2db/service/InitInputFiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package excel2db.service;

import java.io.File;
import java.io.IOException;

import com.ge.mdm.tools.common.ApplicationException;
import org.apache.poi.ss.usermodel.Sheet;

public interface InitInputFiles {
public Sheet initInputFiles(File inputFile) throws IOException, ApplicationException;
}

2 changes: 1 addition & 1 deletion src/main/java/excel2db/service/PopulateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface PopulateTable {

public void populateTable () throws SQLException;
public Integer populateTable(String tableName) throws SQLException;

}
Loading

0 comments on commit 2077eae

Please sign in to comment.