diff --git a/build.gradle b/build.gradle index ced81c5..c1c6aef 100644 --- a/build.gradle +++ b/build.gradle @@ -92,10 +92,11 @@ jar { testLogging.showStandardStreams = true testLogging.exceptionFormat = 'full' - //exclude certain classes to avoid double running - // - suites + test classes + //exclude certain classes to avoid double running - suites + test classes exclude '**/InitInputFilesImplTest.class' exclude '**/PopulateTablePostgresImplTest.class' + exclude '**/PopulateCellsImplTest.class' + } //the copyFiles task executes right after the shadowJar and copy diff --git a/src/main/java/excel2db/excel2db.java b/src/main/java/excel2db/excel2db.java index 016646d..819ae75 100644 --- a/src/main/java/excel2db/excel2db.java +++ b/src/main/java/excel2db/excel2db.java @@ -9,6 +9,7 @@ import excel2db.service.CreateTable; import excel2db.service.DBConnection; import excel2db.service.GenerateFileList; +import excel2db.service.GetFirstRow; import excel2db.service.InitConstants; import excel2db.service.InitInputFiles; import excel2db.service.PopulateTable; @@ -41,6 +42,9 @@ public excel2db(CreateTable createTable, PopulateTable populateTable) { public CreateTable createTable; public PopulateTable populateTable; public InitConstants initConstants; + public GetFirstRow getFirstRow; + + public static ThreadPoolTaskExecutor taskExecutor; @@ -58,6 +62,9 @@ public void setGenerateFileList(GenerateFileList generateFileList) { public void setInitConstants(InitConstants initConstants) { this.initConstants = initConstants; } + public void setGetFirstRow(GetFirstRow getFirstRow) { + this.getFirstRow = getFirstRow; + } public static void main(String[] args) { diff --git a/src/main/java/excel2db/service/GetFirstRow.java b/src/main/java/excel2db/service/GetFirstRow.java new file mode 100644 index 0000000..ff9ebcc --- /dev/null +++ b/src/main/java/excel2db/service/GetFirstRow.java @@ -0,0 +1,10 @@ +package excel2db.service; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public interface GetFirstRow { + + public ResultSet getFirstRow (String tableName) throws SQLException; + +} diff --git a/src/main/java/excel2db/service/impl/GetFirstRowDerbyImpl.java b/src/main/java/excel2db/service/impl/GetFirstRowDerbyImpl.java new file mode 100644 index 0000000..3a8d63e --- /dev/null +++ b/src/main/java/excel2db/service/impl/GetFirstRowDerbyImpl.java @@ -0,0 +1,24 @@ +package excel2db.service.impl; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import excel2db.excel2db; +import excel2db.service.GetFirstRow; + +public class GetFirstRowDerbyImpl implements GetFirstRow { + + ResultSet firstRow ; + + @Override + public ResultSet getFirstRow(String tableName) throws SQLException { + + String sqlSelectFirstRow = "SELECT * FROM " + tableName + " FETCH FIRST ROW ONLY"; + PreparedStatement pstmtSelectFirstRow = excel2db.connection.prepareStatement(sqlSelectFirstRow); + firstRow = pstmtSelectFirstRow.executeQuery(); + return firstRow; + + } + +} diff --git a/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java b/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java index 3f868e1..7642f8e 100644 --- a/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java +++ b/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java @@ -64,8 +64,11 @@ public Integer populateTable(Sheet sheet, String tableName) pstmtInsertRow.execute(); + } + logger.info("The table {} is populated", tableName); + return numOfProcessedRows; } diff --git a/src/main/resources/excel2db.properties b/src/main/resources/excel2db.properties index f5515b8..1196c0d 100644 --- a/src/main/resources/excel2db.properties +++ b/src/main/resources/excel2db.properties @@ -27,5 +27,6 @@ db.oracle.password=oracle # 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 +getFirstRowImplenentation=excel2db.service.impl.GetFirstRow${db.implementation}Impl diff --git a/src/main/resources/spring.xml b/src/main/resources/spring.xml index 2379019..5285dae 100644 --- a/src/main/resources/spring.xml +++ b/src/main/resources/spring.xml @@ -16,11 +16,11 @@ - + @@ -29,6 +29,7 @@ + @@ -40,6 +41,10 @@ + + + + diff --git a/src/test/java/InitInputFilesImplTest.java b/src/test/java/InitInputFilesImplTest.java index fa8739d..9de921b 100644 --- a/src/test/java/InitInputFilesImplTest.java +++ b/src/test/java/InitInputFilesImplTest.java @@ -1,4 +1,4 @@ -// verify if the application reads the Excel sheet by checking sheet name +// verify if the application reads the Excel sheet by checking a sheet name import java.io.File; diff --git a/src/test/java/JUnitPopulateCellsCategory.java b/src/test/java/JUnitPopulateCellsCategory.java new file mode 100644 index 0000000..df7b620 --- /dev/null +++ b/src/test/java/JUnitPopulateCellsCategory.java @@ -0,0 +1,3 @@ + +public interface JUnitPopulateCellsCategory { +} diff --git a/src/test/java/JUnitSuiteInitFiles.java b/src/test/java/JUnitSuiteInitFiles.java index 04457cd..6516784 100644 --- a/src/test/java/JUnitSuiteInitFiles.java +++ b/src/test/java/JUnitSuiteInitFiles.java @@ -9,6 +9,8 @@ public class JUnitSuiteInitFiles { + + } diff --git a/src/test/java/JUnitSuitePopulateCells.java b/src/test/java/JUnitSuitePopulateCells.java new file mode 100644 index 0000000..cc7af34 --- /dev/null +++ b/src/test/java/JUnitSuitePopulateCells.java @@ -0,0 +1,14 @@ + + +import org.junit.experimental.categories.Categories; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Categories.class) +@Categories.IncludeCategory(JUnitPopulateCellsCategory.class) +@Suite.SuiteClasses(PopulateCellsImplTest.class) +public class JUnitSuitePopulateCells { + +} + + diff --git a/src/test/java/JUnitSuitePopulateTable.java b/src/test/java/JUnitSuitePopulateTable.java index d09efe6..48fe831 100644 --- a/src/test/java/JUnitSuitePopulateTable.java +++ b/src/test/java/JUnitSuitePopulateTable.java @@ -4,7 +4,7 @@ @RunWith(Categories.class) @Categories.IncludeCategory(JUnitPopulateTableCategory.class) -@Suite.SuiteClasses({PopulateTableImplTest.class}) +@Suite.SuiteClasses(PopulateTableImplTest.class) public class JUnitSuitePopulateTable { } diff --git a/src/test/java/PopulateCellsImplTest.java b/src/test/java/PopulateCellsImplTest.java new file mode 100644 index 0000000..3dd076d --- /dev/null +++ b/src/test/java/PopulateCellsImplTest.java @@ -0,0 +1,66 @@ +import java.io.File; +import java.io.IOException; +import java.sql.ResultSet; + +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.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; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "/spring.xml") +@Category(JUnitPopulateCellsCategory.class) +@TestPropertySource("/excel2db.properties") + +public class PopulateCellsImplTest { + + private Sheet sheet; + private String resultSetValue; + + @Test + public void testPopulateCellsImpl() throws Exception { + + //initializing application context + ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); + excel2db app = (excel2db) context.getBean("excel2db"); + //executing the test + File fileName = new File(app.initConstants.workingDir + "/test.xlsx"); + + sheet = app.initInputFiles.initInputFiles(fileName); + + //create and populate a table + app.dbConnection.establishDBConnection(); + app.createTable.createTable(sheet, "TEST"); + app.populateTable.populateTable(sheet, "TEST"); + ResultSet rs = app.getFirstRow.getFirstRow("TEST"); + + //concatenate values in the result set row + while (rs.next()){ + resultSetValue = + rs.getString("Column1") + " " + + rs.getString("Column2") + " " + + rs.getString("Column3") + " " + + rs.getString("Column4") + " " + + rs.getString("Column5") + " " + + rs.getString("Column6") + " " + + rs.getString("Column7") + " " + + rs.getString("Column8") + " " + + rs.getString("Column9") + " " + + rs.getString("Column10") + ; + } + + //compare with the expected result + assertEquals("Value 11 Value 12 Value 13 Value 14 Value 15 Value 16 Value 17 Value 18 Value 19 Value 110", resultSetValue); + + } + +} diff --git a/src/test/java/PopulateTableImplTest.java b/src/test/java/PopulateTableImplTest.java index b94257e..36e8880 100644 --- a/src/test/java/PopulateTableImplTest.java +++ b/src/test/java/PopulateTableImplTest.java @@ -35,7 +35,6 @@ public void testPopulateTable() throws Exception { app.dbConnection.establishDBConnection(); app.createTable.createTable(sheet, "testTable"); - //here we should initialize the sheet object Integer numOfProcessedRows = app.populateTable.populateTable(sheet, "testTable"); assertEquals(Integer.valueOf(2), numOfProcessedRows);