1 5 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 ; 24 import javax.servlet.http.HttpServletResponse ; 25 import java.io.IOException ; 26 27 28 34 public class ChartResultTest extends TestCase { 35 37 private ActionInvocation actionInvocation; 38 private JFreeChart mockChart; 39 private Mock responseMock; 40 private MockServletOutputStream os; 41 42 44 public void testChart() throws Exception { 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 result.setChart(null); 64 65 try { 66 result.execute(actionInvocation); 67 fail(); 68 } catch (Exception e) { 69 } 70 71 responseMock.verify(); 72 assertFalse(os.isWritten()); 73 } 74 75 protected void setUp() throws Exception { 76 DefaultPieDataset data = new DefaultPieDataset(); 77 data.setValue("Java", new Double (43.2)); 78 data.setValue("Visual Basic", new Double (0.0)); 79 data.setValue("C/C++", new Double (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 .class); 86 87 ActionContext.setContext(new ActionContext(Ognl.createDefaultContext(null))); 88 ServletActionContext.setResponse((HttpServletResponse ) responseMock.proxy()); 89 } 90 91 96 protected void tearDown() throws Exception { 97 actionInvocation = null; 98 os = null; 99 responseMock = null; 100 } 101 102 104 private class MockServletOutputStream extends ServletOutputStream { 105 private boolean written = false; 107 108 111 public boolean isWritten() { 112 return written; 113 } 114 115 public void write(int arg0) throws IOException { 116 written = true; 117 } 118 } 119 } 120 | Popular Tags |