KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > ChartResultTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 /*
6  * Created on Nov 10, 2003
7  *
8  * To change the template for this generated file go to Window - Preferences -
9  * Java - Code Generation - Code and Comments
10  */

11 package com.opensymphony.webwork.dispatcher;
12
13 import com.mockobjects.dynamic.Mock;
14 import com.opensymphony.webwork.ServletActionContext;
15 import com.opensymphony.xwork.ActionContext;
16 import com.opensymphony.xwork.ActionInvocation;
17 import junit.framework.TestCase;
18 import ognl.Ognl;
19 import org.jfree.chart.ChartFactory;
20 import org.jfree.chart.JFreeChart;
21 import org.jfree.data.DefaultPieDataset;
22
23 import javax.servlet.ServletOutputStream JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import java.io.IOException JavaDoc;
26
27
28 /**
29  * @author bchoi
30  * <p/>
31  * To change the template for this generated type comment go to Window -
32  * Preferences - Java - Code Generation - Code and Comments
33  */

34 public class ChartResultTest extends TestCase {
35     //~ Instance fields ////////////////////////////////////////////////////////
36

37     private ActionInvocation actionInvocation;
38     private JFreeChart mockChart;
39     private Mock responseMock;
40     private MockServletOutputStream os;
41
42     //~ Methods ////////////////////////////////////////////////////////////////
43

44     public void testChart() throws Exception JavaDoc {
45         responseMock.expectAndReturn("getOutputStream", os);
46
47         ChartResult result = new ChartResult();
48
49         result.setChart(mockChart);
50
51         result.setHeight(10);
52         result.setWidth(10);
53         result.execute(actionInvocation);
54
55         responseMock.verify();
56         assertTrue(os.isWritten());
57     }
58
59     public void testChartNotSet() {
60         ChartResult result = new ChartResult();
61
62         // expect exception if chart not set.
63
result.setChart(null);
64
65         try {
66             result.execute(actionInvocation);
67             fail();
68         } catch (Exception JavaDoc e) {
69         }
70
71         responseMock.verify();
72         assertFalse(os.isWritten());
73     }
74
75     protected void setUp() throws Exception JavaDoc {
76         DefaultPieDataset data = new DefaultPieDataset();
77         data.setValue("Java", new Double JavaDoc(43.2));
78         data.setValue("Visual Basic", new Double JavaDoc(0.0));
79         data.setValue("C/C++", new Double JavaDoc(17.5));
80         mockChart = ChartFactory.createPieChart("Pie Chart", data, true, true, false);
81
82         Mock mockActionInvocation = new Mock(ActionInvocation.class);
83         actionInvocation = (ActionInvocation) mockActionInvocation.proxy();
84         os = new MockServletOutputStream();
85         responseMock = new Mock(HttpServletResponse JavaDoc.class);
86
87         ActionContext.setContext(new ActionContext(Ognl.createDefaultContext(null)));
88         ServletActionContext.setResponse((HttpServletResponse JavaDoc) responseMock.proxy());
89     }
90
91     /*
92      * (non-Javadoc)
93      *
94      * @see junit.framework.TestCase#tearDown()
95      */

96     protected void tearDown() throws Exception JavaDoc {
97         actionInvocation = null;
98         os = null;
99         responseMock = null;
100     }
101
102     //~ Inner Classes //////////////////////////////////////////////////////////
103

104     private class MockServletOutputStream extends ServletOutputStream JavaDoc {
105         // very simple check that outputStream was written to.
106
private boolean written = false;
107
108         /**
109          * @return Returns the written.
110          */

111         public boolean isWritten() {
112             return written;
113         }
114
115         public void write(int arg0) throws IOException JavaDoc {
116             written = true;
117         }
118     }
119 }
120
Popular Tags