package task;
import org.japura.Application;
import org.japura.task.Task;
import org.japura.task.TaskExeception;
import org.japura.task.manager.DefaultTaskManager;
import org.japura.task.session.SessionRequired;
import org.japura.task.session.TaskSession;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class TaskSessionsTest{
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void beforeTest() throws InterruptedException {
DefaultTaskManager taskManager = new DefaultTaskManager();
taskManager.setInvokeTaskMethodsOnEDT(false);
Application.setTaskManager(taskManager);
}
private void waitForTaskExecutions() {
while (Application.getTaskManager().getGlobalExecutor().hasTask()) {
Thread.yield();
}
}
@Test(timeout = 5000)
public void test1() throws InterruptedException {
Application.getTaskManager().getGlobalExecutor().submitTask(new TaskA());
waitForTaskExecutions();
}
@Test(timeout = 5000)
public void test2() throws InterruptedException {
Application.getTaskManager().getGlobalExecutor().submitTask(new TaskC());
waitForTaskExecutions();
}
@Test(timeout = 5000)
public void test3() throws InterruptedException {
Application.getTaskManager().getGlobalExecutor()
.submitTask(new TaskA(), new TaskA());
waitForTaskExecutions();
}
@Test(timeout = 5000)
public void test4() throws InterruptedException {
Application.getTaskManager().getGlobalExecutor()
.submitTask(new TaskC(), new TaskC());
waitForTaskExecutions();
}
@Test(timeout = 5000)
public void test5() throws InterruptedException {
Application.getTaskManager().getGlobalExecutor()
.submitTask(new TaskA(), new TaskC());
waitForTaskExecutions();
}
@Test(timeout = 5000)
public void test6() throws InterruptedException {
Application.getTaskManager().getGlobalExecutor()
.submitTask(new TaskC(), new TaskA());
waitForTaskExecutions();
}
@Test(timeout = 5000)
public void test7() throws InterruptedException {
thrown.expect(TaskExeception.class);
Application.getTaskManager().getGlobalExecutor()
.submitTask(new TaskA(), new TaskB());
waitForTaskExecutions();
}
@Test(timeout = 5000)
public void test8() throws InterruptedException {
thrown.expect(TaskExeception.class);
Application.getTaskManager().getGlobalExecutor()
.submitTask(new TaskC(), new TaskA(), new TaskB());
waitForTaskExecutions();
}
@SessionRequired(sessionClass = TaskSessionA.class)
public static class TaskA extends Task{}
@SessionRequired(sessionClass = TaskSessionB.class)
public static class TaskB extends Task{}
public static class TaskC extends Task{}
public static class TaskSessionA extends TaskSession{}
public static class TaskSessionB extends TaskSession{}
}