Untitled

Anonymous
java
02/28/2026 9:27 AM
3.9 KB
20
Indexable
package licence;

import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.*;

/**
 * Unit tests for the DrivingLicence class.
 *
 * @author Kangli
 */
public class DrivingLicenceTest {

    /**
     * Tests that a valid driving licence is created successfully.
     */
    @Test
    void createValid() {
        DrivingLicence dl = new DrivingLicence("ABCD123XYZ", "John Wick", LocalDate.of(2030, 5, 1));
        assertEquals("ABCD123XYZ", dl.getLicenceNumber());
        assertEquals("John Wick", dl.getHolderName());
        assertTrue(dl.isValid());
    }

    /**
     * Tests that an invalid licence number throws an exception.
     */
    @Test
    void badNumber() {
        assertThrows(IllegalArgumentException.class, () ->
            new DrivingLicence("bad", "John Wick", LocalDate.now())
        );
    }

    /**
     * Tests that an invalid holder name throws an exception.
     */
    @Test
    void badHolderName() {
        assertThrows(IllegalArgumentException.class, () ->
            new DrivingLicence("ABCD123XYZ", "John123", LocalDate.of(2030, 5, 1))
        );
    }

    /**
     * Tests that a null licence number throws an exception.
     */
    @Test
    void nullLicenceNumber() {
        assertThrows(IllegalArgumentException.class, () ->
            new DrivingLicence(null, "John Wick", LocalDate.of(2030, 5, 1))
        );
    }

    /**
     * Tests that an expired licence is correctly identified as invalid.
     */
    @Test
    void expired() {
        DrivingLicence dl = new DrivingLicence("ABCD123XYZ", "John Wick", LocalDate.of(2020, 1, 1));
        assertFalse(dl.isValid());
    }

    /**
     * Tests that equality and hash code are based on licence number only.
     */
    @Test
    void equalsUsesNumberOnly() {
        DrivingLicence a = new DrivingLicence("ABCD123XYZ", "John Wick", LocalDate.of(2030, 5, 1));
        DrivingLicence b = new DrivingLicence("ABCD123XYZ", "Kawhi Leonard", LocalDate.of(2025, 1, 1));
        assertEquals(a, b);
        assertEquals(a.hashCode(), b.hashCode());
    }

    /**
     * Tests that the three components of the licence number are returned correctly.
     */
    @Test
    void getComponentsOfLicenceNumber() {
        DrivingLicence dl = new DrivingLicence("ABCD123XYZ", "John Wick", LocalDate.of(2030, 5, 1));
        assertEquals("ABCD", dl.getFirstLetters());
        assertEquals("123", dl.getDigits());
        assertEquals("XYZ", dl.getLastLetters());
    }

    /**
     * Tests that a DrivingLicence can be correctly parsed from its string representation.
     */
    @Test
    void parseFromString() {
        DrivingLicence dl = DrivingLicence.valueOf("ABCD123XYZ,John Wick,2030-05-01");
        assertEquals("ABCD123XYZ", dl.getLicenceNumber());
        assertEquals("John Wick", dl.getHolderName());
        assertTrue(dl.isValid());
    }
}
Editor is loading...
Leave a Comment