alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (XmlTemplateEngineTest.java)

This example Groovy source code file (XmlTemplateEngineTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

christian, christian, exception, hashmap, just, map, stein, stein, stein\n, string, string, template, util, xmltemplateengine, xmltemplateengine

The Groovy XmlTemplateEngineTest.java source code

package groovy.text;

import junit.framework.TestCase;

import java.util.HashMap;
import java.util.Map;

public class XmlTemplateEngineTest extends TestCase {

    public void testBinding() throws Exception {
        Map binding = new HashMap();
        binding.put("Christian", "Stein");

        XmlTemplateEngine xmlTemplateEngine = new XmlTemplateEngine();
        String xmlScript = "<!-- Just a comment. -->\n"
                + "<xml xmlns:gsp=\"http://groovy.codehaus.org/2005/gsp\">"
                + "  ${Christian}"
                + "  <gsp:expression>Christian"
                + "  <gsp:scriptlet>println Christian"
                + "</xml>";
        String xmlResult = "<xml>\n"
                + "  Stein\n"
                + xmlTemplateEngine.getIndentation() + "Stein\n"
                + "Stein" + System.getProperty("line.separator")
                + "</xml>\n";
        Template template = xmlTemplateEngine.createTemplate(xmlScript);
        assertEquals(xmlResult, template.make(binding).toString());
    }

    public void testQuotes() throws Exception {
        Map binding = new HashMap();
        binding.put("Christian", "Stein");

        XmlTemplateEngine xmlTemplateEngine = new XmlTemplateEngine();
        String xmlScript = "<!-- Just a comment. -->\n"
                + "<xml xmlns:mygsp=\"http://groovy.codehaus.org/2005/gsp\">"
                + "  ${Christian + \" \" + Christian}"
                + "  <mygsp:expression>Christian + \" \" + Christian"
                + "  <mygsp:scriptlet>println Christian"
                + "</xml>";
        String xmlResult = "<xml>\n"
                + "  Stein Stein\n"
                + xmlTemplateEngine.getIndentation() + "Stein Stein\n"
                + "Stein" + System.getProperty("line.separator")
                + "</xml>\n";
        Template template = xmlTemplateEngine.createTemplate(xmlScript);
        assertEquals(xmlResult, template.make(binding).toString());
    }

    public void testNamespaces() throws Exception {
        Map binding = new HashMap();
        binding.put("Christian", "Stein");

        XmlTemplateEngine xmlTemplateEngine = new XmlTemplateEngine();
        String xmlScript = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                + "<foo:bar xmlns:foo='urn:baz' xmlns:mygsp=\"http://groovy.codehaus.org/2005/gsp\">"
                + "${Christian + \" \" + Christian}"
                + "<mygsp:expression>Christian + \" \" + Christian"
                + "<nonamespace>println Christian"
                + "</foo:bar>";
        String xmlResult = "<foo:bar xmlns:foo='urn:baz'>\n"
                + "  Stein Stein\n"
                + xmlTemplateEngine.getIndentation() + "Stein Stein\n"
                + xmlTemplateEngine.getIndentation() + "<nonamespace>\n"
                + "Stein" + System.getProperty("line.separator")
                + xmlTemplateEngine.getIndentation() + "</nonamespace>\n"
                + "</foo:bar>\n";
        Template template = xmlTemplateEngine.createTemplate(xmlScript);
        assertEquals(xmlResult, template.make(binding).toString());
    }

    public void testDoubleQuotesInAttributeValues() throws Exception{
        XmlTemplateEngine xmlTemplateEngine = new XmlTemplateEngine();
        String xmlScript = "<document a='quoted \"string\"'/>";
        String xmlResult = "<document a='quoted \"string\"'/>\n";
        Template template = xmlTemplateEngine.createTemplate(xmlScript);
        assertEquals(xmlResult, template.make().toString());
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy XmlTemplateEngineTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.