/* 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.camunda.bpm.engine.test.api.mock;

import java.util.HashMap;

import org.camunda.bpm.engine.ProcessEngineException;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.impl.test.PluggableProcessEngineTestCase;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.engine.test.mock.Mocks;

/**
 * @author Tassilo Weidner
 */
public class MocksTest extends PluggableProcessEngineTestCase {

  public void testMethodsOfMocksAPI() {
    //given
    HashMap<String, Object> map = new HashMap<String, Object>();

    for(int i = 0; i < 5; i++) {
      map.put("key" + i, new Object());
    }

    //when
    for(String key : map.keySet()) {
      Mocks.register(key, map.get(key));
    }

    //then
    for(String key : map.keySet()) {
      assertEquals(map.get(key), Mocks.get(key));
    }

    assertEquals(map, Mocks.getMocks());

    Mocks.reset();

    for(String key : map.keySet()) {
      assertNull(Mocks.get(key));
    }

    assertEquals(0, Mocks.getMocks().size());
  }

  @Deployment
  public void testUnknownMethodInCamundaExpressionAppliedOnServiceTask() {
    //given
    Mocks.register("myMock", new Object() {});

    try {
      //when
      runtimeService.startProcessInstanceByKey("mocksTest");
      fail();
    } catch (ProcessEngineException e) {
      //then
      assertTrue(e.getMessage().contains("Unknown method used in expression: #{myMock.testMethod()}."));
    } finally {
      Mocks.reset();
    }
  }

  @Deployment
  public void testCamundaExpressionAppliedOnServiceTask() {
    //given
    Mocks.register("myMock", new Object() {
      public String getTest() {
        return "testValue";
      }

      public void testMethod(DelegateExecution execution, String str) {
        execution.setVariable("testVar", str);
      }
    });

    //when
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("mocksTest");
    runtimeService.setVariable(pi.getId(), "myVar", 42);
    Mocks.reset();

    //then
    assertEquals(42, runtimeService.getVariable(pi.getId(), "myVar"));
    assertEquals("testValue", runtimeService.getVariable(pi.getId(), "testVar"));
  }

  @Deployment
  public void testConditionExpressionAppliedOnExclusiveGateway() {
    //given
    TaskChooser taskChooser = new TaskChooser();
    Mocks.register("myMock", taskChooser);

    //when
    taskChooser.setTask("red");
    ProcessInstance piWithRedTaskChosen = runtimeService.startProcessInstanceByKey("mocksTest");

    taskChooser.setTask("yellow");
    ProcessInstance piWithYellowTaskChosen = runtimeService.startProcessInstanceByKey("mocksTest");

    taskChooser.setTask("green");
    ProcessInstance piWithGreenTaskChosen = runtimeService.startProcessInstanceByKey("mocksTest");

    Mocks.reset();

    //then
    assertEquals("A Red Task", getTaskName(piWithRedTaskChosen.getId()));
    assertEquals("A Yellow Task", getTaskName(piWithYellowTaskChosen.getId()));
    assertEquals("A Green Task", getTaskName(piWithGreenTaskChosen.getId()));
  }

  //helper ////////////////////////////////////////////////////////////
  private String getTaskName(String id) {
    return taskService
            .createTaskQuery()
            .processInstanceId(id)
            .singleResult()
            .getName();
  }

  class TaskChooser {
    private String taskName = null;

    public String getTask() {
      return taskName;
    }

    private void setTask(String taskName) {
      this.taskName = taskName;
    }
  }

}

@@ -677,24 +677,113 @@
 eException;%0A
+import org.camunda.bpm.engine.RuntimeService;%0Aimport org.camunda.bpm.engine.TaskService;%0A
 import org.c
@@ -862,48 +862,78 @@
 ine.
-impl.test.PluggableProcessE
+runtime.ProcessInstance;%0Aimport org.camunda.bpm.e
 ngine
-TestCase
+.test.Deployment
 ;%0Aim
@@ -956,39 +956,36 @@
 .bpm.engine.
-runtime
+test
 .Process
 Instance;%0Aim
@@ -964,39 +964,41 @@
 ine.test.Process
-Instanc
+EngineRul
 e;%0Aimport org.ca
@@ -1015,34 +1015,34 @@
 engine.test.
-Deployment
+mock.Mocks
 ;%0Aimport org
@@ -1070,120 +1070,671 @@
 est.
-mock.Mocks;%0A%0A/**%0A * @author Tassilo Weidner%0A */%0Apublic class MocksTest extends PluggableProcessEngineTestCase %7B%0A
+util.ProvidedProcessEngineRule;%0Aimport org.junit.Before;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0A%0Aimport static junit.framework.TestCase.assertEquals;%0Aimport static junit.framework.TestCase.assertNull;%0Aimport static junit.framework.TestCase.assertTrue;%0Aimport static junit.framework.TestCase.fail;%0A%0A/**%0A * @author Tassilo Weidner%0A */%0Apublic class MocksTest %7B%0A  @Rule%0A  public ProcessEngineRule rule = new ProvidedProcessEngineRule();%0A%0A  protected RuntimeService runtimeService;%0A  protected TaskService taskService;%0A%0A  @Before%0A  public void initServices() %7B%0A    runtimeService = rule.getRuntimeService();%0A    taskService = rule.getTaskService();%0A  %7D%0A%0A  @Test
 %0A  p
@@ -1855,16 +1855,17 @@
 %0A    for
+ 
 (int i =
@@ -1939,32 +1939,33 @@
   //when%0A    for
+ 
 (String key : ma
@@ -2036,32 +2036,33 @@
   //then%0A    for
+ 
 (String key : ma
@@ -2201,16 +2201,17 @@
 %0A    for
+ 
 (String 
@@ -2316,32 +2316,40 @@
 ).size());%0A  %7D%0A%0A
+  @Test%0A
   @Deployment%0A  
@@ -2782,24 +2782,32 @@
 %0A    %7D%0A  %7D%0A%0A
+  @Test%0A
   @Deploymen
@@ -3466,16 +3466,24 @@
 );%0A  %7D%0A%0A
+  @Test%0A
   @Deplo

convert MocksTest to JUnit 4