Bug 791

Summary: Test Gear Arrive error
Product: Gear and Member Management System for library Reporter: keithlam5
Component: TestAssignee: keithlam5
Status: RESOLVED FIXED    
Severity: enhancement    
Priority: ---    
Version: unspecified   
Hardware: PC   
OS: Windows   

Description keithlam5 2023-11-27 23:20:13 HKT
Actual Result of testExecute() is "Missing record for Gear E2.  Cannot mark this item arrival." 
-----
package org.example;

import org.example.cmd.CmdArrive;
import org.example.day.SystemDate;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.*;

class CmdArriveTest {
    private CmdArrive cmd;
    private Library library;

    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

    @BeforeEach
    public void resetInstance() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Field systemDateInstance = SystemDate.class.getDeclaredField("instance");
        systemDateInstance.setAccessible(true);
        systemDateInstance.set(null, null);

        Field instance = Library.class.getDeclaredField("instance");
        instance.setAccessible(true);
        instance.set(null, null);

        SystemDate.createTheInstance("01-Jan-2020");
        library = Library.getInstance();

        Field field = library.getClass().getDeclaredField("allMembers");
        field.setAccessible(true);
        ArrayList<Member> allMembers = (ArrayList<Member>) field.get(library);
        allMembers.clear();

        Field field2 = library.getClass().getDeclaredField("allGears");
        field2.setAccessible(true);
        ArrayList<Member> allGears = (ArrayList<Member>) field2.get(library);
        allGears.clear();

        System.setOut(new PrintStream(outContent));
        System.setErr(new PrintStream(errContent));
    }


    @BeforeEach
    public void setup() {
        cmd = new CmdArrive();
    }

    
    @Test
    public void testExecute() {
    	cmd.execute(new String[] {"arrive", "E2"});
        String expected =
                """
                Done.
                """;
        assertEquals(expected, outContent.toString());
    }
Comment 1 keithlam5 2023-11-27 23:22:24 HKT
The problem should because we didn't new a Gear class to add the Gear first.
Comment 2 keithlam5 2023-11-27 23:25:44 HKT
Change the test case as follows, new a Gear class to add the E1 Printer first:
----
    @Test
    public void testExecute() {
    	Gear e = new Gear("E1", "Printer");
    	cmd.execute(new String[] {"arrive", "E1"});
        String expected =
                """
                Done.
                """;
        assertEquals(expected, outContent.toString());
    }
---
Also add a test case to test the ExecuteErrorExGearNotFound exception
---
    @Test
    public void testExecuteErrorExGearNotFound()  {
    	String[] args = {"arrive", "E2"};
    	cmd.execute(args);
        String expected =
                """
                Missing record for Gear E2.  Cannot mark this item arrival.
                """;
        assertEquals(expected, outContent.toString());
    }