Skip to content

Commit

Permalink
Add an additional test to verify cells population
Browse files Browse the repository at this point in the history
  • Loading branch information
palych-piter committed Aug 5, 2017
1 parent 089b296 commit a4e3ea3
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 6 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/excel2db/excel2db.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/excel2db/service/GetFirstRow.java
Original file line number Diff line number Diff line change
@@ -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;

}
24 changes: 24 additions & 0 deletions src/main/java/excel2db/service/impl/GetFirstRowDerbyImpl.java
Original file line number Diff line number Diff line change
@@ -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;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ public Integer populateTable(Sheet sheet, String tableName)

pstmtInsertRow.execute();


}

logger.info("The table {} is populated", tableName);

return numOfProcessedRows;

}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/excel2db.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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


7 changes: 6 additions & 1 deletion src/main/resources/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

<bean id="excel2db" class="excel2db.excel2db" scope="singleton">

<!--<property name="dbConnection" ref="dbConnection"></property>-->
<property name="dbConnection" ref="dbConnection"></property>
<property name="initInputFiles" ref="initInputFiles"></property>
<property name="generateFileList" ref="generateFileList"></property>
<property name="initConstants" ref="initConstants"></property>
<property name="getFirstRow" ref="getFirstRow"></property>

<constructor-arg>
<ref bean="createTable"/>
Expand All @@ -29,6 +29,7 @@
<ref bean="populateTable"/>
</constructor-arg>


</bean>

<!-- This way it is possible to get values from a properties file nd use the values -->
Expand All @@ -40,6 +41,10 @@
<bean id="createTable" class="${createTableImplenentation}"></bean>
<!--<bean id="createTable" class="excel2db.service.impl.CreateTablePostgresImpl"></bean>-->

<!--<bean id="getFirstRow" class="${getFirstRowImplementation}"></bean>-->
<bean id="getFirstRow" class="excel2db.service.impl.GetFirstRowDerbyImpl"></bean>


<bean id="populateTable" class="excel2db.service.impl.PopulateTablePostgresImpl"></bean>

<bean id="initInputFiles" class="excel2db.service.impl.InitInputFilesImpl"></bean>
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/InitInputFilesImplTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
3 changes: 3 additions & 0 deletions src/test/java/JUnitPopulateCellsCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

public interface JUnitPopulateCellsCategory {
}
2 changes: 2 additions & 0 deletions src/test/java/JUnitSuiteInitFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
public class JUnitSuiteInitFiles {




}


Expand Down
14 changes: 14 additions & 0 deletions src/test/java/JUnitSuitePopulateCells.java
Original file line number Diff line number Diff line change
@@ -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 {

}


2 changes: 1 addition & 1 deletion src/test/java/JUnitSuitePopulateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@RunWith(Categories.class)
@Categories.IncludeCategory(JUnitPopulateTableCategory.class)
@Suite.SuiteClasses({PopulateTableImplTest.class})
@Suite.SuiteClasses(PopulateTableImplTest.class)
public class JUnitSuitePopulateTable {

}
66 changes: 66 additions & 0 deletions src/test/java/PopulateCellsImplTest.java
Original file line number Diff line number Diff line change
@@ -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);

}

}
1 change: 0 additions & 1 deletion src/test/java/PopulateTableImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit a4e3ea3

Please sign in to comment.