1 /*
2 * Angkor Web Framework
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7
8 package com.tirsen.angkor.test.unit.mock;
9
10 import com.tirsen.angkor.*;
11 import com.tirsen.angkor.test.unit.mock.MockServletRequest;
12 import com.tirsen.angkor.test.unit.mock.MockServletResponse;
13 import com.tirsen.angkor.test.unit.MockApplication;
14
15 import java.io.*;
16 import java.util.Map;
17 import java.util.HashMap;
18
19 import org.w3c.dom.Document;
20 import org.w3c.tidy.Tidy;
21
22 import javax.servlet.ServletException;
23
24 /***
25 * Mock object for the render context.
26 */
27 public class MockRenderContext extends RenderContext
28 {
29 private ByteArrayOutputStream bytes;
30 private PrintStream output;
31 private Map session = new HashMap();
32 private Map requestParams = new HashMap();
33 private Map requestAttribs = new HashMap();
34 private Application application = new MockApplication();
35
36 public MockRenderContext()
37 {
38 super(new MockServletRequest(), new MockServletResponse());
39 resetStreams();
40 }
41
42 public Application getApplication()
43 {
44 return application;
45 }
46
47 private void resetStreams()
48 {
49 bytes = new ByteArrayOutputStream();
50 output = new PrintStream(bytes);
51 }
52
53 public void setApplication(Application application)
54 {
55 this.application = application;
56 }
57
58 protected void setSessionAttribute(String attribute, Object value)
59 {
60 session.put(attribute, value);
61 }
62
63 protected Object getSessionAttribute(String attribute)
64 {
65 return session.get(attribute);
66 }
67
68 public void println(String line) throws IOException
69 {
70 System.out.println(line);
71 output.println(line);
72 }
73
74 public InputStream getRenderedInputStream()
75 {
76 return new ByteArrayInputStream(bytes.toByteArray());
77 }
78
79 public Document parseRenderedHTML()
80 {
81 Tidy tidy = new Tidy();
82 Document doc = tidy.parseDOM(getRenderedInputStream(), System.out);
83 return doc;
84 }
85
86 public boolean isRegisteredForParsing(View view)
87 {
88 return getApplication().getParsingComponents().contains(view);
89 }
90
91 public String getRequestParameter(String name)
92 {
93 return (String) requestParams.get(name);
94 }
95
96 public void setRequestParameter(String name, String value)
97 {
98 requestParams.put(name, value);
99 }
100
101 protected Object getRequestAttribute(String name)
102 {
103 return requestAttribs.get(name);
104 }
105
106 protected void setRequestAttribute(String name, Object value)
107 {
108 requestAttribs.put(name, value);
109 }
110
111 public void indent() throws IOException
112 {
113 for(int j = 0; j < indent; j++)
114 output.print("\t");
115 }
116
117 protected boolean isFirstRequest()
118 {
119 return false;
120 }
121
122 public String getRequestPath()
123 {
124 return "mock/request/path";
125 }
126
127 public String encodeURL(String url)
128 {
129 return url;
130 }
131
132 public void afterRequest() throws IOException, ServletException
133 {
134 super.afterRequest();
135 ((MockServletResponse) getResponse()).resetStreams();
136 resetStreams();
137 }
138 }
This page was automatically generated by Maven