summaryrefslogtreecommitdiff
path: root/src/test/java/simulator/SimpleRaftGUITest.java
blob: 3697db27723c06d49d087368ad9125ece8d4d8ce (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
package simulator;

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

import core.*;
import prefs.*;
import events.*;
import serialize.VSSerialize;

import java.io.File;
import java.lang.reflect.*;

/**
 * Simple GUI test for Raft simulation to verify it loads and runs.
 */
public class SimpleRaftGUITest {
    
    @Test
    @DisplayName("Test loading Raft simulation file")
    public void testLoadRaftSimulation() throws Exception {
        // Initialize
        VSDefaultPrefs prefs = new VSDefaultPrefs();
        prefs.fillWithDefaults();
        VSRegisteredEvents.init(prefs);
        
        // Check if simulation file exists
        File simFile = new File("saved-simulations/raft-working.dat");
        assertTrue(simFile.exists(), "Raft simulation file should exist");
        
        // Load simulation
        VSSimulatorFrame frame = new VSSimulatorFrame(prefs, null);
        VSSerialize serialize = new VSSerialize();
        VSSimulator simulator = serialize.openSimulator(simFile.getAbsolutePath(), frame);
        
        assertNotNull(simulator, "Simulator should be loaded");
        
        // Access visualization
        Field vizField = VSSimulator.class.getDeclaredField("simulatorVisualization");
        vizField.setAccessible(true);
        VSSimulatorVisualization viz = (VSSimulatorVisualization) vizField.get(simulator);
        
        // Verify basic properties
        assertTrue(viz.getNumProcesses() >= 5, "Should have at least 5 processes");
        
        // Check task manager
        VSTaskManager taskManager = viz.getTaskManager();
        assertNotNull(taskManager, "Task manager should exist");
        
        // Get task count using reflection
        Field tasksField = VSTaskManager.class.getDeclaredField("tasks");
        tasksField.setAccessible(true);
        Object taskQueue = tasksField.get(taskManager);
        Method sizeMethod = taskQueue.getClass().getMethod("size");
        int taskCount = (Integer) sizeMethod.invoke(taskQueue);
        
        assertTrue(taskCount > 0, "Should have scheduled tasks");
        
        frame.dispose();
        
        System.out.println("\n=== Test Results ===");
        System.out.println("✓ Raft simulation loads successfully");
        System.out.println("✓ Processes: " + viz.getNumProcesses());
        System.out.println("✓ Scheduled tasks: " + taskCount);
    }
}