3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.service
;
12 import java
.io
.Serializable
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
16 import java
.util
.UUID
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
20 import org
.junit
.After
;
21 import org
.junit
.Assert
;
22 import org
.junit
.Test
;
24 import eu
.etaxonomy
.cdm
.api
.service
.IProgressMonitorService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITestService
;
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);
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);
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);
84 int pollInterval
= 1000;
85 RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
86 expectedMonitor
.setResult(ise
);
87 expectedMonitor
.setIsFailed(true);
88 expectedMonitor
.done();
89 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
92 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
),
93 (IFeedbackGenerator
)null,
94 new NullProgressMonitor());
98 public void testMonitLongRunningMethodWithInterrupt() {
99 IllegalStateException ise
= new IllegalStateException("Interrupted Exception");
100 final UUID uuid
= testService
.monitLongRunningMethod(ise
, null);
101 final int pollInterval
= 1000;
102 final RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
103 expectedMonitor
.setResult(ise
);
104 expectedMonitor
.setCanceled(true);
105 expectedMonitor
.setIsFailed(true);
106 expectedMonitor
.done();
108 Thread thread
= new Thread() {
112 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
116 (IFeedbackGenerator
)null,
117 new NullProgressMonitor());
118 } catch (InterruptedException e
) {
124 while(!progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
126 progressMonitorService
.interrupt(uuid
);
128 while(progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
130 IRemotingProgressMonitor remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
131 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
).postOperation(remotingMonitor
);
136 public void testMonitLongRunningMethodWithCancellation() throws InterruptedException
{
138 final UUID uuid
= testService
.monitLongRunningMethod(null, null);
139 final int pollInterval
= 1000;
140 final RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
141 expectedMonitor
.setResult("Cancelled");
142 expectedMonitor
.setCanceled(true);
143 expectedMonitor
.done();
145 Thread thread
= new Thread() {
149 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
153 (IFeedbackGenerator
)null,
154 new NullProgressMonitor());
155 } catch (InterruptedException e
) {
162 while(!progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
163 progressMonitorService
.cancel(uuid
);
164 while(progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
165 IRemotingProgressMonitor remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
166 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
).postOperation(remotingMonitor
);
171 public void testMonitLongRunningMethodWithWaitForFeedback() throws InterruptedException
{
173 List
<String
> feedbacks
= Arrays
.asList("feedback1", "feedback2");
174 List
<IFeedbackGenerator
> feebackGenerators
= new ArrayList
<IFeedbackGenerator
>();
175 final RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
176 expectedMonitor
.setResult("Success");
177 for(String feedback
: feedbacks
) {
178 feebackGenerators
.add(new MockFeedbackGenerator(feedback
));
179 expectedMonitor
.addReport(feedback
);
181 expectedMonitor
.addReport("Report");
182 expectedMonitor
.done();
184 final UUID uuid
= testService
.monitLongRunningMethod(null, feedbacks
);
185 final int pollInterval
= 1000;
187 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
190 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
),
192 new NullProgressMonitor());
194 feebackGenerators
.remove(1);
196 final UUID newUuid
= testService
.monitLongRunningMethod(null, feedbacks
);
199 CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
202 new MockPostMoniteredOperationEnabled(expectedMonitor
, newUuid
),
204 new NullProgressMonitor());
205 Assert
.fail("Exception due to inconsistent number of feedback generators not thrown");
206 } catch(IllegalStateException ise
) {
214 class MockPostMoniteredOperationEnabled
implements IPostMoniteredOperationEnabled
{
216 private RemotingProgressMonitor expectedMonitor
;
217 private UUID monitorUuid
;
219 public MockPostMoniteredOperationEnabled(RemotingProgressMonitor expectedMonitor
, UUID monitorUuid
) {
220 this.expectedMonitor
= expectedMonitor
;
221 this.monitorUuid
= monitorUuid
;
227 public void postOperation(IRemotingProgressMonitor monitor
) {
228 if(expectedMonitor
.getResult() instanceof Exception
) {
229 Exception expectedException
= (Exception
) expectedMonitor
.getResult();
230 Exception exception
= (Exception
) monitor
.getResult();
231 Assert
.assertEquals(expectedException
.getClass(), expectedException
.getClass());
232 Assert
.assertEquals(expectedException
.getMessage(), expectedException
.getMessage());
234 Assert
.assertEquals(expectedMonitor
.getResult(), monitor
.getResult());
236 Assert
.assertEquals(expectedMonitor
.getReports(), monitor
.getReports());
237 Assert
.assertEquals(expectedMonitor
.isCanceled(), monitor
.isCanceled());
238 Assert
.assertEquals(expectedMonitor
.isFailed(), monitor
.isFailed());
239 Assert
.assertEquals(expectedMonitor
.isDone(), monitor
.isDone());
240 Assert
.assertTrue(!progressMonitorService
.isMonitorThreadRunning(monitorUuid
));
244 class MockFeedbackGenerator
implements IFeedbackGenerator
{
246 private String feedback
;
248 public MockFeedbackGenerator(String feedback
) {
249 this.feedback
= feedback
;
255 public Serializable
generateFeedback() {