|
| 1 | +package com.johnlpage.memex.VehicleInspection.model; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import java.util.List; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 6 | + |
| 7 | +class VehicleInspectionTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + void toString_noStackOverflow_whenVehicleHasInspections() { |
| 11 | + VehicleInspection inspection = new VehicleInspection(); |
| 12 | + inspection.setTestid(1L); |
| 13 | + |
| 14 | + Vehicle vehicle = new Vehicle(); |
| 15 | + vehicle.setVehicleid(100L); |
| 16 | + vehicle.setInspections(List.of(inspection)); |
| 17 | + |
| 18 | + inspection.setVehicle(vehicle); |
| 19 | + |
| 20 | + assertDoesNotThrow(() -> inspection.toString()); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void equals_noStackOverflow_whenVehicleHasInspections() { |
| 25 | + VehicleInspection a = new VehicleInspection(); |
| 26 | + a.setTestid(1L); |
| 27 | + VehicleInspection b = new VehicleInspection(); |
| 28 | + b.setTestid(1L); |
| 29 | + |
| 30 | + // Separate Vehicle instances so equals() can't short-circuit on reference equality |
| 31 | + Vehicle vehicleA = new Vehicle(); |
| 32 | + vehicleA.setVehicleid(100L); |
| 33 | + vehicleA.setInspections(List.of(a)); |
| 34 | + |
| 35 | + Vehicle vehicleB = new Vehicle(); |
| 36 | + vehicleB.setVehicleid(100L); |
| 37 | + vehicleB.setInspections(List.of(b)); |
| 38 | + |
| 39 | + a.setVehicle(vehicleA); |
| 40 | + b.setVehicle(vehicleB); |
| 41 | + |
| 42 | + assertDoesNotThrow(() -> a.equals(b)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void hashCode_noStackOverflow_whenVehicleHasInspections() { |
| 47 | + VehicleInspection inspection = new VehicleInspection(); |
| 48 | + inspection.setTestid(1L); |
| 49 | + |
| 50 | + Vehicle vehicle = new Vehicle(); |
| 51 | + vehicle.setVehicleid(100L); |
| 52 | + vehicle.setInspections(List.of(inspection)); |
| 53 | + |
| 54 | + inspection.setVehicle(vehicle); |
| 55 | + |
| 56 | + assertDoesNotThrow(() -> inspection.hashCode()); |
| 57 | + } |
| 58 | +} |
0 commit comments