Skip to content

Commit

Permalink
Added jUnit test
Browse files Browse the repository at this point in the history
  • Loading branch information
daneeldeveloper committed Jun 11, 2019
1 parent c446430 commit e8061e8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@
<version>4.6</version>
</dependency>

<!-- for testing -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package test.org.fugerit.java.query.export.tool;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.AfterClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestBase {

protected final static Logger logger = LoggerFactory.getLogger( TestBase.class );

/**
* Database initialization for testing i.e.
* <ul>
* <li>Creating Table</li>
* <li>Inserting record</li>
* </ul>
*
* @throws SQLException
*/
protected static void initDatabase() throws SQLException {
try (Connection connection = getConnection(); Statement statement = connection.createStatement();) {
statement.execute("CREATE TABLE test_export (id INT NOT NULL, name VARCHAR(50) NOT NULL,"
+ "email VARCHAR(50) NOT NULL, PRIMARY KEY (id))");
connection.commit();
statement.executeUpdate("INSERT INTO test_export VALUES (1001,'FieldA1', 'FieldB1')");
statement.executeUpdate("INSERT INTO test_export VALUES (1002,'FieldA2', 'FieldB2')");
statement.executeUpdate("INSERT INTO test_export VALUES (1003,'FieldA3', 'FieldB3')");
connection.commit();
}
}

/**
* Create a connection
*
* @return connection object
* @throws SQLException
*/
protected static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:hsqldb:mem:employees", "vinod", "vinod");
}

@AfterClass
public static void destroy() throws SQLException, ClassNotFoundException, IOException {
try (Connection connection = getConnection(); Statement statement = connection.createStatement();) {
statement.executeUpdate("DROP TABLE test_export");
connection.commit();
}
}

}
32 changes: 32 additions & 0 deletions src/test/java/test/org/fugerit/java/query/export/tool/TestCSV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package test.org.fugerit.java.query.export.tool;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileOutputStream;

import org.fugerit.java.query.export.facade.QueryExportConfig;
import org.fugerit.java.query.export.facade.QueryExportFacade;
import org.junit.Test;

public class TestCSV extends TestBase {

@Test
public void test() {
try {
initDatabase();
logger.info( "test start" );
FileOutputStream fos = new FileOutputStream( new File( "target/test_export.csv" ) );
String query = " SELECT * FROM test_export ";
QueryExportConfig config = QueryExportConfig.newConfigCSV( fos , getConnection(), query );
QueryExportFacade.export( config );
logger.info( "test end" );
} catch (Exception e) {
String message = "Error "+e.getMessage();
logger.error( message, e );
fail( message );
}

}

}

0 comments on commit e8061e8

Please sign in to comment.