View Javadoc

    1   /*
    2    * $Id: ChartResultTest.java 471756 2006-11-06 15:01:43Z husted $
    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  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          // expect exception if chart not set.
    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         // very simple check that outputStream was written to.
    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 }