]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixed race condition in upload (#10942)
authorArtur Signell <artur@vaadin.com>
Fri, 8 Feb 2013 07:40:25 +0000 (09:40 +0200)
committerArtur Signell <artur@vaadin.com>
Fri, 8 Feb 2013 07:41:58 +0000 (09:41 +0200)
Updated test to show if upload succeeds or fails

Change-Id: Ia9c3a3d36a5dd163fbad59e8cc41d53aba3ccb5a

client/src/com/vaadin/client/ui/VUpload.java
server/src/com/vaadin/ui/AbstractComponent.java
uitest/src/com/vaadin/tests/components/upload/TestFileUpload.java

index f639e8f0900b7ac87dfe4d19e7186658ee1eedef..866a6ca4cfe9ce5d7b69e38d69abb9002f829e77 100644 (file)
@@ -18,6 +18,7 @@ package com.vaadin.client.ui;
 
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.core.client.Scheduler.ScheduledCommand;
 import com.google.gwt.dom.client.DivElement;
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.FormElement;
@@ -257,6 +258,32 @@ public class VUpload extends SimplePanel {
         });
     }
 
+    ScheduledCommand startUploadCmd = new ScheduledCommand() {
+
+        @Override
+        public void execute() {
+            element.submit();
+            submitted = true;
+
+            disableUpload();
+
+            /*
+             * Visit server a moment after upload has started to see possible
+             * changes from UploadStarted event. Will be cleared on complete.
+             */
+            t = new Timer() {
+                @Override
+                public void run() {
+                    VConsole.log("Visiting server to see if upload started event changed UI.");
+                    client.updateVariable(paintableId, "pollForStart",
+                            nextUploadId, true);
+                }
+            };
+            t.schedule(800);
+        }
+
+    };
+
     /** For internal use only. May be removed or replaced in the future. */
     public void submit() {
         if (fu.getFilename().length() == 0 || submitted || !enabled) {
@@ -267,25 +294,10 @@ public class VUpload extends SimplePanel {
         // before upload
         client.sendPendingVariableChanges();
 
-        element.submit();
-        submitted = true;
-        VConsole.log("Submitted form");
-
-        disableUpload();
-
-        /*
-         * Visit server a moment after upload has started to see possible
-         * changes from UploadStarted event. Will be cleared on complete.
-         */
-        t = new Timer() {
-            @Override
-            public void run() {
-                VConsole.log("Visiting server to see if upload started event changed UI.");
-                client.updateVariable(paintableId, "pollForStart",
-                        nextUploadId, true);
-            }
-        };
-        t.schedule(800);
+        // This is done as deferred because sendPendingVariableChanges is also
+        // deferred and we want to start the upload only after the changes have
+        // been sent to the server
+        Scheduler.get().scheduleDeferred(startUploadCmd);
     }
 
     @Override
index 85c40e0d39b9eeadbe3856c403df4731958cfc62..cdddcb02b0860d5187ead535cefa7bbe8bf8772e 100644 (file)
@@ -532,7 +532,7 @@ public abstract class AbstractComponent extends AbstractClientConnector
      */
     @Override
     public boolean isReadOnly() {
-        return getState().readOnly;
+        return ((AbstractComponentState) getState(false)).readOnly;
     }
 
     /*
index ad86d0f59374dc0e8d12a4a662695e5d843ec403..b57c236b20d3b5f869ba67a8a68589f0e065061f 100644 (file)
@@ -8,8 +8,11 @@ import org.apache.commons.codec.digest.DigestUtils;
 import com.vaadin.tests.components.TestBase;
 import com.vaadin.tests.util.Log;
 import com.vaadin.ui.Upload;
-import com.vaadin.ui.Upload.FinishedEvent;
+import com.vaadin.ui.Upload.FailedEvent;
+import com.vaadin.ui.Upload.FailedListener;
 import com.vaadin.ui.Upload.Receiver;
+import com.vaadin.ui.Upload.SucceededEvent;
+import com.vaadin.ui.Upload.SucceededListener;
 
 public class TestFileUpload extends TestBase implements Receiver {
 
@@ -27,10 +30,22 @@ public class TestFileUpload extends TestBase implements Receiver {
             }
         });
         u.setId("UPL");
-        u.addListener(new Upload.FinishedListener() {
+        u.addFailedListener(new FailedListener() {
 
             @Override
-            public void uploadFinished(FinishedEvent event) {
+            public void uploadFailed(FailedEvent event) {
+                String hash = DigestUtils.md5Hex(baos.toByteArray());
+
+                log.log("<span style=\"color: red;\">Upload failed. Name: "
+                        + event.getFilename() + ", Size: " + baos.size()
+                        + ", md5: " + hash + "</span>");
+                baos.reset();
+            }
+        });
+        u.addSucceededListener(new SucceededListener() {
+
+            @Override
+            public void uploadSucceeded(SucceededEvent event) {
                 String hash = DigestUtils.md5Hex(baos.toByteArray());
                 log.log("Upload finished. Name: " + event.getFilename()
                         + ", Size: " + baos.size() + ", md5: " + hash);