001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023:
024: package examples;
025:
026: import junit.framework.Test;
027: import junit.framework.TestSuite;
028: import org.radeox.macro.Macro;
029: import org.radeox.macro.parameter.BaseMacroParameter;
030: import org.radeox.macro.parameter.MacroParameter;
031: import org.radeox.api.engine.context.InitialRenderContext;
032: import org.radeox.engine.context.BaseInitialRenderContext;
033:
034: import java.io.*;
035:
036: /**
037: * Example for a HelloWorldMacro
038: *
039: * @author Stephan J. Schmidt
040: * @version $Id: MacroExample.java 4158 2005-11-25 23:25:19Z ian@caret.cam.ac.uk $
041: */
042:
043: public class MacroExample extends RadeoxTestSupport {
044: public MacroExample(String name) {
045: super (name);
046: }
047:
048: public static Test suite() {
049: return new TestSuite(MacroExample.class);
050: }
051:
052: public void testRenderHelloWorld() {
053: Macro macro = new HelloWorldMacro();
054: StringWriter writer = new StringWriter();
055: try {
056: macro.execute(writer, new BaseMacroParameter());
057: } catch (IllegalArgumentException e) {
058: e.printStackTrace(); //To change body of catch statement use Options | File Templates.
059: } catch (IOException e) {
060: //
061: }
062: assertEquals("Hello world rendered", "hello world", writer
063: .toString());
064:
065: }
066:
067: // public void testGroovyMacro() {
068: // GroovyMacroCompiler compiler = new GroovyMacroCompiler();
069: // StringBuffer contentOfFile = new StringBuffer();
070: // try {
071: // BufferedReader br = new BufferedReader(
072: // new InputStreamReader(
073: // new FileInputStream("GroovyMacro.groovy")));
074: //
075: // String line;
076: // while ((line = br.readLine()) != null) {
077: // contentOfFile.append(line);
078: // }
079: // } catch (IOException e) {
080: // e.printStackTrace();
081: // }
082: // String content = contentOfFile.toString();
083: // Macro macro = compiler.compileMacro(content);
084: // assertNotNull("Groovy Macro did compile.", macro);
085: // }
086:
087: public void testCompiledGroovyMacro() {
088: Macro macro = new GroovyMacro();
089:
090: StringWriter writer = new StringWriter();
091: try {
092: MacroParameter params = new BaseMacroParameter();
093: macro.execute(writer, params);
094: } catch (IllegalArgumentException e) {
095: e.printStackTrace(); //To change body of catch statement use Options | File Templates.
096: } catch (IOException e) {
097: //
098: }
099: assertEquals("Hello world from Groovy is rendered",
100: "Yipee ay ey, schweinebacke", writer.toString());
101: }
102:
103: public void testRenderHelloWorldWithIntialRenderContext() {
104: Macro macro = new InitialRenderContextHelloWorldMacro();
105: StringWriter writer = new StringWriter();
106: try {
107: MacroParameter params = new BaseMacroParameter();
108: InitialRenderContext context = new BaseInitialRenderContext();
109: context.set("hello.name", "stephan");
110: macro.setInitialContext(context);
111: macro.execute(writer, params);
112: } catch (IllegalArgumentException e) {
113: e.printStackTrace(); //To change body of catch statement use Options | File Templates.
114: } catch (IOException e) {
115: //
116: }
117: assertEquals("Hello world with InitialRenderContext rendered",
118: "hello stephan", writer.toString());
119:
120: }
121:
122: public void testRenderHelloWorldWithParameter() {
123: Macro macro = new ParameterHelloWorldMacro();
124: StringWriter writer = new StringWriter();
125: try {
126: MacroParameter params = new BaseMacroParameter();
127: params.setParams("stephan");
128: macro.execute(writer, params);
129: } catch (IllegalArgumentException e) {
130: e.printStackTrace(); //To change body of catch statement use Options | File Templates.
131: } catch (IOException e) {
132: //
133: }
134: assertEquals("Hello world with parameter rendered",
135: "Hello <b>stephan</b>", writer.toString());
136:
137: }
138:
139: }
|