package infovis.routing.test;

import static org.junit.Assert.*;
import infovis.data.BusData;
import infovis.data.BusStationManager;
import infovis.data.BusTime;
import infovis.routing.RouteFinder;
import infovis.routing.RoutingManager;
import infovis.routing.RoutingManager.CallBack;
import infovis.routing.RoutingResult;

import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.Test;

/**
 * Tests for the {@link RoutingManager} class.
 * 
 * @author Leo Woerteler
 */
public class RoutingManagerTests {

  /**
   * Tests if a new task cancels the old one.
   * 
   * @throws InterruptedException exception
   */
  @Test
  public void terminateTest() throws InterruptedException {
    final CountDownLatch cd = new CountDownLatch(2);
    final AtomicBoolean ref = new AtomicBoolean();
    final RoutingManager rm = RoutingManager.newInstance();
    rm.registerTask(new Callable<Integer>() {
      @Override
      public Integer call() throws Exception {
        try {
          Thread.sleep(1000);
          return 1;
        } catch(final InterruptedException e) {
          ref.set(true);
          throw e;
        } finally {
          cd.countDown();
        }
      }

    }, new RoutingManager.CallBack<Integer>() {
      @Override
      public void callBack(final Integer result) {
        throw new IllegalStateException("should never be executed");
      }
    });

    Thread.sleep(500);

    rm.registerTask(new Callable<Integer>() {
      @Override
      public Integer call() throws Exception {
        return 2;
      }

    }, new RoutingManager.CallBack<Integer>() {
      @Override
      public void callBack(final Integer result) {
        cd.countDown();
      }
    });

    cd.await();
    assertTrue(ref.get());
  }

  /**
   * Tests if all routes are found.
   * 
   * @throws Exception exception
   */
  @Test
  public void findRoutes() throws Exception {
    final BusStationManager man = BusData.load("src/main/resources/");
    final RoutingManager rm = RoutingManager.newInstance();
    final Semaphore sem = new Semaphore(0);
    final AtomicReference<Collection<RoutingResult>> ref =
        new AtomicReference<Collection<RoutingResult>>();

    rm.findRoutes(man.getForId(1), null, new BusTime(12, 00), 1, 24 * 60,
        new RouteFinder(), new CallBack<Collection<RoutingResult>>() {
      @Override
      public void callBack(final Collection<RoutingResult> result) {
        ref.set(result);
        sem.release();
      }
    });

    sem.acquire();
    assertEquals(122, ref.get().size());
  }
}

@@ -2742,10 +2742,1414 @@
 ));%0A  %7D%0A
+%0A  /**%0A   * Tests if a new task cancels routing.%0A   * %0A   * @throws Exception exception%0A   */%0A  @Test%0A  public void terminateRouting() throws Exception %7B%0A    final BusStationManager man = BusData.load(%22src/main/resources/%22);%0A    final CountDownLatch cd = new CountDownLatch(2);%0A    final AtomicBoolean ref = new AtomicBoolean(false);%0A    final RoutingManager rm = RoutingManager.newInstance();%0A    rm.registerTask(new Callable%3CCollection%3CRoutingResult%3E%3E() %7B%0A      @Override%0A      public Collection%3CRoutingResult%3E call() throws InterruptedException %7B%0A        cd.countDown();%0A        final Collection%3CRoutingResult%3E routes = new RouteFinder().findRoutes(%0A            man.getForId(1), null, new BusTime(12, 00),%0A            1, 24 * 60);%0A        ref.getAndSet(true); // should not exit normally%0A        return routes;%0A      %7D%0A    %7D, new CallBack%3CCollection%3CRoutingResult%3E%3E() %7B%0A      @Override%0A      public void callBack(final Collection%3CRoutingResult%3E result) %7B%0A        throw new IllegalStateException(%22should not come here%22);%0A      %7D%0A    %7D);%0A%0A    Thread.sleep(50);%0A%0A    rm.registerTask(new Callable%3CInteger%3E() %7B%0A      @Override%0A      public Integer call() throws Exception %7B%0A        return 0;%0A      %7D%0A%0A    %7D, new RoutingManager.CallBack%3CInteger%3E() %7B%0A      @Override%0A      public void callBack(final Integer result) %7B%0A        cd.countDown();%0A      %7D%0A    %7D);%0A%0A    cd.await();%0A    assertFalse(ref.get());%0A  %7D%0A%0A
 %7D%0A

test case for interruption check