package org.camunda.bpm.engine.test;

import java.util.Date;

import org.camunda.bpm.engine.AuthorizationService;
import org.camunda.bpm.engine.FormService;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.ManagementService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngineDelegate;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.impl.test.TestHelper;
import org.camunda.bpm.engine.impl.util.ClockUtil;
import org.camunda.bpm.extension.engine.test.ProcessEngineExternalResource;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

/**
 * Rewrite of the ProcessEngineRule since TestWatchman is deprecated for junit>4.9.
 *
 * @author Jan Galinski, Holisticon AG (jan.galinski@holisticon.de)
 */
public class ProcessEngineTestWatcher extends ChainedTestRule<ProcessEngineExternalResource, TestWatcher> implements ProcessEngineTestRule {

  /**
   * Holds the deploymentId after execution of @Deployment. can be used to query
   * the {@link RepositoryService}.
   */
  protected String deploymentId;

  private final TestWatcher innerRule;

  /**
   * Use default engine.
   */
  public ProcessEngineTestWatcher(ProcessEngineDelegate processEngineDelegate) {
    super(new ProcessEngineExternalResource(processEngineDelegate));

    this.innerRule = new TestWatcher() {
      @Override
      protected void finished(Description description) {
        TestHelper.annotationDeploymentTearDown(outerRule.getProcessEngine(), deploymentId, description.getTestClass(), description.getMethodName());
        ClockUtil.reset();
      }

      @Override
      protected void starting(Description description) {
        deploymentId = TestHelper.annotationDeploymentSetUp(outerRule.getProcessEngine(), description.getTestClass(), description.getMethodName());
      }
    };
  }


  public ProcessEngineTestWatcher(ProcessEngine processEngine) {
    this(new ProcessEngineDelegate(processEngine));
  }


  @Override
  protected TestWatcher innerRule() {
    return innerRule;
  }


  @Override
  public void setCurrentTime(final Date currentTime) {
    ClockUtil.setCurrentTime(currentTime);
  }

  @Override
  public ProcessEngine getProcessEngine() {
    return outerRule.getProcessEngine();
  }

  @Override
  public String getDeploymentId() {
    return deploymentId;
  }

  @Override
  public RepositoryService getRepositoryService() {
    return outerRule.getRepositoryService();
  }

  @Override
  public RuntimeService getRuntimeService() {
    return outerRule.getRuntimeService();
  }

  @Override
  public FormService getFormService() {
    return outerRule.getFormService();
  }

  @Override
  public TaskService getTaskService() {
    return outerRule.getTaskService();
  }

  @Override
  public HistoryService getHistoryService() {
    return outerRule.getHistoryService();
  }

  @Override
  public IdentityService getIdentityService() {
    return outerRule.getIdentityService();
  }

  @Override
  public ManagementService getManagementService() {
    return outerRule.getManagementService();
  }

  @Override
  public AuthorizationService getAuthorizationService() {
    return outerRule.getAuthorizationService();
  }

}

@@ -857,16 +857,19 @@
 ated for
+%0A *
  junit%3E4
@@ -874,16 +874,17 @@
 %3E4.9.%0A *
+ 
 %0A * @aut
@@ -1253,16 +1253,68 @@
 entId;%0A%0A
+  /**%0A   * Like the old ProcessEngineRule ...%0A   */%0A
   privat
@@ -2074,16 +2074,150 @@
 %7D;%0A  %7D%0A%0A
+  /**%0A   * Create a new rule instance for given process engine.%0A   * %0A   * @param processEngine%0A   *          the process engine%0A   */
 %0A  publi
@@ -2235,32 +2235,38 @@
 gineTestWatcher(
+final 
 ProcessEngine pr
@@ -2333,25 +2333,24 @@
 ine));%0A  %7D%0A%0A
-%0A
   @Override%0A
@@ -2413,14 +2413,13 @@
 le;%0A
+
   %7D%0A%0A
-%0A
   @O

add comments