View Javadoc

1   /*
2    * $Id: ChartResultTest.java 440597 2006-09-06 03:34:39Z wsmoak $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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          // expect exception if chart not set.
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         // very simple check that outputStream was written to.
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 }