WW-2742 Paramters not being set in JFreeChart Plugin

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@768217 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Rainer Hermanns
2009-04-24 08:08:43 +00:00
parent bcef9830de
commit c6e09bf395
3 changed files with 130 additions and 19 deletions
+27 -2
View File
@@ -43,7 +43,7 @@
<dependency>
<groupId>jfree</groupId>
<artifactId>jcommon</artifactId>
<version>1.0.2</version>
<version>1.0.12</version>
<scope>provided</scope>
<exclusions>
<exclusion>
@@ -55,7 +55,7 @@
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.1</version>
<version>1.0.9</version>
<scope>provided</scope>
<exclusions>
<exclusion>
@@ -70,6 +70,31 @@
<version>0.09</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<scope>compile</scope>
<version>${pom.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>${pom.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
@@ -23,7 +23,8 @@ package org.apache.struts2.dispatcher;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
@@ -97,7 +98,9 @@ import java.io.OutputStream;
* &lt;/result&gt;
* <!-- END SNIPPET: example --></pre>
*/
public class ChartResult implements Result {
public class ChartResult extends StrutsResultSupport {
private final static Logger LOG = LoggerFactory.getLogger(ChartResult.class);
private static final long serialVersionUID = -6484761870055986612L;
private static final String DEFAULT_TYPE = "png";
@@ -105,7 +108,7 @@ public class ChartResult implements Result {
private JFreeChart chart; // the JFreeChart to render
private boolean chartSet;
Integer height, width;
String height, width;
String type = DEFAULT_TYPE; // supported are jpg, jpeg or png, defaults to png
String value = DEFAULT_VALUE; // defaults to 'chart'
@@ -115,7 +118,7 @@ public class ChartResult implements Result {
super();
}
public ChartResult(JFreeChart chart, int height, int width) {
public ChartResult(JFreeChart chart, String height, String width) {
this.chart = chart;
this.height = height;
this.width = width;
@@ -123,19 +126,19 @@ public class ChartResult implements Result {
// ACCESSORS ----------------------------
public Integer getHeight() {
public String getHeight() {
return height;
}
public void setHeight(Integer height) {
public void setHeight(String height) {
this.height = height;
}
public Integer getWidth() {
public String getWidth() {
return width;
}
public void setWidth(Integer width) {
public void setWidth(String width) {
this.width = width;
}
@@ -174,7 +177,10 @@ public class ChartResult implements Result {
* @param invocation an encapsulation of the action execution state.
* @throws Exception if an error occurs when creating or writing the chart to the servlet output stream.
*/
public void execute(ActionInvocation invocation) throws Exception {
public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
initializeProperties(invocation);
if (!chartSet) // if our chart hasn't been set (by the testcase), we'll look it up in the value stack
chart = (JFreeChart) invocation.getStack().findValue(value, JFreeChart.class);
if (chart == null) // we need to have a chart object - if not, blow up
@@ -190,13 +196,48 @@ public class ChartResult implements Result {
try {
// check the type to see what kind of output we have to produce
if ("png".equalsIgnoreCase(type))
ChartUtilities.writeChartAsPNG(os, chart, width, height);
ChartUtilities.writeChartAsPNG(os, chart, getIntValueFromString(width), getIntValueFromString(height));
else if ("jpg".equalsIgnoreCase(type) || "jpeg".equalsIgnoreCase(type))
ChartUtilities.writeChartAsJPEG(os, chart, width, height);
ChartUtilities.writeChartAsJPEG(os, chart, getIntValueFromString(width), getIntValueFromString(height));
else
throw new IllegalArgumentException(type + " is not a supported render type (only JPG and PNG are).");
} finally {
if (os != null) os.flush();
}
}
/**
* Sets up result properties, parsing etc.
*
* @param invocation Current invocation.
* @throws Exception on initialization error.
*/
private void initializeProperties(ActionInvocation invocation) throws Exception {
if (height != null) {
height = conditionalParse(height, invocation);
}
if (width != null) {
width = conditionalParse(width, invocation);
}
if (type != null) {
type = conditionalParse(type, invocation);
}
if ( type == null) {
type = DEFAULT_TYPE;
}
}
private Integer getIntValueFromString(String value) {
try {
return Integer.parseInt(value);
} catch (Exception e) {
LOG.error("Specified value for width or height is not of type Integer...", e);
return null;
}
}
}
@@ -23,10 +23,11 @@ package org.apache.struts2.dispatcher;
import com.mockobjects.dynamic.Mock;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsTestCase;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import junit.framework.TestCase;
import ognl.Ognl;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.util.ValueStack;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
@@ -38,12 +39,14 @@ import java.io.IOException;
/**
*/
public class ChartResultTest extends TestCase {
public class ChartResultTest extends StrutsTestCase {
private ActionInvocation actionInvocation;
private JFreeChart mockChart;
private Mock responseMock;
private Mock mockActionProxy;
private MockServletOutputStream os;
private ValueStack stack;
public void testChart() throws Exception {
@@ -53,8 +56,8 @@ public class ChartResultTest extends TestCase {
result.setChart(mockChart);
result.setHeight(10);
result.setWidth(10);
result.setHeight("10");
result.setWidth("10");
result.execute(actionInvocation);
responseMock.verify();
@@ -77,19 +80,59 @@ public class ChartResultTest extends TestCase {
assertFalse(os.isWritten());
}
public void testChartWithOGNLProperties() throws Exception {
responseMock.expectAndReturn("getOutputStream", os);
ChartResult result = new ChartResult();
result.setChart(mockChart);
result.setHeight("${myHeight}");
result.setWidth("${myWidth}");
ValueStack stack = ActionContext.getContext().getValueStack();
stack.set("myHeight", 250);
stack.set("myWidth", 150);
result.execute(actionInvocation);
responseMock.verify();
assertEquals(result.getHeight(), stack.findValue("myHeight").toString());
assertEquals(result.getWidth(), stack.findValue("myWidth").toString());
assertEquals("250", result.getHeight().toString());
assertEquals("150", result.getWidth().toString());
assertTrue(os.isWritten());
}
protected void setUp() throws Exception {
super.setUp();
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Java", new Double(43.2));
data.setValue("Visual Basic", new Double(0.0));
data.setValue("C/C++", new Double(17.5));
mockChart = ChartFactory.createPieChart("Pie Chart", data, true, true, false);
stack = ActionContext.getContext().getValueStack();
ActionContext.getContext().setValueStack(stack);
mockActionProxy = new Mock(ActionProxy.class);
mockActionProxy.expectAndReturn("getNamespace", "/html");
Mock mockActionInvocation = new Mock(ActionInvocation.class);
mockActionInvocation.matchAndReturn("getStack", stack);
// mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy());
actionInvocation = (ActionInvocation) mockActionInvocation.proxy();
os = new MockServletOutputStream();
responseMock = new Mock(HttpServletResponse.class);
ActionContext.setContext(new ActionContext(Ognl.createDefaultContext(null)));
ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
}
@@ -97,6 +140,8 @@ public class ChartResultTest extends TestCase {
actionInvocation = null;
os = null;
responseMock = null;
stack = null;
mockActionProxy = null;
}