blob: 3004ba21a775290e119827b6d601502200f8142f (
plain)
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
|
package events.implementations;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.*;
import core.VSInternalProcess;
import events.VSAbstractEvent;
import events.VSCopyableEvent;
import prefs.VSPrefs;
import simulator.VSMain;
/**
* Unit tests for VSProcessRecoverEvent.
* Tests the process recovery event functionality including initialization,
* execution, and copying behavior.
*
* @author Test Suite
*/
public class VSProcessRecoverEventTest {
@Mock
private VSInternalProcess mockProcess;
@Mock
private VSPrefs mockPrefs;
@Mock
private VSPrefs mockMainPrefs;
private VSProcessRecoverEvent recoverEvent;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
// Setup mocks
when(mockProcess.getPrefs()).thenReturn(mockPrefs);
when(mockPrefs.getString("lang.recovered")).thenReturn("Process has recovered");
when(mockMainPrefs.getString("lang.process.recover")).thenReturn("Recover");
// Mock VSMain.prefs for shortname creation
VSMain.prefs = mockMainPrefs;
recoverEvent = new VSProcessRecoverEvent();
}
@Test
void testImplementsVSCopyableEvent() {
// Verify that VSProcessRecoverEvent implements VSCopyableEvent
assertTrue(recoverEvent instanceof VSCopyableEvent);
}
@Test
void testOnInit() {
// Test initialization
assertNull(recoverEvent.getClassname());
recoverEvent.onInit();
assertNotNull(recoverEvent.getClassname());
assertTrue(recoverEvent.getClassname().contains("VSProcessRecoverEvent"));
}
@Test
void testCreateShortname() {
// Test shortname creation
String shortname = recoverEvent.createShortname("SavedRecover");
assertEquals("Recover", shortname);
// Test that it uses VSMain.prefs
verify(mockMainPrefs).getString("lang.process.recover");
}
@Test
void testOnStartWhenProcessIsCrashed() {
// Setup process as crashed
when(mockProcess.isCrashed()).thenReturn(true);
// Initialize the event with process
recoverEvent.init(mockProcess);
// Execute the event
recoverEvent.onStart();
// Verify process was recovered
verify(mockProcess).isCrashed(false);
verify(mockProcess).log("Process has recovered");
}
@Test
void testOnStartWhenProcessNotCrashed() {
// Setup process as not crashed (already running)
when(mockProcess.isCrashed()).thenReturn(false);
// Initialize the event with process
recoverEvent.init(mockProcess);
// Execute the event
recoverEvent.onStart();
// Verify process state was not changed
verify(mockProcess, never()).isCrashed(false);
verify(mockProcess, never()).log(anyString());
}
@Test
void testInitCopy() {
// Test the copy initialization
VSProcessRecoverEvent copyEvent = new VSProcessRecoverEvent();
// This method should do nothing for VSProcessRecoverEvent
recoverEvent.initCopy(copyEvent);
// Verify no exceptions thrown and copy is still valid
assertNotNull(copyEvent);
}
@Test
void testFullEventLifecycle() {
// Test complete event lifecycle
// 1. Create and initialize event
VSProcessRecoverEvent event = new VSProcessRecoverEvent();
event.init(mockProcess);
// 2. Verify initialization
assertEquals(mockProcess, event.getProcess());
assertEquals(mockPrefs, event.prefs);
assertNotNull(event.getClassname());
// 3. Setup process state as crashed
when(mockProcess.isCrashed()).thenReturn(true);
// 4. Execute event
event.onStart();
// 5. Verify execution results
verify(mockProcess).isCrashed(false);
verify(mockProcess).log("Process has recovered");
}
@Test
void testEventWithNullProcess() {
// Test behavior when process is not set
recoverEvent.onInit();
// Should throw NullPointerException when accessing process
assertThrows(NullPointerException.class, () -> {
recoverEvent.onStart();
});
}
@Test
void testComplementaryBehaviorWithCrashEvent() {
// Test that recover event properly complements crash event
VSProcessCrashEvent crashEvent = new VSProcessCrashEvent();
// Initialize both events
crashEvent.init(mockProcess);
recoverEvent.init(mockProcess);
// Simulate crash then recover sequence
when(mockProcess.isCrashed()).thenReturn(false).thenReturn(true);
// Crash the process
crashEvent.onStart();
verify(mockProcess).isCrashed(true);
// Recover the process
recoverEvent.onStart();
verify(mockProcess).isCrashed(false);
}
@Test
void testGetCopyFunctionality() {
// Initialize the event
recoverEvent.init(mockProcess);
recoverEvent.setClassname("events.implementations.VSProcessRecoverEvent");
recoverEvent.setShortname("Recover");
// Since this implements VSCopyableEvent, getCopy should work
// (though it will fail in unit test due to VSRegisteredEvents static dependencies)
assertDoesNotThrow(() -> {
try {
recoverEvent.getCopy();
} catch (Exception e) {
// Expected in unit test environment
}
});
}
@Test
void testEventEquality() {
// Test event equality based on ID
VSProcessRecoverEvent event1 = new VSProcessRecoverEvent();
VSProcessRecoverEvent event2 = new VSProcessRecoverEvent();
// Same event should equal itself
assertTrue(event1.equals(event1));
// Different instances have different IDs
assertFalse(event1.equals(event2));
}
@Test
void testLogging() {
// Test logging functionality
recoverEvent.init(mockProcess);
String testMessage = "Test recovery message";
recoverEvent.log(testMessage);
verify(mockProcess).log(testMessage);
}
@Test
void testMultipleRecoveryAttempts() {
// Test multiple recovery attempts on already running process
when(mockProcess.isCrashed()).thenReturn(false);
recoverEvent.init(mockProcess);
// Try to recover multiple times
recoverEvent.onStart();
recoverEvent.onStart();
recoverEvent.onStart();
// Verify no state changes or logs
verify(mockProcess, never()).isCrashed(false);
verify(mockProcess, never()).log(anyString());
}
}
|