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