Implementing Expanders

Testing the base content of an expander

By default, {Expander}Test.java contains a test_base method:

@Test
public void test_base() {
  tester.testBase(baseModel());
}

This method will run the expander on the baseModel and compare the content with the template test_base in the {Expander}Test.java file.

To write the test, we can modify the template to what the artifact should look like:

test_base() ::= <<
package net.palver.test;

// @expanderComment@
public void CityFoo {

}
>>

The test will fail on a mismatch:

java.lang.AssertionError:
Expected: "package net.palver.test;

// @expanderComment@
public void CityFoo {

}"
     but: was "INSERT TEMPLATE"

Implementation

The implementation consists of 2 steps:

  1. The mapping file defines all variables to be used in the template
  2. The template then uses this data to create the content

For the test defined above the template can be defined as follows:

base() ::= <<
package <packageName>;

// @expanderComment@
public void <dataElement>Foo {

}
>>

This requires the values packageName and dataElement, which can be defined in the mapping with ognl expressions:

<?xml version="1.0" encoding="UTF-8" ?>
<mapping xmlns="http://nsx.normalizedsystems.org/201806/expanders/mapping">
  <value name="dataElement" eval="dataElement.name"/>
  <value name="packageName" eval="dataElement.packageName"/>
</mapping>

Now the expander will extract the values for both variables based on the eval expressions

Next

Learn more complex mappings in Expander Mapping

References