Esempio n. 1
0
  /**
   * Creates an instance of the service <code>com.sun.star.sdbc.Driver</code>.
   *
   * <p>Object relations created :
   *
   * <ul>
   *   <li><code>'XDriver.URL'</code> for {@link ifc.sdbc._XDriver}: is the URL of the database to
   *       which to connect. The URL is obtained from the parameter <code>mozab.url</code>
   *   <li><code>'XDriver.UNSUITABLE_URL'</code> for {@link ifc.sdbc._XDriver}: the wrong kind of
   *       URL to connect using given driver. The URL is obtained from the parameter <code>jdbc.url
   *       </code>
   *   <li><code>'XDriver.INFO'</code> for {@link ifc.sdbc._XDriver}: a list of arbitrary string
   *       tag/value pairs as connection arguments.
   * </ul>
   */
  @Override
  protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log)
      throws Exception {

    XInterface oObj =
        (XInterface) Param.getMSF().createInstance("com.sun.star.comp.sdbc.MozabDriver");

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

    // adding relation for sdbc.XDriver
    String mozabURL = (String) Param.get("mozab.url");
    if (mozabURL == null) {
      throw new StatusException(Status.failed("Couldn't get 'mozabURL' from ini-file"));
    }
    tEnv.addObjRelation("XDriver.URL", "sdbc:address:" + mozabURL);

    PropertyValue[] info = new PropertyValue[0];
    tEnv.addObjRelation("XDriver.INFO", info);

    String jdbcUrl = (String) Param.get("jdbc.url");
    if (jdbcUrl == null) {
      throw new StatusException(Status.failed("Couldn't get 'jdbc.url' from ini-file"));
    }
    tEnv.addObjRelation("XDriver.UNSUITABLE_URL", "jdbc:" + jdbcUrl);

    return tEnv;
  }
Esempio n. 2
0
  /**
   * Initializes some class fields. Then creates DataSource, which serves as a single source for all
   * tables created in the test. This DataSource then registered in the global <code>DatabaseContext
   * </code> service. This data source's URL points to SOffice temp directory where tables are
   * copied from <code>TestDocuments</code> directory on every environment creation. To create
   * DataSource for MySQL database next parameters required in ini-file:
   *
   * <ul>
   *   <li><code>test.db.url</code> - URL to MySQL database. For example: <code>
   *       mysql://mercury:3306/api_current</code>
   *   <li><code>test.db.user</code> - user for MySQL database
   *   <li><code>test.db.password</code> - password for MySQL database
   * </ul>
   *
   * @throws StatusException if DataSource can not be created or registered.
   */
  protected void initialize(TestParameters Param, PrintWriter _log) throws StatusException {
    m_orb = (XMultiServiceFactory) Param.getMSF();

    String tmpDir = utils.getOfficeTemp(m_orb);

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

    dbTools = new DBTools(m_orb, _log);

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

    log.println("Creating and registering DataSource ...");
    srcInf = new DataSourceDescriptor(m_orb);
    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";
      srcInf.Info = propInfo;
    } else {
      srcInf.URL = "sdbc:dbase:" + DBTools.dirToUrl(tmpDir);
    }
    m_dataSource = srcInf.createDataSource();
    m_dataSource.registerAs(dbSourceName, true);
  }
  /**
   * Creating a Testenvironment for the interfaces to be tested. In case if OS is Win or not
   * specified tries to create an instance of the service <code>com.sun.star.system.SimpleSystemMail
   * </code>.
   *
   * <p>Object relations created :
   *
   * <ul>
   *   <li><code>'SystemMailExist'</code> for {@link ifc.system._XSimpleMailClientSupplier} : the
   *       value depends on tested platform and <code>test.system.mail.isExist</code> INI parameter.
   * </ul>
   */
  public TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log)
      throws StatusException {
    XInterface oObj = null;
    Object oInterface = null;
    boolean isWinOS = false;
    boolean isSystemMailExist = false;

    // if 'OS' parameter is not defined then by default
    // Win platform is used. If really not the error will occurs.
    isWinOS = Param.get("OS") == null || "wntmsci".equals(Param.get("OS"));
    isSystemMailExist = "true".equalsIgnoreCase((String) Param.get("test.system.mail.isExist"));

    XMultiServiceFactory xMSF = Param.getMSF();
    try {
      if (isWinOS) {
        log.println("The OS is Win : trying to create service");
        oInterface = xMSF.createInstance("com.sun.star.system.SimpleSystemMail");
      } else {
        log.println("The OS is NOT Win, SKIPPING this component");
        log.println("Creating Dummy object ...");
        oInterface = xMSF.createInstance("com.sun.star.io.Pipe");
      }
    } catch (com.sun.star.uno.Exception e) {
      log.println("Can't create an object.");
      throw new StatusException("Can't create an object", e);
    }

    oObj = (XInterface) oInterface;

    log.println("ImplementationName: " + util.utils.getImplName(oObj));

    TestEnvironment tEnv = new TestEnvironment(oObj);

    tEnv.addObjRelation("SystemMailExist", new Boolean(isSystemMailExist));

    return tEnv;
  } // finish method getTestEnvironment
  /** * creating a Testenvironment for the interfaces to be tested */
  protected synchronized TestEnvironment createTestEnvironment(
      TestParameters Param, PrintWriter log) {
    if (xTextDoc != null) {
      try {
        XCloseable closer = UnoRuntime.queryInterface(XCloseable.class, xTextDoc);
        closer.close(true);
      } catch (com.sun.star.util.CloseVetoException e) {
        log.println("couldn't close document");
      } catch (com.sun.star.lang.DisposedException e) {
        log.println("couldn't close document");
      }

      log.println("Existing document disposed");
    }

    log.println("creating a text document");
    xTextDoc = WriterTools.createTextDoc(((XMultiServiceFactory) Param.getMSF()));

    // initialize test table
    if (isMySQLDB) {
      try {
        dbTools.initTestTableUsingJDBC(tableName, srcInf);
      } 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"));
      }
    }

    XInterface oObj = null;
    XShapes oShapes = null;
    XInterface oInstance = null;
    XConnection connection = null;

    // creation of testobject here
    // first we write what we are intend to do to log file
    log.println("creating a test environment");

    XNameContainer forms = FormTools.getForms(WriterTools.getDrawPage(xTextDoc));

    try {
      String[] formNames = forms.getElementNames();

      for (int i = 0; i < formNames.length; i++) {
        log.println("Removing form '" + formNames[i] + "' ...");
        forms.removeByName(formNames[i]);
      }
    } catch (com.sun.star.lang.WrappedTargetException e) {
      e.printStackTrace(log);
    } catch (com.sun.star.container.NoSuchElementException e) {
      e.printStackTrace(log);
    }

    String[] formNames = forms.getElementNames();
    FormTools.insertForm(xTextDoc, forms, "MyForm");
    formNames = forms.getElementNames();

    XLoadable formLoader = null;

    try {
      formLoader = FormTools.bindForm(xTextDoc, "MyForm", dbSourceName, tableName);
    } catch (com.sun.star.uno.Exception e) {
      log.println(
          "Cann't bind the form to source '" + dbSourceName + "', table '" + tableName + "' :");
      e.printStackTrace(log);
      throw new StatusException("Cann't bind a form", e);
    }

    // DEBUG
    log.println("Forms before adding controls : ");
    formNames = forms.getElementNames();

    for (int i = 0; i < formNames.length; i++) {
      log.println("    '" + formNames[i] + "'");
    }

    XControlShape shape1 = null;
    XControlShape shape2 = null;

    try {

      log.println("Elements in the 'MyForm' :");

      XIndexAccess formElements1 =
          UnoRuntime.queryInterface(XIndexAccess.class, forms.getByName("MyForm"));

      for (int i = 0; i < formElements1.getCount(); i++) {
        XNamed elemName = UnoRuntime.queryInterface(XNamed.class, formElements1.getByIndex(i));
        log.println("   '" + elemName.getName() + "'");
      }

      // END DEBUG
      // put something on the drawpage
      log.println("inserting some ControlShapes");
      oShapes = DrawTools.getShapes(WriterTools.getDrawPage(xTextDoc));
      shape1 = FormTools.createControlShape(xTextDoc, 3000, 4500, 15000, 1000, "CommandButton");
      shape2 = FormTools.createControlShape(xTextDoc, 5000, 3500, 7500, 5000, "TextField");

      XControlShape shape3 =
          FormTools.createControlShape(xTextDoc, 2000, 1500, 1000, 1000, "CheckBox");
      oShapes.add(shape1);
      oShapes.add(shape2);
      oShapes.add(shape3);
    } catch (Exception e) {
      e.printStackTrace(log);
    }

    log.println("Forms after adding controls : ");
    formNames = forms.getElementNames();

    for (int i = 0; i < formNames.length; i++) {
      log.println("    '" + formNames[i] + "'");
    }

    try {
      log.println("Elements in the 'MyForm' :");

      XIndexAccess formElements1 =
          UnoRuntime.queryInterface(XIndexAccess.class, forms.getByName("MyForm"));

      for (int i = 0; i < formElements1.getCount(); i++) {
        XNamed elemName = UnoRuntime.queryInterface(XNamed.class, formElements1.getByIndex(i));
        log.println("   '" + elemName.getName() + "'");
      }
    } catch (Exception e) {
      e.printStackTrace(log);
    }

    formLoader.load();

    try {
      oObj =
          (XForm)
              AnyConverter.toObject(
                  new Type(XForm.class),
                  (FormTools.getForms(WriterTools.getDrawPage(xTextDoc))).getByName("MyForm"));

      XPropertySet xSetProp = UnoRuntime.queryInterface(XPropertySet.class, oObj);
      connection =
          (XConnection)
              AnyConverter.toObject(
                  new Type(XConnection.class), xSetProp.getPropertyValue("ActiveConnection"));
    } catch (com.sun.star.uno.Exception e) {
      log.println("Couldn't get Form");
      e.printStackTrace(log);
    }

    // get a control
    oInstance = FormTools.createControl(xTextDoc, "TextField");

    log.println("creating a new environment for drawpage object");

    TestEnvironment tEnv = new TestEnvironment(oObj);

    // adding relation for closing connection while environment disposing.
    this.conn = connection;

    // adding relation for XSubmit
    XControlModel the_Model = shape2.getControl();
    XControlAccess the_access =
        UnoRuntime.queryInterface(XControlAccess.class, xTextDoc.getCurrentController());
    XControl cntrl = null;

    // now get the OEditControl
    try {
      cntrl = the_access.getControl(the_Model);
      log.println(cntrl.getClass().getName());
    } catch (com.sun.star.container.NoSuchElementException e) {
      log.println("Couldn't get OEditControl");
      e.printStackTrace(log);
      throw new StatusException("Couldn't get OEditControl", e);
    }

    XResultSet the_set = UnoRuntime.queryInterface(XResultSet.class, oObj);

    try {
      the_set.first();
    } catch (SQLException e) {
      log.println("Cann't move cursor to the first row.");
      e.printStackTrace();
      throw new StatusException("Can't move cursor to the first row.", e);
    }

    tEnv.addObjRelation("Model1", shape1.getControl());
    tEnv.addObjRelation("Model2", shape2.getControl());

    // adding an object for XNameReplace testing
    log.println("adding oInstace as obj relation to environment");
    tEnv.addObjRelation("INSTANCE", oInstance);

    // INDEX : _XNameContainer
    log.println("adding INDEX as obj relation to environment");
    tEnv.addObjRelation("INDEX", "0");

    // INDEX : _XNameReplace
    log.println("adding NameReplaceIndex as obj relation to environment");
    tEnv.addObjRelation("XNameReplaceINDEX", "2");

    // INSTANCEn : _XNameContainer; _XNameReplace
    log.println("adding INSTANCEn as obj relation to environment");

    // XComponent xComp = (XComponent)
    //    UnoRuntime.queryInterface(XComponent.class, xDrawDoc);
    String tc = (String) Param.get("THRCNT");
    int THRCNT = 1;

    if (tc != null) {
      THRCNT = Integer.parseInt(tc);
    }

    for (int n = 1; n < (2 * (THRCNT + 1)); n++) {
      log.println("adding INSTANCE" + n + " as obj relation to environment");
      tEnv.addObjRelation("INSTANCE" + n, FormTools.createControl(xTextDoc, "CheckBox"));
    }

    // adding relation for XNameContainer
    tEnv.addObjRelation("XNameContainer.AllowDuplicateNames", new Object());

    // adding relation for XPersistObject
    tEnv.addObjRelation("OBJNAME", "stardiv.one.form.component.Form");

    if (the_set != null) {
      log.println("The Form has a not empty ResultSet");
    }

    // Adding obj relation for XRowSetApproveBroadcaster test
    final XResultSet xResSet = UnoRuntime.queryInterface(XResultSet.class, oObj);
    final XResultSetUpdate xResSetUpdate = UnoRuntime.queryInterface(XResultSetUpdate.class, oObj);
    final XRowSet xRowSet = UnoRuntime.queryInterface(XRowSet.class, oObj);
    final PrintWriter logF = log;
    tEnv.addObjRelation(
        "XRowSetApproveBroadcaster.ApproveChecker",
        new ifc.sdb._XRowSetApproveBroadcaster.RowSetApproveChecker() {
          public void moveCursor() {
            try {
              xResSet.beforeFirst();
              xResSet.afterLast();
            } catch (com.sun.star.sdbc.SQLException e) {
              logF.println(
                  "### _XRowSetApproveBroadcaster." + "RowSetApproveChecker.moveCursor() :");
              e.printStackTrace(logF);
            }
          }

          public RowChangeEvent changeRow() {
            try {
              xResSet.first();

              XRowUpdate row = UnoRuntime.queryInterface(XRowUpdate.class, xResSet);
              row.updateString(1, "1");
              xResSetUpdate.updateRow();
            } catch (com.sun.star.sdbc.SQLException e) {
              logF.println(
                  "### _XRowSetApproveBroadcaster." + "RowSetApproveChecker.changeRow() :");
              e.printStackTrace(logF);
            }

            RowChangeEvent ev = new RowChangeEvent();
            ev.Action = com.sun.star.sdb.RowChangeAction.UPDATE;
            ev.Rows = 1;

            return ev;
          }

          public void changeRowSet() {
            try {
              xRowSet.execute();
            } catch (com.sun.star.sdbc.SQLException e) {
              logF.println(
                  "### _XRowSetApproveBroadcaster." + "RowSetApproveChecker.changeRowSet() :");
              e.printStackTrace(logF);
            }
          }
        });

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

    // Adding relation for XParameters ifc test
    ArrayList<Object> params = new ArrayList<Object>();

    /** *** statement parameter types and their initial values must be added here as relation. */
    params.add("SAU99");
    params.add(new Boolean(false));
    params.add(new Byte((byte) 123));
    params.add(new Short((short) 234));
    params.add(new Integer(12345));
    params.add(new Long(23456));
    params.add(new Float(1.234));
    params.add(new Double(2.345));
    params.add(new byte[] {1, 2, 3});
    Date d = new Date();
    d.Day = 26;
    d.Month = 1;
    d.Year = 2001;
    params.add(d);
    Time t = new Time();
    t.Hours = 1;
    t.NanoSeconds = 123456789;
    t.Minutes = 25;
    t.Seconds = 14;
    params.add(t);
    DateTime dt = new DateTime();
    dt.Day = 26;
    dt.Month = 1;
    dt.Year = 2001;
    dt.Hours = 1;
    dt.NanoSeconds = 123456789;
    dt.Minutes = 25;
    dt.Seconds = 14;
    params.add(dt);
    tEnv.addObjRelation("XParameters.ParamValues", params);

    // Adding relation for XCompletedExecution
    tEnv.addObjRelation("InteractionHandlerChecker", new InteractionHandlerImpl());

    // Adding for XWarningSupplier
    tEnv.addObjRelation("CheckWarningsSupplier", new Boolean(isMySQLDB));

    // Adding relation for XDatabaseParameterBroadcaster
    tEnv.addObjRelation("ParameterListenerChecker", new ODatabaseForm.ParameterListenerImpl());
    XPropertySet xSetProp = UnoRuntime.queryInterface(XPropertySet.class, oObj);
    try {
      xSetProp.setPropertyValue("DataSourceName", dbSourceName);
      if (isMySQLDB) {
        xSetProp.setPropertyValue(
            "Command", "SELECT Column0  FROM soffice_test_table  WHERE ( (  Column0 = :param1 ) )");
      } else {
        xSetProp.setPropertyValue(
            "Command",
            "SELECT \"_TEXT\" FROM \"ODatabaseForm_tmp0\" WHERE ( ( \"_TEXT\" = :param1 ) )");
      }

      xSetProp.setPropertyValue("CommandType", new Integer(CommandType.COMMAND));
    } catch (Exception e) {
    }

    // Adding relation for XResultSetUpdate
    final XRowUpdate xRowUpdate = UnoRuntime.queryInterface(XRowUpdate.class, oObj);
    final XRow xRow = UnoRuntime.queryInterface(XRow.class, oObj);

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

          public int rowCount() throws SQLException {
            int prevPos = xResSet.getRow();
            xResSet.last();

            int count = xResSet.getRow();
            xResSet.absolute(prevPos);

            return count;
          }

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

          public boolean wasUpdated() throws SQLException {
            String getStr = xRow.getString(1);

            return lastUpdate.equals(getStr);
          }

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

    // 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]);
    }

    tEnv.addObjRelation("CurrentRowData", rowData);

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

    tEnv.addObjRelation("XPropertyContainer.propertyNotRemovable", "Cycle");

    PropertyValue propVal = new PropertyValue();
    propVal.Name = "Name";
    propVal.Value = "Text since XPropertyAccess";

    tEnv.addObjRelation("XPropertyAccess.propertyToChange", propVal);

    return tEnv;
  } // finish method getTestEnvironment
  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));
    }
  }