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.dispatcher;
23
24 import org.apache.struts2.ServletActionContext;
25 import org.apache.struts2.StrutsTestCase;
26 import org.easymock.EasyMock;
27
28 import com.opensymphony.xwork2.ActionContext;
29 import com.opensymphony.xwork2.ActionInvocation;
30 import com.opensymphony.xwork2.ActionProxy;
31 import com.opensymphony.xwork2.util.ValueStack;
32 import org.jfree.chart.ChartFactory;
33 import org.jfree.chart.JFreeChart;
34 import org.jfree.data.general.DefaultPieDataset;
35
36 import javax.servlet.ServletOutputStream;
37 import javax.servlet.http.HttpServletResponse;
38 import java.io.IOException;
39
40
41 /***
42 */
43 public class ChartResultTest extends StrutsTestCase {
44
45 private ActionInvocation actionInvocation;
46 private JFreeChart mockChart;
47 private MockServletOutputStream os;
48 private ValueStack stack;
49 private ActionProxy mockActionProxy;
50 private HttpServletResponse responseMock;
51
52
53 public void testChart() throws Exception {
54 EasyMock.expect(responseMock.getOutputStream()).andReturn(os);
55 EasyMock.replay(responseMock, mockActionProxy, actionInvocation);
56
57 ChartResult result = new ChartResult();
58
59 result.setChart(mockChart);
60
61 result.setHeight("10");
62 result.setWidth("10");
63 result.execute(actionInvocation);
64
65 EasyMock.verify(responseMock);
66 assertTrue(os.isWritten());
67 }
68
69 public void testContentTypePng() throws Exception {
70 EasyMock.expect(responseMock.getOutputStream()).andReturn(os);
71 responseMock.setContentType("image/png");
72 EasyMock.replay(responseMock, mockActionProxy, actionInvocation);
73 ChartResult result = new ChartResult();
74
75 result.setChart(mockChart);
76
77 result.setHeight("10");
78 result.setWidth("10");
79 result.setType("png");
80 result.execute(actionInvocation);
81
82 EasyMock.verify(responseMock);
83 assertTrue(os.isWritten());
84 }
85
86 public void testContentTypeJpg() throws Exception {
87 EasyMock.expect(responseMock.getOutputStream()).andReturn(os);
88 responseMock.setContentType("image/jpg");
89 EasyMock.replay(responseMock, mockActionProxy, actionInvocation);
90 ChartResult result = new ChartResult();
91
92 result.setChart(mockChart);
93
94 result.setHeight("10");
95 result.setWidth("10");
96 result.setType("jpg");
97 result.execute(actionInvocation);
98
99 EasyMock.verify(responseMock);
100 assertTrue(os.isWritten());
101 }
102
103
104 public void testChartNotSet() {
105 ChartResult result = new ChartResult();
106 EasyMock.replay(responseMock, mockActionProxy, actionInvocation);
107
108
109 result.setChart(null);
110
111 try {
112 result.execute(actionInvocation);
113 fail();
114 } catch (Exception e) {
115 }
116
117 EasyMock.verify(responseMock);
118 assertFalse(os.isWritten());
119 }
120
121
122 public void testChartWithOGNLProperties() throws Exception {
123 EasyMock.expect(responseMock.getOutputStream()).andReturn(os);
124 EasyMock.replay(responseMock, mockActionProxy, actionInvocation);
125
126
127 ChartResult result = new ChartResult();
128
129 result.setChart(mockChart);
130
131 result.setHeight("${myHeight}");
132 result.setWidth("${myWidth}");
133
134 ValueStack stack = ActionContext.getContext().getValueStack();
135 stack.set("myHeight", 250);
136 stack.set("myWidth", 150);
137
138 result.execute(actionInvocation);
139
140 EasyMock.verify(responseMock);
141 assertEquals(result.getHeight(), stack.findValue("myHeight").toString());
142 assertEquals(result.getWidth(), stack.findValue("myWidth").toString());
143 assertEquals("250", result.getHeight().toString());
144 assertEquals("150", result.getWidth().toString());
145 assertTrue(os.isWritten());
146 }
147
148 protected void setUp() throws Exception {
149 super.setUp();
150
151 DefaultPieDataset data = new DefaultPieDataset();
152 data.setValue("Java", new Double(43.2));
153 data.setValue("Visual Basic", new Double(0.0));
154 data.setValue("C/C++", new Double(17.5));
155 mockChart = ChartFactory.createPieChart("Pie Chart", data, true, true, false);
156
157
158 stack = ActionContext.getContext().getValueStack();
159 ActionContext.getContext().setValueStack(stack);
160
161
162 mockActionProxy = EasyMock.createNiceMock(ActionProxy.class);
163 EasyMock.expect(mockActionProxy.getNamespace()).andReturn("/html");
164
165 actionInvocation = EasyMock.createMock(ActionInvocation.class);
166
167 EasyMock.expect(actionInvocation.getStack()).andReturn(stack).anyTimes();
168
169
170 os = new MockServletOutputStream();
171 responseMock = EasyMock.createNiceMock(HttpServletResponse.class);
172
173 ServletActionContext.setResponse((HttpServletResponse) responseMock);
174 }
175
176 protected void tearDown() throws Exception {
177 actionInvocation = null;
178 os = null;
179 responseMock = null;
180 stack = null;
181 mockActionProxy = null;
182 }
183
184
185 private class MockServletOutputStream extends ServletOutputStream {
186
187 private boolean written = false;
188
189 /***
190 * @return Returns the written.
191 */
192 public boolean isWritten() {
193 return written;
194 }
195
196 public void write(int arg0) throws IOException {
197 written = true;
198 }
199 }
200 }