2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.service
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.List
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
18 import org
.junit
.After
;
19 import org
.junit
.Assert
;
20 import org
.junit
.Test
;
22 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
23 import eu
.etaxonomy
.cdm
.api
.service
.IProgressMonitorService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.ITestService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
;
26 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
27 import eu
.etaxonomy
.cdm
.common
.monitor
.RemotingProgressMonitor
;
28 import eu
.etaxonomy
.taxeditor
.httpinvoker
.RemotingSessionAwareTest
;
29 import eu
.etaxonomy
.taxeditor
.operation
.IFeedbackGenerator
;
30 import eu
.etaxonomy
.taxeditor
.operation
.IPostMoniteredOperationEnabled
;
31 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
38 public class ProgressMonitorServiceTest
extends RemotingSessionAwareTest
{
40 private static final Logger logger
= Logger
.getLogger(ProgressMonitorServiceTest
.class);
42 ITestService testService
= getRemoteApplicationController().getTestService();
44 IProgressMonitorService progressMonitorService
= getRemoteApplicationController().getProgressMonitorService();
49 public void revertAuthentication() {
50 authenticateDefaultUser();
54 public void testMonitLongRunningMethod() throws InterruptedException
{
55 UUID uuid
= testService
.monitLongRunningMethod(null, null, 0);
56 int pollInterval
= 1000;
57 RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
58 expectedMonitor
.setResult("Success");
59 expectedMonitor
.addReport("Report");
60 expectedMonitor
.done();
61 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
64 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
),
65 (IFeedbackGenerator
)null,
66 new NullProgressMonitor());
70 public void testMonitLongRunningMethodByChangingUser() throws InterruptedException
{
72 IllegalStateException ise
= new IllegalStateException("IllegalStateException");
73 UUID uuid
= testService
.monitLongRunningMethod(ise
, null, 0);
74 authenticateExtraUser();
75 IRemotingProgressMonitor monitor
= progressMonitorService
.getRemotingMonitor(uuid
);
76 Assert
.assertNull(monitor
);
81 public void testMonitLongRunningMethodWithException() throws InterruptedException
{
82 IllegalStateException ise
= new IllegalStateException("IllegalStateException");
83 UUID uuid
= testService
.monitLongRunningMethod(ise
, null, 0);
84 int pollInterval
= 1000;
85 RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
86 expectedMonitor
.setResult(ise
);
87 expectedMonitor
.setIsFailed(true);
88 expectedMonitor
.done();
90 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
93 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
),
94 (IFeedbackGenerator
)null,
95 new NullProgressMonitor());
96 if (expectedMonitor
.getResult() instanceof UpdateResult
){
97 UpdateResult result
= (UpdateResult
)expectedMonitor
.getResult();
98 if (!result
.getExceptions().contains(ise
)){
99 Assert
.fail("Exception due to inconsistent number of feedback generators not thrown");
102 // Assert.fail("Exception due to inconsistent number of feedback generators not thrown");
103 } catch(IllegalStateException e
) {
109 public void testMonitLongRunningMethodWithInterrupt() {
110 IllegalStateException ise
= new IllegalStateException("Interrupted Exception");
111 final UUID uuid
= testService
.monitLongRunningMethod(ise
, null, 0);
112 final int pollInterval
= 1000;
113 final RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
114 expectedMonitor
.setResult(ise
);
115 expectedMonitor
.setCanceled(true);
116 expectedMonitor
.setIsFailed(true);
117 expectedMonitor
.done();
119 Thread thread
= new Thread() {
123 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
127 (IFeedbackGenerator
)null,
128 new NullProgressMonitor());
129 if (expectedMonitor
.getResult() instanceof UpdateResult
){
130 UpdateResult result
= (UpdateResult
)expectedMonitor
.getResult();
131 if (!result
.getExceptions().contains(ise
)){
132 Assert
.fail("Exception due to inconsistent number of feedback generators not thrown");
135 // Assert.fail("InterruptedException wrapped in an IllegalStateException should be thrown");
136 } catch (InterruptedException e
) {
138 } catch (IllegalStateException e
) {
144 while(!progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
146 progressMonitorService
.interrupt(uuid
);
148 while(progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
150 IRemotingProgressMonitor remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
151 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
).postOperation(remotingMonitor
);
156 public void testMonitLongRunningMethodWithCancellation() throws InterruptedException
{
158 final UUID uuid
= testService
.monitLongRunningMethod(null, null, 0);
159 final int pollInterval
= 1000;
160 final RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
161 expectedMonitor
.setResult("Cancelled");
162 expectedMonitor
.setCanceled(true);
163 expectedMonitor
.done();
165 Thread thread
= new Thread() {
169 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
173 (IFeedbackGenerator
)null,
174 new NullProgressMonitor());
175 } catch (InterruptedException e
) {
182 while(!progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
183 progressMonitorService
.cancel(uuid
);
184 while(progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
185 IRemotingProgressMonitor remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
186 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
).postOperation(remotingMonitor
);
191 public void testMonitLongRunningMethodWithWaitForFeedback() throws InterruptedException
{
193 List
<String
> feedbacks
= Arrays
.asList("feedback1", "feedback2");
194 List
<IFeedbackGenerator
> feebackGenerators
= new ArrayList
<IFeedbackGenerator
>();
195 final RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
196 expectedMonitor
.setResult("Success");
197 for(String feedback
: feedbacks
) {
198 feebackGenerators
.add(new MockFeedbackGenerator(feedback
, 0));
199 expectedMonitor
.addReport(feedback
);
201 expectedMonitor
.addReport("Report");
202 expectedMonitor
.done();
204 final UUID uuid
= testService
.monitLongRunningMethod(null, feedbacks
, 0);
205 final int pollInterval
= 1000;
207 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
210 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
),
212 new NullProgressMonitor());
217 public void testMonitLongRunningMethodWithInconsistentWaitForFeedback() throws InterruptedException
{
219 List
<String
> feedbacks
= Arrays
.asList("feedback1", "feedback2");
220 List
<IFeedbackGenerator
> feebackGenerators
= new ArrayList
<IFeedbackGenerator
>();
221 final RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
222 expectedMonitor
.setResult("Success");
223 for(String feedback
: feedbacks
) {
224 feebackGenerators
.add(new MockFeedbackGenerator(feedback
, 0));
225 expectedMonitor
.addReport(feedback
);
227 expectedMonitor
.addReport("Report");
228 expectedMonitor
.done();
230 final UUID uuid
= testService
.monitLongRunningMethod(null, feedbacks
, 0);
231 final int pollInterval
= 1000;
233 feebackGenerators
.remove(1);
235 final UUID newUuid
= testService
.monitLongRunningMethod(null, feedbacks
, 0);
238 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
241 new MockPostMoniteredOperationEnabled(expectedMonitor
, newUuid
),
243 new NullProgressMonitor());
244 if (expectedMonitor
.getResult() instanceof UpdateResult
){
245 UpdateResult result
= (UpdateResult
)expectedMonitor
.getResult();
246 if (result
.getExceptions().isEmpty()){
247 Assert
.fail("Exception due to inconsistent number of feedback generators not thrown");
250 // Assert.fail("Exception due to inconsistent number of feedback generators not thrown");
251 } catch(IllegalStateException ise
) {
257 public void testMonitLongRunningMethodWithWaitForFeedbackTimeout() throws InterruptedException
{
259 List
<String
> feedbacks
= Arrays
.asList("feedback1");
260 List
<IFeedbackGenerator
> feebackGenerators
= new ArrayList
<IFeedbackGenerator
>();
261 final RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
262 expectedMonitor
.setResult(new IllegalStateException(new InterruptedException()));
263 for(String feedback
: feedbacks
) {
264 feebackGenerators
.add(new MockFeedbackGenerator(feedback
, 1000 * 15));
267 expectedMonitor
.setCanceled(true);
268 expectedMonitor
.setIsFailed(true);
269 expectedMonitor
.done();
271 expectedMonitor
.done();
273 final UUID uuid
= testService
.monitLongRunningMethod(null, feedbacks
, 1000 * 5);
274 final int pollInterval
= 1000;
278 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
281 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
),
283 new NullProgressMonitor());
284 if (expectedMonitor
.getResult() instanceof UpdateResult
){
285 UpdateResult result
= (UpdateResult
)expectedMonitor
.getResult();
286 if (result
.getExceptions().isEmpty()){
287 Assert
.fail("Exception due to inconsistent number of feedback generators not thrown");
290 // Assert.fail("Exception due to inconsistent number of feedback generators not thrown");
291 } catch(IllegalStateException ise
) {
297 class MockPostMoniteredOperationEnabled
implements IPostMoniteredOperationEnabled
{
299 private RemotingProgressMonitor expectedMonitor
;
300 private UUID monitorUuid
;
302 public MockPostMoniteredOperationEnabled(RemotingProgressMonitor expectedMonitor
, UUID monitorUuid
) {
303 this.expectedMonitor
= expectedMonitor
;
304 this.monitorUuid
= monitorUuid
;
310 public void postOperation(IRemotingProgressMonitor monitor
) {
311 if(expectedMonitor
.getResult() instanceof Exception
) {
312 Exception expectedException
= (Exception
) expectedMonitor
.getResult();
313 Exception exception
= (Exception
) monitor
.getResult();
314 Assert
.assertEquals(expectedException
.getClass(), expectedException
.getClass());
315 Assert
.assertEquals(expectedException
.getMessage(), expectedException
.getMessage());
317 Assert
.assertEquals(expectedMonitor
.getResult(), monitor
.getResult());
319 Assert
.assertEquals(expectedMonitor
.getReports(), monitor
.getReports());
320 Assert
.assertEquals(expectedMonitor
.isCanceled(), monitor
.isCanceled());
321 Assert
.assertEquals(expectedMonitor
.isFailed(), monitor
.isFailed());
322 Assert
.assertEquals(expectedMonitor
.isDone(), monitor
.isDone());
323 Assert
.assertTrue(!progressMonitorService
.isMonitorThreadRunning(monitorUuid
));
327 class MockFeedbackGenerator
implements IFeedbackGenerator
{
329 private String feedback
;
330 private long waitTime
;
332 public MockFeedbackGenerator(String feedback
, long waitTime
) {
333 this.feedback
= feedback
;
334 this.waitTime
= waitTime
;
341 public void setFeedbackForMonitor(UUID uuid
) {
344 Thread
.sleep(waitTime
);
345 } catch (InterruptedException e
) {
349 CdmApplicationState
.getCurrentAppConfig().getProgressMonitorService().setFeedback(uuid
, feedback
);