/** * Tests whether overriding a running bank creates an interruption and starts on the same bank * * @throws InterruptedException */ @Test(timeOut = 1000) public void testModifyRunningBank() throws InterruptedException { World mockWorld = mock(World.class); Player mockPlayer = mock(Player.class); Delayer mockDelayer = mock(Delayer.class); final Semaphore mutex = new Semaphore(0); Bank initial = new Bank() { @Override public void run() throws BankInterruptedException { synchronized (mutex) { mutex.release(); try { mutex.wait(); // block to allow main thread to swap bank control.scan(); // this is necessary so that the robot realizes it was // interrupted } catch (InterruptedException exc) { System.out.println("error"); } } fail("Should have changed bank"); } }; initial.setTeamId(123); Robot TU = new Robot( mockWorld, mockDelayer, new Bank[] {initial, initial}, "Unit test robot", mockPlayer); TU.setEventDispatcher(mock(EventDispatcher.class)); TU.getData().setActiveState(1); Direction original = TU.getData().getFacing(); Thread firstThread = new Thread(TU); firstThread.start(); mutex.acquire(); // block until bank executes synchronized (mutex) { // robot is now wait()ing ChangerBank otherBank = new ChangerBank(); // this bank changes the direction of the // robot otherBank.setTeamId(311); TU.setBank(otherBank, 0, true); // change the running bank mutex.notifyAll(); } firstThread.join(); assertNotEquals(original, TU.getData().getFacing(), "Overwriten bank didn't execute"); }
/** Tests setting a normal bank */ @Test public void testSetBank() { World mockWorld = mock(World.class); Player mockPlayer = mock(Player.class); Delayer delayer = mock(Delayer.class); Robot TU = new Robot(mockWorld, delayer, new Bank[] {null, null}, "Test Robot", mockPlayer); Bank testBank = mock(Bank.class); TU.setBank(testBank, 0, false); assertSame(TU.getBank(0), testBank, "Bank set was the same"); }
/** Tests setting a null bank */ @Test public void testSetNullBank() { World mockWorld = mock(World.class); Player mockPlayer = mock(Player.class); Delayer delayer = mock(Delayer.class); Robot TU = new Robot( mockWorld, delayer, new Bank[] {new ChangerBank(), new ChangerBank()}, "Test Robot", mockPlayer); TU.setBank(null, 0, false); assertNull(TU.getBank(0), "Bank set was the same"); }