From ac062fc5f88371007048648b0a7a64d1fdaccd3b Mon Sep 17 00:00:00 2001 From: "Matteo Franci a.k.a. Fugerit" Date: Fri, 8 Sep 2023 13:00:46 +0200 Subject: [PATCH] Coverate result set extractor --- .../java/core/db/dao/rse/BigDecimalRSE.java | 1 + .../java/core/db/dao/rse/PropertyRSE.java | 21 ++- .../java/core/db/dao/rse/StringRSE.java | 12 ++ .../fugerit/java/core/db/BasicDBHelper.java | 25 +++ .../fugerit/java/core/db/dao/rse/TestRSE.java | 146 ++++++++++++++++++ 5 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 fj-core/src/test/java/test/org/fugerit/java/core/db/BasicDBHelper.java create mode 100644 fj-core/src/test/java/test/org/fugerit/java/core/db/dao/rse/TestRSE.java diff --git a/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/BigDecimalRSE.java b/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/BigDecimalRSE.java index 52688b58..b33b2a36 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/BigDecimalRSE.java +++ b/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/BigDecimalRSE.java @@ -48,6 +48,7 @@ public BigDecimalRSE(String name) { @Override protected BigDecimal convert(Object o) { + // not used in this rs exctractor return null; } diff --git a/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/PropertyRSE.java b/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/PropertyRSE.java index c86e6b5f..51d88a01 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/PropertyRSE.java +++ b/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/PropertyRSE.java @@ -45,6 +45,8 @@ public abstract class PropertyRSE implements RSExtractor, Serializab */ private static final long serialVersionUID = -2284313730808746483L; + public static final PropertyRSE DEFAULT_REUSABLE = newReusableRSE(); + /** * Creates a new reusable PropertyRSE * @@ -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; @@ -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 ); diff --git a/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/StringRSE.java b/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/StringRSE.java index e70df16f..718a2293 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/StringRSE.java +++ b/fj-core/src/main/java/org/fugerit/java/core/db/dao/rse/StringRSE.java @@ -30,6 +30,18 @@ public class StringRSE extends SingleColumnRSE { 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; diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/db/BasicDBHelper.java b/fj-core/src/test/java/test/org/fugerit/java/core/db/BasicDBHelper.java new file mode 100644 index 00000000..b4e49fcf --- /dev/null +++ b/fj-core/src/test/java/test/org/fugerit/java/core/db/BasicDBHelper.java @@ -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" ); + } ); + } + +} diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/db/dao/rse/TestRSE.java b/fj-core/src/test/java/test/org/fugerit/java/core/db/dao/rse/TestRSE.java new file mode 100644 index 00000000..c3a0bca6 --- /dev/null +++ b/fj-core/src/test/java/test/org/fugerit/java/core/db/dao/rse/TestRSE.java @@ -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 worker( String sql, String userId, RSExtractor 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 altResName = (RSExtractor) c.getConstructor( PARAM_STRING ).newInstance( colName ); + log.info( "result col name {} -> {}", colName, DAOUtilsNG.extraOne( conn , sql, altResName, userId ) ); + @SuppressWarnings({ "unchecked" }) + RSExtractor altResIndex = (RSExtractor) 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 rse = new SingleColumnRSE() { + @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 void worker( SingleColumnRSE 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" ) ); + } + +}