Skip to main content

Defining the Expanders template

Testing the base content of an expander

By default, {Expander} contains a test_base method:

public void test_base() {

This method will run the expander on the baseModel and compare the content with the template test_base in the {Expander} 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:

Expected: "package net.palver.test;

// @expanderComment@
public void CityFoo {



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="">
<value name="dataElement" eval=""/>
<value name="packageName" eval="dataElement.packageName"/>

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

Next: Learn more complex mappings in Expander Mapping