From 2d8383ab6eca276e3c98ddebd6f4be7fc4ecefd7 Mon Sep 17 00:00:00 2001 From: Andrey Bespalov Date: Fri, 26 May 2017 23:49:15 +0300 Subject: [PATCH] Submitting the current version: additional implementations of Oracle classes (empty) --- .gradle/2.13/taskArtifacts/cache.properties | 1 + .gradle/3.1/taskArtifacts/cache.properties | 1 + .gradle/buildOutputCleanup/cache.properties | 2 + .../buildOutputCleanup/cache.properties | 2 + build/resources/main/excel2db.properties | 17 ++++++ .../tools/common/ApplicationException.java | 3 - .../mdm/tools/common/AuthOutInterceptor.java | 3 - .../mdm/tools/common/SheetEntityManager.java | 1 - .../com/ge/mdm/tools/common/TaskExecutor.java | 4 +- .../excel2db/ApplicationContextUtils.java | 21 +++++++ .../java/excel2db/ApplicationException.java | 25 ++++++++ src/main/java/excel2db/Excel2dbTask.java | 13 +++-- src/main/java/excel2db/excel2db.java | 21 ++++--- .../java/excel2db/service/CreateTable.java | 4 +- .../java/excel2db/service/InitInputFiles.java | 2 +- .../java/excel2db/service/PopulateTable.java | 4 +- .../service/impl/CreateTableOracleImpl.java | 19 ++++++ .../service/impl/CreateTablePostgresImpl.java | 17 ++++-- .../impl/DBConnectionPostgresImpl.java | 1 - .../service/impl/InitInputFilesImpl.java | 40 ++++++++++++- .../service/impl/PopulateTableOracleImpl.java | 24 ++++++++ .../impl/PopulateTablePostgresImpl.java | 12 ++-- src/main/resources/excel2db.properties | 13 ++++- src/main/resources/spring-batch.xml | 0 src/main/resources/spring-job.xml | 0 src/main/resources/spring.xml | 55 +++++++++++++----- src/test/java/InitInputFilesImplTest.java | 23 ++++++++ src/test/java/JUnitInitFileCategory.java | 3 + src/test/java/JUnitPopulateTableCategory.java | 3 + src/test/java/JUnitSuiteInitFiles.java | 15 +++++ src/test/java/JUnitSuitePopulateTable.java | 11 ++++ .../java/PopulateTablePostgresImplTest.java | 41 +++++++++++-- unitTest.xls | Bin 0 -> 22528 bytes 33 files changed, 340 insertions(+), 61 deletions(-) create mode 100644 .gradle/2.13/taskArtifacts/cache.properties create mode 100644 .gradle/3.1/taskArtifacts/cache.properties create mode 100644 .gradle/buildOutputCleanup/cache.properties create mode 100644 build/resources/main/.gradle/buildOutputCleanup/cache.properties create mode 100644 build/resources/main/excel2db.properties create mode 100644 src/main/java/excel2db/ApplicationContextUtils.java create mode 100644 src/main/java/excel2db/ApplicationException.java create mode 100644 src/main/java/excel2db/service/impl/CreateTableOracleImpl.java create mode 100644 src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java delete mode 100644 src/main/resources/spring-batch.xml delete mode 100644 src/main/resources/spring-job.xml create mode 100644 src/test/java/JUnitInitFileCategory.java create mode 100644 src/test/java/JUnitPopulateTableCategory.java create mode 100644 src/test/java/JUnitSuiteInitFiles.java create mode 100644 src/test/java/JUnitSuitePopulateTable.java create mode 100644 unitTest.xls 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 0000000000000000000000000000000000000000..3b58ff314a4824b3f08dad748785db33e05409b1 GIT binary patch literal 22528 zcmeI43!EEAeaA=IUG1*lk35~k**I|`C-zy7dnP2YPN%b#-03co&SxiqL%!45N_3LZ z=^VR-VxFZ1TEH}^fi}>lFWwXiZ3)4lEv2m~G%cl2QUWa$%Ae1F~+u~2vf*E@W@L5Lf0 zO<~`kpP%=Garx8Fk5LPJ&#SA34%BfNIH=D}92O26$0{7Faje0y7RNdq>v3QRh>bWl z;nz zeFgFt(X+ycWHA0~q9W>IL9F;u#$GAQwS-@8EHaET8{)86vKt@NYw7sg&vXB!=ZW8V zp7_n@iC=Y|czfCSt>RN?aR2!u!i;3)lh?!{+5Sn)5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