/** @throws Exception */
  private void genSerializable() throws Exception {
    Context context = Context.enter();
    Scriptable scope = context.initStandardObjects();
    Context.exit();

    QueryDefinition queryDefn = newReportQuery();

    // column mapping
    String[] name = new String[] {"serializable"};
    ScriptExpression[] se = new ScriptExpression[name.length];
    se[0] = new ScriptExpression("new java.lang.StringBuffer(\"ss\")");
    se[0].setDataType(DataType.JAVA_OBJECT_TYPE);
    for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i]));

    // generation
    IQueryResults qr = myGenDataEngine.prepare(queryDefn).execute(scope);

    // important step
    queryResultID = qr.getID();

    IResultIterator ri = qr.getResultIterator();
    assertEquals(DataType.JAVA_OBJECT_TYPE, ri.getResultMetaData().getColumnType(1));
    while (ri.next()) {
      assertTrue(ri.getValue("serializable") instanceof StringBuffer);
      assertEquals("ss", ri.getValue("serializable").toString());
    }

    ri.close();
    qr.close();
    myGenDataEngine.shutdown();
  }
  /** @throws Exception */
  private void genBasic() throws Exception {
    expectedValue = new ArrayList();

    Context context = Context.enter();
    Scriptable scope = context.initStandardObjects();
    Context.exit();

    // ------------generation----------------
    QueryDefinition qd = newReportQuery();

    // prepare
    IBaseExpression[] rowBeArray = getRowExpr();
    IBinding[] totalBeArray = getAggrBinding();
    prepareExprNameAndQuery(rowBeArray, totalBeArray, qd);

    // generation
    IQueryResults qr = myGenDataEngine.prepare(qd).execute(scope);

    // important step
    queryResultID = qr.getID();

    IResultIterator ri = qr.getResultIterator();
    while (ri.next()) {
      for (int i = 0; i < rowBeArray.length; i++)
        expectedValue.add(ri.getValue(this.rowExprName[i]));

      for (int i = 0; i < totalBeArray.length; i++)
        expectedValue.add(ri.getValue(this.totalExprName[i]));
    }

    ri.close();
    qr.close();
    myGenDataEngine.shutdown();
  }
  /**
   * @param name
   * @param dataType
   * @param se
   * @throws BirtException
   */
  private void basicTestNoDataSet(String[] name, int[] dataType, ScriptExpression[] se)
      throws BirtException {
    DataEngine dataEngine =
        new DataEngineImpl(
            DataEngineContext.newInstance(DataEngineContext.DIRECT_PRESENTATION, null, null, null));
    QueryDefinition queryDefn = new QueryDefinition();

    for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i]));

    IResultIterator ri = dataEngine.prepare(queryDefn).execute(null).getResultIterator();
    if (ri.next()) {
      String str = "";
      for (int i = 0; i < name.length; i++) {
        Object value = ri.getValue(name[i]);
        str += value;

        if (i < name.length - 1) str += ", ";

        if (dataType[i] == DataType.INTEGER_TYPE)
          assertTrue(value.getClass().equals(Integer.class));
        else if (dataType[i] == DataType.DOUBLE_TYPE)
          assertTrue(value.getClass().equals(Double.class));
        else if (dataType[i] == DataType.DATE_TYPE) assertTrue(value.getClass().equals(Date.class));
      }
      testPrintln(str);
    }
  }
  /** @throws Exception */
  @Test
  public void testNoDataSetWithSubQuery() throws Exception {
    // outer query without data set
    int[] dataType = new int[] {DataType.DATE_TYPE};
    String[] name = new String[] {"testColumn1"};

    IResultIterator ri2 = null;
    {
      ScriptExpression[] se =
          new ScriptExpression[] {
            new ScriptExpression("new Date()", dataType[0]),
          };

      QueryDefinition queryDefn = new QueryDefinition();
      for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i]));

      // sub query
      String subQueryName = "TEST";
      SubqueryDefinition subQueryDefn = new SubqueryDefinition(subQueryName, queryDefn);
      for (int i = 0; i < name.length; i++)
        subQueryDefn.addBinding(
            new Binding(name[i], new ScriptExpression("row._outer." + name[i], dataType[i])));
      queryDefn.addSubquery(subQueryDefn);

      DataEngine myDataEngine =
          new DataEngineImpl(
              DataEngineContext.newInstance(
                  DataEngineContext.DIRECT_PRESENTATION, null, null, null));
      IResultIterator ri = myDataEngine.prepare(queryDefn).execute(null).getResultIterator();
      ri.next();
      ri2 = ri.getSecondaryIterator(subQueryName, null);
    }

    if (ri2.next()) {
      String str = "";
      for (int i = 0; i < name.length; i++) {
        Object value = ri2.getValue(name[i]);
        str += value;

        if (i < name.length - 1) str += ", ";

        if (dataType[0] == DataType.DATE_TYPE) assertTrue(value.getClass().equals(Date.class));
      }
      testPrintln(str);
    }
    ri2.close();
  }
  /** @throws Exception */
  @Test
  public void testNoDataSetWithNestedQuery() throws Exception {
    // outer query without data set
    String[] name = new String[] {"testColumn1"};
    IQueryResults queryResult = null;

    {
      int[] dataType = new int[] {DataType.DATE_TYPE};
      ScriptExpression[] se =
          new ScriptExpression[] {
            new ScriptExpression("new Date()", dataType[0]),
          };

      DataEngine myDataEngine =
          new DataEngineImpl(
              DataEngineContext.newInstance(
                  DataEngineContext.DIRECT_PRESENTATION, null, null, null));
      QueryDefinition queryDefn = new QueryDefinition();

      for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i]));

      IResultIterator ri = myDataEngine.prepare(queryDefn).execute(null).getResultIterator();
      queryResult = ri.getQueryResults();
    }

    // inner query with data set
    QueryDefinition queryDefn2 = this.newReportQuery();
    for (int i = 0; i < name.length; i++)
      queryDefn2.addBinding(new Binding(name[i], new ScriptExpression("row._outer." + name[i])));

    IResultIterator ri2 =
        this.dataEngine.prepare(queryDefn2).execute(queryResult, null).getResultIterator();
    if (ri2.next()) {
      String str = "";
      for (int i = 0; i < name.length; i++) {
        Object value = ri2.getValue(name[i]);
        str += value;

        if (i < name.length - 1) str += ", ";
      }
      testPrintln(str);
    }
    ri2.close();
  }
  /** @throws Exception */
  private void genDummy2() throws Exception {
    expectedValue = new ArrayList();

    Context context = Context.enter();
    Scriptable scope = context.initStandardObjects();
    Context.exit();

    // ------------generation----------------
    QueryDefinition qd = new QueryDefinition();

    // prepare
    IBaseExpression[] rowBeArray = getDummyRowExpr();
    prepareExprNameAndQuery(rowBeArray, null, qd);

    // prepare sub query
    SubqueryDefinition subQueryDefn = new SubqueryDefinition(subName, qd);
    qd.addSubquery(subQueryDefn);
    IBaseExpression[] rowBeArray2 = getDummyRowExpr();
    prepareExprNameAndQuery(rowBeArray2, null, subQueryDefn);

    // generation
    IQueryResults qr = myGenDataEngine.prepare(qd).execute(scope);

    // important step
    queryResultID = qr.getID();

    IResultIterator ri = qr.getResultIterator();
    while (ri.next()) {
      for (int i = 0; i < rowBeArray.length; i++)
        expectedValue.add(ri.getValue(this.rowExprName[i]));

      IResultIterator ri2 = ri.getSecondaryIterator(subName, scope);
      while (ri2.next()) {
        for (int i = 0; i < rowBeArray2.length; i++)
          expectedValue.add(ri2.getValue(this.rowExprName[i]));
      }
      ri2.close();
    }

    ri.close();
    qr.close();
    myGenDataEngine.shutdown();
  }
  /** @throws Exception */
  private void genUnserializable() throws Exception {
    Context context = Context.enter();
    Scriptable scope = context.initStandardObjects();
    Context.exit();

    QueryDefinition queryDefn = newReportQuery();

    // column mapping
    String[] name = new String[] {"unserializable"};
    ScriptExpression[] se = new ScriptExpression[name.length];
    se[0] = new ScriptExpression("new java.lang.ThreadGroup(\"ss\")");
    se[0].setDataType(DataType.JAVA_OBJECT_TYPE);
    for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i]));

    // generation
    IQueryResults qr = myGenDataEngine.prepare(queryDefn).execute(scope);

    // important step
    queryResultID = qr.getID();

    try {

      IResultIterator ri = qr.getResultIterator();
      assertEquals(DataType.JAVA_OBJECT_TYPE, ri.getResultMetaData().getColumnType(1));
      while (ri.next()) {
        assertTrue(ri.getValue("unserializable") instanceof ThreadGroup);
      }
      ri.close();
      assertTrue(false);
    } catch (Exception e) {
      // Currently, unserializable objects can't be saved in report doc
      e.printStackTrace();
    } finally {
      qr.close();
      myGenDataEngine.shutdown();
    }
  }