Skip to content

Commit

Permalink
Merge pull request #33 from fugerit-org/feature/issue_31_coverage_80
Browse files Browse the repository at this point in the history
Coverate result set extractor
  • Loading branch information
fugerit79 authored Sep 8, 2023
2 parents cecbefc + ac062fc commit 894234d
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public BigDecimalRSE(String name) {

@Override
protected BigDecimal convert(Object o) {
// not used in this rs exctractor
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public abstract class PropertyRSE implements RSExtractor<Properties>, Serializab
*/
private static final long serialVersionUID = -2284313730808746483L;

public static final PropertyRSE DEFAULT_REUSABLE = newReusableRSE();

/**
* Creates a new reusable PropertyRSE
*
Expand Down Expand Up @@ -84,6 +86,23 @@ public static PropertyRSE newNoReusableRSE( final ResultSet configRS ) throws SQ
return new PropertyRSECached( configRS );
}

public static PropertyRSE newAutoCachingMetadataRSE() {
return new PropertyRSE() {
/**
*
*/
private static final long serialVersionUID = -1924594840530557931L;
private PropertyRSE wrapped = null;
@Override
public Properties extractNext(ResultSet rs) throws SQLException {
if ( this.wrapped == null ) {
this.wrapped = PropertyRSECached.newNoReusableRSE( rs );
}
return this.wrapped.extractNext(rs);
}
};
}

@Override
public abstract Properties extractNext(ResultSet rs) throws SQLException;

Expand Down Expand Up @@ -112,7 +131,7 @@ public PropertyRSECached( ResultSet configRS ) throws SQLException {
public Properties extractNext(ResultSet rs) throws SQLException {
Properties props = new Properties();
for ( int k=1; k<=this.columnNames.size(); k++) {
String name = this.columnNames.get( k );
String name = this.columnNames.get( k-1 );
String value = rs.getString( name );
if ( value != null ) {
props.setProperty( name, value );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public class StringRSE extends SingleColumnRSE<String> {

public static final StringRSE DEFAULT = new StringRSE();

public StringRSE() {
super();
}

public StringRSE(int index) {
super(index);
}

public StringRSE(String name) {
super(name);
}

@Override
protected String convert(Object o) {
String c = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package test.org.fugerit.java.core.db;

import org.fugerit.java.test.db.helper.MemTestDBHelper;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class BasicDBHelper extends MemTestDBHelper {

public static final String DEFAULT_DB_CONN_PATH = "test/memdb/base-db-conn.properties";
public static final String DEFAULT_DB_INIT_PATH = "test/memdb/base_db_init.sql";

@BeforeClass
public static void init() {
MemTestDBHelper.init( DEFAULT_DB_CONN_PATH , DEFAULT_DB_INIT_PATH );
}

@Test
public void testFail() {
Assert.assertThrows( Exception.class , () -> {
MemTestDBHelper.initWorker( "fail-test", "script.sql" );
} );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package test.org.fugerit.java.core.db.dao.rse;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.fugerit.java.core.db.dao.DAORuntimeException;
import org.fugerit.java.core.db.dao.DAOUtilsNG;
import org.fugerit.java.core.db.dao.RSExtractor;
import org.fugerit.java.core.db.dao.rse.BigDecimalRSE;
import org.fugerit.java.core.db.dao.rse.DoubleRSE;
import org.fugerit.java.core.db.dao.rse.IntegerRSE;
import org.fugerit.java.core.db.dao.rse.LongRSE;
import org.fugerit.java.core.db.dao.rse.OptionItemRSE;
import org.fugerit.java.core.db.dao.rse.PropertyRSE;
import org.fugerit.java.core.db.dao.rse.SingleColumnRSE;
import org.fugerit.java.core.db.dao.rse.StringRSE;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.util.collection.OptionItem;
import org.junit.Assert;
import org.junit.Test;

import lombok.extern.slf4j.Slf4j;
import test.org.fugerit.java.core.db.BasicDBHelper;

@Slf4j
public class TestRSE extends BasicDBHelper {

private static final String TEST_USERNAME = "user1";

private static final int TEST_STATE = 1;

private static Class<?>[] PARAM_STRING = { String.class };

private static Class<?>[] PARAM_INTEGER = { Integer.TYPE };

private <T> T worker( String sql, String userId, RSExtractor<T> rse, String colName ) throws SQLException {
T res = null;
try ( Connection conn = newConnection() ) {
res = DAOUtilsNG.extraOne( conn , sql, rse, userId );
if ( colName != null ) {
Class<?> c = rse.getClass();
SafeFunction.apply( () -> {
@SuppressWarnings({ "unchecked" })
RSExtractor<T> altResName = (RSExtractor<T>) c.getConstructor( PARAM_STRING ).newInstance( colName );
log.info( "result col name {} -> {}", colName, DAOUtilsNG.extraOne( conn , sql, altResName, userId ) );
@SuppressWarnings({ "unchecked" })
RSExtractor<T> altResIndex = (RSExtractor<T>) c.getConstructor( PARAM_INTEGER ).newInstance( 1 );
log.info( "result col index {} -> {}", colName, DAOUtilsNG.extraOne( conn , sql, altResIndex, userId ) );
} );
;
}
}
return res;
}

@Test
public void testRSEString() throws SQLException {
String result = this.worker( "SELECT username FROM fugerit.user WHERE username = ?" , TEST_USERNAME, StringRSE.DEFAULT, "username" );
Assert.assertEquals( "user1" , result );
}

@Test
public void testRSEBigDecimal() throws SQLException {
BigDecimal result = this.worker( "SELECT state FROM fugerit.user WHERE username = ?" , TEST_USERNAME, BigDecimalRSE.DEFAULT, "state" );
Assert.assertEquals( BigDecimal.valueOf( TEST_STATE ) , result );
// test convert method
BigDecimalRSE tester = new BigDecimalRSE() {
public String toString() {
return String.valueOf( "TestRSE:"+this.convert( null ) );
}
};
log.info( "test : {}", tester.toString() );
}

@Test
public void testRSEDouble() throws SQLException {
Double result = this.worker( "SELECT state FROM fugerit.user WHERE username = ?" , TEST_USERNAME, DoubleRSE.DEFAULT, "state" );
Assert.assertEquals( Double.valueOf( TEST_STATE ) , result );
}

@Test
public void testRSEInteger() throws SQLException {
Integer result = this.worker( "SELECT state FROM fugerit.user WHERE username = ?" , TEST_USERNAME, IntegerRSE.DEFAULT, "state" );
Assert.assertEquals( Integer.valueOf( TEST_STATE ) , result );
}

@Test
public void testRSELong() throws SQLException {
Long result = this.worker( "SELECT state FROM fugerit.user WHERE username = ?" , TEST_USERNAME, LongRSE.DEFAULT, "state" );
Assert.assertEquals( Long.valueOf( TEST_STATE ) , result );
}

@Test
public void testRSEFail() throws SQLException {
SingleColumnRSE<Long> rse = new SingleColumnRSE<Long>() {
@Override
protected Long convert(Object o) {
return null;
}
};
Assert.assertThrows( DAORuntimeException.class , () -> this.worker( "SELECT state FROM fugerit.user WHERE username = ?" , TEST_USERNAME, rse, "state" ) );
}

private <T> void worker( SingleColumnRSE<T> rse ) {
log.info( "use column : {}, column name : {}", rse.isUseColumnName(), rse.getColumnName() );
log.info( "use index : {}, column index : {}", rse.isUseColumnIndex(), rse.getColumnIndex() );
}

@Test
public void testSingleColumn() {
boolean ok = false;
this.worker( new StringRSE( 1 ) );
this.worker( new StringRSE( "test" ) );
ok = true;
Assert.assertTrue(ok);
}

@Test
public void testOptionItemRSE() throws SQLException {
OptionItem result1 = this.worker( "SELECT username FROM fugerit.user WHERE username = ?" , TEST_USERNAME, OptionItemRSE.getInstance( "username" ), null );
Assert.assertEquals( TEST_USERNAME , result1.getValue() );
OptionItemRSE rse = OptionItemRSE.getInstance( "state", "username" );
OptionItem result2 = this.worker( "SELECT username, state FROM fugerit.user WHERE username = ?" , TEST_USERNAME, rse, null );
Assert.assertEquals( String.valueOf( TEST_STATE ) , result2.getValue() );
Assert.assertEquals( TEST_USERNAME , result2.getLabel() );
log.info( "result2 label field : {}, value field : {}", rse.getLabelField(), rse.getValueField() );
}

@Test
public void testPropertyReusableRSE() throws SQLException {
PropertyRSE rse = PropertyRSE.newReusableRSE();
Properties result = this.worker( "SELECT username, state FROM fugerit.user WHERE username = ?" , TEST_USERNAME, rse, null );
log.info( "result : {}", result );
Assert.assertEquals( TEST_USERNAME , result.getProperty( "USERNAME" ) );
}

@Test
public void testPropertyCachingRSE() throws SQLException {
PropertyRSE rse = PropertyRSE.newAutoCachingMetadataRSE();
Properties result = this.worker( "SELECT username, state FROM fugerit.user WHERE username = ?" , TEST_USERNAME, rse, null );
Assert.assertEquals( TEST_USERNAME , result.getProperty( "USERNAME" ) );
}

}

0 comments on commit 894234d

Please sign in to comment.