OOoRunner test harness

util
Class DBTools

java.lang.Object
  |
  +--util.DBTools

public class DBTools
extends Object

Provides useful methods for working with SOffice databases. Database creation, data transfering, outputting infromation.


Inner Class Summary
 class DBTools.DataSourceInfo
          It's just a structure with some useful methods for representing com.sun.star.sdb.DataSource service.
 
Field Summary
static int TST_BINARY_STREAM
           
static String TST_BINARY_STREAM_F
           
static int TST_BOOLEAN
           
static String TST_BOOLEAN_F
           
static int TST_CHARACTER_STREAM
           
static String TST_CHARACTER_STREAM_F
           
static int TST_DATE
           
static String TST_DATE_F
           
static int TST_DOUBLE
           
static String TST_DOUBLE_F
           
static int TST_INT
           
static String TST_INT_F
           
static String TST_JDBC_DRIVER
           
static int[] TST_STREAM_LENGTHS
          Array of lengths of streams for each row in of the TST_TABLE_VALUES constants.
static int TST_STRING
           
static String TST_STRING_F
           
static Object[][] TST_TABLE_VALUES
          Values for filling test table.
 
Constructor Summary
DBTools(XMultiServiceFactory xMSF)
          Creates class instance.
 
Method Summary
 void addRowToTestTable(XConnection con, String table, Object[] values, int streamLength)
          Inserts row into test table of the specified connection.
 XConnection connectToDBase(String contextName, String dbDir)
          Registers DBase database (directory with DBF files) in the global DB context, then connects to it.
 XConnection connectToSource(Object dbSource)
          Performs connection to DataSource specified.
 XConnection connectToTestDB(String docPath)
          Connects to DataSource specially created for testing.
 XConnection connectToTextDB(String contextName, String dbDir, String fileExtension)
          RESERVED.
protected  void createMySQLTable(Statement statement, String tbl_name)
          Creates test table specified.
 int deleteAllRows(XConnection con, String table)
          Empties the table in the specified source.
static String dirToUrl(String dir)
          Convert system pathname to SOffice URL string (for example 'C:\Temp\DBDir\' -> 'file:///C|/Temp/DBDir/').
protected  void dropMySQLTable(Statement statement, String tbl_name)
          Drops table.
 void initializeTestTable(XConnection con, String table)
          Initializes test table specified of the connection specified.
 void initTestTableUsingJDBC(String tbl_name, DBTools.DataSourceInfo dsi)
          Initializes test table specified of the connection specified using JDBC driver.
protected  void insertContentMySQLTable(Statement statement, String tbl_name)
          Inserts data from TST_TABLE_VALUES constant array to test table tbl_name.
 DBTools.DataSourceInfo newDataSourceInfo()
          Returns new instance of DataSourceInfo class.
 DBTools.DataSourceInfo newDataSourceInfo(Object dataSource)
          Returns new instance of DataSourceInfo class.
 void printRegisteredDatabasesInfo(PrintWriter out)
          Prints full info about currently registered DataSource's.
 void registerDB(String name, Object dataSource)
          Registers the datasource on the specified name in DatabaseContext service.
 Object registerTestDB(String docPath)
          Registers Test data source in the DatabaseContext service.
 void reRegisterDB(String name, Object dataSource)
          First tries to revoke the datasource with the specified name and then registers a new one.
 void revokeDB(String name)
          Revokes datasource from global DB context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TST_JDBC_DRIVER

public static final String TST_JDBC_DRIVER

TST_STRING

public static final int TST_STRING

TST_INT

public static final int TST_INT

TST_DOUBLE

public static final int TST_DOUBLE

TST_DATE

public static final int TST_DATE

TST_BOOLEAN

public static final int TST_BOOLEAN

TST_CHARACTER_STREAM

public static final int TST_CHARACTER_STREAM

TST_BINARY_STREAM

public static final int TST_BINARY_STREAM

TST_STRING_F

public static final String TST_STRING_F

TST_INT_F

public static final String TST_INT_F

TST_DOUBLE_F

public static final String TST_DOUBLE_F

TST_DATE_F

public static final String TST_DATE_F

TST_BOOLEAN_F

public static final String TST_BOOLEAN_F

TST_CHARACTER_STREAM_F

public static final String TST_CHARACTER_STREAM_F

TST_BINARY_STREAM_F

public static final String TST_BINARY_STREAM_F

TST_TABLE_VALUES

public static final Object[][] TST_TABLE_VALUES
Values for filling test table.

TST_STREAM_LENGTHS

public static final int[] TST_STREAM_LENGTHS
Array of lengths of streams for each row in of the TST_TABLE_VALUES constants.
Constructor Detail

DBTools

public DBTools(XMultiServiceFactory xMSF)
Creates class instance.
Parameters:
xMSF - XMultiServiceFactory.
Method Detail

newDataSourceInfo

public DBTools.DataSourceInfo newDataSourceInfo()
Returns new instance of DataSourceInfo class.

newDataSourceInfo

public DBTools.DataSourceInfo newDataSourceInfo(Object dataSource)
Returns new instance of DataSourceInfo class.

registerDB

public void registerDB(String name,
                       Object dataSource)
                throws Exception
Registers the datasource on the specified name in DatabaseContext service.
Parameters:
name - Name which dataSource will have in global context.
dataSource - DataSource object which is to be registered.

reRegisterDB

public void reRegisterDB(String name,
                         Object dataSource)
                  throws Exception
First tries to revoke the datasource with the specified name and then registers a new one.
Parameters:
name - Name which dataSource will have in global context.
dataSource - DataSource object which is to be registered.

connectToTextDB

public XConnection connectToTextDB(String contextName,
                                   String dbDir,
                                   String fileExtension)
                            throws Exception
RESERVED. Not used.

connectToDBase

public XConnection connectToDBase(String contextName,
                                  String dbDir)
                           throws Exception
Registers DBase database (directory with DBF files) in the global DB context, then connects to it.
Parameters:
contextName - Name under which DB will be registered.
dbDir - The directory with DBF tables.
Returns:
Connection to the DB.

connectToSource

public XConnection connectToSource(Object dbSource)
                            throws Exception
Performs connection to DataSource specified.
Parameters:
dbSource - com.sun.star.sdb.DataSource service specified data source which must be already registered in the DatabaseContext service.
dbSource - Data source to be connected to.
Returns:
Connection to the data source.

registerTestDB

public Object registerTestDB(String docPath)
                      throws Exception
Registers Test data source in the DatabaseContext service. This source always has name 'APITestDatabase' and it is registered in subdirectory TestDB of directory docPath which is supposed to be a directory with test documents, but can be any other (it must have subdirectory with DBF tables). If such data source doesn't exists or exists with different URL it is recreated and reregistered.
Parameters:
docPath - Path to database TestDB directory.
Returns:
com.sun.star.sdb.DataSource service implementation which represents TestDB.

connectToTestDB

public XConnection connectToTestDB(String docPath)
                            throws Exception
Connects to DataSource specially created for testing. This source always has name 'APITestDatabase' and it is registered in subdirectory TestDB of directory docPath which is supposed to be a directory with test documents, but can be any other (it must have subdirectory with DBF tables). If such data source doesn't exists or exists with different URL it is recreated and reregistered. Finally connection performed.
Parameters:
docPath - Path to database TestDB directory.
Returns:
Connection to test database.

deleteAllRows

public int deleteAllRows(XConnection con,
                         String table)
                  throws SQLException
Empties the table in the specified source.
Parameters:
con - Connection to the DataSource where appropriate table exists.
table - The name of the table where all rows will be deleted.
Returns:
Number of rows deleted.

addRowToTestTable

public void addRowToTestTable(XConnection con,
                              String table,
                              Object[] values,
                              int streamLength)
                       throws SQLException
Inserts row into test table of the specified connection. Test table has some predefined format which includes as much field types as possible. For every column type constants TST_STRING, TST_INT, etc. are declared for column index fast find.
Parameters:
con - Connection to data source where test table exists.
table - Test table name.
values - Values to be inserted into test table. Values of this array inserted into appropriate fields depending on their types. So String value of the array is inserted into the field of CHARACTER type, etc.
streamLength - Is optional. It is used only if in values list XCharacterInputStream or XBinaryInputStream types specified. In this case the parameter specifies the length of the stream for inserting.

initializeTestTable

public void initializeTestTable(XConnection con,
                                String table)
                         throws SQLException
Initializes test table specified of the connection specified. Deletes all record from table, and then inserts data from TST_TABLE_VALUES constant array.

Test table has some predefined format which includes as much field types as possible. For every column type constants TST_STRING, TST_INT, etc. are declared for column index fast find.

Parameters:
con - Connection to data source where test table exists.
table - Test table name.

printRegisteredDatabasesInfo

public void printRegisteredDatabasesInfo(PrintWriter out)
Prints full info about currently registered DataSource's.

dirToUrl

public static String dirToUrl(String dir)
Convert system pathname to SOffice URL string (for example 'C:\Temp\DBDir\' -> 'file:///C|/Temp/DBDir/'). Already converted string retured unchanged.

revokeDB

public void revokeDB(String name)
              throws Exception
Revokes datasource from global DB context.
Parameters:
name - DataSource name to be revoked.

initTestTableUsingJDBC

public void initTestTableUsingJDBC(String tbl_name,
                                   DBTools.DataSourceInfo dsi)
                            throws SQLException,
                                   ClassNotFoundException
Initializes test table specified of the connection specified using JDBC driver. Drops table with the name tbl_name, creates new table with this name and then inserts data from TST_TABLE_VALUES constant array.

Test table has some predefined format which includes as much field types as possible. For every column type constants TST_STRING, TST_INT, etc. are declared for column index fast find.

Parameters:
tbl_name - Test table name.

insertContentMySQLTable

protected void insertContentMySQLTable(Statement statement,
                                       String tbl_name)
                                throws SQLException
Inserts data from TST_TABLE_VALUES constant array to test table tbl_name.
Parameters:
statement - object used for executing a static SQL statement and obtaining the results produced by it.
tbl_name - Test table name.

createMySQLTable

protected void createMySQLTable(Statement statement,
                                String tbl_name)
                         throws SQLException
Creates test table specified. Test table has some predefined format which includes as much field types as possible. For every column type constants TST_STRING, TST_INT, etc. are declared for column index fast find.
Parameters:
statement - object used for executing a static SQL statement and obtaining the results produced by it.
table - Test table name.

dropMySQLTable

protected void dropMySQLTable(Statement statement,
                              String tbl_name)
                       throws SQLException
Drops table.
Parameters:
statement - object used for executing a static SQL statement and obtaining the results produced by it.
table - Test table name.

OOoRunner test harness