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 expanderExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact. on the baseModel and compare the content with the template test_base in the {Expander}Test.java file.

Typically, expanderExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact. 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/normalizedsystemsSystemA set of interconnected components that work together to achieve a specific purpose or function./sandcastle -> ExpanderTest.stg) to what the artifactArtifactA build or deployment result, such as a Docker container image or Java archive, produced by a software project. 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 expanderExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact. will extract the values for both variables based on the eval expressions


Next: Learn more complex mappings in Expander Mapping