mirror of
https://github.com/apache/struts.git
synced 2026-08-06 23:27:07 +00:00
WW-3210 JFreeChart plugin not setting correct mime type
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@817318 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -64,12 +64,6 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mockobjects</groupId>
|
||||
<artifactId>mockobjects-core</artifactId>
|
||||
<version>0.09</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.struts</groupId>
|
||||
<artifactId>struts2-core</artifactId>
|
||||
@@ -88,14 +82,18 @@
|
||||
<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>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<version>2.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -30,6 +30,8 @@ import org.jfree.chart.JFreeChart;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: description -->
|
||||
* <p/>
|
||||
@@ -192,13 +194,18 @@ public class ChartResult extends StrutsResultSupport {
|
||||
throw new NullPointerException("No width parameter was given.");
|
||||
|
||||
// get a reference to the servlet output stream to write our chart image to
|
||||
OutputStream os = ServletActionContext.getResponse().getOutputStream();
|
||||
HttpServletResponse response = ServletActionContext.getResponse();
|
||||
OutputStream os = response.getOutputStream();
|
||||
try {
|
||||
// check the type to see what kind of output we have to produce
|
||||
if ("png".equalsIgnoreCase(type))
|
||||
if ("png".equalsIgnoreCase(type)) {
|
||||
response.setContentType("image/png");
|
||||
ChartUtilities.writeChartAsPNG(os, chart, getIntValueFromString(width), getIntValueFromString(height));
|
||||
else if ("jpg".equalsIgnoreCase(type) || "jpeg".equalsIgnoreCase(type))
|
||||
}
|
||||
else if ("jpg".equalsIgnoreCase(type) || "jpeg".equalsIgnoreCase(type)) {
|
||||
response.setContentType("image/jpg");
|
||||
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 {
|
||||
|
||||
+55
-18
@@ -21,9 +21,10 @@
|
||||
|
||||
package org.apache.struts2.dispatcher;
|
||||
|
||||
import com.mockobjects.dynamic.Mock;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.StrutsTestCase;
|
||||
import org.easymock.EasyMock;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
@@ -43,15 +44,16 @@ public class ChartResultTest extends StrutsTestCase {
|
||||
|
||||
private ActionInvocation actionInvocation;
|
||||
private JFreeChart mockChart;
|
||||
private Mock responseMock;
|
||||
private Mock mockActionProxy;
|
||||
private MockServletOutputStream os;
|
||||
private ValueStack stack;
|
||||
private ActionProxy mockActionProxy;
|
||||
private HttpServletResponse responseMock;
|
||||
|
||||
|
||||
public void testChart() throws Exception {
|
||||
responseMock.expectAndReturn("getOutputStream", os);
|
||||
|
||||
EasyMock.expect(responseMock.getOutputStream()).andReturn(os);
|
||||
EasyMock.replay(responseMock, mockActionProxy, actionInvocation);
|
||||
|
||||
ChartResult result = new ChartResult();
|
||||
|
||||
result.setChart(mockChart);
|
||||
@@ -60,13 +62,49 @@ public class ChartResultTest extends StrutsTestCase {
|
||||
result.setWidth("10");
|
||||
result.execute(actionInvocation);
|
||||
|
||||
responseMock.verify();
|
||||
EasyMock.verify(responseMock);
|
||||
assertTrue(os.isWritten());
|
||||
}
|
||||
|
||||
public void testContentTypePng() throws Exception {
|
||||
EasyMock.expect(responseMock.getOutputStream()).andReturn(os);
|
||||
responseMock.setContentType("image/png");
|
||||
EasyMock.replay(responseMock, mockActionProxy, actionInvocation);
|
||||
ChartResult result = new ChartResult();
|
||||
|
||||
result.setChart(mockChart);
|
||||
|
||||
result.setHeight("10");
|
||||
result.setWidth("10");
|
||||
result.setType("png");
|
||||
result.execute(actionInvocation);
|
||||
|
||||
EasyMock.verify(responseMock);
|
||||
assertTrue(os.isWritten());
|
||||
}
|
||||
|
||||
public void testContentTypeJpg() throws Exception {
|
||||
EasyMock.expect(responseMock.getOutputStream()).andReturn(os);
|
||||
responseMock.setContentType("image/jpg");
|
||||
EasyMock.replay(responseMock, mockActionProxy, actionInvocation);
|
||||
ChartResult result = new ChartResult();
|
||||
|
||||
result.setChart(mockChart);
|
||||
|
||||
result.setHeight("10");
|
||||
result.setWidth("10");
|
||||
result.setType("jpg");
|
||||
result.execute(actionInvocation);
|
||||
|
||||
EasyMock.verify(responseMock);
|
||||
assertTrue(os.isWritten());
|
||||
}
|
||||
|
||||
|
||||
public void testChartNotSet() {
|
||||
ChartResult result = new ChartResult();
|
||||
|
||||
EasyMock.replay(responseMock, mockActionProxy, actionInvocation);
|
||||
|
||||
// expect exception if chart not set.
|
||||
result.setChart(null);
|
||||
|
||||
@@ -76,13 +114,14 @@ public class ChartResultTest extends StrutsTestCase {
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
responseMock.verify();
|
||||
EasyMock.verify(responseMock);
|
||||
assertFalse(os.isWritten());
|
||||
}
|
||||
|
||||
|
||||
public void testChartWithOGNLProperties() throws Exception {
|
||||
responseMock.expectAndReturn("getOutputStream", os);
|
||||
EasyMock.expect(responseMock.getOutputStream()).andReturn(os);
|
||||
EasyMock.replay(responseMock, mockActionProxy, actionInvocation);
|
||||
|
||||
|
||||
ChartResult result = new ChartResult();
|
||||
@@ -98,7 +137,7 @@ public class ChartResultTest extends StrutsTestCase {
|
||||
|
||||
result.execute(actionInvocation);
|
||||
|
||||
responseMock.verify();
|
||||
EasyMock.verify(responseMock);
|
||||
assertEquals(result.getHeight(), stack.findValue("myHeight").toString());
|
||||
assertEquals(result.getWidth(), stack.findValue("myWidth").toString());
|
||||
assertEquals("250", result.getHeight().toString());
|
||||
@@ -120,20 +159,18 @@ public class ChartResultTest extends StrutsTestCase {
|
||||
ActionContext.getContext().setValueStack(stack);
|
||||
|
||||
|
||||
mockActionProxy = new Mock(ActionProxy.class);
|
||||
mockActionProxy.expectAndReturn("getNamespace", "/html");
|
||||
mockActionProxy = EasyMock.createNiceMock(ActionProxy.class);
|
||||
EasyMock.expect(mockActionProxy.getNamespace()).andReturn("/html");
|
||||
|
||||
Mock mockActionInvocation = new Mock(ActionInvocation.class);
|
||||
actionInvocation = EasyMock.createMock(ActionInvocation.class);
|
||||
|
||||
mockActionInvocation.matchAndReturn("getStack", stack);
|
||||
// mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy());
|
||||
EasyMock.expect(actionInvocation.getStack()).andReturn(stack).anyTimes();
|
||||
|
||||
actionInvocation = (ActionInvocation) mockActionInvocation.proxy();
|
||||
|
||||
os = new MockServletOutputStream();
|
||||
responseMock = new Mock(HttpServletResponse.class);
|
||||
responseMock = EasyMock.createNiceMock(HttpServletResponse.class);
|
||||
|
||||
ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
|
||||
ServletActionContext.setResponse((HttpServletResponse) responseMock);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user