TestProjectManager.java

// 
// Lapetus Ltd Java Class.  Copyright (c) Lapetus Systems Ltd, 2009, 2010. 
// ----------------------------------------------------------------------- 
// This is the intellectual property of Lapetus Systems Ltd, Artemis, Greece. 
// -------------------------------------------------------------------------- 
// www.lapetus-ltd.com, www.lapetus.com.gr, www.lapetus.eu 
// ------------------------------------------------------- 
// $LastChangedRevision: 1190 $ 
// $LastChangedDate:: 2010-11-17 13:21:35#$ 
// ---------------------------------------- 
// 
 
import com.lapetus_ltd._2009.xml.types.*; 
import com.lapetus_ltd.api.TLptsMainDatabase; 
import com.lapetus_ltd.api.common.TLptsCryptoUtil; 
import com.lapetus_ltd.api.common.TLptsFileUtil; 
import com.lapetus_ltd.api.common.TLptsSysInfoUtil; 
import com.lapetus_ltd.api.common.logger.TLptsLog; 
import com.lapetus_ltd.api.db.control.*; 
import com.lapetus_ltd.api.db.utils.TLptsDriverLoader; 
import com.lapetus_ltd.api.db.xml.types.TLptsDBConnectionType; 
import com.lapetus_ltd.api.db.xml.types.TLptsDBProjectType; 
import com.lapetus_ltd.api.db.xml.types.TLptsDriverType; 
import junit.framework.TestCase; 
 
import java.util.ResourceBundle; 
 
 
// ###################################################################################################### 
// ####   Test code for the dbJAPI build process. This code will stop the build process on ERROR     #### 
// ###################################################################################################### 
 
 
// 
// Class Description : This is the test class for the Project Manager. 
// 
// This test is a bit creative. We start by connecting to a data source and then by creating 2 statements. 
// In the statement listener we wait for the 2nd statement and then save the current situation to a project file. 
// The project is then loaded and the connection and statement are verified. 
// 
 
 
public class TestProjectManager extends TestCase 
{ 
  private ConnectionStatementListener connectionStatementListener = null; 
  private int loadedConnectionCount = 0; 
  private int loadedStatementCount = 0; 
 
  public static void main(String[] args) 
  { 
    TestProjectManager test = new TestProjectManager(); 
    test.testProject(); 
  } 
 
  public void testProject() 
  { 
    TLptsMainDatabase.init(); 
 
    // add our listeners for controlling the flow of data 
    // keeping connectionStatementListener for later removal 
    TLptsFactoryConnection.addListener(connectionStatementListener = new ConnectionStatementListener()); 
    TLptsFactoryStatement.addListener(new StatementListener()); 
 
    // the odbc drivers are Windows only 
    if (TLptsSysInfoUtil.isHostWindows()) 
    { 
      ResourceBundle bundle = ResourceBundle.getBundle("resources"); // we have stored all the database info in this bundle 
      String fileName = TLptsFileUtil.getCurrentDirectory() + bundle.getString("Testing.database.access.testdb1"); 
 
      TLptsDBConnectionType connectionType = new TLptsDBConnectionType(); 
      connectionType.setTitle(fileName); 
 
      XLptsDriverType driverType = TLptsDriverLoader.getDriverTypeByClassName("sun.jdbc.odbc.JdbcOdbcDriver"); 
 
      if (driverType != null) 
        connectionType.setDriverType(new TLptsDriverType(driverType)); 
      else 
      { 
        System.out.println("Could not find driver for class : sun.jdbc.odbc.JdbcOdbcDriver. Cannot connect."); 
        fail(); 
      } 
 
      connectionType.setDataFileUrl(fileName); 
      connectionType.setConnectionString(driverType.getConnectionStringFormat()); 
      connectionType.setCredentials(TLptsCryptoUtil.defaultEncryptCredentialsRSA(driverType.getGuestUser(), driverType.getGuestPassword())); 
 
      TLptsFactoryConnection.initiateConnection(connectionType); 
    } 
  } 
 
 
//  ############################################################################################################### 
//  ####             C O N N E C T I O N      L I S T E N E R 
//  ############################################################################################################### 
 
  private class ConnectionStatementListener implements ILptsFactoryConnectionListener 
  { 
    public synchronized void newConnectionCreated(TLptsConnection connection) 
    { 
      XLptsDBStatementType statement = new XLptsDBStatementType(); 
      statement.setTitle("Customers"); 
      statement.setSqlStatementUser("SELECT * FROM Customers"); 
      statement.setSqlStatementModified(true); 
      statement.setSqlDatabaseFormat(XLptsDBSqlDatabaseFormat.DEFAULT_ODBC); 
      statement.setTypeOfStatement(XLptsDBTypeOfStatementType.SELECT); 
      TLptsFactoryStatement.createNewStatement(connection, statement); 
 
      statement = new XLptsDBStatementType(); 
      statement.setTitle("TRANS"); 
      statement.setSqlStatementUser("SELECT * FROM TRANSACTIONS"); 
      statement.setSqlStatementModified(true); 
      statement.setSqlDatabaseFormat(XLptsDBSqlDatabaseFormat.DEFAULT_ODBC); 
      statement.setTypeOfStatement(XLptsDBTypeOfStatementType.SELECT); 
      TLptsFactoryStatement.createNewStatement(connection, statement); 
    } 
 
    public void newConnectionProcessStarted() 
    { 
    } 
 
    public void newConnectionFailed(TLptsLog log) 
    { 
    } 
 
    public void removedAndClosedConnection(TLptsConnection connection) 
    { 
    } 
  } 
 
 
//  ############################################################################################################### 
//  ####                   S T A T E M E N T       L I S T E N E R 
//  ############################################################################################################### 
 
  private class StatementListener implements ILptsFactoryStatementListener 
  { 
    public void newStatementProcessStarted(TLptsConnection connection) 
    { 
    } 
 
    public void newStatementCreated(TLptsConnection connection, TLptsStatement statement) 
    { 
      if (!statement.getTitle().equals("TRANS")) 
        return; 
 
      System.out.println("Testing Project Manger"); 
 
      TLptsFactoryStatement.removeListener(this); 
      TLptsFactoryConnection.removeListener(connectionStatementListener); 
 
      TLptsFactoryProject.addProjectListener(new ProjectListener()); 
 
      TLptsDBProjectType pt = TLptsFactoryProject.getCurrentSettingsAsProject(); 
      TLptsFactoryProject.saveProject(TLptsFileUtil.createDirectoryPath(TLptsFileUtil.getUserHomeLapetusDirectory() + "test/") + 
                                      "test.project.load", pt); 
 
      TLptsFactoryConnection.removeConnection(connection, true); // true = get rid of all statements for this connection. 
 
      TLptsFactoryConnection.addListener(new CountConnectionListener()); 
      TLptsFactoryStatement.addListener(new CountStatementListener()); 
      TLptsFactoryProject.loadProject(TLptsFileUtil.getUserHomeLapetusDirectory() + "test/test.project.load"); 
 
    } 
 
    public void newStatementFailed(TLptsConnection connection) 
    { 
    } 
 
    public void removedAndClosedStatement(TLptsConnection connection, TLptsStatement statement) 
    { 
    } 
  } 
 
 
//  ############################################################################################################### 
//  ####             P R O J E C T      L I S T E N E R 
//  ############################################################################################################### 
 
  private class ProjectListener implements ILptsFactoryProjectListener 
  { 
    public void projectSaveStart(String fileName, XLptsDBProjectType project) 
    { 
    } 
 
    public void projectSaveComplete(String fileName, XLptsDBProjectType project, boolean isSaved) 
    { 
    } 
 
    public void projectLoadStarting() 
    { 
    } 
 
    public void projectLoadComplete(XLptsDBProjectType projectType) 
    { 
      int connectionSize = projectType.getConnectionGroup().getConnectionListItem().size(); 
      int statementSize = projectType.getStatementGroup().getStatementListItem().size(); 
 
      if (connectionSize != 1 || statementSize != 2) 
        fail("Loaded project, but the connection and statement counts do not match"); 
 
      System.out.println("Connection and Statement count match in Project XML file ..."); 
    } 
  } 
 
 
//  ############################################################################################################### 
//  ####             C O N N E C T I O N      L I S T E N E R 
//  ############################################################################################################### 
 
  private class CountConnectionListener implements ILptsFactoryConnectionListener 
  { 
    public void newConnectionProcessStarted() 
    { 
    } 
 
    public void newConnectionCreated(TLptsConnection connection) 
    { 
      loadedConnectionCount++; 
    } 
 
    public void newConnectionFailed(TLptsLog log) 
    { 
    } 
 
    public void removedAndClosedConnection(TLptsConnection connection) 
    { 
    } 
  } 
 
 
//  ############################################################################################################### 
//  ####                   S T A T E M E N T       L I S T E N E R 
//  ############################################################################################################### 
 
  private class CountStatementListener implements ILptsFactoryStatementListener 
  { 
    public void newStatementProcessStarted(TLptsConnection connection) 
    { 
    } 
 
    public void newStatementCreated(TLptsConnection connection, TLptsStatement statement) 
    { 
      loadedStatementCount++; 
      if (loadedStatementCount == 2 && loadedConnectionCount == 1) 
      { 
        System.out.println("Connection and Statement count match after load..."); 
        System.out.println("Passed..."); 
        System.exit(0); 
      } 
    } 
 
    public void newStatementFailed(TLptsConnection connection) 
    { 
    } 
 
    public void removedAndClosedStatement(TLptsConnection connection, TLptsStatement statement) 
    { 
    } 
  } 
}