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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
package core;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import events.VSAbstractEvent;
import events.implementations.VSProcessCrashEvent;
import events.implementations.VSProcessRecoverEvent;
import events.internal.VSMessageReceiveEvent;
import events.internal.VSProtocolEvent;
import exceptions.VSEventNotCopyableException;
import prefs.VSPrefs;
import protocols.VSAbstractProtocol;
import serialize.VSSerialize;
/**
* Unit tests for VSTask class.
* Tests task creation, execution, comparison, and serialization functionality.
*
* @author Test Suite
*/
class VSTaskTest {
@Mock
private VSInternalProcess mockProcess;
@Mock
private VSAbstractEvent mockEvent;
@Mock
private VSPrefs mockPrefs;
@Mock
private VSAbstractProtocol mockProtocol;
private VSTask task;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
when(mockProcess.getPrefs()).thenReturn(mockPrefs);
when(mockPrefs.getString("lang.task")).thenReturn("Task");
when(mockProcess.getProcessID()).thenReturn(1);
}
@Test
@DisplayName("Test VSTask constructor with local timing")
void testConstructorLocalTiming() {
// Given
long taskTime = 1000L;
// When
task = new VSTask(taskTime, mockProcess, mockEvent, VSTask.LOCAL);
// Then
assertEquals(taskTime, task.getTaskTime());
assertEquals(mockProcess, task.getProcess());
assertEquals(mockEvent, task.getEvent());
assertFalse(task.isGlobalTimed());
assertTrue(task.getTaskNum() > 0);
}
@Test
@DisplayName("Test VSTask constructor with global timing")
void testConstructorGlobalTiming() {
// Given
long taskTime = 2000L;
// When
task = new VSTask(taskTime, mockProcess, mockEvent, VSTask.GLOBAL);
// Then
assertEquals(taskTime, task.getTaskTime());
assertTrue(task.isGlobalTimed());
}
@Test
@DisplayName("Test VSTask constructor with negative time")
void testConstructorNegativeTime() {
// Given
long negativeTime = -100L;
// When
task = new VSTask(negativeTime, mockProcess, mockEvent, VSTask.LOCAL);
// Then
assertEquals(0L, task.getTaskTime()); // Should be set to 0
}
@Test
@DisplayName("Test VSTask copy constructor with copyable event")
void testCopyConstructorWithCopyableEvent() throws VSEventNotCopyableException {
// Given
VSTask originalTask = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
VSAbstractEvent mockCopyEvent = mock(VSAbstractEvent.class);
when(mockEvent.getCopy()).thenReturn(mockCopyEvent);
// When
VSTask copiedTask = new VSTask(originalTask);
// Then
assertEquals(originalTask.getTaskTime(), copiedTask.getTaskTime());
assertEquals(originalTask.getProcess(), copiedTask.getProcess());
assertEquals(mockCopyEvent, copiedTask.getEvent());
assertEquals(originalTask.isGlobalTimed(), copiedTask.isGlobalTimed());
}
@Test
@DisplayName("Test VSTask copy constructor with non-copyable event")
void testCopyConstructorWithNonCopyableEvent() throws VSEventNotCopyableException {
// Given
VSTask originalTask = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
when(mockEvent.getCopy()).thenThrow(new VSEventNotCopyableException("Not copyable"));
// When
VSTask copiedTask = new VSTask(originalTask);
// Then
assertEquals(originalTask.getEvent(), copiedTask.getEvent()); // Should use original event
}
@Test
@DisplayName("Test isProgrammed getter and setter")
void testIsProgrammedGetterSetter() {
// Given
task = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
// When/Then
assertFalse(task.isProgrammed()); // Default value
task.isProgrammed(true);
assertTrue(task.isProgrammed());
task.isProgrammed(false);
assertFalse(task.isProgrammed());
}
@Test
@DisplayName("Test hasInternalEvent method")
void testHasInternalEvent() {
// Given
VSAbstractEvent normalEvent = mock(VSAbstractEvent.class);
VSMessageReceiveEvent internalEvent = mock(VSMessageReceiveEvent.class);
// Setup mocks to return correct values
when(normalEvent.isInternalEvent()).thenReturn(false);
when(internalEvent.isInternalEvent()).thenReturn(true);
// When/Then
task = new VSTask(1000L, mockProcess, normalEvent, VSTask.LOCAL);
assertFalse(task.hasInternalEvent());
task = new VSTask(1000L, mockProcess, internalEvent, VSTask.LOCAL);
assertTrue(task.hasInternalEvent());
}
@Test
@DisplayName("Test hasMessageReceiveEvent method")
void testHasMessageReceiveEvent() {
// Given
VSMessageReceiveEvent messageEvent = mock(VSMessageReceiveEvent.class);
when(messageEvent.isMessageReceiveEvent()).thenReturn(true);
// When
task = new VSTask(1000L, mockProcess, messageEvent, VSTask.LOCAL);
// Then
assertTrue(task.hasMessageReceiveEvent());
}
@Test
@DisplayName("Test hasProcessRecoverEvent method")
void testHasProcessRecoverEvent() {
// Given
VSProcessRecoverEvent recoverEvent = mock(VSProcessRecoverEvent.class);
when(recoverEvent.isProcessRecoverEvent()).thenReturn(true);
// When
task = new VSTask(1000L, mockProcess, recoverEvent, VSTask.LOCAL);
// Then
assertTrue(task.hasProcessRecoverEvent());
}
@Test
@DisplayName("Test isProtocol method")
void testIsProtocol() {
// Given
when(mockProtocol.equals(mockProtocol)).thenReturn(true);
// When
task = new VSTask(1000L, mockProcess, mockProtocol, VSTask.LOCAL);
// Then
assertTrue(task.isProtocol(mockProtocol));
VSAbstractProtocol otherProtocol = mock(VSAbstractProtocol.class);
assertFalse(task.isProtocol(otherProtocol));
}
@Test
@DisplayName("Test timeOver method for local timed task")
void testTimeOverLocalTimed() {
// Given
task = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
// When/Then
when(mockProcess.getTime()).thenReturn(500L);
assertFalse(task.timeOver());
when(mockProcess.getTime()).thenReturn(1001L);
assertTrue(task.timeOver());
}
@Test
@DisplayName("Test timeOver method for global timed task")
void testTimeOverGlobalTimed() {
// Given
task = new VSTask(1000L, mockProcess, mockEvent, VSTask.GLOBAL);
// When/Then
when(mockProcess.getGlobalTime()).thenReturn(500L);
assertFalse(task.timeOver());
when(mockProcess.getGlobalTime()).thenReturn(1001L);
assertTrue(task.timeOver());
}
@Test
@DisplayName("Test equals method")
void testEquals() {
// Given
VSTask task1 = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
VSTask task2 = new VSTask(2000L, mockProcess, mockEvent, VSTask.GLOBAL);
VSTask task3 = new VSTask(task1); // Copy constructor ensures different taskNum
// When/Then
assertTrue(task1.equals(task1)); // Same object
assertFalse(task1.equals(task2)); // Different task nums
assertFalse(task1.equals(task3)); // Different task nums even though copied
}
@Test
@DisplayName("Test isProcess method")
void testIsProcess() {
// Given
task = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
VSInternalProcess otherProcess = mock(VSInternalProcess.class);
when(mockProcess.equals(mockProcess)).thenReturn(true);
when(mockProcess.equals(otherProcess)).thenReturn(false);
// When/Then
assertTrue(task.isProcess(mockProcess));
assertFalse(task.isProcess(otherProcess));
}
@Test
@DisplayName("Test run method initializes event and calls onStart")
void testRunMethod() {
// Given
task = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
when(mockEvent.getProcess()).thenReturn(null);
when(mockEvent.shouldIncreaseTimestamps()).thenReturn(true);
// When
task.run();
// Then
verify(mockEvent).init(mockProcess);
verify(mockProcess).increaseVectorAndLamportTimeIfAll();
verify(mockEvent).onStart();
}
@Test
@DisplayName("Test run method with MessageReceiveEvent does not increase time")
void testRunMethodWithMessageReceiveEvent() {
// Given
VSMessageReceiveEvent messageEvent = mock(VSMessageReceiveEvent.class);
task = new VSTask(1000L, mockProcess, messageEvent, VSTask.LOCAL);
when(messageEvent.getProcess()).thenReturn(mockProcess);
when(messageEvent.shouldIncreaseTimestamps()).thenReturn(false);
// When
task.run();
// Then
verify(mockProcess, never()).increaseVectorAndLamportTimeIfAll();
verify(messageEvent).onStart();
}
@Test
@DisplayName("Test run method with Protocol does not increase time")
void testRunMethodWithProtocol() {
// Given
task = new VSTask(1000L, mockProcess, mockProtocol, VSTask.LOCAL);
when(mockProtocol.getProcess()).thenReturn(mockProcess);
when(mockProtocol.shouldIncreaseTimestamps()).thenReturn(false);
// When
task.run();
// Then
verify(mockProcess, never()).increaseVectorAndLamportTimeIfAll();
verify(mockProtocol).onStart();
}
@Test
@DisplayName("Test setTaskTime method")
void testSetTaskTime() {
// Given
task = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
// When
task.setTaskTime(2000L);
// Then
assertEquals(2000L, task.getTaskTime());
}
@Test
@DisplayName("Test setProcess method with different process")
void testSetProcessWithDifferentProcess() throws VSEventNotCopyableException {
// Given
task = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
VSInternalProcess newProcess = mock(VSInternalProcess.class);
VSAbstractEvent copiedEvent = mock(VSAbstractEvent.class);
when(mockProcess.equals(newProcess)).thenReturn(false);
when(mockEvent.getCopy(newProcess)).thenReturn(copiedEvent);
// When
task.setProcess(newProcess);
// Then
assertEquals(newProcess, task.getProcess());
assertEquals(copiedEvent, task.getEvent());
}
@Test
@DisplayName("Test setProcess method with same process")
void testSetProcessWithSameProcess() throws VSEventNotCopyableException {
// Given
task = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
when(mockProcess.equals(mockProcess)).thenReturn(true);
// When
task.setProcess(mockProcess);
// Then
verify(mockEvent, never()).getCopy(any());
}
@Test
@DisplayName("Test setProcess method with non-copyable protocol")
void testSetProcessWithNonCopyableProtocol() throws VSEventNotCopyableException {
// Given
task = new VSTask(1000L, mockProcess, mockProtocol, VSTask.LOCAL);
VSInternalProcess newProcess = mock(VSInternalProcess.class);
VSAbstractProtocol newProtocol = mock(VSAbstractProtocol.class);
when(mockProcess.equals(newProcess)).thenReturn(false);
when(mockProtocol.getCopy(newProcess)).thenThrow(new VSEventNotCopyableException("Not copyable"));
when(mockProtocol.getClassname()).thenReturn("test.Protocol");
when(mockProtocol.getShortname()).thenReturn("TEST");
when(newProcess.getProtocolObject("test.Protocol")).thenReturn(newProtocol);
// When
task.setProcess(newProcess);
// Then
assertEquals(newProcess, task.getProcess());
assertEquals(newProtocol, task.getEvent());
verify(newProtocol).setShortname("TEST");
}
@Test
@DisplayName("Test toString method")
void testToString() {
// Given
task = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
when(mockEvent.toString()).thenReturn("TestEvent");
when(mockProcess.getProcessID()).thenReturn(42);
// When
String result = task.toString();
// Then
assertTrue(result.contains("Task"));
assertTrue(result.contains("1000"));
assertTrue(result.contains("TestEvent"));
assertTrue(result.contains("PID: 42"));
}
@Test
@DisplayName("Test compareTo method with different task times")
void testCompareToWithDifferentTimes() {
// Given
VSTask task1 = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
VSTask task2 = new VSTask(2000L, mockProcess, mockEvent, VSTask.LOCAL);
// When/Then
assertTrue(task1.compareTo(task2) < 0);
assertTrue(task2.compareTo(task1) > 0);
assertEquals(0, task1.compareTo(task1));
}
@Test
@DisplayName("Test compareTo method prioritizes ProcessRecoverEvent")
void testCompareToProcessRecoverEventPriority() {
// Given
VSProcessRecoverEvent recoverEvent = mock(VSProcessRecoverEvent.class);
when(recoverEvent.getEventPriority()).thenReturn(-3); // Highest priority
when(mockEvent.getEventPriority()).thenReturn(0); // Normal priority
VSTask recoverTask = new VSTask(1000L, mockProcess, recoverEvent, VSTask.LOCAL);
VSTask normalTask = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
// When/Then
assertTrue(recoverTask.compareTo(normalTask) < 0);
assertTrue(normalTask.compareTo(recoverTask) > 0);
}
@Test
@DisplayName("Test compareTo method prioritizes ProcessCrashEvent second")
void testCompareToProcessCrashEventPriority() {
// Given
VSProcessCrashEvent crashEvent = mock(VSProcessCrashEvent.class);
when(crashEvent.getEventPriority()).thenReturn(-2); // Second highest priority
when(mockEvent.getEventPriority()).thenReturn(0); // Normal priority
VSTask crashTask = new VSTask(1000L, mockProcess, crashEvent, VSTask.LOCAL);
VSTask normalTask = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
// When/Then
assertTrue(crashTask.compareTo(normalTask) < 0);
assertTrue(normalTask.compareTo(crashTask) > 0);
}
@Test
@DisplayName("Test compareTo method prioritizes ProtocolEvent third")
void testCompareToProtocolEventPriority() {
// Given
VSProtocolEvent protocolEvent = mock(VSProtocolEvent.class);
when(protocolEvent.getEventPriority()).thenReturn(-1); // Third highest priority
when(mockEvent.getEventPriority()).thenReturn(0); // Normal priority
VSTask protocolTask = new VSTask(1000L, mockProcess, protocolEvent, VSTask.LOCAL);
VSTask normalTask = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
// When/Then
assertTrue(protocolTask.compareTo(normalTask) < 0);
assertTrue(normalTask.compareTo(protocolTask) > 0);
}
@Test
@DisplayName("Test compareTo method with null shortnames")
void testCompareToWithNullShortnames() {
// Given
VSTask task1 = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
VSTask task2 = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
when(mockEvent.getShortname()).thenReturn(null);
// When/Then
assertEquals(0, task1.compareTo(task2));
}
@Test
@DisplayName("Test compareTo method with non-VSTask object")
void testCompareToWithNonTaskObject() {
// Given
task = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL);
Object notATask = new Object();
// When/Then
assertEquals(0, task.compareTo(notATask));
}
@Test
@DisplayName("Test serialization and deserialization")
void testSerializationDeserialization() throws Exception {
// Given
task = new VSTask(1500L, mockProcess, mockEvent, VSTask.GLOBAL);
task.isProgrammed(true);
when(mockEvent.getClassname()).thenReturn("test.Event");
when(mockEvent.getID()).thenReturn(123);
when(mockProcess.getProcessNum()).thenReturn(5);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
VSSerialize serialize = mock(VSSerialize.class);
// When - Serialize
task.serialize(serialize, oos);
oos.flush();
// Then - Verify serialization calls
verify(mockEvent).serialize(serialize, oos);
// When - Deserialize
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
// Mock the deserialization environment
when(serialize.getObject(5, "process")).thenReturn(mockProcess);
when(serialize.objectExists(123, "event")).thenReturn(true);
when(serialize.getObject(123, "event")).thenReturn(mockEvent);
VSTask deserializedTask = new VSTask(serialize, ois);
// Then - Verify deserialization
assertNotNull(deserializedTask);
verify(mockEvent).deserialize(serialize, ois);
}
}
|