01: package org.andromda.templateengines.velocity;
02:
03: import junit.framework.TestCase;
04:
05: import org.andromda.core.configuration.Namespace;
06: import org.andromda.core.configuration.NamespaceProperties;
07: import org.andromda.core.configuration.Namespaces;
08: import org.andromda.core.configuration.Property;
09: import org.apache.velocity.VelocityContext;
10: import org.apache.velocity.app.VelocityEngine;
11:
12: import java.io.StringWriter;
13: import java.net.URL;
14: import java.util.HashMap;
15: import java.util.Map;
16:
17: /**
18: * Tests the direct interpretation of a string by VelocityTemplateEngine.
19: *
20: * @author <a href="http://www.mbohlen.de">Matthias Bohlen</a>
21: * @author Chad Brandon
22: */
23: public class VelocityTemplateEngineTest extends TestCase {
24: public VelocityTemplateEngineTest(String arg0) {
25: super (arg0);
26: }
27:
28: public void testDirectVelocity() throws Exception {
29: StringWriter writer = new StringWriter();
30:
31: VelocityEngine engine = new VelocityEngine();
32: engine.init();
33:
34: VelocityContext velocityContext = new VelocityContext();
35:
36: velocityContext.put("test1", "@test1@");
37: velocityContext.put("test2", "@test2@");
38:
39: assertTrue(engine.evaluate(velocityContext, writer, "mylogtag",
40: "$test1$test2"));
41: assertEquals("@test1@@test2@", writer.getBuffer().toString());
42: }
43:
44: /**
45: * Tests the dynamic template merging.
46: *
47: * @throws Exception
48: */
49: public void testTemplateMerging() throws Exception {
50: final String packagePath = this .getClass().getPackage()
51: .getName().replace('.', '/');
52: Property mergeMappings = new Property();
53: URL mergeMappingsUri = VelocityTemplateEngineTest.class
54: .getResource("/" + packagePath + "/merge-mappings.xml");
55: assertNotNull(mergeMappingsUri);
56: mergeMappings.setName(NamespaceProperties.MERGE_MAPPINGS_URI);
57: mergeMappings.setValue(mergeMappingsUri.toString());
58: final String namespaceName = "test-namespace";
59: final Namespace namespace = new Namespace();
60: namespace.setName(namespaceName);
61: namespace.addProperty(mergeMappings);
62: Namespaces.instance().addNamespace(namespace);
63: final VelocityTemplateEngine engine = new VelocityTemplateEngine();
64: engine.initialize(namespaceName);
65: StringWriter writer = new StringWriter();
66: String path = packagePath + "/merge-test.vsl";
67:
68: final Map context = new HashMap();
69: context.put("contextObject", "aValue");
70: engine.processTemplate(path, context, writer);
71: assertEquals("<test>merged value aValue</test>", writer
72: .toString());
73: }
74: }
|