DataSourceInitializer { @SuppressWarnings("unchecked") DataSource getDataSourceInstance( DataSourceConnectionProvider.DataSource config ) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { DataSource ds = null; final Class<?> klass = getClass( config.getJavaClass() ); if ( config.getFactoryMethod() != null ) { ds = invokeStaticFactoryMethod( klass, config.getFactoryMethod(), config.getArgument() ); } else { ds = invokeDataSourceConstructor( (Class<DataSource>) klass, config.getArgument() ); } return ds; } DataSourceInitializer( final ClassLoader classLoader );   }
@Test public void testGetDataSourceInstanceConstructor() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { DataSourceConnectionProvider.DataSource config = new DataSourceConnectionProvider.DataSource(); config.setJavaClass( DataSourceMock.class.getCanonicalName() ); DataSource ds = initializer.getDataSourceInstance( config ); assertNotNull( ds ); assertTrue( ds instanceof DataSourceMock ); }
@Test public void testGetDataSourceInstanceConstructorWithArguments() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { DataSourceConnectionProvider.DataSource config = new DataSourceConnectionProvider.DataSource(); config.setJavaClass( DataSourceMock.class.getCanonicalName() ); DataSourceConnectionProvider.DataSource.Argument stringArgument = new DataSourceConnectionProvider.DataSource.Argument(); stringArgument.setJavaClass( "java.lang.String" ); stringArgument.setValue( "aeiou" ); config.getArgument().add( stringArgument ); DataSourceConnectionProvider.DataSource.Argument intArgument = new DataSourceConnectionProvider.DataSource.Argument(); intArgument.setJavaClass( "java.lang.Integer" ); intArgument.setValue( "4711" ); config.getArgument().add( intArgument ); DataSourceMock ds = (DataSourceMock) initializer.getDataSourceInstance( config ); assertNotNull( ds ); assertTrue( ds instanceof DataSourceMock ); Assert.assertEquals( "aeiou", ds.getStringConstructorArg() ); Assert.assertEquals( 4711, ds.getIntConstructorArg() ); }
@Test public void testGetDataSourceInstanceStaticFactory() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { DataSourceConnectionProvider.DataSource config = new DataSourceConnectionProvider.DataSource(); config.setJavaClass( DataSourceMockFactory.class.getCanonicalName() ); config.setFactoryMethod( "create" ); DataSource ds = initializer.getDataSourceInstance( config ); assertNotNull( ds ); assertTrue( ds instanceof DataSourceMock ); }
@Test public void testGetDataSourceInstanceStaticFactoryWithArguments() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { DataSourceConnectionProvider.DataSource config = new DataSourceConnectionProvider.DataSource(); config.setJavaClass( DataSourceMockFactory.class.getCanonicalName() ); config.setFactoryMethod( "create" ); DataSourceConnectionProvider.DataSource.Argument stringArgument = new DataSourceConnectionProvider.DataSource.Argument(); stringArgument.setJavaClass( "java.lang.String" ); stringArgument.setValue( "aeiou" ); config.getArgument().add( stringArgument ); DataSourceConnectionProvider.DataSource.Argument intArgument = new DataSourceConnectionProvider.DataSource.Argument(); intArgument.setJavaClass( "java.lang.Integer" ); intArgument.setValue( "4711" ); config.getArgument().add( intArgument ); DataSourceMock ds = (DataSourceMock) initializer.getDataSourceInstance( config ); assertNotNull( ds ); assertTrue( ds instanceof DataSourceMock ); Assert.assertEquals( "aeiou", ds.getStringConstructorArg() ); Assert.assertEquals( 4711, ds.getIntConstructorArg() ); }