Skip to content

Commit 30e652b

Browse files
committed
FSM issue iluwatar#203
1 parent d6cc250 commit 30e652b

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

FiniteStateMachine/pom.xml

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,46 @@
1717
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1818
</properties>
1919

20-
</project>
20+
<dependencies>
21+
<!-- Mockito dependency for mocking objects -->
22+
<dependency>
23+
<groupId>org.mockito</groupId>
24+
<artifactId>mockito-core</artifactId>
25+
<version>5.5.0</version> <!-- Specify the version for Mockito -->
26+
<scope>test</scope>
27+
</dependency>
28+
29+
<!-- JUnit 5 for unit testing -->
30+
<dependency>
31+
<groupId>org.junit.jupiter</groupId>
32+
<artifactId>junit-jupiter-api</artifactId>
33+
<version>5.8.2</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-engine</artifactId>
39+
<version>5.8.2</version>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<!-- JaCoCo for code coverage -->
47+
<plugin>
48+
<groupId>org.jacoco</groupId>
49+
<artifactId>jacoco-maven-plugin</artifactId>
50+
<version>0.8.7</version>
51+
<executions>
52+
<execution>
53+
<goals>
54+
<goal>prepare-agent</goal>
55+
<goal>report</goal>
56+
</goals>
57+
</execution>
58+
</executions>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
</project>

FiniteStateMachine/src/main/java/com/iluwatar/TrafficLightFsm.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* implementation using a traffic light system.
66
*/
77
public class TrafficLightFsm {
8+
89
/**
910
* State interface for traffic light states.
1011
*/
@@ -75,6 +76,14 @@ public void setState(TrafficLightState newState) {
7576
public void handleEvent() {
7677
currentState.handleEvent(this);
7778
}
79+
80+
/**
81+
* Gets the current state of the traffic light.
82+
* This can be useful for testing purposes.
83+
*/
84+
public TrafficLightState getCurrentState() {
85+
return currentState;
86+
}
7887
}
7988

8089
/**
@@ -94,3 +103,4 @@ public static void main(String[] args) {
94103
}
95104

96105

106+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.iluwatar;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.mockito.Mockito.*;
7+
8+
class TrafficLightFsmTest {
9+
10+
private TrafficLightContext context;
11+
private TrafficLightState redState;
12+
private TrafficLightState greenState;
13+
private TrafficLightState yellowState;
14+
15+
@BeforeEach
16+
void setUp() {
17+
// Set up the initial states for testing
18+
redState = mock(RedLightState.class);
19+
greenState = mock(GreenLightState.class);
20+
yellowState = mock(YellowLightState.class);
21+
22+
// Initialize the context with the Red state
23+
context = new TrafficLightContext(redState);
24+
}
25+
26+
@Test
27+
void testRedToGreenStateTransition() {
28+
// Mock the behavior of handleEvent for red state
29+
context.handleEvent(); // Expected transition to green state
30+
verify(redState).handleEvent(context); // Verify that handleEvent was called
31+
assert context.getCurrentState() instanceof GreenLightState; // Assert the next state is Green
32+
}
33+
34+
@Test
35+
void testGreenToYellowStateTransition() {
36+
// Change to green state manually
37+
context.setState(greenState);
38+
context.handleEvent(); // Expected transition to yellow state
39+
verify(greenState).handleEvent(context); // Verify the handleEvent for green state
40+
assert context.getCurrentState() instanceof YellowLightState; // Assert the next state is Yellow
41+
}
42+
43+
@Test
44+
void testYellowToRedStateTransition() {
45+
// Change to yellow state manually
46+
context.setState(yellowState);
47+
context.handleEvent(); // Expected transition to red state
48+
verify(yellowState).handleEvent(context); // Verify the handleEvent for yellow state
49+
assert context.getCurrentState() instanceof RedLightState; // Assert the next state is Red
50+
}
51+
52+
@Test
53+
void testStateTransitionOrder() {
54+
// Test the full cycle: Red -> Green -> Yellow -> Red
55+
context = new TrafficLightContext(new RedLightState());
56+
57+
// Red to Green
58+
context.handleEvent();
59+
assert context.getCurrentState() instanceof GreenLightState;
60+
61+
// Green to Yellow
62+
context.handleEvent();
63+
assert context.getCurrentState() instanceof YellowLightState;
64+
65+
// Yellow to Red
66+
context.handleEvent();
67+
assert context.getCurrentState() instanceof RedLightState;
68+
}
69+
}
70+

0 commit comments

Comments
 (0)