001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: /*
006: * Created on Nov 10, 2003
007: *
008: * To change the template for this generated file go to Window - Preferences -
009: * Java - Code Generation - Code and Comments
010: */
011: package com.opensymphony.webwork.dispatcher;
012:
013: import com.mockobjects.dynamic.Mock;
014: import com.opensymphony.webwork.ServletActionContext;
015: import com.opensymphony.xwork.ActionContext;
016: import com.opensymphony.xwork.ActionInvocation;
017: import junit.framework.TestCase;
018: import ognl.Ognl;
019: import org.jfree.chart.ChartFactory;
020: import org.jfree.chart.JFreeChart;
021: import org.jfree.data.general.DefaultPieDataset;
022:
023: import javax.servlet.ServletOutputStream;
024: import javax.servlet.http.HttpServletResponse;
025: import java.io.IOException;
026:
027: /**
028: * @author bchoi
029: * <p/>
030: * To change the template for this generated type comment go to Window -
031: * Preferences - Java - Code Generation - Code and Comments
032: */
033: public class ChartResultTest extends TestCase {
034:
035: private ActionInvocation actionInvocation;
036: private JFreeChart mockChart;
037: private Mock responseMock;
038: private MockServletOutputStream os;
039:
040: public void testChart() throws Exception {
041: responseMock.expectAndReturn("getOutputStream", os);
042:
043: ChartResult result = new ChartResult();
044:
045: result.setChart(mockChart);
046:
047: result.setHeight(10);
048: result.setWidth(10);
049: result.execute(actionInvocation);
050:
051: responseMock.verify();
052: assertTrue(os.isWritten());
053: }
054:
055: public void testChartNotSet() {
056: ChartResult result = new ChartResult();
057:
058: // expect exception if chart not set.
059: result.setChart(null);
060:
061: try {
062: result.execute(actionInvocation);
063: fail();
064: } catch (Exception e) {
065: }
066:
067: responseMock.verify();
068: assertFalse(os.isWritten());
069: }
070:
071: protected void setUp() throws Exception {
072: DefaultPieDataset data = new DefaultPieDataset();
073: data.setValue("Java", new Double(43.2));
074: data.setValue("Visual Basic", new Double(0.0));
075: data.setValue("C/C++", new Double(17.5));
076: mockChart = ChartFactory.createPieChart("Pie Chart", data,
077: true, true, false);
078:
079: Mock mockActionInvocation = new Mock(ActionInvocation.class);
080: actionInvocation = (ActionInvocation) mockActionInvocation
081: .proxy();
082: os = new MockServletOutputStream();
083: responseMock = new Mock(HttpServletResponse.class);
084:
085: ActionContext.setContext(new ActionContext(Ognl
086: .createDefaultContext(null)));
087: ServletActionContext
088: .setResponse((HttpServletResponse) responseMock.proxy());
089: }
090:
091: protected void tearDown() throws Exception {
092: actionInvocation = null;
093: os = null;
094: responseMock = null;
095: }
096:
097: private class MockServletOutputStream extends ServletOutputStream {
098: // very simple check that outputStream was written to.
099: private boolean written = false;
100:
101: /**
102: * @return Returns the written.
103: */
104: public boolean isWritten() {
105: return written;
106: }
107:
108: public void write(int arg0) throws IOException {
109: written = true;
110: }
111: }
112: }
|