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 com.mockobjects.dynamic.Mock;
25 import org.apache.struts2.ServletActionContext;
26 import com.opensymphony.xwork2.ActionContext;
27 import com.opensymphony.xwork2.ActionInvocation;
28 import junit.framework.TestCase;
29 import ognl.Ognl;
30 import org.jfree.chart.ChartFactory;
31 import org.jfree.chart.JFreeChart;
32 import org.jfree.data.general.DefaultPieDataset;
33
34 import javax.servlet.ServletOutputStream;
35 import javax.servlet.http.HttpServletResponse;
36 import java.io.IOException;
37
38
39 /***
40 */
41 public class ChartResultTest extends TestCase {
42
43 private ActionInvocation actionInvocation;
44 private JFreeChart mockChart;
45 private Mock responseMock;
46 private MockServletOutputStream os;
47
48
49 public void testChart() throws Exception {
50 responseMock.expectAndReturn("getOutputStream", os);
51
52 ChartResult result = new ChartResult();
53
54 result.setChart(mockChart);
55
56 result.setHeight(10);
57 result.setWidth(10);
58 result.execute(actionInvocation);
59
60 responseMock.verify();
61 assertTrue(os.isWritten());
62 }
63
64 public void testChartNotSet() {
65 ChartResult result = new ChartResult();
66
67
68 result.setChart(null);
69
70 try {
71 result.execute(actionInvocation);
72 fail();
73 } catch (Exception e) {
74 }
75
76 responseMock.verify();
77 assertFalse(os.isWritten());
78 }
79
80 protected void setUp() throws Exception {
81 DefaultPieDataset data = new DefaultPieDataset();
82 data.setValue("Java", new Double(43.2));
83 data.setValue("Visual Basic", new Double(0.0));
84 data.setValue("C/C++", new Double(17.5));
85 mockChart = ChartFactory.createPieChart("Pie Chart", data, true, true, false);
86
87 Mock mockActionInvocation = new Mock(ActionInvocation.class);
88 actionInvocation = (ActionInvocation) mockActionInvocation.proxy();
89 os = new MockServletOutputStream();
90 responseMock = new Mock(HttpServletResponse.class);
91
92 ActionContext.setContext(new ActionContext(Ognl.createDefaultContext(null)));
93 ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
94 }
95
96 protected void tearDown() throws Exception {
97 actionInvocation = null;
98 os = null;
99 responseMock = null;
100 }
101
102
103 private class MockServletOutputStream extends ServletOutputStream {
104
105 private boolean written = false;
106
107 /***
108 * @return Returns the written.
109 */
110 public boolean isWritten() {
111 return written;
112 }
113
114 public void write(int arg0) throws IOException {
115 written = true;
116 }
117 }
118 }