summaryrefslogtreecommitdiff
path: root/src/test/java/events/VSAbstractEventTest.java
blob: 8806f3f8acc3300fd6a3664caf7e60071c036daa (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
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
package events;

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 exceptions.VSEventNotCopyableException;
import prefs.VSPrefs;
import events.implementations.VSProcessCrashEvent;
import events.implementations.VSProcessRecoverEvent;

/**
 * Unit tests for VSAbstractEvent class.
 * Tests the abstract event base class functionality including initialization,
 * copying, serialization, and name management.
 *
 * @author Test Suite
 */
public class VSAbstractEventTest {
    
    @Mock
    private VSInternalProcess mockProcess;
    
    @Mock
    private VSPrefs mockPrefs;
    
    private TestEvent testEvent;
    private CopyableTestEvent copyableTestEvent;
    
    /**
     * Test implementation of VSAbstractEvent for testing abstract methods
     */
    private static class TestEvent extends VSAbstractEvent {
        private boolean initCalled = false;
        private boolean startCalled = false;
        
        @Override
        public void onInit() {
            initCalled = true;
            setClassname(getClass().toString());
        }
        
        @Override
        public void onStart() {
            startCalled = true;
        }
        
        @Override
        protected String createShortname(String savedShortname) {
            return "TestEvent";
        }
        
        public boolean isInitCalled() { return initCalled; }
        public boolean isStartCalled() { return startCalled; }
    }
    
    /**
     * Copyable test event implementation
     */
    private static class CopyableTestEvent extends TestEvent implements VSCopyableEvent {
        private String customData = "original";
        
        @Override
        public void initCopy(VSAbstractEvent copy) {
            if (copy instanceof CopyableTestEvent) {
                ((CopyableTestEvent) copy).customData = this.customData;
            }
        }
        
        public String getCustomData() { return customData; }
        public void setCustomData(String data) { this.customData = data; }
    }
    
    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        when(mockProcess.getPrefs()).thenReturn(mockPrefs);
        
        testEvent = new TestEvent();
        copyableTestEvent = new CopyableTestEvent();
    }
    
    @Test
    void testInitWithProcess() {
        // Test initialization with process
        assertNull(testEvent.process);
        assertNull(testEvent.prefs);
        assertFalse(testEvent.isInitCalled());
        
        testEvent.init(mockProcess);
        
        assertEquals(mockProcess, testEvent.process);
        assertEquals(mockPrefs, testEvent.prefs);
        assertTrue(testEvent.isInitCalled());
        
        // Test that init doesn't run twice
        testEvent.initCalled = false;
        testEvent.init(mockProcess);
        assertFalse(testEvent.isInitCalled());
    }
    
    @Test
    void testInitWithoutProcess() {
        // Test direct init() call
        testEvent.init();
        
        assertTrue(testEvent.isInitCalled());
        assertNull(testEvent.process);
        assertNull(testEvent.prefs);
    }
    
    @Test
    void testOnStart() {
        // Test onStart method
        assertFalse(testEvent.isStartCalled());
        testEvent.onStart();
        assertTrue(testEvent.isStartCalled());
    }
    
    @Test
    void testClassnameHandling() {
        // Test setClassname with "class " prefix
        testEvent.setClassname("class events.TestEvent");
        assertEquals("events.TestEvent", testEvent.getClassname());
        
        // Test setClassname without prefix
        testEvent.setClassname("events.AnotherEvent");
        assertEquals("events.AnotherEvent", testEvent.getClassname());
    }
    
    @Test
    void testGetName() {
        // Mock the static method behavior by setting classname first
        testEvent.setClassname("events.implementations.VSProcessCrashEvent");
        
        // Since VSRegisteredEvents.getNameByClassname is static and not easily mockable,
        // we'll test that getName() delegates properly
        String name = testEvent.getName();
        // The actual name depends on VSRegisteredEvents initialization
        assertNotNull(testEvent.getClassname());
    }
    
    @Test
    void testShortnameHandling() {
        // Test with custom shortname
        testEvent.setShortname("CustomShort");
        assertEquals("CustomShort", testEvent.getShortname());
        
        // Test without custom shortname (falls back to registered)
        TestEvent newEvent = new TestEvent();
        newEvent.setClassname("events.TestEvent");
        // Since this event is not registered, getShortname will return null from VSRegisteredEvents
        String shortname = newEvent.getShortname();
        // The shortname will be null for unregistered events
        assertNull(shortname);
    }
    
    @Test
    void testGetProcess() {
        assertNull(testEvent.getProcess());
        
        testEvent.init(mockProcess);
        assertEquals(mockProcess, testEvent.getProcess());
    }
    
    @Test
    void testLog() {
        testEvent.init(mockProcess);
        
        String message = "Test log message";
        testEvent.log(message);
        
        verify(mockProcess).log(message);
    }
    
    @Test
    void testEquals() {
        TestEvent event1 = new TestEvent();
        TestEvent event2 = new TestEvent();
        
        // Events with same ID should be equal
        assertTrue(event1.equals(event1));
        
        // Different events have different IDs by default
        assertFalse(event1.equals(event2));
    }
    
    @Test
    void testGetCopyForCopyableEvent() throws VSEventNotCopyableException {
        // Setup for copyable event
        copyableTestEvent.init(mockProcess);
        copyableTestEvent.setClassname("events.VSAbstractEventTest$CopyableTestEvent");
        copyableTestEvent.setShortname("CopyableTest");
        copyableTestEvent.setCustomData("modified");
        
        // Since VSRegisteredEvents.createEventInstanceByClassname returns null for unregistered classes,
        // we expect a NullPointerException when trying to copy
        assertThrows(NullPointerException.class, () -> {
            copyableTestEvent.getCopy();
        });
    }
    
    @Test
    void testGetCopyForNonCopyableEvent() {
        // Test non-copyable event throws exception
        testEvent.init(mockProcess);
        testEvent.setClassname("events.VSAbstractEventTest$TestEvent");
        testEvent.setShortname("Test");
        
        assertThrows(VSEventNotCopyableException.class, () -> {
            testEvent.getCopy();
        });
    }
    
    @Test
    void testGetCopyWithDifferentProcess() throws VSEventNotCopyableException {
        VSInternalProcess newProcess = mock(VSInternalProcess.class);
        when(newProcess.getPrefs()).thenReturn(mockPrefs);
        
        copyableTestEvent.init(mockProcess);
        copyableTestEvent.setClassname("events.VSAbstractEventTest$CopyableTestEvent");
        
        // Since VSRegisteredEvents.createEventInstanceByClassname returns null for unregistered classes,
        // we expect a NullPointerException
        assertThrows(NullPointerException.class, () -> {
            copyableTestEvent.getCopy(newProcess);
        });
    }
    
    @Test
    void testGetCopyWithNullProcess() throws VSEventNotCopyableException {
        copyableTestEvent.init(mockProcess);
        copyableTestEvent.setClassname("events.VSAbstractEventTest$CopyableTestEvent");
        
        // When null is passed, it should use the current process
        // Since VSRegisteredEvents.createEventInstanceByClassname returns null for unregistered classes,
        // we expect a NullPointerException
        assertThrows(NullPointerException.class, () -> {
            copyableTestEvent.getCopy(null);
        });
    }
    
    @Test
    void testCreateShortname() {
        // Test the abstract createShortname method implementation
        String shortname = testEvent.createShortname("SavedName");
        assertEquals("TestEvent", shortname);
    }
    
    @Test
    void testRealEventImplementations() {
        // Test with actual event implementations
        VSProcessCrashEvent crashEvent = new VSProcessCrashEvent();
        VSProcessRecoverEvent recoverEvent = new VSProcessRecoverEvent();
        
        // Test that they implement VSCopyableEvent
        assertTrue(crashEvent instanceof VSCopyableEvent);
        assertTrue(recoverEvent instanceof VSCopyableEvent);
        
        // Test initialization
        crashEvent.init(mockProcess);
        recoverEvent.init(mockProcess);
        
        assertNotNull(crashEvent.getClassname());
        assertNotNull(recoverEvent.getClassname());
    }
}