View Javadoc

1   /*
2    * $Id: ChartResultTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // expect exception if chart not set.
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         // very simple check that outputStream was written to.
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 }