Skip to content
Snippets Groups Projects
Commit 16167f2e authored by gmantele's avatar gmantele
Browse files

[TAP] JUnit of the previous commit (i.e. allow the usage of a custom TAPFactory

with a constructor (ServiceConnection, Properties)).
parent 50a31ac0
No related branches found
No related tags found
No related merge requests found
...@@ -105,7 +105,8 @@ public class TestConfigurableServiceConnection { ...@@ -105,7 +105,8 @@ public class TestConfigurableServiceConnection {
udfsWithWrongParamLengthProp, udfsWithMissingBracketsProp, udfsWithWrongParamLengthProp, udfsWithMissingBracketsProp,
udfsWithMissingDefProp1, udfsWithMissingDefProp2, udfsWithMissingDefProp1, udfsWithMissingDefProp2,
emptyUdfItemProp1, emptyUdfItemProp2, udfWithMissingEndBracketProp, emptyUdfItemProp1, emptyUdfItemProp2, udfWithMissingEndBracketProp,
customFactoryProp, badCustomFactoryProp; customFactoryProp, customConfigurableFactoryProp,
badCustomFactoryProp;
@BeforeClass @BeforeClass
public static void setUp() throws Exception{ public static void setUp() throws Exception{
...@@ -313,6 +314,9 @@ public class TestConfigurableServiceConnection { ...@@ -313,6 +314,9 @@ public class TestConfigurableServiceConnection {
customFactoryProp = (Properties)validProp.clone(); customFactoryProp = (Properties)validProp.clone();
customFactoryProp.setProperty(KEY_TAP_FACTORY, "{tap.config.TestConfigurableServiceConnection$CustomTAPFactory}"); 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 = (Properties)validProp.clone();
badCustomFactoryProp.setProperty(KEY_TAP_FACTORY, "{tap.config.TestConfigurableServiceConnection$BadCustomTAPFactory}"); badCustomFactoryProp.setProperty(KEY_TAP_FACTORY, "{tap.config.TestConfigurableServiceConnection$BadCustomTAPFactory}");
} }
...@@ -1077,7 +1081,16 @@ public class TestConfigurableServiceConnection { ...@@ -1077,7 +1081,16 @@ public class TestConfigurableServiceConnection {
assertNotNull(connection.getFactory()); assertNotNull(connection.getFactory());
assertEquals(CustomTAPFactory.class, connection.getFactory().getClass()); assertEquals(CustomTAPFactory.class, connection.getFactory().getClass());
}catch(Exception e){ }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): // Bad custom TAPFactory (required constructor missing):
...@@ -1214,6 +1227,39 @@ public class TestConfigurableServiceConnection { ...@@ -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. * TAPFactory just to test whether the property tap_factory is rejected when no constructor with a single parameter of type ServiceConnection exists.
* *
......
...@@ -9,7 +9,9 @@ import static org.junit.Assert.fail; ...@@ -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_DEFAULT_OUTPUT_LIMIT;
import static tap.config.TAPConfiguration.KEY_FILE_MANAGER; import static tap.config.TAPConfiguration.KEY_FILE_MANAGER;
import static tap.config.TAPConfiguration.KEY_MAX_OUTPUT_LIMIT; 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.fetchClass;
import static tap.config.TAPConfiguration.hasConstructor;
import static tap.config.TAPConfiguration.isClassName; import static tap.config.TAPConfiguration.isClassName;
import static tap.config.TAPConfiguration.newInstance; import static tap.config.TAPConfiguration.newInstance;
import static tap.config.TAPConfiguration.parseLimit; import static tap.config.TAPConfiguration.parseLimit;
...@@ -19,12 +21,15 @@ import java.io.File; ...@@ -19,12 +21,15 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Properties;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import tap.ServiceConnection;
import tap.ServiceConnection.LimitUnit; import tap.ServiceConnection.LimitUnit;
import tap.TAPException; import tap.TAPException;
import tap.TAPFactory;
import tap.metadata.TAPMetadata; import tap.metadata.TAPMetadata;
import tap.metadata.TAPSchema; import tap.metadata.TAPSchema;
import adql.query.ColumnReference; import adql.query.ColumnReference;
...@@ -65,13 +70,13 @@ public class TestTAPConfiguration { ...@@ -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 * - null, "", "{}", "an incorrect syntax", "{ }", "{ }" => NULL must be returned
* - "{java.lang.String}", "{ java.lang.String }" => a valid DefaultServiceConnection 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 * - "{mypackage.foo}", "{java.util.ArrayList}" (while a String is expected) => a TAPException must be thrown
*/ */
@Test @Test
public void testGetClassStringStringString(){ public void testGetClassStringStringClass(){
// NULL and EMPTY: // NULL and EMPTY:
try{ try{
assertNull(fetchClass(null, KEY_FILE_MANAGER, String.class)); assertNull(fetchClass(null, KEY_FILE_MANAGER, String.class));
...@@ -148,6 +153,70 @@ public class TestTAPConfiguration { ...@@ -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 @Test
public void testNewInstance(){ public void testNewInstance(){
// VALID CONSTRUCTOR with no parameters: // VALID CONSTRUCTOR with no parameters:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment