/** * 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 running a robot that has a null 0 bank */ @Test(dependsOnMethods = {"testFirstRobotCreation"}) public void testDataHunger() { World mockWorld = mock(World.class); Bank[] dummyBanks = new Bank[3]; Delayer delayer = mock(Delayer.class); Player pla = mock(Player.class); Robot TU = new Robot(mockWorld, delayer, dummyBanks, "Test Robot", pla); TU.setEventDispatcher(mock(EventDispatcher.class)); TU.getData().setActiveState(1); TU.run(); verify(mockWorld).remove(TU); }
/** Tests the jump to bank 0 after a bank finished execution */ @Test(dependsOnMethods = {"testFirstRobotCreation"}) public void testReboot() { World mockWorld = mock(World.class); Delayer mockDelayer = mock(Delayer.class); Bank[] dummyBanks = new Bank[3]; Bank first = new Bank() { int repeat = 0; @Override public void run() { if (repeat < 1) { control.changeBank(2); } else { control.die(); } repeat++; } }; first = spy(first); dummyBanks[0] = first; Bank second = mock(Bank.class); // empty bank after which the robot jumps to bank 0 dummyBanks[2] = second; Player pla = mock(Player.class); Robot TU = new Robot(mockWorld, mockDelayer, dummyBanks, "Test Robot", pla); TU.setEventDispatcher(mock(EventDispatcher.class)); TU.getData().setActiveState(1); TU.run(); /*InOrder interleaved seems to not work InOrder banksOrder = inOrder(first, second); banksOrder.verify(first, atLeast(1)).run(); banksOrder.verify(second).run(); banksOrder.verify(first).run(); */ verify(first, times(2)).run(); verify(second).run(); }
/** * Tests whether a robot's thread ends when a robot dies * * @throws InterruptedException */ @Test(timeOut = 1000) public void testRobotClean() throws InterruptedException { World mockWorld = mock(World.class); Player mockPlayer = mock(Player.class); Delayer delayer = mock(Delayer.class); ChangerBank bank = new ChangerBank(); Robot TU = new Robot(mockWorld, delayer, new Bank[] {bank, null}, "Test Robot", mockPlayer); TU.setEventDispatcher(mock(EventDispatcher.class)); // clock.addListener(TU.getSerialNumber()); TU.getData().setActiveState(1); Thread thread = new Thread(TU); thread.start(); while (bank.ticking) { delayer.tick(); } thread.join(); }