Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9299923f0c57861b21527779af2f6a880d70a673 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2010 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.executor.admin.internal;

import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.eclipse.osee.executor.admin.CancellableCallable;
import org.eclipse.osee.executor.admin.mock.MockEventService;
import org.eclipse.osee.executor.admin.mock.MockExecutionCallback;
import org.eclipse.osee.executor.admin.mock.MockLog;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author Roberto E. Escobar
 */
public class ExecutionCallbackTest {

   @Test
   public void testCallbackOnSuccess() throws Exception {
      ExecutorAdminImpl admin = new ExecutorAdminImpl();
      admin.setLogger(new MockLog());
      admin.setEventService(new MockEventService());
      admin.start(new HashMap<String, Object>());

      final String expected = "Was Called";

      MockExecutionCallback<String> callback = new MockExecutionCallback<String>();
      Callable<String> callable = new Callable<String>() {

         @Override
         public String call() throws Exception {
            return expected;
         }

      };
      Future<String> future = admin.schedule(callable, callback);
      String actual = future.get();

      Assert.assertEquals(expected, actual);

      Assert.assertTrue(callback.wasOnSuccess());
      Assert.assertFalse(callback.wasOnCancelled());
      Assert.assertFalse(callback.wasOnFailure());

      Assert.assertEquals(expected, callback.getResult());
      Assert.assertNull(callback.getThrowable());
   }

   @Test
   public void testCallbackOnFailure() throws Exception {
      ExecutorAdminImpl admin = new ExecutorAdminImpl();
      admin.setLogger(new MockLog());
      admin.setEventService(new MockEventService());
      admin.start(new HashMap<String, Object>());

      final Exception expectedException = new IllegalStateException();
      MockExecutionCallback<String> callback = new MockExecutionCallback<String>();
      Callable<String> callable = new Callable<String>() {

         @Override
         public String call() throws Exception {
            throw expectedException;
         }

      };
      Future<String> future = admin.schedule(callable, callback);

      try {
         future.get();
         Assert.assertTrue("An exception should have been thrown", false);
      } catch (Exception ex) {
         Assert.assertEquals(ExecutionException.class, ex.getClass());
         Assert.assertEquals(expectedException, ex.getCause());
      }

      Assert.assertFalse(callback.wasOnSuccess());
      Assert.assertFalse(callback.wasOnCancelled());
      Assert.assertTrue(callback.wasOnFailure());

      Assert.assertNull(callback.getResult());
      Assert.assertEquals(IllegalStateException.class, callback.getThrowable().getClass());
   }

   @Test
   public void testCallbackOnCancel() throws Exception {
      ExecutorAdminImpl admin = new ExecutorAdminImpl();
      admin.setLogger(new MockLog());
      admin.setEventService(new MockEventService());
      admin.start(new HashMap<String, Object>());

      final String results = "results";

      MockExecutionCallback<String> callback = new MockExecutionCallback<String>();

      TestCancellableCallable callable = new TestCancellableCallable(results);
      Future<String> future = admin.schedule(callable, callback);
      future.cancel(true);

      Assert.assertFalse(callback.wasOnSuccess());
      Assert.assertTrue(callback.wasOnCancelled());
      Assert.assertFalse(callback.wasOnFailure());

      Assert.assertNull(callback.getResult());
      Assert.assertNull(callback.getThrowable());

      Assert.assertEquals(true, callable.isCancelled());
      Assert.assertEquals(true, future.isCancelled());

      try {
         future.get();
         Assert.assertTrue("An exception should have been thrown", false);
      } catch (Exception ex) {
         Assert.assertEquals(CancellationException.class, ex.getClass());
      }
   }

   private class TestCancellableCallable extends CancellableCallable<String> {

      private final String results;

      public TestCancellableCallable(String results) {
         this.results = results;
      }

      @Override
      public String call() throws Exception {
         while (!isCancelled()) {
            checkForCancelled();
            // System.out.println("working...");
         }
         return results;
      }
   }
}

Back to the top