Author: wesw
Date: Thu Jun  4 18:42:41 2009
New Revision: 781828

URL: http://svn.apache.org/viewvc?rev=781828&view=rev
Log:
added an action, it's configuration and unit test 
the action is a shim for getting the status of currently running uploads

Added:
    
struts/sandbox/trunk/struts2-fileupload-plugin/src/main/java/org/apache/struts2/fileupload/UploadStatusAction.java
    
struts/sandbox/trunk/struts2-fileupload-plugin/src/test/java/org/apache/struts2/fileupload/UploadStatusActionTest.java
Modified:
    
struts/sandbox/trunk/struts2-fileupload-plugin/src/main/resources/struts-plugin.xml

Added: 
struts/sandbox/trunk/struts2-fileupload-plugin/src/main/java/org/apache/struts2/fileupload/UploadStatusAction.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-fileupload-plugin/src/main/java/org/apache/struts2/fileupload/UploadStatusAction.java?rev=781828&view=auto
==============================================================================
--- 
struts/sandbox/trunk/struts2-fileupload-plugin/src/main/java/org/apache/struts2/fileupload/UploadStatusAction.java
 (added)
+++ 
struts/sandbox/trunk/struts2-fileupload-plugin/src/main/java/org/apache/struts2/fileupload/UploadStatusAction.java
 Thu Jun  4 18:42:41 2009
@@ -0,0 +1,43 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.fileupload;
+
+import com.opensymphony.xwork2.ActionSupport;
+
+import java.util.List;
+
+import org.apache.struts2.ServletActionContext;
+
+/**
+ * Class that provides the ability to retrieve the
+ * status of the session's running uploads.
+ *
+ * @author $Author$
+ * <p/>
+ */
+public class UploadStatusAction extends ActionSupport {
+
+    public List<UploadStatus> getUploadStatus() {
+        UploadStatusHolder holder = new UploadStatusHolder();
+        return holder.getAllStatusesInSession(
+                ServletActionContext.getRequest().getSession(true).getId() );
+    }
+}

Modified: 
struts/sandbox/trunk/struts2-fileupload-plugin/src/main/resources/struts-plugin.xml
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-fileupload-plugin/src/main/resources/struts-plugin.xml?rev=781828&r1=781827&r2=781828&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-fileupload-plugin/src/main/resources/struts-plugin.xml
 (original)
+++ 
struts/sandbox/trunk/struts2-fileupload-plugin/src/main/resources/struts-plugin.xml
 Thu Jun  4 18:42:41 2009
@@ -46,5 +46,12 @@
     <constant name="struts.fileuploadplugin.listenerupdatefrequency" 
value="2048" />
     <constant name="struts.fileuploadplugin.isportletupload" value="false"/>
     <constant name="struts.fileuploadplugin.timetokeepstatus" value="600" />
-    
+
+    <package name="uploadStatus" namespace="/upload">
+        <action name="status" 
class="org.apache.struts2.fileupload.UploadStatusAction">
+            <result name="success" type="xslt">
+                <param name="exposedValue">uploadStatus</param>
+            </result>
+        </action>
+    </package>
 </struts>
\ No newline at end of file

Added: 
struts/sandbox/trunk/struts2-fileupload-plugin/src/test/java/org/apache/struts2/fileupload/UploadStatusActionTest.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-fileupload-plugin/src/test/java/org/apache/struts2/fileupload/UploadStatusActionTest.java?rev=781828&view=auto
==============================================================================
--- 
struts/sandbox/trunk/struts2-fileupload-plugin/src/test/java/org/apache/struts2/fileupload/UploadStatusActionTest.java
 (added)
+++ 
struts/sandbox/trunk/struts2-fileupload-plugin/src/test/java/org/apache/struts2/fileupload/UploadStatusActionTest.java
 Thu Jun  4 18:42:41 2009
@@ -0,0 +1,90 @@
+package org.apache.struts2.fileupload;
+
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.assertTrue;
+import org.springframework.mock.web.MockServletContext;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.apache.struts2.ServletActionContext;
+import org.apache.struts2.StrutsStatics;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.List;
+
+import com.opensymphony.xwork2.ActionContext;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Describe your class here
+ *
+ * @author Your Name
+ *         <p/>
+ *         $Id$
+ */
+public class UploadStatusActionTest implements StrutsStatics {
+
+    ActionContext actionContext;
+
+    private MockServletContext servletContext;
+    private HttpServletRequest request;
+    private HttpServletResponse response;
+
+    /**
+     *
+     */
+    @Before
+    public void setUp() {
+        // the basic progress listener keys off of the session id
+        // and the file item id. To find the session id, it will
+        // try to get a session from the servletActionContext
+        // so, we setup a mock context to let it work. I'm not sure
+        // if all of the following is necessary, but it appears to
+        // work and ids are properly generating as of $Date: 2009-06-04 
11:46:32 -0400 (Thu, 04 Jun 2009) $
+
+        Map extraContext = new HashMap();
+
+        servletContext = new MockServletContext();
+
+        request = new MockHttpServletRequest();
+        response = new MockHttpServletResponse();
+
+        extraContext.put(HTTP_REQUEST, request);
+        extraContext.put(HTTP_RESPONSE, response);
+        extraContext.put(SERVLET_CONTEXT, servletContext);
+
+        actionContext = new ActionContext(extraContext);
+        ServletActionContext.setContext(actionContext);
+    }
+
+    /**
+     *
+     */
+    @Test
+    public void testUploadStatusActionGetUploadStatus() {
+        BasicProgressListener listener = new BasicProgressListener();
+        UploadStatusTracker tracker = new UploadStatusHolder();
+        listener.setTracker(tracker);
+        listener.setUpdateFrequency("10");
+        listener.update(10L, 10L, 1);
+        listener.update(100L, 100L, 2);
+
+        UploadStatusAction action = new UploadStatusAction();
+        List<UploadStatus> statuses = action.getUploadStatus();
+
+        for (UploadStatus status : statuses) {
+            if (status.getItemId() == 1) {
+                assertTrue(status.getBytesRead() == 10L);
+                assertTrue(status.getContentLength() == 10L);
+            }
+            else {
+                assertTrue(status.getBytesRead() == 100L);
+                assertTrue(status.getContentLength() == 100L);
+            }
+        }
+    }
+
+}


Reply via email to