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
.util
.UUID
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
16 import org
.junit
.After
;
17 import org
.junit
.Assert
;
18 import org
.junit
.Test
;
20 import eu
.etaxonomy
.cdm
.api
.service
.IProgressMonitorService
;
21 import eu
.etaxonomy
.cdm
.api
.service
.ITestService
;
22 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
23 import eu
.etaxonomy
.cdm
.common
.monitor
.RemotingProgressMonitor
;
24 import eu
.etaxonomy
.taxeditor
.httpinvoker
.RemotingSessionAwareTest
;
25 import eu
.etaxonomy
.taxeditor
.operation
.IPostMoniteredOperationEnabled
;
26 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
33 public class ProgressMonitorServiceTest
extends RemotingSessionAwareTest
{
35 private static final Logger logger
= Logger
.getLogger(ProgressMonitorServiceTest
.class);
37 ITestService testService
= getRemoteApplicationController().getTestService();
39 IProgressMonitorService progressMonitorService
= getRemoteApplicationController().getProgressMonitorService();
44 public void revertAuthentication() {
45 authenticateDefaultUser();
49 public void testMonitLongRunningMethod() throws InterruptedException
{
50 UUID uuid
= testService
.monitLongRunningMethod(null);
51 int pollInterval
= 1000;
52 RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
53 expectedMonitor
.setResult("Success");
54 expectedMonitor
.addReport("Report");
55 expectedMonitor
.done();
56 IRemotingProgressMonitor remotingMonitor
= CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
59 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
),
60 new NullProgressMonitor());
64 public void testMonitLongRunningMethodByChangingUser() throws InterruptedException
{
66 IllegalStateException ise
= new IllegalStateException("IllegalStateException");
67 UUID uuid
= testService
.monitLongRunningMethod(ise
);
68 authenticateExtraUser();
69 IRemotingProgressMonitor monitor
= progressMonitorService
.getRemotingMonitor(uuid
);
70 Assert
.assertNull(monitor
);
75 public void testMonitLongRunningMethodWithException() throws InterruptedException
{
76 IllegalStateException ise
= new IllegalStateException("IllegalStateException");
77 UUID uuid
= testService
.monitLongRunningMethod(ise
);
78 int pollInterval
= 1000;
79 RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
80 expectedMonitor
.setResult(ise
);
81 expectedMonitor
.setIsFailed(true);
82 expectedMonitor
.done();
83 IRemotingProgressMonitor remotingMonitor
= CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
86 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
),
87 new NullProgressMonitor());
91 public void testMonitLongRunningMethodWithInterrupt() throws InterruptedException
{
92 IllegalStateException ise
= new IllegalStateException("Interrupted Exception");
93 final UUID uuid
= testService
.monitLongRunningMethod(ise
);
94 final int pollInterval
= 1000;
95 final RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
96 expectedMonitor
.setResult(ise
);
97 expectedMonitor
.setCanceled(true);
98 expectedMonitor
.setIsFailed(true);
99 expectedMonitor
.done();
101 Thread thread
= new Thread() {
105 IRemotingProgressMonitor remotingMonitor
= CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
109 new NullProgressMonitor());
110 } catch (InterruptedException e
) {
117 while(!progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
119 progressMonitorService
.interrupt(uuid
);
121 while(progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
123 IRemotingProgressMonitor remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
124 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
).postOperation(remotingMonitor
);
129 public void testMonitLongRunningMethodWithCancellation() throws InterruptedException
{
131 final UUID uuid
= testService
.monitLongRunningMethod(null);
132 final int pollInterval
= 1000;
133 final RemotingProgressMonitor expectedMonitor
= new RemotingProgressMonitor();
134 expectedMonitor
.setResult("Cancelled");
135 expectedMonitor
.setCanceled(true);
136 expectedMonitor
.done();
138 Thread thread
= new Thread() {
142 IRemotingProgressMonitor remotingMonitor
= CdmStore
.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
146 new NullProgressMonitor());
147 } catch (InterruptedException e
) {
154 while(!progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
155 progressMonitorService
.cancel(uuid
);
156 while(progressMonitorService
.isMonitorThreadRunning(uuid
)) {}
157 IRemotingProgressMonitor remotingMonitor
= progressMonitorService
.getRemotingMonitor(uuid
);
158 new MockPostMoniteredOperationEnabled(expectedMonitor
, uuid
).postOperation(remotingMonitor
);
163 class MockPostMoniteredOperationEnabled
implements IPostMoniteredOperationEnabled
{
165 private RemotingProgressMonitor expectedMonitor
;
166 private UUID monitorUuid
;
168 public MockPostMoniteredOperationEnabled(RemotingProgressMonitor expectedMonitor
, UUID monitorUuid
) {
169 this.expectedMonitor
= expectedMonitor
;
170 this.monitorUuid
= monitorUuid
;
176 public void postOperation(IRemotingProgressMonitor monitor
) {
177 if(expectedMonitor
.getResult() instanceof Exception
) {
178 Exception expectedException
= (Exception
) expectedMonitor
.getResult();
179 Exception exception
= (Exception
) monitor
.getResult();
180 Assert
.assertEquals(expectedException
.getClass(), expectedException
.getClass());
181 Assert
.assertEquals(expectedException
.getMessage(), expectedException
.getMessage());
183 Assert
.assertEquals(expectedMonitor
.getResult(), monitor
.getResult());
185 Assert
.assertEquals(expectedMonitor
.getReports(), monitor
.getReports());
186 Assert
.assertEquals(expectedMonitor
.isCanceled(), monitor
.isCanceled());
187 Assert
.assertEquals(expectedMonitor
.isFailed(), monitor
.isFailed());
188 Assert
.assertEquals(expectedMonitor
.isDone(), monitor
.isDone());
189 Assert
.assertTrue(!progressMonitorService
.isMonitorThreadRunning(monitorUuid
));