/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.activiti.examples.bpmn.tasklistener;

import java.util.List;

import org.activiti.engine.impl.test.PluggableActivitiTestCase;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.Deployment;
import org.activiti.examples.bpmn.executionlistener.RecorderExecutionListener;
import org.activiti.examples.bpmn.executionlistener.RecorderExecutionListener.RecordedEvent;


/**
 * @author Joram Barrez
 */
public class TaskListenerTest extends PluggableActivitiTestCase {
  
  @Deployment(resources = {"org/activiti/examples/bpmn/tasklistener/TaskListenerTest.bpmn20.xml"})
  public void testTaskCreateListener() {
    runtimeService.startProcessInstanceByKey("taskListenerProcess");
    Task task = taskService.createTaskQuery().singleResult();
    assertEquals("Schedule meeting", task.getName());
    assertEquals("TaskCreateListener is listening!", task.getDescription());
  }
  
  @Deployment(resources = {"org/activiti/examples/bpmn/tasklistener/TaskListenerTest.bpmn20.xml"})
  public void testTaskAssignmentListener() {
    runtimeService.startProcessInstanceByKey("taskListenerProcess");
    Task task = taskService.createTaskQuery().singleResult();
    assertEquals("TaskCreateListener is listening!", task.getDescription());
    
    // Set assignee and check if event is received
    taskService.setAssignee(task.getId(), "kermit");
    task = taskService.createTaskQuery().singleResult();
  assertEquals("TaskAssignmentListener is listening: kermit", task.getDescription());
  }
  
  @Deployment(resources = {"org/activiti/examples/bpmn/tasklistener/TaskListenerTest.bpmn20.xml"})
  public void testTaskCompleteListener() {
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("taskListenerProcess");
    assertEquals(null, runtimeService.getVariable(processInstance.getId(), "greeting"));
    assertEquals(null, runtimeService.getVariable(processInstance.getId(), "expressionValue"));
    
    // Completing first task will change the description 
    Task task = taskService.createTaskQuery().singleResult();
    taskService.complete(task.getId());
    
    assertEquals("Hello from The Process", runtimeService.getVariable(processInstance.getId(), "greeting"));
    assertEquals("Act", runtimeService.getVariable(processInstance.getId(), "shortName"));
  }
  
  @Deployment(resources = {"org/activiti/examples/bpmn/tasklistener/TaskListenerTest.bpmn20.xml"})
  public void testTaskListenerWithExpression() {
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("taskListenerProcess");
    assertEquals(null, runtimeService.getVariable(processInstance.getId(), "greeting2"));
    
    // Completing first task will change the description 
    Task task = taskService.createTaskQuery().singleResult();
    taskService.complete(task.getId());
    
    assertEquals("Write meeting notes", runtimeService.getVariable(processInstance.getId(), "greeting2"));
  }
  
  @Deployment(resources = {"org/activiti/examples/bpmn/tasklistener/TaskListenerTest.bpmn20.xml"})
  public void testAllEventsTaskListener() {
    runtimeService.startProcessInstanceByKey("taskListenerProcess");
    Task task = taskService.createTaskQuery().singleResult();
    
    // Set assignee and complete task
    taskService.setAssignee(task.getId(), "kermit");
    taskService.complete(task.getId());
    
    // Verify the all-listener has received all events
    String eventsReceived = (String) runtimeService.getVariable(task.getProcessInstanceId(), "events");
    assertEquals("create - assignment - complete", eventsReceived);
  }
  
  @Deployment
  public void testTaskListenersOnDelete() {
	  TaskDeleteListener.clear();
	  runtimeService.startProcessInstanceByKey("executionListenersOnDelete");
	  
	  List<Task> tasks = taskService.createTaskQuery().list();
	  assertNotNull(tasks);
	  assertEquals(2, tasks.size());
	  
	  Task task2 = taskService.createTaskQuery().taskName("User Task 2").singleResult();
	  assertNotNull(task2);

	  assertEquals(0, TaskDeleteListener.getCurrentMessages().size());

	  	  
	  taskService.complete(task2.getId());
	  
	  tasks = taskService.createTaskQuery().list();
	  
	  assertNotNull(tasks);
	  assertEquals(0, tasks.size());
	   

	  assertEquals(1, TaskDeleteListener.getCurrentMessages().size());
	  assertEquals("Delete Task Listener executed.", TaskDeleteListener.getCurrentMessages().get(0));

	 
	    }

}

@@ -4216,16 +4216,127 @@
 ployment
+(resources = %7B%22org/activiti/examples/bpmn/tasklistener/TaskListenerTest.testTaskListenersOnDelete.bpmn20.xml%22%7D)
 %0A  publi
@@ -4367,16 +4367,26 @@
 OnDelete
+ByComplete
 () %7B%0A%09  
@@ -5140,14 +5140,1064 @@
 %0A%09 %0A
-
 %09    %7D
+%0A  @Deployment(resources = %7B%22org/activiti/examples/bpmn/tasklistener/TaskListenerTest.testTaskListenersOnDelete.bpmn20.xml%22%7D)%0A  public void testTaskListenersOnDeleteByDeleteProcessInstance() %7B%0A    TaskDeleteListener.clear();%0A    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(%22executionListenersOnDelete%22);%0A    %0A    List%3CTask%3E tasks = taskService.createTaskQuery().list();%0A    assertNotNull(tasks);%0A    assertEquals(2, tasks.size());%0A    %0A    Task task2 = taskService.createTaskQuery().taskName(%22User Task 2%22).singleResult();%0A    assertNotNull(task2);%0A%0A    assertEquals(0, TaskDeleteListener.getCurrentMessages().size());%0A%0A    runtimeService.deleteProcessInstance(processInstance.getProcessInstanceId(), %22%22);%0A%0A    %0A    tasks = taskService.createTaskQuery().list();%0A    %0A    assertNotNull(tasks);%0A    assertEquals(0, tasks.size());%0A     %0A%0A    assertEquals(1, TaskDeleteListener.getCurrentMessages().size());%0A    assertEquals(%22Delete Task Listener executed.%22, TaskDeleteListener.getCurrentMessages().get(0));%0A%0A   %0A      %7D%0A  
 %0A%0A%7D%0A

add test case for check event when task is deleted by deleteProcessInstance