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 testMonitLongRunningMethodWithCancellation() throws InterruptedException
{
92 IllegalStateException ise
= new IllegalStateException("Cancelled 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 class MockPostMoniteredOperationEnabled
implements IPostMoniteredOperationEnabled
{
131 private RemotingProgressMonitor expectedMonitor
;
132 private UUID monitorUuid
;
134 public MockPostMoniteredOperationEnabled(RemotingProgressMonitor expectedMonitor
, UUID monitorUuid
) {
135 this.expectedMonitor
= expectedMonitor
;
136 this.monitorUuid
= monitorUuid
;
142 public void postOperation(IRemotingProgressMonitor monitor
) {
143 if(expectedMonitor
.getResult() instanceof Exception
) {
144 Exception expectedException
= (Exception
) expectedMonitor
.getResult();
145 Exception exception
= (Exception
) monitor
.getResult();
146 Assert
.assertEquals(expectedException
.getClass(), expectedException
.getClass());
147 Assert
.assertEquals(expectedException
.getMessage(), expectedException
.getMessage());
149 Assert
.assertEquals(expectedMonitor
.getResult(), monitor
.getResult());
151 Assert
.assertEquals(expectedMonitor
.getReports(), monitor
.getReports());
152 Assert
.assertEquals(expectedMonitor
.isCanceled(), monitor
.isCanceled());
153 Assert
.assertEquals(expectedMonitor
.isFailed(), monitor
.isFailed());
154 Assert
.assertEquals(expectedMonitor
.isDone(), monitor
.isDone());
155 Assert
.assertTrue(!progressMonitorService
.isMonitorThreadRunning(monitorUuid
));