1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.freemarker;
23
24 import java.io.File;
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27
28 import junit.framework.TestCase;
29
30 import org.apache.struts2.ServletActionContext;
31 import org.apache.struts2.StrutsStatics;
32 import org.apache.struts2.StrutsTestCase;
33 import org.apache.struts2.views.jsp.StrutsMockHttpServletResponse;
34 import org.apache.struts2.views.jsp.StrutsMockServletContext;
35 import org.springframework.mock.web.MockHttpServletRequest;
36
37 import com.opensymphony.xwork2.ActionContext;
38 import com.opensymphony.xwork2.mock.MockActionInvocation;
39 import com.opensymphony.xwork2.util.ValueStack;
40 import com.opensymphony.xwork2.util.ValueStackFactory;
41
42 /***
43 * Test case for FreeMarkerResult.
44 *
45 */
46 public class FreeMarkerResultTest extends StrutsTestCase {
47
48 ValueStack stack;
49 MockActionInvocation invocation;
50 ActionContext context;
51 StrutsMockHttpServletResponse response;
52 PrintWriter writer;
53 StringWriter stringWriter;
54 StrutsMockServletContext servletContext;
55 private FreemarkerManager mgr;
56 private MockHttpServletRequest request;
57
58 public void testWriteIfCompleted() throws Exception {
59 FreemarkerResult result = new FreemarkerResult();
60 result.setLocation("someFreeMarkerFile.ftl");
61 result.setFreemarkerManager(mgr);
62 result.setWriteIfCompleted(true);
63
64 try {
65 result.execute(invocation);
66 assertTrue(false);
67 } catch (Exception e) {
68 assertEquals(0, stringWriter.getBuffer().length());
69 }
70 }
71
72 public void testWithoutWriteIfCompleted() throws Exception {
73 FreemarkerResult result = new FreemarkerResult();
74 result.setLocation("someFreeMarkerFile.ftl");
75 result.setFreemarkerManager(mgr);
76
77 try {
78 result.execute(invocation);
79 assertTrue(false);
80 } catch (Exception e) {
81 assertTrue(stringWriter.getBuffer().length() > 0);
82 }
83 }
84
85 protected void setUp() throws Exception {
86 super.setUp();
87 mgr = new FreemarkerManager();
88 mgr.setEncoding("UTF-8");
89 stringWriter = new StringWriter();
90 writer = new PrintWriter(stringWriter);
91 response = new StrutsMockHttpServletResponse();
92 response.setWriter(writer);
93 request = new MockHttpServletRequest();
94 servletContext = new StrutsMockServletContext();
95 stack = ActionContext.getContext().getValueStack();
96 context = new ActionContext(stack.getContext());
97 context.put(StrutsStatics.HTTP_RESPONSE, response);
98 context.put(StrutsStatics.HTTP_REQUEST, request);
99 context.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
100 ServletActionContext.setServletContext(servletContext);
101 ServletActionContext.setRequest(request);
102 ServletActionContext.setResponse(response);
103 servletContext.setAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY, null);
104 invocation = new MockActionInvocation();
105 invocation.setStack(stack);
106 invocation.setInvocationContext(context);
107 servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource(
108 "someFreeMarkerFile.ftl").toURI()).toURL().getFile());
109 }
110
111 protected void tearDown() throws Exception {
112 stack = null;
113 invocation = null;
114 context = null;
115 response = null;
116 writer = null;
117 stringWriter = null;
118 servletContext = null;
119
120 super.tearDown();
121 }
122 }