Skip to content

Commit e8061e8

Browse files
Added jUnit test
1 parent c446430 commit e8061e8

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,18 @@
8383
<version>4.6</version>
8484
</dependency>
8585

86-
<!-- for testing -->
86+
<dependency>
87+
<groupId>org.hsqldb</groupId>
88+
<artifactId>hsqldb</artifactId>
89+
<version>2.3.4</version>
90+
<scope>test</scope>
91+
</dependency>
92+
8793
<dependency>
8894
<groupId>junit</groupId>
8995
<artifactId>junit</artifactId>
90-
<version>4.10</version>
96+
<version>4.12</version>
9197
<scope>test</scope>
92-
<exclusions>
93-
<exclusion>
94-
<groupId>org.hamcrest</groupId>
95-
<artifactId>hamcrest-core</artifactId>
96-
</exclusion>
97-
</exclusions>
9898
</dependency>
9999

100100
</dependencies>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package test.org.fugerit.java.query.export.tool;
2+
3+
import java.io.IOException;
4+
import java.sql.Connection;
5+
import java.sql.DriverManager;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
9+
import org.junit.AfterClass;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public class TestBase {
14+
15+
protected final static Logger logger = LoggerFactory.getLogger( TestBase.class );
16+
17+
/**
18+
* Database initialization for testing i.e.
19+
* <ul>
20+
* <li>Creating Table</li>
21+
* <li>Inserting record</li>
22+
* </ul>
23+
*
24+
* @throws SQLException
25+
*/
26+
protected static void initDatabase() throws SQLException {
27+
try (Connection connection = getConnection(); Statement statement = connection.createStatement();) {
28+
statement.execute("CREATE TABLE test_export (id INT NOT NULL, name VARCHAR(50) NOT NULL,"
29+
+ "email VARCHAR(50) NOT NULL, PRIMARY KEY (id))");
30+
connection.commit();
31+
statement.executeUpdate("INSERT INTO test_export VALUES (1001,'FieldA1', 'FieldB1')");
32+
statement.executeUpdate("INSERT INTO test_export VALUES (1002,'FieldA2', 'FieldB2')");
33+
statement.executeUpdate("INSERT INTO test_export VALUES (1003,'FieldA3', 'FieldB3')");
34+
connection.commit();
35+
}
36+
}
37+
38+
/**
39+
* Create a connection
40+
*
41+
* @return connection object
42+
* @throws SQLException
43+
*/
44+
protected static Connection getConnection() throws SQLException {
45+
return DriverManager.getConnection("jdbc:hsqldb:mem:employees", "vinod", "vinod");
46+
}
47+
48+
@AfterClass
49+
public static void destroy() throws SQLException, ClassNotFoundException, IOException {
50+
try (Connection connection = getConnection(); Statement statement = connection.createStatement();) {
51+
statement.executeUpdate("DROP TABLE test_export");
52+
connection.commit();
53+
}
54+
}
55+
56+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package test.org.fugerit.java.query.export.tool;
2+
3+
import static org.junit.Assert.fail;
4+
5+
import java.io.File;
6+
import java.io.FileOutputStream;
7+
8+
import org.fugerit.java.query.export.facade.QueryExportConfig;
9+
import org.fugerit.java.query.export.facade.QueryExportFacade;
10+
import org.junit.Test;
11+
12+
public class TestCSV extends TestBase {
13+
14+
@Test
15+
public void test() {
16+
try {
17+
initDatabase();
18+
logger.info( "test start" );
19+
FileOutputStream fos = new FileOutputStream( new File( "target/test_export.csv" ) );
20+
String query = " SELECT * FROM test_export ";
21+
QueryExportConfig config = QueryExportConfig.newConfigCSV( fos , getConnection(), query );
22+
QueryExportFacade.export( config );
23+
logger.info( "test end" );
24+
} catch (Exception e) {
25+
String message = "Error "+e.getMessage();
26+
logger.error( message, e );
27+
fail( message );
28+
}
29+
30+
}
31+
32+
}

0 commit comments

Comments
 (0)