-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartFactoryTest.java
More file actions
68 lines (59 loc) · 2.91 KB
/
Copy pathSmartFactoryTest.java
File metadata and controls
68 lines (59 loc) · 2.91 KB
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
package smartfactory;
import smartfactory.model.Machine;
import smartfactory.model.MachineStatus;
import smartfactory.model.SensorReading;
import smartfactory.service.SmartFactoryService;
/** ทดสอบด้วย Java ล้วนโดยไม่ต้องติดตั้ง JUnit หรือ Maven */
public class SmartFactoryTest {
public static void main(String[] args) {
testSafeReadingSetsRunning();
testHighTemperatureSetsWarning();
testEmergencyTemperatureStopsMachine();
testMaintenanceResetsMachine();
testDuplicateIdIsRejected();
System.out.println("PASS: 5 tests");
}
private static void testSafeReadingSetsRunning() {
Machine machine = new Machine("T-001", "Test Machine", "Test Area");
machine.updateReading(new SensorReading(60, 2));
assertEquals(MachineStatus.RUNNING, machine.getStatus(), "safe reading");
}
private static void testHighTemperatureSetsWarning() {
Machine machine = new Machine("T-002", "Hot Machine", "Test Area");
machine.updateReading(new SensorReading(85, 2));
assertEquals(MachineStatus.WARNING, machine.getStatus(), "high temperature");
assertTrue(machine.requiresMaintenance(), "warning requires maintenance");
}
private static void testEmergencyTemperatureStopsMachine() {
Machine machine = new Machine("T-005", "Emergency Machine", "Test Area");
machine.updateReading(new SensorReading(100.0, 2));
assertEquals(MachineStatus.EMERGENCY_STOP, machine.getStatus(), "emergency temperature");
assertTrue(machine.requiresMaintenance(), "emergency stop requires maintenance");
}
private static void testMaintenanceResetsMachine() {
Machine machine = new Machine("T-003", "Old Machine", "Test Area", MachineStatus.RUNNING, 600);
machine.performMaintenance();
assertEquals(0, machine.getOperatingHours(), "reset hours");
assertEquals(MachineStatus.OFFLINE, machine.getStatus(), "maintenance status");
}
private static void testDuplicateIdIsRejected() {
SmartFactoryService service = new SmartFactoryService();
service.addMachine(new Machine("T-004", "First", "Test Area"));
try {
service.addMachine(new Machine("t-004", "Second", "Test Area"));
throw new AssertionError("duplicate id should fail");
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage().contains("ซ้ำ"), "duplicate error message");
}
}
private static void assertTrue(boolean actual, String message) {
if (!actual) {
throw new AssertionError(message);
}
}
private static void assertEquals(Object expected, Object actual, String message) {
if (!expected.equals(actual)) {
throw new AssertionError(message + " expected=" + expected + " actual=" + actual);
}
}
}