/**
   * without column edge
   *
   * @throws OLAPException
   * @throws BirtException
   */
  @Test
  public void testCursorOnCountry() throws OLAPException, BirtException {
    ICubeQueryDefinition cqd = creator.createMirroredQueryDefinition("cube", true);

    IBinding rowGrandTotal = new Binding("countryGrandTotal");
    rowGrandTotal.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC);
    rowGrandTotal.setExpression(new ScriptExpression("measure[\"measure1\"]"));
    rowGrandTotal.addAggregateOn("dimension[\"dimension1\"][\"level11\"]");
    rowGrandTotal.addAggregateOn("dimension[\"dimension5\"][\"level21\"]");
    rowGrandTotal.addAggregateOn("dimension[\"dimension6\"][\"level22\"]");

    cqd.addBinding(rowGrandTotal);

    // Create cube view.
    BirtCubeView cubeView =
        new BirtCubeView(
            new CubeQueryExecutor(null, cqd, de.getSession(), this.scope, de.getContext()));

    CubeCursor dataCursor = cubeView.getCubeCursor(new StopSign(), cube1);

    List columnEdgeBindingNames = new ArrayList();
    columnEdgeBindingNames.add("level11");
    columnEdgeBindingNames.add("level12");
    columnEdgeBindingNames.add("level13");
    columnEdgeBindingNames.add("level14");

    List rowEdgeBindingNames = new ArrayList();
    rowEdgeBindingNames.add("level21");
    rowEdgeBindingNames.add("level22");

    List measureBindingNames = new ArrayList();
    measureBindingNames.add("measure1");

    List grandBindingNames = new ArrayList();
    grandBindingNames.add("countryGrandTotal");

    try {
      testOut.print(
          creator.printCubeAlongEdge(
              dataCursor,
              columnEdgeBindingNames,
              rowEdgeBindingNames,
              measureBindingNames,
              null,
              null,
              null,
              grandBindingNames));
      this.checkOutputFile();
    } catch (Exception e) {
      fail("fail to get here!");
    }
  }
 /**
  * @param l
  * @param binding
  * @param type
  * @return
  * @throws DataException
  */
 private boolean contains(List l, IBinding binding, int type) throws DataException {
   for (int i = 0; i < l.size(); i++) {
     IBindingMetaInfo bm = (IBindingMetaInfo) l.get(i);
     if (bm.getBindingName().equals(binding.getBindingName()) && bm.getBindingType() == type)
       return true;
   }
   return false;
 }
  public void testGetReferableBindings() throws AdapterException, DataException {
    IBinding binding1 = new Binding("b1", new ScriptExpression("dimension[\"dim1\"][\"level1\"]"));
    int type1 = IBindingMetaInfo.DIMENSION_TYPE;
    IBinding binding2 = new Binding("b2", new ScriptExpression("dimension.dim1.level2 + 1"));
    int type2 = IBindingMetaInfo.DIMENSION_TYPE;
    IBinding binding3 =
        new Binding(
            "b3", new ScriptExpression("dimension[\"dim1\"][\"level3\"] + dimension.dim1.level2"));
    int type3 = IBindingMetaInfo.DIMENSION_TYPE;
    IBinding binding4 = new Binding("b4", new ScriptExpression("data.b1"));
    int type4 = IBindingMetaInfo.DIMENSION_TYPE;
    IBinding binding5 = new Binding("b5", new ScriptExpression("dimension.dim1.level1 + 25"));
    int type5 = IBindingMetaInfo.DIMENSION_TYPE;
    IBinding binding6 = new Binding("b6", new ScriptExpression("data.b4 + 1"));
    int type6 = IBindingMetaInfo.DIMENSION_TYPE;
    IBinding binding7 = new Binding("b7", new ScriptExpression("measure[\"abc\"]"));
    binding7.addAggregateOn("dimension[\"dim1\"][\"level2\"]");
    binding7.addAggregateOn("dimension[\"dim1\"][\"level1\"]");
    binding7.addAggregateOn("dimension[\"dim2\"][\"level1\"]");
    int type7 = IBindingMetaInfo.SUB_TOTAL_TYPE;

    IBinding binding71 = new Binding("b71", new ScriptExpression("measure[\"abc\"]"));
    binding71.addAggregateOn("dimension[\"dim1\"][\"level1\"]");
    binding71.addAggregateOn("dimension[\"dim1\"][\"level2\"]");
    binding71.addAggregateOn("dimension[\"dim2\"][\"level1\"]");
    int type71 = IBindingMetaInfo.SUB_TOTAL_TYPE;

    IBinding binding8 = new Binding("b8", new ScriptExpression("dimension[\"dim2\"][\"level1\"]"));
    int type8 = IBindingMetaInfo.DIMENSION_TYPE;

    IBinding binding9 = new Binding("b9", new ScriptExpression("measure[\"abc\"]"));
    binding9.addAggregateOn("dimension[\"dim1\"][\"level1\"]");
    binding9.addAggregateOn("dimension[\"dim1\"][\"level2\"]");
    int type9 = IBindingMetaInfo.GRAND_TOTAL_TYPE;

    IBinding binding10 = new Binding("b10", new ScriptExpression("measure[\"abc\"]"));
    int type10 = IBindingMetaInfo.MEASURE_TYPE;

    IBinding binding11 = new Binding("b11", new ScriptExpression("measure[\"abc\"]"));
    binding11.setAggrFunction("SUM");
    int type11 = IBindingMetaInfo.GRAND_TOTAL_TYPE;

    ICubeQueryDefinition query = new CubeQueryDefinition("query");
    query.addBinding(binding1);
    query.addBinding(binding2);
    query.addBinding(binding3);
    query.addBinding(binding4);
    query.addBinding(binding5);
    query.addBinding(binding6);
    query.addBinding(binding7);
    query.addBinding(binding71);
    query.addBinding(binding8);

    IEdgeDefinition columnEdge = query.createEdge(ICubeQueryDefinition.COLUMN_EDGE);
    IDimensionDefinition dim1 = columnEdge.createDimension("dim1");
    IHierarchyDefinition hier1 = dim1.createHierarchy("hier1");
    hier1.createLevel("level1");
    hier1.createLevel("level2");
    hier1.createLevel("level3");

    IEdgeDefinition rowEdge = query.createEdge(ICubeQueryDefinition.COLUMN_EDGE);
    IDimensionDefinition dim2 = rowEdge.createDimension("dim2");
    IHierarchyDefinition hier2 = dim2.createHierarchy("hier2");
    hier2.createLevel("level1");

    String targetLevel1 = "dimension[\"dim1\"][\"level1\"]";
    List l1 = this.session.getCubeQueryUtil().getReferableBindings(targetLevel1, query, true);
    assertTrue(l1.size() == 3);
    assertTrue(contains(l1, binding1, type1));
    assertTrue(contains(l1, binding4, type4));
    assertTrue(contains(l1, binding7, type7));

    String targetLevel2 = "dimension[\"dim1\"][\"level1\"]";
    List l2 = this.session.getCubeQueryUtil().getReferableBindings(targetLevel2, query, false);
    assertTrue(l2.size() == 5);
    assertTrue(contains(l2, binding1, type1));
    assertTrue(contains(l2, binding4, type4));
    assertTrue(contains(l2, binding5, type5));
    assertTrue(contains(l2, binding6, type6));
    assertTrue(contains(l2, binding7, type7));

    String targetLevel3 = "dimension[\"dim1\"][\"level2\"]";
    List l3 = this.session.getCubeQueryUtil().getReferableBindings(targetLevel3, query, false);
    assertTrue(l3.size() == 2);
    assertTrue(contains(l3, binding2, type2));
    assertTrue(contains(l3, binding71, type71));

    String targetLevel4 = "dimension[\"dim1\"][\"level2\"]";
    query.addBinding(binding9);
    List l4 = this.session.getCubeQueryUtil().getReferableBindings(targetLevel4, query, false);

    assertTrue(l4.size() == 3);
    assertTrue(contains(l4, binding2, type2));
    assertTrue(contains(l4, binding71, type71));
    assertTrue(contains(l4, binding9, type9));

    String targetLevel5 = "dimension[\"dim2\"][\"level1\"]";
    query.addBinding(binding10);
    List l5 = this.session.getCubeQueryUtil().getReferableBindings(targetLevel5, query, false);
    assertTrue(l5.size() == 4);
    assertTrue(contains(l5, binding7, type7));
    assertTrue(contains(l5, binding71, type71));
    assertTrue(contains(l5, binding8, type8));
    assertTrue(contains(l5, binding10, type10));

    String targetLevel6 = "dimension[\"dim1\"][\"level2\"]";
    query.addBinding(binding11);
    List l6 = this.session.getCubeQueryUtil().getReferableBindings(targetLevel6, query, false);
    assertTrue(l6.size() == 4);
    assertTrue(contains(l6, binding2, type2));
    assertTrue(contains(l6, binding71, type71));
    assertTrue(contains(l6, binding9, type9));
    assertTrue(contains(l6, binding11, type11));
  }
  public void testGetReferencedLevels() throws DataException, AdapterException {
    IBinding binding1 = new Binding("b1", new ScriptExpression("dimension[\"dim1\"][\"level1\"]"));
    IBinding binding2 = new Binding("b2", new ScriptExpression("dimension.dim1.level2 + 1"));
    IBinding binding3 =
        new Binding(
            "b3", new ScriptExpression("dimension[\"dim1\"][\"level3\"] + dimension.dim1.level2"));
    IBinding binding4 = new Binding("b4", new ScriptExpression("data.b1"));
    IBinding binding5 = new Binding("b5", new ScriptExpression("dimension.dim1.level1 + 25"));
    IBinding binding6 = new Binding("b6", new ScriptExpression("data.b4 + 1"));
    IBinding binding7 = new Binding("b7", new ScriptExpression("measure[\"abc\"]"));
    binding7.addAggregateOn("dimension[\"dim1\"][\"level2\"]");
    binding7.addAggregateOn("dimension[\"dim1\"][\"level1\"]");
    binding7.addAggregateOn("dimension[\"dim2\"][\"level1\"]");

    IBinding binding71 = new Binding("b71", new ScriptExpression("measure[\"abc\"]"));
    binding71.addAggregateOn("dimension[\"dim1\"][\"level1\"]");
    binding71.addAggregateOn("dimension[\"dim1\"][\"level2\"]");
    binding71.addAggregateOn("dimension[\"dim2\"][\"level1\"]");

    IBinding binding8 = new Binding("b8", new ScriptExpression("dimension[\"dim2\"][\"level1\"]"));

    ICubeQueryDefinition query = new CubeQueryDefinition("query");
    query.addBinding(binding1);
    query.addBinding(binding2);
    query.addBinding(binding3);
    query.addBinding(binding4);
    query.addBinding(binding5);
    query.addBinding(binding6);
    query.addBinding(binding7);
    query.addBinding(binding71);
    query.addBinding(binding8);

    IEdgeDefinition columnEdge = query.createEdge(ICubeQueryDefinition.COLUMN_EDGE);
    IDimensionDefinition dim1 = columnEdge.createDimension("dim1");
    IHierarchyDefinition hier1 = dim1.createHierarchy("hier1");
    hier1.createLevel("level1");
    hier1.createLevel("level2");
    hier1.createLevel("level3");

    IEdgeDefinition rowEdge = query.createEdge(ICubeQueryDefinition.ROW_EDGE);
    IDimensionDefinition dim2 = rowEdge.createDimension("dim2");
    IHierarchyDefinition hier2 = dim2.createHierarchy("hier2");
    hier2.createLevel("level1");

    List l1 =
        this.session
            .getCubeQueryUtil()
            .getReferencedLevels("dimension[\"dim1\"][\"level1\"]", "data[\"b1\"]", query);
    assertTrue(l1.size() == 0);

    List l2 =
        this.session
            .getCubeQueryUtil()
            .getReferencedLevels("dimension[\"dim1\"][\"level1\"]", "data[\"b7\"]", query);
    assertTrue(l2.size() == 1);
    assertTrue(((ILevelDefinition) l2.get(0)).getName().equals("level1"));

    List l3 =
        this.session
            .getCubeQueryUtil()
            .getReferencedLevels("dimension[\"dim2\"][\"level1\"]", "data[\"b71\"]", query);
    assertTrue(l3.size() == 2);
    assertTrue(((ILevelDefinition) l3.get(0)).getName().equals("level1"));
    assertTrue(((ILevelDefinition) l3.get(1)).getName().equals("level2"));
  }
  @Test
  public void testCursorOnPageEdge() throws Exception {
    ICubeQueryDefinition cqd = creator.createMirroredQueryDefinitionWithPage();

    IBinding rowGrandTotal = new Binding("rowGrandTotal");
    rowGrandTotal.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC);
    rowGrandTotal.setExpression(new ScriptExpression("measure[\"measure1\"]"));
    rowGrandTotal.addAggregateOn("dimension[\"dimension5\"][\"level21\"]");
    rowGrandTotal.addAggregateOn("dimension[\"dimension6\"][\"level22\"]");

    IBinding columnGrandTotal = new Binding("columnGrandTotal");
    columnGrandTotal.setAggrFunction(IBuildInAggregation.TOTAL_AVE_FUNC);
    columnGrandTotal.setExpression(new ScriptExpression("measure[\"measure1\"]"));
    columnGrandTotal.addAggregateOn("dimension[\"dimension5\"][\"level21\"]");
    columnGrandTotal.addAggregateOn("dimension[\"dimension1\"][\"level11\"]");
    columnGrandTotal.addAggregateOn("dimension[\"dimension2\"][\"level12\"]");
    columnGrandTotal.addAggregateOn("dimension[\"dimension3\"][\"level13\"]");
    columnGrandTotal.addAggregateOn("dimension[\"dimension4\"][\"level14\"]");

    IBinding totalGrandTotal = new Binding("totalGrandTotal");
    totalGrandTotal.setAggrFunction(IBuildInAggregation.TOTAL_SUM_FUNC);
    totalGrandTotal.setExpression(new ScriptExpression("measure[\"measure1\"]"));
    totalGrandTotal.addAggregateOn("dimension[\"dimension5\"][\"level21\"]");

    cqd.addBinding(rowGrandTotal);
    cqd.addBinding(columnGrandTotal);
    cqd.addBinding(totalGrandTotal);

    // Create cube view.
    BirtCubeView cubeView =
        new BirtCubeView(
            new CubeQueryExecutor(null, cqd, de.getSession(), this.scope, de.getContext()));

    CubeCursor dataCursor = cubeView.getCubeCursor(new StopSign(), cube1);

    List columnEdgeBindingNames = new ArrayList();
    columnEdgeBindingNames.add("level11");
    columnEdgeBindingNames.add("level12");
    columnEdgeBindingNames.add("level13");
    columnEdgeBindingNames.add("level14");

    List rowEdgeBindingNames = new ArrayList();
    rowEdgeBindingNames.add("level22");

    List measureBindingNames = new ArrayList();
    measureBindingNames.add("measure1");

    List rowGrandTotalNames = new ArrayList();
    rowGrandTotalNames.add("rowGrandTotal");

    List pageBindingNames = new ArrayList();
    pageBindingNames.add("level21");

    testOut.print(
        creator.printCubeAlongPageEdge(
            dataCursor,
            pageBindingNames,
            columnEdgeBindingNames,
            rowEdgeBindingNames,
            measureBindingNames,
            rowGrandTotalNames,
            "columnGrandTotal",
            "totalGrandTotal",
            null));
    this.checkOutputFile();
  }