// 2012-04-15 johnpfeiffer
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Properties;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class SQLTableTest
{
private String dbms;
private String host;
private String port;
private String user;
private String password;
private static Connection databaseConnection;
@BeforeClass public static void getDatabaseConnection() throws SQLException
{
SQLTableTest test = new SQLTableTest();
test.dbms = "mysql";
test.host = "localhost";
test.port = "3306";
test.user = "root";
test.password = "password";
Properties connectionProps = new Properties();
connectionProps.put( "user" , test.user );
connectionProps.put( "password" , test.password );
if( test.dbms.equals( "mysql" ) )
{ databaseConnection = DriverManager.getConnection( "jdbc:" + test.dbms + "://" + test.host + ":" + test.port + "/", connectionProps );
}else if( test.dbms.equals( "derby" ) )
{ // databaseConnection = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps );
}
}
@AfterClass public static void closeDatabaseConnection()
{
try{
if( databaseConnection != null ){ databaseConnection.close(); }
}catch( SQLException sqle )
{ System.err.println( sqle.getMessage() );
}
}
@Test public void testSQLTable() //
{ SQLTable test = new SQLTable( "mysql" , "user" , databaseConnection );
if( test == null )
{ fail( "Constructor SQLTable error" );
}
}
@Test public void testSQLTableConnectionNull()
{
try{ SQLTable test = new SQLTable( "mysql" , "user" , null );
}catch( IllegalArgumentException iae )
{ return;
}
fail( "Expected IllegalArgumentException" );
}
@Test public void testSQLTableDatabaseNull()
{
try{ SQLTable test = new SQLTable( null , "user" , databaseConnection );
}catch( IllegalArgumentException iae )
{ return;
}
fail( "Expected IllegalArgumentException" );
}
@Test public void testSQLTableTableNull()
{
try{ SQLTable test = new SQLTable( "mysql" , null , databaseConnection );
}catch( IllegalArgumentException iae )
{ return;
}
fail( "Expected IllegalArgumentException" );
}
@Test public void testSQLTableDatabaseEmpty()
{
try{ SQLTable test = new SQLTable( "" , "user" , databaseConnection );
}catch( IllegalArgumentException iae )
{ return;
}
fail( "Expected IllegalArgumentException" );
}
@Test public void testSQLTableTableEmpty()
{
try{ SQLTable test = new SQLTable( "mysql" , "" , databaseConnection );
}catch( IllegalArgumentException iae )
{ return;
}
fail( "Expected IllegalArgumentException" );
}
@Test public void testGetDatabaseName()
{ SQLTable test = new SQLTable( "mysql" , "user" , databaseConnection );
assertEquals( "mysql" , test.getDatabaseName() );
}
@Test public void testGetTableName()
{ SQLTable test = new SQLTable( "mysql" , "user" , databaseConnection );
assertEquals( "user" , test.getTableName() );
}
@Test public void testGetRowCount()
{ SQLTable test = new SQLTable( "mysql" , "user" , databaseConnection );
assertEquals( 4 , test.getRowCount() ); //this depends on your test db setup
}
@Test public void testGetColumnCount()
{ SQLTable test = new SQLTable( "mysql" , "user" , databaseConnection );
assertEquals( 6 , test.getColumnCount() ); //MySQL schema definition
}
@Test public void testGetColumnHeaders()
{ SQLTable testTable = new SQLTable( "mysql" , "user" , databaseConnection );
ArrayList <SQLColumn> test = testTable.getColumnHeaders();
if( test == null )
{ fail( "testGetColumnHeaders returned null" );
}
ListIterator <SQLColumn> it = test.listIterator();
if( it.hasNext() )
{
SQLColumn temp = it.next();
assertEquals( "Host" , temp.getField() );
assertEquals( "char(60)" , temp.getType() );
assertEquals( "NO" , temp.getNullable() );
assertEquals( "PRI" , temp.getKey() );
assertEquals( "" , temp.getDefaultValue() );
assertEquals( "" , temp.getExtra() );
}
}
} //end class