package org.camunda.bpm.extension.process_test_coverage.spring;

import org.camunda.bpm.engine.ProcessEngineConfiguration;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.impl.el.ExpressionManager;
import org.camunda.bpm.engine.spring.ProcessEngineFactoryBean;
import org.camunda.bpm.engine.spring.SpringExpressionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * Adapted from: https://github.com/camunda/camunda-bpm-platform/blob/master/engine-spring/src/test/java/org/camunda/bpm/engine/spring/test/configuration/InMemProcessEngineConfiguration.java
 */
@Configuration
public class InMemProcessEngineConfiguration {

  @Autowired
  ApplicationContext applicationContext;

  @Bean
  public DataSource dataSource() {

    SimpleDriverDataSource dataSource = new SimpleDriverDataSource();

    dataSource.setDriverClass(org.h2.Driver.class);
    dataSource.setUrl("jdbc:h2:mem:camunda-test;DB_CLOSE_DELAY=-1");
    dataSource.setUsername("sa");
    dataSource.setPassword("");

    return dataSource;

  }

  @Bean
  public PlatformTransactionManager transactionManager() {

    return new DataSourceTransactionManager(dataSource());

  }

  @Bean
  public ProcessEngineConfigurationImpl processEngineConfiguration() throws IOException {

    SpringProcessWithCoverageEngineConfiguration config = new SpringProcessWithCoverageEngineConfiguration();

    config.setApplicationContext(applicationContext);
    config.setExpressionManager(expressionManager());
    config.setTransactionManager(transactionManager());
    config.setDataSource(dataSource());
    config.setDatabaseSchemaUpdate("true");
    config.setHistory(ProcessEngineConfiguration.HISTORY_FULL);
    config.setJobExecutorActivate(false);
    config.init();
    return config;

  }

  @Bean
  ExpressionManager expressionManager() {

    return new SpringExpressionManager(applicationContext, null);

  }

  @Bean
  public ProcessEngineFactoryBean processEngine() throws IOException {

    ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
    factoryBean.setProcessEngineConfiguration(processEngineConfiguration());
    return factoryBean;

  }

}

@@ -58,16 +58,80 @@
 pring;%0A%0A
+import java.lang.reflect.Method;%0A%0Aimport javax.sql.DataSource;%0A%0A
 import o
@@ -172,32 +172,32 @@
 eConfiguration;%0A
-
 import org.camun
@@ -425,32 +425,103 @@
 ressionManager;%0A
+import org.camunda.bpm.engine.spring.SpringProcessEngineConfiguration;%0A
 import org.sprin
@@ -947,66 +947,8 @@
 r;%0A%0A
-import javax.sql.DataSource;%0Aimport java.io.IOException;%0A%0A
 /**%0A
@@ -1801,34 +1801,32 @@
 ration() throws 
-IO
 Exception %7B%0A%0A   
@@ -1941,57 +1941,365 @@
     
-config.setApplicationContext(applicationContext);
+%0A    try %7B%0A      Method setApplicationContext = SpringProcessEngineConfiguration.class.getDeclaredMethod(%22setApplicationContext%22, ApplicationContext.class);%0A      if (setApplicationContext != null) %7B%0A        setApplicationContext.invoke(config, applicationContext);%0A      %7D%0A    %7D catch (NoSuchMethodException e) %7B%0A      // expected for Camunda %3C 7.8.0%0A    %7D
 %0A   
@@ -2762,32 +2762,32 @@
 ;%0A%0A  %7D%0A%0A  @Bean%0A
+
   public Process
@@ -2831,10 +2831,8 @@
 ows 
-IO
 Exce

Fix test for all Camunda version using reflection