Package task

Source Code of task.TaskTest

package task;

import java.util.HashMap;

import junit.framework.Assert;

import org.japura.task.Task;
import org.japura.task.TaskExeception;
import org.japura.task.TaskStatus;
import org.japura.task.executors.TaskExecutor;
import org.japura.task.session.TaskSession;
import org.japura.task.ui.TaskExecutionUI;
import org.japura.task.ui.TaskExecutionUIEvent;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class TaskTest{

  @Rule
  public ExpectedException thrown = ExpectedException.none();

  @Test
  public void name() {
  Task task = new Task();
  task.setName("name");
  Assert.assertEquals("name", task.getName());
  task = new Task("name2");
  Assert.assertEquals("name2", task.getName());
  }

  @Test
  public void id() {
  Task task = new Task();
  Assert.assertNotNull(task.getId());
  Assert.assertEquals(task.hashCode(), task.getId().hashCode());
  }

  @Test
  public void equals() {
  Task task = new Task();
  Task task2 = new Task();
  Assert.assertEquals(false, task.equals(null));
  Assert.assertEquals(false, task.equals(""));
  Assert.assertEquals(false, task.equals(task2));
  Assert.assertEquals(true, task.equals(task));
  }

  @Test
  public void initialValues() {
  Task task = new Task();
  Assert.assertEquals(0, task.getBackgroundTimeSpent());
  Assert.assertNull(task.getOwner());
  Assert.assertNull(task.getMessage());
  Assert.assertNull(task.getParentId());
  Assert.assertNull(task.getTaskExecutionUI());
  Assert.assertNull(task.getException());
  Assert.assertEquals("", task.getName());
  Assert.assertEquals(false, task.isWaitForEDT());
  }

  @Test
  public void extra() {
  Task task = new Task();
  task.canceled(new TaskSession());
  Assert.assertNotNull(task.toString());
  }

  @Test
  public void changingMessage() {
  Task task = new Task();
  task.setMessage("message");
  Assert.assertNotNull(task.getMessage());
  Assert.assertEquals("message", task.getMessage());
  }

  @Test
  public void changingWaitForEDT() {
  Task task = new Task();
  task.setWaitForEDT(true);
  Assert.assertEquals(true, task.isWaitForEDT());
  }

  @Test
  public void changingUI() {
  Task task = new Task();
  TaskExecutionUI ui = new TaskExecutionUIFake();
  task.setTaskExecutionUI(ui);
  Assert.assertNotNull(task.getTaskExecutionUI());
  Assert.assertEquals(ui, task.getTaskExecutionUI());
  }

  @Test
  public void registeringOwner1() {
  Task task = new Task();
  TaskExecutorFake executor = new TaskExecutorFake();
  task.registerOwner(executor);
  Assert.assertNotNull(task.getOwner());
  Assert.assertEquals(executor, task.getOwner());
  }

  @Test
  public void registeringOwner2() {
  thrown.expect(TaskExeception.class);
  Task task = new Task();
  task.registerOwner(null);
  }

  @Test
  public void registeringException() {
  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  task.registerStatus(TaskStatus.EXECUTING);
  Exception exception = new Exception();
  task.registerException(exception);
  Assert.assertNotNull(task.getException());
  Assert.assertEquals(exception, task.getException());
  Assert.assertEquals(TaskStatus.ERROR, task.getStatus());
  }

  @Test
  public void submitNestedTask1() {
  Task parentTask = new Task();
  parentTask.setMessage("message");
  TaskExecutorFake executor = new TaskExecutorFake();
  parentTask.registerOwner(executor);
  Task task = new Task();
  parentTask.registerStatus(TaskStatus.SUBMITTED);
  parentTask.registerStatus(TaskStatus.EXECUTING);
  Assert.assertNull(executor.getNestedTasks());
  parentTask.submitNestedTask(task);
  Assert.assertNotNull(executor.getNestedTasks());
  Assert.assertNotNull(task.getParentId());
  Assert.assertEquals(parentTask.getId(), task.getParentId());
  Assert.assertNotNull(task.getMessage());
  Assert.assertEquals("message", task.getMessage());
  }

  @Test
  public void submitNestedTask2() {
  // keeping current message
  Task parentTask = new Task();
  parentTask.setMessage("message");
  parentTask.registerOwner(new TaskExecutorFake());
  Task task = new Task();
  task.setMessage("message2");
  parentTask.registerStatus(TaskStatus.SUBMITTED);
  parentTask.registerStatus(TaskStatus.EXECUTING);
  parentTask.submitNestedTask(task);
  Assert.assertEquals("message2", task.getMessage());
  }

  /**
   * parent Task status = PENDING must be EXECUTING
   */
  @Test
  public void submitNestedTask3() {
  // parent task status =
  thrown.expect(TaskExeception.class);
  Task parentTask = buildTaskWithOwner(); //
  parentTask.submitNestedTask(new Task());
  }

  /**
   * parent Task status = SUBMITTED must be EXECUTING
   */
  @Test
  public void submitNestedTask4() {
  thrown.expect(TaskExeception.class);
  Task parentTask = buildTaskWithOwner();
  parentTask.registerStatus(TaskStatus.SUBMITTED);
  Task task = new Task();
  parentTask.submitNestedTask(task);
  }

  /**
   * parent Task status = ERROR must be EXECUTING
   */
  @Test
  public void submitNestedTask5() {
  thrown.expect(TaskExeception.class);
  Task parentTask = buildTaskWithOwner();
  parentTask.registerStatus(TaskStatus.SUBMITTED);
  parentTask.registerStatus(TaskStatus.EXECUTING);
  parentTask.registerStatus(TaskStatus.ERROR);
  parentTask.submitNestedTask(new Task());
  }

  /**
   * parent Task status = DONE must be EXECUTING
   */
  @Test
  public void submitNestedTask6() {
  thrown.expect(TaskExeception.class);
  Task parentTask = buildTaskWithOwner();
  parentTask.registerStatus(TaskStatus.SUBMITTED);
  parentTask.registerStatus(TaskStatus.EXECUTING);
  parentTask.registerStatus(TaskStatus.DONE);
  parentTask.submitNestedTask(new Task());
  }

  /**
   * parent Task status = CANCELED must be EXECUTING
   */
  @Test
  public void submitNestedTask7() {
  thrown.expect(TaskExeception.class);
  Task parentTask = buildTaskWithOwner();
  parentTask.registerStatus(TaskStatus.SUBMITTED);
  parentTask.registerStatus(TaskStatus.CANCELED);
  parentTask.submitNestedTask(new Task());
  }

  @Test
  public void submitNestedTask8() {
  thrown.expect(TaskExeception.class);

  Task parentTask = buildExecutingTask();

  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  parentTask.submitNestedTask(task);
  }

  @Test
  public void submitNestedTask9() {
  thrown.expect(TaskExeception.class);

  Task parentTask = buildExecutingTask();

  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  task.registerStatus(TaskStatus.EXECUTING);
  parentTask.submitNestedTask(task);
  }

  @Test
  public void submitNestedTask10() {
  Task parentTask = buildExecutingTask();

  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  task.registerStatus(TaskStatus.EXECUTING);
  task.registerStatus(TaskStatus.ERROR);
  parentTask.submitNestedTask(task);
  }

  @Test
  public void submitNestedTask11() {
  Task parentTask = buildExecutingTask();

  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  task.registerStatus(TaskStatus.EXECUTING);
  task.registerStatus(TaskStatus.DONE);
  parentTask.submitNestedTask(task);
  }

  @Test
  public void submitNestedTask12() {
  Task parentTask = buildExecutingTask();

  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  task.registerStatus(TaskStatus.CANCELED);
  parentTask.submitNestedTask(task);
  }

  @Test
  public void cancelingTask1() {
  Task task = new Task();
  task.cancel();
  Assert.assertEquals(TaskStatus.CANCELED, task.getStatus());
  }

  @Test
  public void cancelingTask2() {
  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  task.cancel();
  Assert.assertEquals(TaskStatus.CANCELED, task.getStatus());
  }

  @Test
  public void cancelingTask3() {
  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  task.registerStatus(TaskStatus.EXECUTING);
  task.cancel();
  Assert.assertEquals(TaskStatus.EXECUTING, task.getStatus());
  }

  @Test
  public void cancelingTask4() {
  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  task.registerStatus(TaskStatus.EXECUTING);
  task.registerStatus(TaskStatus.DONE);
  task.cancel();
  Assert.assertEquals(TaskStatus.DONE, task.getStatus());
  }

  @Test
  public void cancelingTask5() {
  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  task.registerStatus(TaskStatus.EXECUTING);
  task.registerStatus(TaskStatus.ERROR);
  task.cancel();
  Assert.assertEquals(TaskStatus.ERROR, task.getStatus());
  }

  @Test
  public void cancelingTask6() {
  Task task = new Task();
  task.registerStatus(TaskStatus.SUBMITTED);
  task.registerStatus(TaskStatus.DISCARDED);
  task.cancel();
  Assert.assertEquals(TaskStatus.DISCARDED, task.getStatus());
  }

  private Task buildExecutingTask() {
  Task task = new Task();
  task.registerOwner(new TaskExecutorFake());
  task.registerStatus(TaskStatus.SUBMITTED);
  task.registerStatus(TaskStatus.EXECUTING);
  return task;
  }

  private Task buildTaskWithOwner() {
  Task task = new Task();
  task.registerOwner(new TaskExecutorFake());
  return task;
  }

  private static class TaskExecutorFake implements TaskExecutor{

  private Task[] nestedTasks;
  private TaskExecutor owner;

  @Override
  public void submitTask(Task... tasks) {}

  @Override
  public void submitTask(boolean isParallel, Task... tasks) {}

  @Override
  public void submitNestedTask(Task... tasks) {
    this.nestedTasks = tasks;
  }

  public Task[] getNestedTasks() {
    return nestedTasks;
  }

  @Override
  public void cancelTasks() {}

  @Override
  public HashMap<String, Object> getHandlerExceptionParameters(Task task) {
    return new HashMap<String, Object>();
  }

  @Override
  public boolean hasTask() {
    return false;
  }

  @Override
  public boolean isShutdown() {
    return false;
  }

  @Override
  public void shutdown() {}

  @Override
  public void registerOwner(TaskExecutor owner) {
    this.owner = owner;
  }

  }

  private class TaskExecutionUIFake implements TaskExecutionUI{

  @Override
  public void submitted(TaskExecutionUIEvent event) {}

  @Override
  public void beforeExecute(TaskExecutionUIEvent event) {}

  @Override
  public void afterExecute(TaskExecutionUIEvent event) {}

  }

}
TOP

Related Classes of task.TaskTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.