#5297 Implement client side remoting progress monitor feedback mechanism, Add test...
authorCherian Mathew <c.mathew@bgbm.org>
Fri, 30 Oct 2015 09:28:14 +0000 (10:28 +0100)
committerCherian Mathew <c.mathew@bgbm.org>
Fri, 30 Oct 2015 15:31:55 +0000 (16:31 +0100)
#5297 Add test for monitor feedback timeout

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/CdmProgressMonitorAdapter.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/operation/FeedbackGenerator.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/operation/IFeedbackGenerator.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/FeedbackMessageBox.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/util/ProgressMonitorClientManager.java
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/service/ProgressMonitorServiceTest.java

index 9edcdf79286d01b55e2845cbbd1cfda33225e047..70a7130dbcadccd960bf964632c5bd1bea5ae0a4 100644 (file)
@@ -3,6 +3,8 @@
  */
 package eu.etaxonomy.taxeditor.model;
 
+import java.io.Serializable;
+
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
 
@@ -108,12 +110,78 @@ public class CdmProgressMonitorAdapter implements eu.etaxonomy.cdm.common.monito
                progressMonitor.internalWorked(arg0);
        }
 
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Serializable getFeedback() {
+        return null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean getIsWaitingForFeedback() {
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setFeedback(Serializable arg0) {
+
+    }
+
     /**
      * {@inheritDoc}
      */
     @Override
     public void waitForFeedback() {
-        // TODO Auto-generated method stub
 
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getOwner() {
+        return null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean hasFeedbackWaitTimedOut() {
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void interrupt() {
+
+
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setOwner(String arg0) {
+
+
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void waitForFeedback(long arg0) {
+
+    }
+
 }
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/operation/FeedbackGenerator.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/operation/FeedbackGenerator.java
new file mode 100644 (file)
index 0000000..5b084f5
--- /dev/null
@@ -0,0 +1,49 @@
+// $Id$
+/**
+* Copyright (C) 2015 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.operation;
+
+import java.io.Serializable;
+import java.util.UUID;
+
+import org.eclipse.swt.widgets.Display;
+
+import eu.etaxonomy.cdm.api.application.CdmApplicationState;
+
+/**
+ * Default implementation of the {@link IFeedbackGenerator} interface.
+ *
+ * @author cmathew
+ * @date 29 Oct 2015
+ *
+ */
+public abstract class FeedbackGenerator implements IFeedbackGenerator {
+
+
+    /**
+     * Generates feedback for a waiting remoting progress monitor
+     *
+     * @return feedback object
+     */
+    public abstract Serializable generateFeedback();
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setFeedbackForMonitor(final UUID uuid) {
+        Display.getDefault().syncExec(new Runnable() {
+            @Override
+            public void run() {
+                CdmApplicationState.getCurrentAppConfig().getProgressMonitorService().setFeedback(uuid, generateFeedback());
+            }
+        });
+    }
+
+}
index a7cee6ae710c12f9bf42a8cd52b08fdc306c34c5..36a6af169df279166222d512efbb7533410c3463 100644 (file)
@@ -9,15 +9,24 @@
 */
 package eu.etaxonomy.taxeditor.operation;
 
-import java.io.Serializable;
+import java.util.UUID;
 
 /**
+ * Generates feeback for waiting remoting progress monitors.
+ * The default implementation of this interface is {@link FeedbackGenerator}
+ *
  * @author cmathew
  * @date 28 Oct 2015
  *
  */
 public interface IFeedbackGenerator {
 
-    public Serializable generateFeedback();
+    /**
+     * Generates and sets feedback for a waiting remoting progress
+     * monitor corresponding to the given uuid.
+     *
+     * @param uuid of target waiting remoting progress monitor
+     */
+    public void setFeedbackForMonitor(UUID uuid);
 
 }
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/FeedbackMessageBox.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/FeedbackMessageBox.java
new file mode 100644 (file)
index 0000000..50b1568
--- /dev/null
@@ -0,0 +1,52 @@
+// $Id$
+/**
+* Copyright (C) 2015 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.ui.dialog;
+
+import java.io.Serializable;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.MessageBox;
+import org.eclipse.swt.widgets.Shell;
+
+import eu.etaxonomy.taxeditor.operation.FeedbackGenerator;
+
+/**
+ * Feedback Generator corresponding to a {@link MessageBox}
+ *
+ * @author cmathew
+ * @date 29 Oct 2015
+ *
+ */
+public class FeedbackMessageBox extends FeedbackGenerator {
+
+    private Shell shell;
+    /**
+     * @param parent
+     */
+    public FeedbackMessageBox(Shell parent) {
+        this.shell = parent;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Serializable generateFeedback() {
+
+     // create a dialog with ok and cancel buttons and a question icon
+        MessageBox dialog =
+          new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK| SWT.CANCEL);
+        dialog.setText("Monitor Feedback");
+        dialog.setMessage("Do you really want to continue?");
+
+        // open dialog and await user selection
+        return dialog.open();
+    }
+}
index c50686fa191d2779dd56624e9b7bb6c84c69170e..5e7e97ab21dfc0056a3e64ee0d1286b48bfd05d9 100644 (file)
@@ -113,7 +113,7 @@ public class ProgressMonitorClientManager {
                 Thread.sleep(pollInterval);
                 remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
                 // check if remoting monitor is waiting for feedback
-                if(remotingMonitor.isWaitingForFeedback()) {
+                if(remotingMonitor.getIsWaitingForFeedback()) {
                     if(feedbackGenerators != null) {
                         // if we have run out of feedback generators while
                         // the remoting monitor is waiting on feedback
@@ -121,7 +121,7 @@ public class ProgressMonitorClientManager {
                         if(feedbackCount + 1 > feedbackGenerators.size()) {
                             throw new IllegalStateException("Remoting monitor waiting on feedback that does not exist");
                         }
-                        progressMonitorService.setFeedback(uuid, feedbackGenerators.get(feedbackCount).generateFeedback());
+                        feedbackGenerators.get(feedbackCount).setFeedbackForMonitor(uuid);
                         feedbackCount++;
                     }
                 }
index 15d110e38e7dd858217d74e8527c3d00328930d9..76df490d2f2597102224d61128b003a893d1ea38 100644 (file)
@@ -9,7 +9,6 @@
  */
 package eu.etaxonomy.taxeditor.service;
 
-import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -21,6 +20,7 @@ import org.junit.After;
 import org.junit.Assert;
 import org.junit.Test;
 
+import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
 import eu.etaxonomy.cdm.api.service.ITestService;
 import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
@@ -52,7 +52,7 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
 
     @Test
     public void testMonitLongRunningMethod() throws InterruptedException {
-        UUID uuid = testService.monitLongRunningMethod(null, null);
+        UUID uuid = testService.monitLongRunningMethod(null, null, 0);
         int pollInterval = 1000;
         RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
         expectedMonitor.setResult("Success");
@@ -70,7 +70,7 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
     public void testMonitLongRunningMethodByChangingUser() throws InterruptedException {
 
         IllegalStateException ise = new IllegalStateException("IllegalStateException");
-        UUID uuid = testService.monitLongRunningMethod(ise, null);
+        UUID uuid = testService.monitLongRunningMethod(ise, null, 0);
         authenticateExtraUser();
         IRemotingProgressMonitor monitor = progressMonitorService.getRemotingMonitor(uuid);
         Assert.assertNull(monitor);
@@ -80,7 +80,7 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
     @Test
     public void testMonitLongRunningMethodWithException() throws InterruptedException {
         IllegalStateException ise = new IllegalStateException("IllegalStateException");
-        UUID uuid = testService.monitLongRunningMethod(ise, null);
+        UUID uuid = testService.monitLongRunningMethod(ise, null, 0);
         int pollInterval = 1000;
         RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
         expectedMonitor.setResult(ise);
@@ -102,7 +102,7 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
     @Test
     public void testMonitLongRunningMethodWithInterrupt() {
         IllegalStateException ise = new IllegalStateException("Interrupted Exception");
-        final UUID uuid = testService.monitLongRunningMethod(ise, null);
+        final UUID uuid = testService.monitLongRunningMethod(ise, null, 0);
         final int pollInterval = 1000;
         final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
         expectedMonitor.setResult(ise);
@@ -120,8 +120,11 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
                             null,
                             (IFeedbackGenerator)null,
                             new NullProgressMonitor());
+                    Assert.fail("InterruptedException wrapped in an IllegalStateException should be thrown");
                 } catch (InterruptedException e) {
 
+                } catch (IllegalStateException e) {
+
                 }
             }
         };
@@ -140,7 +143,7 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
     @Test
     public void testMonitLongRunningMethodWithCancellation() throws InterruptedException {
 
-        final UUID uuid = testService.monitLongRunningMethod(null, null);
+        final UUID uuid = testService.monitLongRunningMethod(null, null, 0);
         final int pollInterval = 1000;
         final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
         expectedMonitor.setResult("Cancelled");
@@ -180,13 +183,13 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
         final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
         expectedMonitor.setResult("Success");
         for(String feedback : feedbacks) {
-            feebackGenerators.add(new MockFeedbackGenerator(feedback));
+            feebackGenerators.add(new MockFeedbackGenerator(feedback, 0));
             expectedMonitor.addReport(feedback);
         }
         expectedMonitor.addReport("Report");
         expectedMonitor.done();
 
-        final UUID uuid = testService.monitLongRunningMethod(null, feedbacks);
+        final UUID uuid = testService.monitLongRunningMethod(null, feedbacks, 0);
         final int pollInterval = 1000;
 
         CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
@@ -196,9 +199,28 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
                 feebackGenerators,
                 new NullProgressMonitor());
 
+    }
+
+    @Test
+    public void testMonitLongRunningMethodWithInconsistentWaitForFeedback() throws InterruptedException {
+
+        List<String> feedbacks = Arrays.asList("feedback1", "feedback2");
+        List<IFeedbackGenerator> feebackGenerators = new ArrayList<IFeedbackGenerator>();
+        final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
+        expectedMonitor.setResult("Success");
+        for(String feedback : feedbacks) {
+            feebackGenerators.add(new MockFeedbackGenerator(feedback, 0));
+            expectedMonitor.addReport(feedback);
+        }
+        expectedMonitor.addReport("Report");
+        expectedMonitor.done();
+
+        final UUID uuid = testService.monitLongRunningMethod(null, feedbacks, 0);
+        final int pollInterval = 1000;
+
         feebackGenerators.remove(1);
 
-        final UUID newUuid = testService.monitLongRunningMethod(null, feedbacks);
+        final UUID newUuid = testService.monitLongRunningMethod(null, feedbacks, 0);
 
         try {
             CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
@@ -211,8 +233,40 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
         } catch(IllegalStateException ise) {
 
         }
+    }
+
+    @Test
+    public void testMonitLongRunningMethodWithWaitForFeedbackTimeout() throws InterruptedException {
 
+        List<String> feedbacks = Arrays.asList("feedback1");
+        List<IFeedbackGenerator> feebackGenerators = new ArrayList<IFeedbackGenerator>();
+        final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
+        expectedMonitor.setResult(new IllegalStateException(new InterruptedException()));
+        for(String feedback : feedbacks) {
+            feebackGenerators.add(new MockFeedbackGenerator(feedback, 1000 * 15));
 
+        }
+        expectedMonitor.setCanceled(true);
+        expectedMonitor.setIsFailed(true);
+        expectedMonitor.done();
+
+        expectedMonitor.done();
+
+        final UUID uuid = testService.monitLongRunningMethod(null, feedbacks, 1000 * 5);
+        final int pollInterval = 1000;
+
+
+        try {
+            CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
+                    uuid,
+                    pollInterval,
+                    new MockPostMoniteredOperationEnabled(expectedMonitor, uuid),
+                    feebackGenerators,
+                    new NullProgressMonitor());
+            Assert.fail("Exception due to inconsistent number of feedback generators not thrown");
+        } catch(IllegalStateException ise) {
+
+        }
     }
 
 
@@ -249,16 +303,26 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
     class MockFeedbackGenerator implements IFeedbackGenerator {
 
         private String feedback;
+        private long waitTime;
 
-        public MockFeedbackGenerator(String feedback) {
+        public MockFeedbackGenerator(String feedback, long waitTime) {
             this.feedback = feedback;
+            this.waitTime = waitTime;
         }
+
         /**
          * {@inheritDoc}
          */
         @Override
-        public Serializable generateFeedback() {
-            return feedback;
+        public void setFeedbackForMonitor(UUID uuid) {
+            if(waitTime > 0) {
+                try {
+                    Thread.sleep(waitTime);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+            CdmApplicationState.getCurrentAppConfig().getProgressMonitorService().setFeedback(uuid, feedback);
         }
 
     }