Skip to main content

Defining the Expanders template

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.

Typically, expander tests have a baseModel() method, where the model to test against is built. This can be done by using the ModelSpecBuilder, which creates Composites from onion-specifications.

To write the test, we can modify the template (test/java/org/normalizedsystems/sandcastle -> ExpanderTest.stg) 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="https://schemas.normalizedsystems.org/xsd/expanders/2023/0/0/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