IsApplicable Condition

Not all expanders should be applied to each element. Most expanders only run when some option is defined or a certain setting is used. For this, we can use the isApplicable condition.

Testing the condition

By default, {Expander}Test.java will contain a test method test_isApplicable to test this condition:

@Test
public void test_isApplicable() {
  DataElementComposite model = baseModel();

  tester.testIsNotApplicable(model);

  tester.testIsApplicable(model);
}

Let’s say that the expander should only run when the dataOption demo.includeSomeClass is added.

We then modify the test to test the 2 cases:

@Test
public void test_isApplicable() {
  DataElementComposite model = baseModel();

  tester.testIsNotApplicable(model);

  specBuilder.extendModel(model, dataOption("demo.includeSomeClass"));

  tester.testIsApplicable(model);
}

The extendModel() method will take the DataElementComposite and add a dataOption as defined in the second argument.

Implementing the isApplicable condition

Next, the isApplicable condition should be implemented. To do this, we implement the condition as an OGNL expression in the xml:

<isApplicable>dataElement.getOption('demo.includeSomeClass').defined</isApplicable>

Note: each element with options has the getOption(String optionTypeName) which will return an object with a value and isDefined()/isEmpty() methods

Next

Learn how to implement an expander