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