Skip to content

Commit 7474939

Browse files
committed
Fixed test coverage issues and reorganized files to adhere to the Single Responsibility Principle.
1 parent 60a67c2 commit 7474939

File tree

12 files changed

+543
-127
lines changed

12 files changed

+543
-127
lines changed

FiniteStateMachine/pom.xml

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,17 @@
1111

1212
<artifactId>FiniteStateMachine</artifactId>
1313

14-
1514
<properties>
1615
<maven.compiler.source>17</maven.compiler.source>
1716
<maven.compiler.target>17</maven.compiler.target>
1817
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1918
</properties>
2019
<dependencies>
2120
<dependency>
22-
<groupId>org.mockito</groupId>
23-
<artifactId>mockito-core</artifactId>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<scope>test</scope>
2424
</dependency>
2525
</dependencies>
2626

27-
<build>
28-
<plugins>
29-
<plugin>
30-
<groupId>org.jacoco</groupId>
31-
<artifactId>jacoco-maven-plugin</artifactId>
32-
<version>0.8.7</version>
33-
<executions>
34-
<execution>
35-
<goals>
36-
<goal>prepare-agent</goal>
37-
<goal>report</goal>
38-
</goals>
39-
</execution>
40-
</executions>
41-
</plugin>
42-
</plugins>
43-
</build>
44-
4527
</project>

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

Lines changed: 0 additions & 106 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package trafficlight;
2+
3+
4+
/**
5+
* Concrete state representing the Green Light.
6+
*/
7+
public class GreenLightState implements TrafficLightState {
8+
@Override
9+
public void handleEvent(TrafficLightContext context) {
10+
System.out.println("Green Light: Go!");
11+
context.setState(new YellowLightState());
12+
}
13+
}
14+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package trafficlight;
2+
/**
3+
* Concrete state representing the Red Light.
4+
*/
5+
public class RedLightState implements TrafficLightState {
6+
@Override
7+
public void handleEvent(TrafficLightContext context) {
8+
System.out.println("Red Light: Stop!");
9+
context.setState(new GreenLightState());
10+
}
11+
}
12+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package trafficlight;
2+
3+
/**
4+
* Context class for managing the current state and transitions.
5+
*/
6+
public class TrafficLightContext {
7+
private TrafficLightState currentState;
8+
9+
/**
10+
* Initializes the context with the given initial state.
11+
*
12+
* @param initialState the initial state of the traffic light
13+
*/
14+
public TrafficLightContext(TrafficLightState initialState) {
15+
this.currentState = initialState;
16+
}
17+
18+
/**
19+
* Updates the current state of the traffic light.
20+
*
21+
* @param newState the new state to transition to
22+
*/
23+
public void setState(TrafficLightState newState) {
24+
this.currentState = newState;
25+
}
26+
27+
/**
28+
* Handles the current state's event and transitions to the next state.
29+
*/
30+
public void handleEvent() {
31+
currentState.handleEvent(this);
32+
}
33+
34+
/**
35+
* Gets the current state of the traffic light.
36+
* This can be useful for testing purposes.
37+
*/
38+
public TrafficLightState getCurrentState() {
39+
return currentState;
40+
}
41+
}
42+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Package for simulating a traffic light system using states.
3+
*/
4+
package trafficlight;
5+
6+
/**
7+
* Simulates a traffic light system using a Finite State Machine (FSM).
8+
*/
9+
public class TrafficLightFsm {
10+
11+
/**
12+
* Runs the traffic light simulation.
13+
*
14+
* @param args command-line arguments (not used here)
15+
*/
16+
public static void main(String[] args) {
17+
// Start with the Red light
18+
TrafficLightContext trafficLight = new TrafficLightContext(new RedLightState());
19+
20+
// Cycle through the traffic light states
21+
for (int i = 0; i < 6; i++) {
22+
trafficLight.handleEvent();
23+
}
24+
}
25+
}
26+
27+
28+
29+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package trafficlight;
2+
3+
/**
4+
* State interface for traffic light states.
5+
*/
6+
public interface TrafficLightState {
7+
8+
/**
9+
* Handles the transition to the next state based on the current state.
10+
*
11+
* @param context the context object that manages the traffic light's state
12+
*/
13+
void handleEvent(TrafficLightContext context);
14+
}
15+
16+
17+
18+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package trafficlight;
2+
3+
/**
4+
* Concrete state representing the Yellow Light.
5+
*/
6+
public class YellowLightState implements TrafficLightState {
7+
@Override
8+
public void handleEvent(TrafficLightContext context) {
9+
System.out.println("Yellow Light: Caution!");
10+
context.setState(new RedLightState());
11+
}
12+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package trafficlight;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Test class for the Traffic Light FSM.
10+
*/
11+
public class TrafficLightTest {
12+
13+
private TrafficLightContext context;
14+
15+
@BeforeEach
16+
void setUp() {
17+
// Start with the Red Light state
18+
context = new TrafficLightContext(new RedLightState());
19+
}
20+
21+
@Test
22+
void testInitialState() {
23+
assertTrue(context.getCurrentState() instanceof RedLightState, "Initial state should be RedLightState.");
24+
}
25+
26+
@Test
27+
void testRedToGreenTransition() {
28+
context.handleEvent();
29+
assertTrue(context.getCurrentState() instanceof GreenLightState, "Red Light should transition to Green Light.");
30+
}
31+
32+
@Test
33+
void testGreenToYellowTransition() {
34+
context.setState(new GreenLightState());
35+
context.handleEvent();
36+
assertTrue(context.getCurrentState() instanceof YellowLightState, "Green Light should transition to Yellow Light.");
37+
}
38+
39+
@Test
40+
void testYellowToRedTransition() {
41+
context.setState(new YellowLightState());
42+
context.handleEvent();
43+
assertTrue(context.getCurrentState() instanceof RedLightState, "Yellow Light should transition to Red Light.");
44+
}
45+
46+
@Test
47+
void testFullCycle() {
48+
context.handleEvent(); // Red -> Green
49+
assertTrue(context.getCurrentState() instanceof GreenLightState);
50+
51+
context.handleEvent(); // Green -> Yellow
52+
assertTrue(context.getCurrentState() instanceof YellowLightState);
53+
54+
context.handleEvent(); // Yellow -> Red
55+
assertTrue(context.getCurrentState() instanceof RedLightState);
56+
}
57+
}

0 commit comments

Comments
 (0)