001: /*
002: * Copyright (c) 2002-2007 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.freemarker;
006:
007: import java.io.IOException;
008: import java.io.StringReader;
009: import java.io.StringWriter;
010: import java.io.Writer;
011: import java.util.Locale;
012:
013: import com.opensymphony.webwork.WebWorkTestCase;
014: import com.opensymphony.xwork.mock.MockActionInvocation;
015:
016: import freemarker.template.Configuration;
017: import freemarker.template.SimpleHash;
018: import freemarker.template.Template;
019: import freemarker.template.TemplateException;
020: import freemarker.template.TemplateModel;
021: import freemarker.template.TemplateModelException;
022:
023: /**
024: * @author tmjee
025: * @version $Date$ $Id$
026: */
027: public class FreemarkerResultTest extends WebWorkTestCase {
028:
029: private boolean postTemplateProcessCalled = false;
030: private boolean preTemplateProcessCalled = false;
031:
032: /**
033: * This is just a very simple sanity test just to make sure that with
034: * writeCompleted=true, we still get the normal output writen.
035: * @throws Exception
036: */
037: public void testFreemarkerResultMakeSureWeGetTheDesiredResultWhenWriteCompleteIsTrue()
038: throws Exception {
039: preTemplateProcessCalled = false;
040: postTemplateProcessCalled = false;
041: final StringWriter writer = new StringWriter();
042:
043: FreemarkerResult result = new FreemarkerResult() {
044: protected Configuration getConfiguration()
045: throws TemplateException {
046: return new Configuration() {
047: public Template getTemplate(String arg0,
048: Locale locale) throws IOException {
049: return new Template("test.ftl",
050: new StringReader("testing")) {
051: };
052: }
053: };
054: }
055:
056: protected Writer getWriter() throws IOException {
057: return writer;
058: }
059:
060: protected boolean preTemplateProcess(Template template,
061: TemplateModel model) throws IOException,
062: TemplateException {
063: preTemplateProcessCalled = true;
064: return true;
065: }
066:
067: protected void postTemplateProcess(Template template,
068: TemplateModel data) throws IOException {
069: postTemplateProcessCalled = true;
070: }
071:
072: protected TemplateModel createModel()
073: throws TemplateModelException {
074: return new SimpleHash();
075: }
076: };
077:
078: result.setBufferOutput(true);
079: result.doExecute("/test.ftl", new MockActionInvocation());
080:
081: assertEquals(writer.getBuffer().toString(), "testing");
082: assertTrue(preTemplateProcessCalled);
083: assertTrue(postTemplateProcessCalled);
084: }
085:
086: /**
087: * This is just a very simple sanity test just to make sure that with
088: * writeCompleted=false, we still get the normal output writen.
089: * @throws Exception
090: */
091: public void testFreemarkerResultMakeSureWeGetTheDesiredResultWhenWriteCompleteIsFalse()
092: throws Exception {
093: preTemplateProcessCalled = false;
094: postTemplateProcessCalled = false;
095: final StringWriter writer = new StringWriter();
096:
097: FreemarkerResult result = new FreemarkerResult() {
098: protected Configuration getConfiguration()
099: throws TemplateException {
100: return new Configuration() {
101: public Template getTemplate(String arg0,
102: Locale locale) throws IOException {
103: return new Template("test.ftl",
104: new StringReader("testing")) {
105: };
106: }
107: };
108: }
109:
110: protected Writer getWriter() throws IOException {
111: return writer;
112: }
113:
114: protected boolean preTemplateProcess(Template template,
115: TemplateModel model) throws IOException,
116: TemplateException {
117: preTemplateProcessCalled = true;
118: return true;
119: }
120:
121: protected void postTemplateProcess(Template template,
122: TemplateModel data) throws IOException {
123: postTemplateProcessCalled = true;
124: }
125:
126: protected TemplateModel createModel()
127: throws TemplateModelException {
128: return new SimpleHash();
129: }
130: };
131:
132: result.setBufferOutput(false);
133: result.doExecute("/test.ftl", new MockActionInvocation());
134:
135: assertEquals(writer.getBuffer().toString(), "testing");
136: assertTrue(preTemplateProcessCalled);
137: assertTrue(postTemplateProcessCalled);
138: }
139: }
|