Example #1
0
  /**
   * 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");
  }