Skip to content

Commit

Permalink
fixes NPE and wrong assumptions for an assert
Browse files Browse the repository at this point in the history
NPE in JDBCDriverInfoManager
remove assertion for url != null in Connection
adding test for jndi connection without any other properties
  • Loading branch information
Robert Rettig committed Oct 18, 2016
1 parent 993423e commit c6d1eb3
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ public void testIsOpen( ) throws Exception
assertFalse( conn.isOpen( ) );
}

@Test
public void testJndiConnection( ) throws Exception
{
Connection conn = TestUtil.openJndiConnection( );
assertTrue( conn.isOpen( ) );
conn.close( );
assertFalse( conn.isOpen( ) );
}

/*
* Class under test for void open(Properties)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,29 @@

package org.eclipse.birt.report.data.oda.jdbc;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.DriverManager;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Date;
import java.util.Properties;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Hashtable;
import java.util.Objects;
import java.util.logging.Logger;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NameClassPair;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
import javax.sql.DataSource;

import org.eclipse.datatools.connectivity.oda.OdaException;

Expand All @@ -26,6 +43,203 @@
*/
public class TestUtil
{

public static final class SimpleContext implements Context
{

private String name;
private Object obj;

@Override
public Object lookup( Name name ) throws NamingException
{
return null;
}

@Override
public Object lookup( String name ) throws NamingException
{
return this.name != null
? ( this.name.equals( name ) ? obj : null )
: null;
}

@Override
public void bind( Name name, Object obj ) throws NamingException
{
}

@Override
public void bind( String name, Object obj ) throws NamingException
{
Objects.requireNonNull( name );
this.name = name;
this.obj = obj;
}

@Override
public void rebind( Name name, Object obj ) throws NamingException
{
}

@Override
public void rebind( String name, Object obj ) throws NamingException
{
}

@Override
public void unbind( Name name ) throws NamingException
{
}

@Override
public void unbind( String name ) throws NamingException
{
}

@Override
public void rename( Name oldName, Name newName ) throws NamingException
{
}

@Override
public void rename( String oldName, String newName )
throws NamingException
{
}

@Override
public NamingEnumeration<NameClassPair> list( Name name )
throws NamingException
{
return null;
}

@Override
public NamingEnumeration<NameClassPair> list( String name )
throws NamingException
{
return null;
}

@Override
public NamingEnumeration<Binding> listBindings( Name name )
throws NamingException
{
return null;
}

@Override
public NamingEnumeration<Binding> listBindings( String name )
throws NamingException
{
return null;
}

@Override
public void destroySubcontext( Name name ) throws NamingException
{
}

@Override
public void destroySubcontext( String name ) throws NamingException
{
}

@Override
public Context createSubcontext( Name name ) throws NamingException
{
return null;
}

@Override
public Context createSubcontext( String name ) throws NamingException
{
return null;
}

@Override
public Object lookupLink( Name name ) throws NamingException
{
return null;
}

@Override
public Object lookupLink( String name ) throws NamingException
{
return null;
}

@Override
public NameParser getNameParser( Name name ) throws NamingException
{
return null;
}

@Override
public NameParser getNameParser( String name ) throws NamingException
{
return null;
}

@Override
public Name composeName( Name name, Name prefix ) throws NamingException
{
return null;
}

@Override
public String composeName( String name, String prefix )
throws NamingException
{
return null;
}

@Override
public Object addToEnvironment( String propName, Object propVal )
throws NamingException
{
return null;
}

@Override
public Object removeFromEnvironment( String propName )
throws NamingException
{
return null;
}

@Override
public Hashtable<?, ?> getEnvironment( ) throws NamingException
{
return null;
}

@Override
public void close( ) throws NamingException
{
}

@Override
public String getNameInNamespace( ) throws NamingException
{
return null;
}

}

public static final class SimpleInitialContextFactory implements InitialContextFactory {

private static final SimpleContext SIMPLE_CONTEXT = new SimpleContext();

@Override
public Context getInitialContext( Hashtable<?, ?> environment )
throws NamingException
{
return SIMPLE_CONTEXT;
}

}

/** Default JDBC Driver class name */
final static String DEFAULT_DRIVER_CLASS = "net.sourceforge.jtds.jdbc.Driver";
Expand Down Expand Up @@ -64,6 +278,98 @@ static Connection openConnection( ) throws OdaException
return conn;
}

static Connection openJndiConnection( ) throws OdaException
{
String jndiName = getJndiName( );
try
{
System.setProperty( Context.INITIAL_CONTEXT_FACTORY,
SimpleInitialContextFactory.class.getName( ) );
InitialContext ic = new InitialContext( );
ic.bind(jndiName, new DataSource( ) {

private PrintWriter _logWriter;

@Override
public <T> T unwrap( Class<T> iface ) throws SQLException
{
throw new SQLException( "This is not a wrapper." );
}

@Override
public boolean isWrapperFor( Class<?> iface )
throws SQLException
{
return false;
}

@Override
public void setLoginTimeout( int seconds ) throws SQLException
{
throw new UnsupportedOperationException(
"Login timeout is not supported." );
}

@Override
public void setLogWriter( PrintWriter out ) throws SQLException
{
this._logWriter = out;

}

@Override
public Logger getParentLogger( )
throws SQLFeatureNotSupportedException
{
return Logger.getLogger( TestUtil.class.getName( )
.substring( 0, TestUtil.class.getName( )
.lastIndexOf( "." ) ) );
}

@Override
public int getLoginTimeout( ) throws SQLException
{
throw new UnsupportedOperationException(
"Login timeout is not supported." );
}

@Override
public PrintWriter getLogWriter( ) throws SQLException
{
return this._logWriter;
}

@Override
public java.sql.Connection getConnection( String username,
String password ) throws SQLException
{
return getConnection( );
}

@Override
public java.sql.Connection getConnection( ) throws SQLException
{
try
{
return TestUtil.openJDBCConnection( );
}
catch ( Exception e )
{
}
return null;
}
});
}
catch ( Exception e )
{
}
Connection conn = new Connection( );
Properties props = new Properties( );
props.setProperty( Connection.Constants.ODAJndiName, jndiName );
conn.open( props );
return conn;
}

static java.sql.Connection openJDBCConnection( ) throws Exception
{
Class.forName( getDriverClassName( ) );
Expand Down Expand Up @@ -227,6 +533,15 @@ static String getDatabase( )
return "DTETest";
}

static String getJndiName( )
{
String jndiName = System.getProperty( "DTETest.jndiname" );
if ( jndiName != null )
return jndiName;
else
return "DTETest";
}

public static void selectData( int a, int[] b )
{
assert a == 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ protected void connectByUrl( String url, Properties connProperties )
throws OdaException
{
assert connProperties != null;
assert url != null;

// Copy connProperties to props; skip property starting with
// "oda"; those are properties read by this driver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public synchronized static JDBCDriverInfoManager getInstance( )
public JDBCDriverInformation getDriversInfo( String driverClassName )
{
JDBCDriverInformation[] infos = getDriversInfo( );
for( JDBCDriverInformation info: infos )
if (driverClassName != null) for( JDBCDriverInformation info: infos )
{
if( driverClassName.equals( info.getDriverClassName( )) )
{
Expand Down

0 comments on commit c6d1eb3

Please sign in to comment.