diff --git a/.gradle/2.13/taskArtifacts/cache.properties b/.gradle/2.13/taskArtifacts/cache.properties new file mode 100644 index 0000000..c0e3863 --- /dev/null +++ b/.gradle/2.13/taskArtifacts/cache.properties @@ -0,0 +1 @@ +#Sat Apr 01 20:46:15 MSK 2017 diff --git a/.gradle/3.1/taskArtifacts/cache.properties b/.gradle/3.1/taskArtifacts/cache.properties new file mode 100644 index 0000000..9caf226 --- /dev/null +++ b/.gradle/3.1/taskArtifacts/cache.properties @@ -0,0 +1 @@ +#Mon Mar 27 14:28:08 MSK 2017 diff --git a/.gradle/buildOutputCleanup/cache.properties b/.gradle/buildOutputCleanup/cache.properties new file mode 100644 index 0000000..25dabbf --- /dev/null +++ b/.gradle/buildOutputCleanup/cache.properties @@ -0,0 +1,2 @@ +#Fri Mar 24 14:21:21 MSK 2017 +gradle.version=3.4.1 diff --git a/build/resources/main/.gradle/buildOutputCleanup/cache.properties b/build/resources/main/.gradle/buildOutputCleanup/cache.properties new file mode 100644 index 0000000..617a29b --- /dev/null +++ b/build/resources/main/.gradle/buildOutputCleanup/cache.properties @@ -0,0 +1,2 @@ +#Tue Mar 28 13:08:49 MSK 2017 +gradle.version=3.4.1 diff --git a/build/resources/main/excel2db.properties b/build/resources/main/excel2db.properties new file mode 100644 index 0000000..244c517 --- /dev/null +++ b/build/resources/main/excel2db.properties @@ -0,0 +1,17 @@ +# names of input tables +input.files=test-input.xls,test-input1.xls + +# db implementation classes + +# postgres section +# dbConnectionImplenentation=excel2db.service.impl.DBConnectionPostgresImpl +# oracle section +dbConnectionImplenentation=excel2db.service.impl.DBConnectionOracleImpl + +# postgres connection parameters +db.server=localhost +db.database=excel2db +db.port=5433 +db.user=postgres +db.password=postgres + diff --git a/common/src/main/java/com/ge/mdm/tools/common/ApplicationException.java b/common/src/main/java/com/ge/mdm/tools/common/ApplicationException.java index 4c8278b..35b636e 100644 --- a/common/src/main/java/com/ge/mdm/tools/common/ApplicationException.java +++ b/common/src/main/java/com/ge/mdm/tools/common/ApplicationException.java @@ -1,8 +1,5 @@ package com.ge.mdm.tools.common; -/** - * @author Stanislav Mamontov - */ public class ApplicationException extends Exception { static final long serialVersionUID = 1L; diff --git a/common/src/main/java/com/ge/mdm/tools/common/AuthOutInterceptor.java b/common/src/main/java/com/ge/mdm/tools/common/AuthOutInterceptor.java index ab34335..5f5d1c2 100644 --- a/common/src/main/java/com/ge/mdm/tools/common/AuthOutInterceptor.java +++ b/common/src/main/java/com/ge/mdm/tools/common/AuthOutInterceptor.java @@ -10,9 +10,6 @@ import java.util.*; -/** - * @author Stanislav Mamontov - */ public class AuthOutInterceptor extends AbstractPhaseInterceptor { private static final Logger logger = LoggerFactory.getLogger(AuthOutInterceptor.class); diff --git a/common/src/main/java/com/ge/mdm/tools/common/SheetEntityManager.java b/common/src/main/java/com/ge/mdm/tools/common/SheetEntityManager.java index 7ebaf7a..efc7827 100644 --- a/common/src/main/java/com/ge/mdm/tools/common/SheetEntityManager.java +++ b/common/src/main/java/com/ge/mdm/tools/common/SheetEntityManager.java @@ -25,7 +25,6 @@ * Read Excel files in JPA way, considering sheet as a simple database table with columns and rows. * First row is assumed to be header containing column names, which could be mapped to JPA @Entity. * - * @author Stanislav Mamontov */ public class SheetEntityManager implements EntityManager { diff --git a/common/src/main/java/com/ge/mdm/tools/common/TaskExecutor.java b/common/src/main/java/com/ge/mdm/tools/common/TaskExecutor.java index 2cfcdc7..aa87cc1 100644 --- a/common/src/main/java/com/ge/mdm/tools/common/TaskExecutor.java +++ b/common/src/main/java/com/ge/mdm/tools/common/TaskExecutor.java @@ -5,9 +5,7 @@ import java.util.function.Consumer; import java.util.function.Supplier; -/** - * @author Stanislav Mamontov - */ + public class TaskExecutor { private int poolSize; diff --git a/src/main/java/excel2db/ApplicationContextUtils.java b/src/main/java/excel2db/ApplicationContextUtils.java new file mode 100644 index 0000000..a1ef5f4 --- /dev/null +++ b/src/main/java/excel2db/ApplicationContextUtils.java @@ -0,0 +1,21 @@ +package excel2db; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +public class ApplicationContextUtils implements ApplicationContextAware { + + private static ApplicationContext ctx; + + @Override + public void setApplicationContext(ApplicationContext appContext) + throws BeansException { + ctx = appContext; + + } + + public static ApplicationContext getApplicationContext() { + return ctx; + } +} diff --git a/src/main/java/excel2db/ApplicationException.java b/src/main/java/excel2db/ApplicationException.java new file mode 100644 index 0000000..b84a96d --- /dev/null +++ b/src/main/java/excel2db/ApplicationException.java @@ -0,0 +1,25 @@ +package excel2db; + +/** + * Created by Andrey on 5/26/2017. + */ + +public class ApplicationException extends Exception { + static final long serialVersionUID = 1L; + + public ApplicationException() { + super(); + } + + public ApplicationException(String message) { + super(message); + } + + public ApplicationException(String message, Throwable cause) { + super(message, cause); + } + + public ApplicationException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/excel2db/Excel2dbTask.java b/src/main/java/excel2db/Excel2dbTask.java index 34b9b34..9df62b6 100644 --- a/src/main/java/excel2db/Excel2dbTask.java +++ b/src/main/java/excel2db/Excel2dbTask.java @@ -4,8 +4,10 @@ import java.io.IOException; import java.sql.SQLException; -import com.ge.mdm.tools.common.ApplicationException; +import excel2db.ApplicationException; +import excel2db.excel2db; import org.apache.commons.io.FilenameUtils; +import org.apache.poi.ss.usermodel.Sheet; public class Excel2dbTask extends Thread { @@ -16,17 +18,20 @@ public Excel2dbTask(excel2db app, File fileName) { public excel2db app; public File fileName; + private Sheet sheet; + + static public Integer numberOfProcessedRows = 0; String tableName; @Override public void run() { try { - app.initInputFiles.initInputFiles(fileName); + sheet = app.initInputFiles.initInputFiles(fileName); tableName = FilenameUtils.removeExtension(fileName.getName()); - app.createTable.createTable(tableName); - app.populateTable.populateTable(tableName); + app.createTable.createTable(sheet, tableName); + numberOfProcessedRows = app.populateTable.populateTable(sheet, tableName); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/excel2db/excel2db.java b/src/main/java/excel2db/excel2db.java index c51a08d..001b582 100644 --- a/src/main/java/excel2db/excel2db.java +++ b/src/main/java/excel2db/excel2db.java @@ -4,13 +4,14 @@ import java.sql.Connection; import java.sql.SQLException; -import com.ge.mdm.tools.common.ApplicationException; +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; @@ -31,10 +32,8 @@ public excel2db(CreateTable createTable, PopulateTable populateTable) { this.populateTable = populateTable; } - //vars are used in interface implementations public static Connection connection = null; - public static Short numberProcessedRecords = Short.valueOf((short) 0); // declaring variables to meet to the Spring framework convention @@ -68,6 +67,11 @@ public static void main(String[] args) { try { + + //for profiling, to launch the VisualVM first, + // then start the application by pressing the Enter + //System.in.read(); + ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); excel2db app = (excel2db) context.getBean("excel2db"); @@ -76,7 +80,8 @@ 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 inn properties file + // iterate through file name values in properties file, + // for each file start a new thread for (String fileNameValue : app.generateFileList.generateFileList()) { File fileName = new File(fileNameValue); taskExecutor.execute(new Excel2dbTask(app, fileName)); @@ -97,17 +102,15 @@ public static void main(String[] args) { } - public void closeConnections() throws ApplicationException, SQLException { - logger.info("Closing connections"); + connection.close(); + logger.info("Connections are closed"); - 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()); + logger.info("The process is completed. Number of processed records: " + Excel2dbTask.numberOfProcessedRows.toString()); } diff --git a/src/main/java/excel2db/service/CreateTable.java b/src/main/java/excel2db/service/CreateTable.java index 3d49482..dfee94e 100644 --- a/src/main/java/excel2db/service/CreateTable.java +++ b/src/main/java/excel2db/service/CreateTable.java @@ -2,8 +2,10 @@ import java.sql.SQLException; +import org.apache.poi.ss.usermodel.Sheet; + public interface CreateTable { - public void createTable(String tableName) throws SQLException; + public void createTable(Sheet sheet, String tableName) throws SQLException; } diff --git a/src/main/java/excel2db/service/InitInputFiles.java b/src/main/java/excel2db/service/InitInputFiles.java index 5baf6dd..c2bacec 100644 --- a/src/main/java/excel2db/service/InitInputFiles.java +++ b/src/main/java/excel2db/service/InitInputFiles.java @@ -3,7 +3,7 @@ import java.io.File; import java.io.IOException; -import com.ge.mdm.tools.common.ApplicationException; +import excel2db.ApplicationException; import org.apache.poi.ss.usermodel.Sheet; public interface InitInputFiles { diff --git a/src/main/java/excel2db/service/PopulateTable.java b/src/main/java/excel2db/service/PopulateTable.java index 6fe97ea..6f6eb32 100644 --- a/src/main/java/excel2db/service/PopulateTable.java +++ b/src/main/java/excel2db/service/PopulateTable.java @@ -2,8 +2,10 @@ import java.sql.SQLException; +import org.apache.poi.ss.usermodel.Sheet; + public interface PopulateTable { - public Integer populateTable(String tableName) throws SQLException; + public Integer populateTable(Sheet sheet, String tableName) throws SQLException; } diff --git a/src/main/java/excel2db/service/impl/CreateTableOracleImpl.java b/src/main/java/excel2db/service/impl/CreateTableOracleImpl.java new file mode 100644 index 0000000..633a033 --- /dev/null +++ b/src/main/java/excel2db/service/impl/CreateTableOracleImpl.java @@ -0,0 +1,19 @@ +package excel2db.service.impl; + +import java.sql.SQLException; +import excel2db.service.CreateTable; +import org.apache.poi.ss.usermodel.Sheet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CreateTableOracleImpl implements CreateTable { + + public static final Logger logger = LoggerFactory.getLogger(CreateTableOracleImpl.class); + + public void createTable(Sheet sheet, String tableName) throws SQLException { + + logger.info("Create table method : Oracle implementation is not covered yet"); + + } + +} diff --git a/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java b/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java index ae2b336..457b923 100644 --- a/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java +++ b/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java @@ -2,21 +2,28 @@ import java.sql.PreparedStatement; import java.sql.SQLException; +import java.util.LinkedHashMap; +import java.util.Map; -import com.ge.mdm.tools.common.SheetEntityManager; +//import com.ge.mdm.tools.common.SheetEntityManager; import excel2db.excel2db; import excel2db.service.CreateTable; +import org.apache.poi.ss.usermodel.Sheet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CreateTablePostgresImpl implements CreateTable { public static final Logger logger = LoggerFactory.getLogger(CreateTablePostgresImpl.class); - private SheetEntityManager sheetEntityManager; + //private SheetEntityManager sheetEntityManager; - public void createTable(String tableName) throws SQLException { + public void createTable(Sheet sheet, String tableName) throws SQLException { + + //sheetEntityManager = new SheetEntityManager(sheet); + //initializing var for a header + Map header = new LinkedHashMap<>(); + header = InitInputFilesImpl.readSheetHeader(sheet); - sheetEntityManager = new SheetEntityManager(InitInputFilesImpl.sheet); //dropping statement String sqlTableDropStatement = "DROP TABLE IF EXISTS \"" + @@ -26,7 +33,7 @@ public void createTable(String tableName) throws SQLException { StringBuilder sqlTableCreateStatement = new StringBuilder(); sqlTableCreateStatement.append("CREATE TABLE \"" + tableName + "\"("); - for (String headerColumnName : sheetEntityManager.header.keySet()) { + for (String headerColumnName : header.keySet()) { sqlTableCreateStatement.append("\"" + headerColumnName + "\"" + " text, "); } diff --git a/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java b/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java index 50287c6..8c4d1f5 100644 --- a/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java +++ b/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java @@ -27,7 +27,6 @@ public class DBConnectionPostgresImpl implements DBConnection { @Value("${db.database}") String dbDatabase; - public static final Logger logger = LoggerFactory.getLogger(DBConnectionPostgresImpl.class); public void establishDBConnection() { diff --git a/src/main/java/excel2db/service/impl/InitInputFilesImpl.java b/src/main/java/excel2db/service/impl/InitInputFilesImpl.java index f297c39..f089c26 100644 --- a/src/main/java/excel2db/service/impl/InitInputFilesImpl.java +++ b/src/main/java/excel2db/service/impl/InitInputFilesImpl.java @@ -2,14 +2,19 @@ import java.io.File; import java.io.IOException; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; -import com.ge.mdm.tools.common.ApplicationException; +import excel2db.ApplicationException; import excel2db.service.InitInputFiles; 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.Cell; +import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -99,5 +104,38 @@ private static Workbook createWorkbook(NPOIFSFileSystem pkg) throws ApplicationE } } + public static Map readSheetHeader(Sheet sheet) { + // first row is always considered as header + Row firstRow = sheet.getRow(sheet.getFirstRowNum()); + if(firstRow == null) { + throw new IllegalArgumentException("Sheet is empty"); + } + + Map header = new LinkedHashMap(); + + Iterator it = firstRow.cellIterator(); + + while(it.hasNext()) { + Cell cell = it.next(); + + if(cell.getCellType() == Cell.CELL_TYPE_STRING) { + String name = cell.getStringCellValue(); + if(! name.equals("")) { + name = name.toUpperCase(); + if(! header.containsKey(name)) { + header.put(name, cell.getColumnIndex()); + } else { + logger.warn("Ignoring duplicate header name {} at ({}, {})", + name, cell.getRowIndex(), cell.getColumnIndex()); + } + } + } else { + logger.warn("Ignoring header cell with non-string type {} at ({}, {})", + cell.getCellType(), cell.getRowIndex(), cell.getColumnIndex()); + } + + } + return header; + } } diff --git a/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java b/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java new file mode 100644 index 0000000..21d117d --- /dev/null +++ b/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java @@ -0,0 +1,24 @@ +package excel2db.service.impl; + +import java.sql.SQLException; +import excel2db.service.PopulateTable; +import org.apache.poi.ss.usermodel.Sheet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PopulateTableOracleImpl implements PopulateTable { + + public static final Logger logger = LoggerFactory.getLogger(PopulateTablePostgresImpl.class); + + private Integer numOfProcessedRows = 0; + + public Integer populateTable(Sheet sheet, String tableName) + throws SQLException { + + logger.info("Populate table method : Oracle implementation is not covered yet"); + + return numOfProcessedRows; + + } + +} diff --git a/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java b/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java index 2a4ed4f..3f868e1 100644 --- a/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java +++ b/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java @@ -8,6 +8,7 @@ import excel2db.service.PopulateTable; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,8 +19,7 @@ public class PopulateTablePostgresImpl implements PopulateTable { private String cellStringValue; private Integer numOfProcessedRows = 0; - - public Integer populateTable(String tableName) + public Integer populateTable(Sheet sheet, String tableName) throws SQLException { Iterator rowIterator = InitInputFilesImpl.sheet.rowIterator(); @@ -34,10 +34,9 @@ public Integer populateTable(String tableName) while (rowIterator.hasNext()) { Row row = (Row) rowIterator.next(); - numOfProcessedRows = numOfProcessedRows++; - Short localShort1 = excel2db.numberProcessedRecords; - Short localShort2 = excel2db.numberProcessedRecords = Short.valueOf((short) (excel2db.numberProcessedRecords.shortValue() + 1)); + //count rows + numOfProcessedRows++; StringBuilder sqlTableInsertStatement = new StringBuilder(); sqlTableInsertStatement.append("INSERT INTO \"" + @@ -45,11 +44,9 @@ public Integer populateTable(String tableName) sqlTableInsertStatement.append("("); - short minColIdx = row.getFirstCellNum(); short maxColIdx = row.getLastCellNum(); - for (short colIdx = minColIdx; colIdx < maxColIdx; colIdx = (short) (colIdx + 1)) { cellStringValue = ""; Cell cell = row.getCell(colIdx); @@ -73,5 +70,4 @@ public Integer populateTable(String tableName) } - } diff --git a/src/main/resources/excel2db.properties b/src/main/resources/excel2db.properties index 2c3dcc5..244c517 100644 --- a/src/main/resources/excel2db.properties +++ b/src/main/resources/excel2db.properties @@ -1,6 +1,17 @@ -# postgres parameters +# names of input tables +input.files=test-input.xls,test-input1.xls + +# db implementation classes + +# postgres section +# dbConnectionImplenentation=excel2db.service.impl.DBConnectionPostgresImpl +# oracle section +dbConnectionImplenentation=excel2db.service.impl.DBConnectionOracleImpl + +# postgres connection parameters db.server=localhost db.database=excel2db db.port=5433 db.user=postgres db.password=postgres + diff --git a/src/main/resources/spring-batch.xml b/src/main/resources/spring-batch.xml deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/spring-job.xml b/src/main/resources/spring-job.xml deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/spring.xml b/src/main/resources/spring.xml index b2cc20f..0b600ba 100644 --- a/src/main/resources/spring.xml +++ b/src/main/resources/spring.xml @@ -1,35 +1,62 @@ + - + + - - + - + - + + + + + - - - + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/InitInputFilesImplTest.java b/src/test/java/InitInputFilesImplTest.java index 8a12445..ea19f69 100644 --- a/src/test/java/InitInputFilesImplTest.java +++ b/src/test/java/InitInputFilesImplTest.java @@ -1,19 +1,42 @@ +// verify if the application reads the Excel sheet by checking sheet name + import java.io.File; +import excel2db.ApplicationContextUtils; import excel2db.excel2db; import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; import org.springframework.context.ApplicationContext; +import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.assertEquals; +//specify category of the class + runner +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "/spring.xml") +@Category(JUnitInitFileCategory.class) +@TestPropertySource("/excel2db.properties") public class InitInputFilesImplTest { @Test public void testInitInputFilesImpl() throws Exception { + //initializing application context + //need to check if we really need to get the spring.xml if the config is in "locations" +// AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); +// ApplicationContextUtils appcontext= (ApplicationContextUtils) context.getBean("applicationContextUtils"); +// ApplicationContext appCon = appcontext.getApplicationContext(); +// excel2db app = (excel2db) appCon.getBean("excel2db"); + ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); excel2db app = (excel2db) context.getBean("excel2db"); + + //executing the test File fileName = new File(app.initConstants.workingDir + "/unitTest.xls"); //here we should initialize the sheet object diff --git a/src/test/java/JUnitInitFileCategory.java b/src/test/java/JUnitInitFileCategory.java new file mode 100644 index 0000000..714a9a9 --- /dev/null +++ b/src/test/java/JUnitInitFileCategory.java @@ -0,0 +1,3 @@ + +public interface JUnitInitFileCategory { +} diff --git a/src/test/java/JUnitPopulateTableCategory.java b/src/test/java/JUnitPopulateTableCategory.java new file mode 100644 index 0000000..74978df --- /dev/null +++ b/src/test/java/JUnitPopulateTableCategory.java @@ -0,0 +1,3 @@ + +public interface JUnitPopulateTableCategory { +} diff --git a/src/test/java/JUnitSuiteInitFiles.java b/src/test/java/JUnitSuiteInitFiles.java new file mode 100644 index 0000000..04457cd --- /dev/null +++ b/src/test/java/JUnitSuiteInitFiles.java @@ -0,0 +1,15 @@ +import org.junit.experimental.categories.Categories; +import org.junit.experimental.categories.Categories.IncludeCategory; +import org.junit.runner.RunWith; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Categories.class) +@IncludeCategory(JUnitInitFileCategory.class) +@SuiteClasses({InitInputFilesImplTest.class}) +public class JUnitSuiteInitFiles { + + +} + + + diff --git a/src/test/java/JUnitSuitePopulateTable.java b/src/test/java/JUnitSuitePopulateTable.java new file mode 100644 index 0000000..58483f3 --- /dev/null +++ b/src/test/java/JUnitSuitePopulateTable.java @@ -0,0 +1,11 @@ +import org.junit.experimental.categories.Categories; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + + +@RunWith(Categories.class) +@Categories.IncludeCategory(JUnitPopulateTableCategory.class) +@Suite.SuiteClasses({PopulateTablePostgresImplTest.class}) +public class JUnitSuitePopulateTable { + +} diff --git a/src/test/java/PopulateTablePostgresImplTest.java b/src/test/java/PopulateTablePostgresImplTest.java index 467b684..72c1b46 100644 --- a/src/test/java/PopulateTablePostgresImplTest.java +++ b/src/test/java/PopulateTablePostgresImplTest.java @@ -1,16 +1,47 @@ -import excel2db.service.impl.PopulateTablePostgresImpl; +import java.io.File; + +import excel2db.ApplicationContextUtils; +import excel2db.excel2db; +import org.apache.poi.ss.usermodel.Sheet; import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + import static org.junit.Assert.assertEquals; -public class PopulateTablePostgresImplTest { +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "/spring.xml") +@Category(JUnitPopulateTableCategory.class) +public class PopulateTablePostgresImplTest { + + private Sheet sheet; @Test public void testPopulateTable() throws Exception { - Integer processedRows = 100; - PopulateTablePostgresImpl popTableObject = new PopulateTablePostgresImpl(); + + //initializing application context +// AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); +// ApplicationContextUtils appcontext= (ApplicationContextUtils) context.getBean("applicationContextUtils"); +// ApplicationContext appCon =appcontext.getApplicationContext(); +// excel2db app = (excel2db) appCon.getBean("excel2db"); + + ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); + excel2db app = (excel2db) context.getBean("excel2db"); + + //executing the test + File fileName = new File(app.initConstants.workingDir + "/unitTest.xls"); + sheet = app.initInputFiles.initInputFiles(fileName); + app.dbConnection.establishDBConnection(); + app.createTable.createTable(sheet, "testTable"); //here we should initialize the sheet object - assertEquals(processedRows, processedRows ); + Integer numOfProcessedRows = app.populateTable.populateTable(sheet, "testTable"); + assertEquals(Integer.valueOf(9), numOfProcessedRows); } } \ No newline at end of file diff --git a/unitTest.xls b/unitTest.xls new file mode 100644 index 0000000..3b58ff3 Binary files /dev/null and b/unitTest.xls differ