You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by he...@apache.org on 2009/04/24 10:08:43 UTC

svn commit: r768217 - in /struts/struts2/trunk/plugins/jfreechart: pom.xml src/main/java/org/apache/struts2/dispatcher/ChartResult.java src/test/java/org/apache/struts2/dispatcher/ChartResultTest.java

Author: hermanns
Date: Fri Apr 24 08:08:43 2009
New Revision: 768217

URL: http://svn.apache.org/viewvc?rev=768217&view=rev
Log:
WW-2742 Paramters not being set in JFreeChart Plugin

Modified:
    struts/struts2/trunk/plugins/jfreechart/pom.xml
    struts/struts2/trunk/plugins/jfreechart/src/main/java/org/apache/struts2/dispatcher/ChartResult.java
    struts/struts2/trunk/plugins/jfreechart/src/test/java/org/apache/struts2/dispatcher/ChartResultTest.java

Modified: struts/struts2/trunk/plugins/jfreechart/pom.xml
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/jfreechart/pom.xml?rev=768217&r1=768216&r2=768217&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/jfreechart/pom.xml (original)
+++ struts/struts2/trunk/plugins/jfreechart/pom.xml Fri Apr 24 08:08:43 2009
@@ -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>

Modified: struts/struts2/trunk/plugins/jfreechart/src/main/java/org/apache/struts2/dispatcher/ChartResult.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/jfreechart/src/main/java/org/apache/struts2/dispatcher/ChartResult.java?rev=768217&r1=768216&r2=768217&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/jfreechart/src/main/java/org/apache/struts2/dispatcher/ChartResult.java (original)
+++ struts/struts2/trunk/plugins/jfreechart/src/main/java/org/apache/struts2/dispatcher/ChartResult.java Fri Apr 24 08:08:43 2009
@@ -23,7 +23,8 @@
 
 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 @@
  * &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 @@
 
     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 @@
         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 @@
 
     // 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 @@
      * @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 @@
         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;
+        }
+    }
+
 }
\ No newline at end of file

Modified: struts/struts2/trunk/plugins/jfreechart/src/test/java/org/apache/struts2/dispatcher/ChartResultTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/jfreechart/src/test/java/org/apache/struts2/dispatcher/ChartResultTest.java?rev=768217&r1=768216&r2=768217&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/jfreechart/src/test/java/org/apache/struts2/dispatcher/ChartResultTest.java (original)
+++ struts/struts2/trunk/plugins/jfreechart/src/test/java/org/apache/struts2/dispatcher/ChartResultTest.java Fri Apr 24 08:08:43 2009
@@ -23,10 +23,11 @@
 
 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 @@
 
 /**
  */
-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 @@
 
         result.setChart(mockChart);
 
-        result.setHeight(10);
-        result.setWidth(10);
+        result.setHeight("10");
+        result.setWidth("10");
         result.execute(actionInvocation);
 
         responseMock.verify();
@@ -77,19 +80,59 @@
         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 @@
         actionInvocation = null;
         os = null;
         responseMock = null;
+        stack = null;
+        mockActionProxy = null;
     }