Skip to content

Instantly share code, notes, and snippets.

@cboehme
Created December 8, 2016 20:20
Show Gist options
  • Save cboehme/9cd1f71f5364cc68b5cb1638acddbdb3 to your computer and use it in GitHub Desktop.
Save cboehme/9cd1f71f5364cc68b5cb1638acddbdb3 to your computer and use it in GitHub Desktop.
Convert metamorph-test xml files to standard JUnit tests
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mt="http://www.culturegraph.org/metamorph-test"
xmlns:mm="http://www.culturegraph.org/metamorph"
xmlns:cgxml="http://www.culturegraph.org/cgxml">
<xsl:output method="text" />
<xsl:strip-space elements="*" />
<xsl:param name="classUnderTest" />
<xsl:template match="mt:metamorph-test">
<xsl:text>package org.culturegraph.mf.morph.maps;&#xa;&#xa;</xsl:text>
<xsl:text>import static org.mockito.Mockito.inOrder;&#xa;&#xa;</xsl:text>
<xsl:text>import org.culturegraph.mf.framework.StreamReceiver;&#xa;</xsl:text>
<xsl:text>import org.culturegraph.mf.morph.InlineMorph;&#xa;</xsl:text>
<xsl:text>import org.culturegraph.mf.morph.Metamorph;&#xa;</xsl:text>
<xsl:text>import org.junit.Rule;&#xa;</xsl:text>
<xsl:text>import org.junit.Test;&#xa;</xsl:text>
<xsl:text>import org.mockito.InOrder;&#xa;</xsl:text>
<xsl:text>import org.mockito.Mock;&#xa;</xsl:text>
<xsl:text>import org.mockito.junit.MockitoJUnit;&#xa;</xsl:text>
<xsl:text>import org.mockito.junit.MockitoRule;&#xa;&#xa;</xsl:text>
<xsl:text>/**&#xa;</xsl:text>
<xsl:text> * Tests for class {@link </xsl:text>
<xsl:value-of select="$classUnderTest" />
<xsl:text>}.&#xa;</xsl:text>
<xsl:text> */&#xa;</xsl:text>
<xsl:text>public final class </xsl:text>
<xsl:value-of select="$classUnderTest" />
<xsl:text>Test {&#xa;&#xa;</xsl:text>
<xsl:text> @Rule&#xa;</xsl:text>
<xsl:text> public final MockitoRule mockitoRule = MockitoJUnit.rule();&#xa;&#xa;</xsl:text>
<xsl:text> @Mock&#xa;</xsl:text>
<xsl:text> private StreamReceiver receiver;&#xa;&#xa;</xsl:text>
<xsl:text> private Metamorph metamorph;&#xa;&#xa;</xsl:text>
<xsl:apply-templates select="./*" />
<xsl:text>}&#xa;</xsl:text>
</xsl:template>
<xsl:template match="mt:test-case">
<xsl:text> @Test&#xa;</xsl:text>
<xsl:text> public void </xsl:text>
<xsl:value-of select="translate(@name, ' ', '')" />
<xsl:text>() {&#xa;</xsl:text>
<xsl:apply-templates select="./mt:transformation" />
<xsl:apply-templates select="./mt:input" />
<xsl:apply-templates select="./mt:result" />
<xsl:text> }&#xa;&#xa;</xsl:text>
</xsl:template>
<xsl:template match="mt:transformation[@type='text/x-metamorph+xml' and not(@src)]">
<xsl:text> metamorph = InlineMorph.in(this)&#xa;</xsl:text>
<xsl:apply-templates select="./*">
<xsl:with-param name="indent" select="''" />
</xsl:apply-templates>
<xsl:text> .createConnectedTo(receiver);&#xa;&#xa;</xsl:text>
</xsl:template>
<xsl:template match="mt:transformation">
<xsl:message>Unsupported transformation format</xsl:message>
</xsl:template>
<xsl:template match="mm:metamorph[not(@entityMarker) and @version = '1']">
<xsl:apply-templates select="./*">
<xsl:with-param name="indent" select="''" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="mm:*">
<xsl:param name="indent" />
<xsl:call-template name="open-element">
<xsl:with-param name="indent" select="$indent"/>
</xsl:call-template>
<xsl:choose>
<xsl:when test="not(text()) and count(child::*) = 0">
<xsl:call-template name="close-empty-element"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>&gt;")&#xa;</xsl:text>
<xsl:apply-templates select="./*">
<xsl:with-param name="indent" select="concat(' ', $indent)" />
</xsl:apply-templates>
<xsl:call-template name="close-element">
<xsl:with-param name="indent" select="$indent"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="open-element">
<xsl:param name="indent"/>
<xsl:text> .with("</xsl:text>
<xsl:value-of select="$indent"/>
<xsl:text>&lt;</xsl:text>
<xsl:value-of select="local-name(.)"/>
<xsl:call-template name="print-attributes"/>
</xsl:template>
<xsl:template name="print-attributes">
<xsl:for-each select="@*">
<xsl:text> </xsl:text>
<xsl:value-of select="local-name(.)" />
<xsl:text>='</xsl:text>
<xsl:value-of select="." />
<xsl:text>'</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template name="close-element">
<xsl:param name="indent"/>
<xsl:text> .with("</xsl:text>
<xsl:value-of select="$indent"/>
<xsl:text>&lt;/</xsl:text>
<xsl:value-of select="local-name(.)"/>
<xsl:text>&gt;")&#xa;</xsl:text>
</xsl:template>
<xsl:template name="close-empty-element">
<xsl:text> /&gt;")&#xa;</xsl:text>
</xsl:template>
<xsl:template match="mt:input[@type='text/x-cg+xml']">
<xsl:apply-templates select="./*" mode="as-input" />
<xsl:text>&#xa;</xsl:text>
</xsl:template>
<xsl:template match="mt:input">
<xsl:message>Unsupported input format</xsl:message>
</xsl:template>
<xsl:template match="cgxml:record" mode="as-input">
<xsl:text> metamorph.startRecord("</xsl:text>
<xsl:value-of select="@id" />
<xsl:text>");&#xa;</xsl:text>
<xsl:apply-templates select="./*" mode="as-input"/>
<xsl:text> metamorph.endRecord();&#xa;</xsl:text>
</xsl:template>
<xsl:template match="cgxml:entity" mode="as-input">
<xsl:text> metamorph.startEntity("</xsl:text>
<xsl:value-of select="@name" />
<xsl:text>");&#xa;</xsl:text>
<xsl:apply-templates select="./*" mode="as-input" />
<xsl:text> metamorph.endEntity();&#xa;</xsl:text>
</xsl:template>
<xsl:template match="cgxml:literal" mode="as-input">
<xsl:text> metamorph.literal("</xsl:text>
<xsl:value-of select="@name" />
<xsl:text>", "</xsl:text>
<xsl:value-of select="@value" />
<xsl:text>");&#xa;</xsl:text>
</xsl:template>
<xsl:template match="mt:result[@type='text/x-cg+xml']">
<xsl:text> final InOrder ordered = inOrder(receiver);&#xa;</xsl:text>
<xsl:apply-templates select="./*" mode="as-result" />
<xsl:text> ordered.verifyNoMoreInteractions();&#xa;</xsl:text>
</xsl:template>
<xsl:template match="mt:result">
<xsl:message>Unsupported result format</xsl:message>
</xsl:template>
<xsl:template match="cgxml:record" mode="as-result">
<xsl:text> ordered.verify(receiver).startRecord("</xsl:text>
<xsl:value-of select="@id" />
<xsl:text>");&#xa;</xsl:text>
<xsl:apply-templates select="./*" mode="as-result"/>
<xsl:text> ordered.verify(receiver).endRecord();&#xa;</xsl:text>
</xsl:template>
<xsl:template match="cgxml:entity" mode="as-result">
<xsl:text> ordered.verify(receiver).startEntity("</xsl:text>
<xsl:value-of select="@name" />
<xsl:text>");&#xa;</xsl:text>
<xsl:apply-templates select="./*" mode="as-result" />
<xsl:text> ordered.verify(receiver).endEntity();&#xa;</xsl:text>
</xsl:template>
<xsl:template match="cgxml:literal" mode="as-result">
<xsl:text> ordered.verify(receiver).literal("</xsl:text>
<xsl:value-of select="@name" />
<xsl:text>", "</xsl:text>
<xsl:value-of select="@value" />
<xsl:text>");&#xa;</xsl:text>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment