diff --git a/test/tap/config/TestConfigurableServiceConnection.java b/test/tap/config/TestConfigurableServiceConnection.java index ac62179c47e058a9bf4e37975301d0ca83af5416..f634369c6275e831594123966f55a4b0e7596cd0 100644 --- a/test/tap/config/TestConfigurableServiceConnection.java +++ b/test/tap/config/TestConfigurableServiceConnection.java @@ -105,7 +105,8 @@ public class TestConfigurableServiceConnection { udfsWithWrongParamLengthProp, udfsWithMissingBracketsProp, udfsWithMissingDefProp1, udfsWithMissingDefProp2, emptyUdfItemProp1, emptyUdfItemProp2, udfWithMissingEndBracketProp, - customFactoryProp, badCustomFactoryProp; + customFactoryProp, customConfigurableFactoryProp, + badCustomFactoryProp; @BeforeClass public static void setUp() throws Exception{ @@ -313,6 +314,9 @@ public class TestConfigurableServiceConnection { customFactoryProp = (Properties)validProp.clone(); customFactoryProp.setProperty(KEY_TAP_FACTORY, "{tap.config.TestConfigurableServiceConnection$CustomTAPFactory}"); + customConfigurableFactoryProp = (Properties)validProp.clone(); + customConfigurableFactoryProp.setProperty(KEY_TAP_FACTORY, "{tap.config.TestConfigurableServiceConnection$CustomConfigurableTAPFactory}"); + badCustomFactoryProp = (Properties)validProp.clone(); badCustomFactoryProp.setProperty(KEY_TAP_FACTORY, "{tap.config.TestConfigurableServiceConnection$BadCustomTAPFactory}"); } @@ -1077,7 +1081,16 @@ public class TestConfigurableServiceConnection { assertNotNull(connection.getFactory()); assertEquals(CustomTAPFactory.class, connection.getFactory().getClass()); }catch(Exception e){ - fail("This MUST have succeeded because the given custom TAPFactory exists and have the required constructor! \nCaught exception: " + getPertinentMessage(e)); + fail("This MUST have succeeded because the given custom TAPFactory exists and have the required constructor (ServiceConnection)! \nCaught exception: " + getPertinentMessage(e)); + } + + // Valid custom "configurable" TAPFactory: + try{ + ServiceConnection connection = new ConfigurableServiceConnection(customConfigurableFactoryProp); + assertNotNull(connection.getFactory()); + assertEquals(CustomConfigurableTAPFactory.class, connection.getFactory().getClass()); + }catch(Exception e){ + fail("This MUST have succeeded because the given custom configurable TAPFactory exists and have the required constructor (ServiceConnection, Properties)! \nCaught exception: " + getPertinentMessage(e)); } // Bad custom TAPFactory (required constructor missing): @@ -1214,6 +1227,39 @@ public class TestConfigurableServiceConnection { } + /** + * ConfigurableTAPFactory just to test whether the property tap_factory allows TAPFactory + * with a constructor (ServiceConnection, Properties). + * + * @author Grégory Mantelet (ARI) + * @version 02/2015 + */ + private static class CustomConfigurableTAPFactory extends AbstractTAPFactory { + + private final JDBCConnection dbConn; + + public CustomConfigurableTAPFactory(final ServiceConnection conn, final Properties prop) throws DBException{ + super(conn); + dbConn = new JDBCConnection("", "jdbc:postgresql:gmantele", "gmantele", null, new PostgreSQLTranslator(), "TheOnlyConnection", conn.getLogger()); + } + + @Override + public DBConnection getConnection(final String jobID) throws TAPException{ + return dbConn; + } + + @Override + public void freeConnection(final DBConnection conn){} + + @Override + public void destroy(){ + try{ + dbConn.getInnerConnection().close(); + }catch(Exception ex){} + } + + } + /** * TAPFactory just to test whether the property tap_factory is rejected when no constructor with a single parameter of type ServiceConnection exists. * diff --git a/test/tap/config/TestTAPConfiguration.java b/test/tap/config/TestTAPConfiguration.java index 61a4104250964ee88730598361fd0985d488f4b5..30d8092d5e4decb7498b6312326ad5c03f5a7a8b 100644 --- a/test/tap/config/TestTAPConfiguration.java +++ b/test/tap/config/TestTAPConfiguration.java @@ -9,7 +9,9 @@ import static org.junit.Assert.fail; import static tap.config.TAPConfiguration.KEY_DEFAULT_OUTPUT_LIMIT; import static tap.config.TAPConfiguration.KEY_FILE_MANAGER; import static tap.config.TAPConfiguration.KEY_MAX_OUTPUT_LIMIT; +import static tap.config.TAPConfiguration.KEY_TAP_FACTORY; import static tap.config.TAPConfiguration.fetchClass; +import static tap.config.TAPConfiguration.hasConstructor; import static tap.config.TAPConfiguration.isClassName; import static tap.config.TAPConfiguration.newInstance; import static tap.config.TAPConfiguration.parseLimit; @@ -19,12 +21,15 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.Properties; import org.junit.Before; import org.junit.Test; +import tap.ServiceConnection; import tap.ServiceConnection.LimitUnit; import tap.TAPException; +import tap.TAPFactory; import tap.metadata.TAPMetadata; import tap.metadata.TAPSchema; import adql.query.ColumnReference; @@ -65,13 +70,13 @@ public class TestTAPConfiguration { } /** - * TEST getClass(String,String,String): + * TEST getClass(String,String,Class): * - null, "", "{}", "an incorrect syntax", "{ }", "{ }" => NULL must be returned * - "{java.lang.String}", "{ java.lang.String }" => a valid DefaultServiceConnection must be returned * - "{mypackage.foo}", "{java.util.ArrayList}" (while a String is expected) => a TAPException must be thrown */ @Test - public void testGetClassStringStringString(){ + public void testGetClassStringStringClass(){ // NULL and EMPTY: try{ assertNull(fetchClass(null, KEY_FILE_MANAGER, String.class)); @@ -148,6 +153,70 @@ public class TestTAPConfiguration { } } + /** + * TEST hasConstructor(String,String,Class,Class[]): + * (tests already performed by {@link #testGetClassStringStringClass()}) + * - null, "", "{}", "an incorrect syntax", "{ }", "{ }" => must fail with a TAPException + * - "{java.lang.String}", "{ java.lang.String }" => a valid DefaultServiceConnection must be returned + * - "{mypackage.foo}", "{java.util.ArrayList}" (while a String is expected) => a TAPException must be thrown + * (new tests) + * - if the specified constructor exists return <code>true</code>, else <code>false</code> must be returned. + */ + @Test + public void testHasConstructor(){ + /* hasConstructor(...) must throw an exception if the specification of the class (1st and 3rd parameters) + * is wrong. But that is performed by fetchClass(...) which is called at the beginning of the function + * and is not surrounded by a try-catch. So all these tests are already done by testGetClassStringStringClass(). */ + + // With a missing list of parameters: + try{ + assertTrue(hasConstructor("{java.lang.String}", "STRING", String.class, null)); + }catch(TAPException te){ + te.printStackTrace(); + fail("\"No list of parameters\" MUST be interpreted as the specification of a constructor with no parameter! This test has failed."); + } + + // With an empty list of parameters + try{ + assertTrue(hasConstructor("{java.lang.String}", "STRING", String.class, new Class[0])); + }catch(TAPException te){ + te.printStackTrace(); + fail("\"An empty list of parameters\" MUST be interpreted as the specification of a constructor with no parameter! This test has failed."); + } + + // With a wrong list of parameters - 1 + try{ + assertFalse(hasConstructor("{tap.config.ConfigurableTAPFactory}", KEY_TAP_FACTORY, TAPFactory.class, new Class[]{})); + }catch(TAPException te){ + te.printStackTrace(); + fail("ConfigurableTAPFactory does not have an empty constructor ; this test should have failed!"); + } + + // With a wrong list of parameters - 2 + try{ + assertFalse(hasConstructor("{tap.config.ConfigurableTAPFactory}", KEY_TAP_FACTORY, TAPFactory.class, new Class[]{String.class,String.class})); + }catch(TAPException te){ + te.printStackTrace(); + fail("ConfigurableTAPFactory does not have a constructor with 2 Strings as parameter ; this test should have failed!"); + } + + // With a good list of parameters - 1 + try{ + assertTrue(hasConstructor("{tap.config.ConfigurableTAPFactory}", KEY_TAP_FACTORY, TAPFactory.class, new Class[]{ServiceConnection.class,Properties.class})); + }catch(TAPException te){ + te.printStackTrace(); + fail("ConfigurableTAPFactory has a constructor with a ServiceConnection and a Properties in parameters ; this test should have failed!"); + } + + // With a good list of parameters - 2 + try{ + assertTrue(hasConstructor("{java.lang.String}", "STRING", String.class, new Class[]{String.class})); + }catch(TAPException te){ + te.printStackTrace(); + fail("String has a constructor with a String as parameter ; this test should have failed!"); + } + } + @Test public void testNewInstance(){ // VALID CONSTRUCTOR with no parameters: