Skip to content

Commit adefdc3

Browse files
committed
JUnit for QueryHelper api
1 parent 0177086 commit adefdc3

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.fugerit.java.core.db.dao.daogen;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
6+
import org.fugerit.java.core.db.dao.RSExtractor;
7+
8+
import test.org.fugerit.java.core.db.helpers.MemDBTestBase;
9+
10+
public class TestAddressSelectHelper extends MemDBTestBase {
11+
12+
public static final String COL_INFO = "INFO";
13+
14+
public static RSExtractor<String> RSE_ADDRESS_COL_INFO = new RSExtractor<String>() {
15+
@Override
16+
public String extractNext(ResultSet rs) throws SQLException {
17+
return rs.getString( COL_INFO );
18+
}
19+
};
20+
21+
22+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package test.org.fugerit.java.core.db.dao.daogen;
2+
3+
import java.sql.Statement;
4+
5+
import org.fugerit.java.core.db.daogen.BasicDAOHelper;
6+
import org.fugerit.java.core.db.daogen.CloseableDAOContext;
7+
import org.fugerit.java.core.db.daogen.CloseableDAOContextSC;
8+
import org.fugerit.java.core.db.daogen.SelectHelper;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
public class TestSelectHelperSimple extends TestAddressSelectHelper {
13+
14+
15+
protected void testQueryHelperSimple( String syntax ) {
16+
try ( CloseableDAOContext context = new CloseableDAOContextSC( this.getConnection() ) ) {
17+
try ( Statement stm = context.getConnection().createStatement() ) {
18+
stm.execute( "SET DATABASE SQL SYNTAX "+syntax+" TRUE" );
19+
}
20+
// SelectHelper setup
21+
BasicDAOHelper<String> daoHelper = new BasicDAOHelper<>(context);
22+
SelectHelper selectHelper = daoHelper.newSelectHelper( "fugerit.address" );
23+
// add param
24+
selectHelper.andEqualParam( COL_INFO , "Test address 01" );
25+
logger.info( "sql -> {}", selectHelper.getQueryContent() ); // method getQueryContent() provides access to current query buffer
26+
// result
27+
String res = daoHelper.loadOneHelper( selectHelper, RSE_ADDRESS_COL_INFO );
28+
logger.info( "res -> {}", res );
29+
Assert.assertNotNull( "Result should not be null" , res );
30+
} catch (Exception e) {
31+
this.failEx(e);
32+
}
33+
}
34+
35+
@Test
36+
public void testQuerySelectSimplePostgres() {
37+
testQueryHelperSimple( "PGS" );
38+
}
39+
40+
@Test
41+
public void testQuerySelectSimpleOracle() {
42+
testQueryHelperSimple( "ORA" );
43+
}
44+
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package test.org.fugerit.java.core.db.dao.daogen;
2+
3+
import java.sql.Statement;
4+
5+
import org.fugerit.java.core.db.daogen.BasicDAOHelper;
6+
import org.fugerit.java.core.db.daogen.CloseableDAOContext;
7+
import org.fugerit.java.core.db.daogen.CloseableDAOContextSC;
8+
import org.fugerit.java.core.db.daogen.SelectHelper;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
public class TestSelectHelperUpper extends TestAddressSelectHelper {
13+
14+
protected void testQueryHelperIgnoreCase( String syntax ) {
15+
try ( CloseableDAOContext context = new CloseableDAOContextSC( this.getConnection() ) ) {
16+
try ( Statement stm = context.getConnection().createStatement() ) {
17+
stm.execute( "SET DATABASE SQL SYNTAX "+syntax+" TRUE" );
18+
}
19+
// SelectHelper setup
20+
BasicDAOHelper<String> daoHelper = new BasicDAOHelper<>(context);
21+
SelectHelper selectHelper = daoHelper.newSelectHelper( "fugerit.address" );
22+
// select helper is basically a query builder, it is possible to add functions, instead of the simple column name
23+
String column = String.format( "UPPER(%s)" , COL_INFO ); // will add 'UPPER(INFO)'
24+
// it is possible to transform upper case the value to be compared
25+
String value = "test address 01".toUpperCase();
26+
// this will perform an ignore case comparison, as both the column and the value are now UPPERCASE
27+
selectHelper.andEqualParam( column , value );
28+
logger.info( "sql -> {}", selectHelper.getQueryContent() ); // method getQueryContent() provides access to current query buffer
29+
// the result
30+
String res = daoHelper.loadOneHelper( selectHelper, RSE_ADDRESS_COL_INFO );
31+
logger.info( "res -> {}", res );
32+
Assert.assertNotNull( "Result should not be null" , res );
33+
} catch (Exception e) {
34+
this.failEx(e);
35+
}
36+
}
37+
38+
@Test
39+
public void testQuerySelectIgnoreCasePostgres() {
40+
testQueryHelperIgnoreCase( "PGS" );
41+
}
42+
43+
@Test
44+
public void testQuerySelectgnoreCaseOracle() {
45+
testQueryHelperIgnoreCase( "ORA" );
46+
}
47+
48+
}

fj-core/src/test/resources/test/memdb/base_db_init.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ CREATE SEQUENCE fugerit.seq_test START WITH 1;
6565
INSERT INTO fugerit.user ( id, username, password, last_login, state ) VALUES ( nextval('fugerit.seq_test'), 'user1', 'a3f8h5h4h3n3n1n9', sysdate, 1 );
6666
INSERT INTO fugerit.user ( id, username, password, last_login, state ) VALUES ( nextval('fugerit.seq_test'), 'user2', '5h4h3n3n1n9a3f8h', sysdate, 0 );
6767

68-
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( nextval('fugerit.seq_test'), 1, 'test address 01' );
69-
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( nextval('fugerit.seq_test'), 2, 'test address 02' );
70-
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( nextval('fugerit.seq_test'), 1, 'test address 03' );
68+
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( nextval('fugerit.seq_test'), 1, 'Test address 01' );
69+
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( nextval('fugerit.seq_test'), 2, 'Test address 02' );
70+
INSERT INTO fugerit.address ( id, id_user, info ) VALUES ( nextval('fugerit.seq_test'), 1, 'Test address 03' );
7171

7272
INSERT INTO fugerit.test_two_field_key ( id_one, id_two, info ) VALUES ( nextval('fugerit.seq_test'), nextval('fugerit.seq_test'), 'test info 01' );

0 commit comments

Comments
 (0)