protected void initialize(TestParameters tParam, PrintWriter log) {
    // log.println( "creating a draw document" );
    // xTextDoc = WriterTools.createTextDoc(t((XMultiServiceFactory) Param.getMSF));
    tmpDir = utils.getOfficeTemp(((XMultiServiceFactory) tParam.getMSF()));

    origDB = util.utils.getFullTestDocName("TestDB/testDB.dbf");

    dbTools = new DBTools((XMultiServiceFactory) tParam.getMSF(), log);

    // creating DataSource and registering it in DatabaseContext
    String dbURL = (String) tParam.get("test.db.url");
    String dbUser = (String) tParam.get("test.db.user");
    String dbPassword = (String) tParam.get("test.db.password");

    log.println("Creating and registering DataSource ...");
    srcInf = dbTools.newDataSourceInfo();

    if ((dbURL != null) && (dbUser != null) && (dbPassword != null)) {
      isMySQLDB = true;
      log.println("dbURL = " + dbURL);
      log.println("dbUSER = "******"dbPASSWORD = "******"soffice_test_table";
        srcInf.URL = "jdbc:" + dbURL;
        srcInf.IsPasswordRequired = new Boolean(true);
        srcInf.Password = dbPassword;
        srcInf.User = dbUser;

        PropertyValue[] propInfo = new PropertyValue[1];
        propInfo[0] = new PropertyValue();
        propInfo[0].Name = "JavaDriverClass";
        //                propInfo[0].Value = "org.gjt.mm.mysql.Driver";
        propInfo[0].Value = "util.dddriver.Driver";
        srcInf.Info = propInfo;

        dbSrc = srcInf.getDataSourceService();
        dbTools.reRegisterDB(dbSourceName, dbSrc);
      } catch (com.sun.star.uno.Exception e) {
        log.println("Error while object test initialization :");
        e.printStackTrace(log);
        throw new StatusException("Error while object test" + " initialization", e);
      }
    } else {
      // DataSource for sdbc db
      try {
        String myDbUrl = "sdbc:dbase:" + DBTools.dirToUrl(tmpDir);
        srcInf.URL = myDbUrl;

        log.println("try to register '" + myDbUrl + "' as '" + dbSourceName + "'");

        dbSrc = srcInf.getDataSourceService();
        dbTools.reRegisterDB(dbSourceName, dbSrc);
      } catch (com.sun.star.uno.Exception e) {
        log.println("Error while object test initialization :");
        e.printStackTrace(log);
        throw new StatusException("Error while object test initialization", e);
      }

      String oldF = null;
      String newF = null;

      do {
        tableName = "ODatabaseForm_tmp" + uniqueSuffix;
        oldF = utils.getFullURL(origDB);
        newF = utils.getOfficeTemp((XMultiServiceFactory) tParam.getMSF()) + tableName + ".dbf";
      } while (!utils.tryOverwriteFile(((XMultiServiceFactory) tParam.getMSF()), oldF, newF)
          && (uniqueSuffix++ < 50));
    }
  }
Example #2
0
  /**
   * Creating a Testenvironment for the interfaces to be tested. The database (DBF) file is copied
   * from test document directory into SOffice temp dir with unique name for each environment
   * creation. If the file cann't be copied (is not released) then another unique name is used (file
   * name suffix incremented by 1).
   *
   * <p><code>com.sun.star.sdb.RowSet</code> service created and its source is all rows from the
   * current copy of the table. Then row set command ("select all rows from a table") is executed
   * and cursor is positioned to the first row.
   *
   * <p>Object relations created :
   *
   * <ul>
   *   <li><code>'ORowSet.Connection'</code> for internal component test usage. Is used for closing
   *       connection when cleaning up environment.
   *   <li><code>'XRowSetApproveBroadcaster.ApproveChecker'</code> for {@link
   *       ifc.sdb._XRowSetApproveBroadcaster} interface implementation which made actions required
   *   <li><code>'CurrentRowData'</code> for {@link ifc.sdbc._XRow}, {@link ifc.sdbc._XRowUpdate} :
   *       exports types and values of the current row data.
   *   <li><code>'XColumnLocate.ColumnName'</code> for {@link ifc.sdbc._XColumnLocate} : the name of
   *       the first column of the table.
   *   <li><code>'XParameters.ParamValues'</code> for {@link ifc.sdbc._XParameters} : Collection of
   *       parameter types presented in the query.
   *   <li><code>'XRowUpdate.XRow'</code> for {@link ifc.sdbc._XRowUpdate} : <code>XRow</code>
   *       interface of the current component.
   *   <li><code>'XResultSetUpdate.UpdateTester'</code> for {@link ifc.sdbc._XResultSetUpdate}
   * </ul>
   *
   * @see com.sun.star.sdb.DatabaseContext
   * @see com.sun.star.sdb.DataSource
   */
  protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
    XMultiServiceFactory orb = (XMultiServiceFactory) Param.getMSF();
    uniqueSuffix++;
    boolean envCreatedOK = false;

    // initialize test table
    if (isMySQLDB) {
      try {
        DBTools.DataSourceInfo legacyDescriptor = dbTools.newDataSourceInfo();
        legacyDescriptor.Name = srcInf.Name;
        legacyDescriptor.User = srcInf.User;
        legacyDescriptor.Password = srcInf.Password;
        legacyDescriptor.Info = srcInf.Info;
        legacyDescriptor.URL = srcInf.URL;
        legacyDescriptor.IsPasswordRequired = srcInf.IsPasswordRequired;
        dbTools.initTestTableUsingJDBC(tableName, legacyDescriptor);
      } catch (java.sql.SQLException e) {
        e.printStackTrace(log);
        throw new StatusException(Status.failed("Couldn't " + " init test table. SQLException..."));
      } catch (java.lang.ClassNotFoundException e) {
        throw new StatusException(Status.failed("Couldn't " + "register mysql driver"));
      }
    } else {
      String oldF = null;
      String newF = null;
      String tempFolder = utils.getOfficeTemp(orb);
      do {
        tableName = "ORowSet_tmp" + uniqueSuffix;
        oldF = utils.getFullURL(origDB);
        newF = tempFolder + tableName + ".dbf";
      } while (!utils.tryOverwriteFile(orb, oldF, newF));
      m_tableFile = newF;
    }

    try {
      m_rowSet = orb.createInstance("com.sun.star.sdb.RowSet");

      XPropertySet rowSetProps = UnoRuntime.queryInterface(XPropertySet.class, m_rowSet);

      log.println("Trying to open: " + tableName);

      rowSetProps.setPropertyValue("DataSourceName", dbSourceName);
      rowSetProps.setPropertyValue("Command", tableName);
      rowSetProps.setPropertyValue("CommandType", new Integer(CommandType.TABLE));

      final XRowSet rowSet = UnoRuntime.queryInterface(XRowSet.class, m_rowSet);
      rowSet.execute();
      m_connection =
          UnoRuntime.queryInterface(
              XConnection.class, rowSetProps.getPropertyValue("ActiveConnection"));

      XResultSet xRes = UnoRuntime.queryInterface(XResultSet.class, m_rowSet);
      xRes.first();

      log.println("creating a new environment for object");
      TestEnvironment tEnv = new TestEnvironment((XInterface) m_rowSet);

      // Adding obj relation for XRowSetApproveBroadcaster test
      {
        final XResultSet resultSet = UnoRuntime.queryInterface(XResultSet.class, m_rowSet);
        final XResultSetUpdate resultSetUpdate =
            UnoRuntime.queryInterface(XResultSetUpdate.class, m_rowSet);
        final XRowUpdate rowUpdate = UnoRuntime.queryInterface(XRowUpdate.class, m_rowSet);
        final PrintWriter logF = log;
        tEnv.addObjRelation(
            "XRowSetApproveBroadcaster.ApproveChecker",
            new ifc.sdb._XRowSetApproveBroadcaster.RowSetApproveChecker() {
              public void moveCursor() {
                try {
                  resultSet.beforeFirst();
                  resultSet.afterLast();
                  resultSet.first();
                } catch (com.sun.star.sdbc.SQLException e) {
                  logF.println(
                      "### _XRowSetApproveBroadcaster.RowSetApproveChecker.moveCursor() :");
                  e.printStackTrace(logF);
                  throw new StatusException("RowSetApproveChecker.moveCursor failed", e);
                }
              }

              public RowChangeEvent changeRow() {
                try {
                  resultSet.first();
                  rowUpdate.updateString(1, "ORowSetTest2");
                  resultSetUpdate.updateRow();
                } catch (com.sun.star.sdbc.SQLException e) {
                  logF.println("### _XRowSetApproveBroadcaster.RowSetApproveChecker.changeRow() :");
                  e.printStackTrace(logF);
                  throw new StatusException("RowSetApproveChecker.changeRow failed", e);
                }
                RowChangeEvent ev = new RowChangeEvent();
                ev.Action = com.sun.star.sdb.RowChangeAction.UPDATE;
                ev.Rows = 1;

                return ev;
              }

              public void changeRowSet() {
                try {
                  // since we gave the row set a parametrized statement, we need to ensure the
                  // parameter is actually filled, otherwise we would get an empty result set,
                  // which would imply some further tests failing
                  XParameters rowSetParams =
                      UnoRuntime.queryInterface(XParameters.class, resultSet);
                  rowSetParams.setString(1, "String2");
                  rowSet.execute();
                  resultSet.first();
                } catch (com.sun.star.sdbc.SQLException e) {
                  logF.println(
                      "### _XRowSetApproveBroadcaster.RowSetApproveChecker.changeRowSet() :");
                  e.printStackTrace(logF);
                  throw new StatusException("RowSetApproveChecker.changeRowSet failed", e);
                }
              }
            });
      }
      // Adding relations for XRow as a Vector with all data
      // of current row of RowSet.

      ArrayList<Object> rowData = new ArrayList<Object>();

      for (int i = 0; i < DBTools.TST_TABLE_VALUES[0].length; i++) {
        rowData.add(DBTools.TST_TABLE_VALUES[0][i]);
      }

      // here XRef must be added
      // here XBlob must be added
      // here XClob must be added
      // here XArray must be added

      tEnv.addObjRelation("CurrentRowData", rowData);

      // Adding relation for XColumnLocate ifc test
      tEnv.addObjRelation("XColumnLocate.ColumnName", DBTools.TST_STRING_F);

      // Adding relation for XCompletedExecution
      tEnv.addObjRelation("InteractionHandlerChecker", new InteractionHandlerImpl());
      try {
        String sqlCommand =
            isMySQLDB
                ? "SELECT Column0  FROM soffice_test_table  WHERE ( (  Column0 = :param1 ) )"
                : "SELECT \"_TEXT\" FROM \"" + tableName + "\" WHERE ( ( \"_TEXT\" = :param1 ) )";
        rowSetProps.setPropertyValue("DataSourceName", dbSourceName);
        rowSetProps.setPropertyValue("Command", sqlCommand);
        rowSetProps.setPropertyValue("CommandType", new Integer(CommandType.COMMAND));
      } catch (Exception e) {
        throw new StatusException("setting up the RowSet with a parametrized command failed", e);
      }

      // Adding relation for XParameters ifc test
      tEnv.addObjRelation("XParameters.ParamValues", new ArrayList<String>());

      // Adding relation for XRowUpdate
      final XRow row = UnoRuntime.queryInterface(XRow.class, m_rowSet);
      tEnv.addObjRelation("XRowUpdate.XRow", row);

      // Adding relation for XResultSetUpdate
      {
        final XResultSet resultSet = UnoRuntime.queryInterface(XResultSet.class, m_rowSet);
        final XRowUpdate rowUpdate = UnoRuntime.queryInterface(XRowUpdate.class, m_rowSet);

        tEnv.addObjRelation(
            "XResultSetUpdate.UpdateTester",
            new ifc.sdbc._XResultSetUpdate.UpdateTester() {
              String lastUpdate = null;

              public int rowCount() throws SQLException {
                int prevPos = resultSet.getRow();
                resultSet.last();
                int count = resultSet.getRow();
                resultSet.absolute(prevPos);

                return count;
              }

              public void update() throws SQLException {
                lastUpdate = row.getString(1);
                lastUpdate += "_";
                rowUpdate.updateString(1, lastUpdate);
              }

              public boolean wasUpdated() throws SQLException {
                String getStr = row.getString(1);
                return lastUpdate.equals(getStr);
              }

              public int currentRow() throws SQLException {
                return resultSet.getRow();
              }
            });
      }

      envCreatedOK = true;
      return tEnv;

    } catch (com.sun.star.uno.Exception e) {
      log.println("couldn't set up test environment:");
      e.printStackTrace(log);
      try {
        if (m_connection != null) m_connection.close();
      } catch (Exception ex) {
      }
      throw new StatusException("couldn't set up test environment", e);
    } finally {
      if (!envCreatedOK) {
        try {
          m_connection.close();
        } catch (Exception ex) {
        }
      }
    }
  } // finish method getTestEnvironment