Esempio n. 1
0
  /** Checks that triggers are called in the correct order. */
  public void testTriggerCallOrder() {
    final MyProperties props = new MyProperties();
    String path = "test.mondrian.properties.call.order";
    BooleanProperty boolProp = new BooleanProperty(props, path, false);

    final State2 state = new State2();
    state.callCounter = 0;

    // now explicitly set the property
    props.setProperty(path, "false");

    String v = props.getProperty(path);
    assertTrue("Check property value is null", (v != null));
    assertTrue("Check property value is true", (!Boolean.valueOf(v).booleanValue()));

    // primaryOne
    Trigger primaryOneTrigger =
        new Trigger() {
          public boolean isPersistent() {
            return false;
          }

          public int phase() {
            return Trigger.PRIMARY_PHASE;
          }

          public void execute(Property property, String value) {
            state.primaryOne = state.callCounter++;
          }
        };
    boolProp.addTrigger(primaryOneTrigger);

    // secondaryOne
    Trigger secondaryOneTrigger =
        new Trigger() {
          public boolean isPersistent() {
            return false;
          }

          public int phase() {
            return Trigger.SECONDARY_PHASE;
          }

          public void execute(Property property, String value) {
            state.secondaryOne = state.callCounter++;
          }
        };
    boolProp.addTrigger(secondaryOneTrigger);

    // tertiaryOne
    Trigger tertiaryOneTrigger =
        new Trigger() {
          public boolean isPersistent() {
            return false;
          }

          public int phase() {
            return Trigger.TERTIARY_PHASE;
          }

          public void execute(Property property, String value) {
            state.tertiaryOne = state.callCounter++;
          }
        };
    boolProp.addTrigger(tertiaryOneTrigger);

    // tertiaryTwo
    Trigger tertiaryTwoTrigger =
        new Trigger() {
          public boolean isPersistent() {
            return false;
          }

          public int phase() {
            return Trigger.TERTIARY_PHASE;
          }

          public void execute(Property property, String value) {
            state.tertiaryTwo = state.callCounter++;
          }
        };
    boolProp.addTrigger(tertiaryTwoTrigger);

    // secondaryTwo
    Trigger secondaryTwoTrigger =
        new Trigger() {
          public boolean isPersistent() {
            return false;
          }

          public int phase() {
            return Trigger.SECONDARY_PHASE;
          }

          public void execute(Property property, String value) {
            state.secondaryTwo = state.callCounter++;
          }
        };
    boolProp.addTrigger(secondaryTwoTrigger);

    // primaryTwo
    Trigger primaryTwoTrigger =
        new Trigger() {
          public boolean isPersistent() {
            return false;
          }

          public int phase() {
            return Trigger.PRIMARY_PHASE;
          }

          public void execute(Property property, String value) {
            state.primaryTwo = state.callCounter++;
          }
        };
    boolProp.addTrigger(primaryTwoTrigger);

    String falseStr = "false";
    props.setProperty(path, falseStr);
    assertTrue("Check trigger was called", (state.callCounter == 0));

    String trueStr = "true";
    props.setProperty(path, trueStr);

    assertTrue("Check trigger was NOT called", (state.callCounter != 0));
    assertTrue("Check triggers was NOT called correct number of times", (state.callCounter == 6));

    // now make sure that primary are called before secondary which are
    // before tertiary
    assertTrue("Check primaryOne > secondaryOne", (state.primaryOne < state.secondaryOne));
    assertTrue("Check primaryOne > secondaryTwo", (state.primaryOne < state.secondaryTwo));
    assertTrue("Check primaryOne > tertiaryOne", (state.primaryOne < state.tertiaryOne));
    assertTrue("Check primaryOne > tertiaryTwo", (state.primaryOne < state.tertiaryTwo));

    assertTrue("Check primaryTwo > secondaryOne", (state.primaryTwo < state.secondaryOne));
    assertTrue("Check primaryTwo > secondaryTwo", (state.primaryTwo < state.secondaryTwo));
    assertTrue("Check primaryTwo > tertiaryOne", (state.primaryTwo < state.tertiaryOne));
    assertTrue("Check primaryTwo > tertiaryTwo", (state.primaryTwo < state.tertiaryTwo));

    assertTrue("Check secondaryOne > tertiaryOne", (state.secondaryOne < state.tertiaryOne));
    assertTrue("Check secondaryOne > tertiaryTwo", (state.secondaryOne < state.tertiaryTwo));

    assertTrue("Check secondaryTwo > tertiaryOne", (state.secondaryTwo < state.tertiaryOne));
    assertTrue("Check secondaryTwo > tertiaryTwo", (state.secondaryTwo < state.tertiaryTwo));

    // remove some of the triggers
    boolProp.removeTrigger(primaryTwoTrigger);
    boolProp.removeTrigger(secondaryTwoTrigger);
    boolProp.removeTrigger(tertiaryTwoTrigger);

    // reset
    state.callCounter = 0;
    state.primaryOne = 0;
    state.primaryTwo = 0;
    state.secondaryOne = 0;
    state.secondaryTwo = 0;
    state.tertiaryOne = 0;
    state.tertiaryTwo = 0;

    props.setProperty(path, falseStr);
    assertTrue("Check trigger was NOT called", (state.callCounter != 0));
    assertTrue("Check triggers was NOT called correct number of times", (state.callCounter == 3));

    // now make sure that primary are called before secondary which are
    // before tertiary
    assertTrue("Check primaryOne > secondaryOne", (state.primaryOne < state.secondaryOne));
    assertTrue("Check primaryOne > tertiaryOne", (state.primaryOne < state.tertiaryOne));

    assertTrue("Check secondaryOne > tertiaryOne", (state.secondaryOne < state.tertiaryOne));
  }