org.activiti.engine.impl.test.AbstractActivitiTestCase Maven / Gradle / Ivy
The newest version!
/* 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.engine.impl.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.DynamicBpmnService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.db.DbSqlSession;
import org.activiti.engine.impl.history.HistoryLevel;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandConfig;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.interceptor.CommandExecutor;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Assert;
import junit.framework.AssertionFailedError;
public abstract class AbstractActivitiTestCase extends AbstractTestCase {
private static final List TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK = Collections.singletonList("ACT_GE_PROPERTY");
protected ProcessEngine processEngine;
protected String deploymentIdFromDeploymentAnnotation;
protected List deploymentIdsForAutoCleanup = new ArrayList();
protected Throwable exception;
protected ProcessEngineConfigurationImpl processEngineConfiguration;
protected RepositoryService repositoryService;
protected RuntimeService runtimeService;
protected TaskService taskService;
protected HistoryService historyService;
protected ManagementService managementService;
protected DynamicBpmnService dynamicBpmnService;
@Override
protected void setUp() throws Exception {
super.setUp();
// Always reset authenticated user to avoid any mistakes
Authentication.setAuthenticatedUserId(null);
}
protected abstract void initializeProcessEngine();
// Default: do nothing
protected void closeDownProcessEngine() {
}
@Override
public void runBare() throws Throwable {
initializeProcessEngine();
if (repositoryService == null) {
initializeServices();
}
try {
deploymentIdFromDeploymentAnnotation = TestHelper.annotationDeploymentSetUp(processEngine, getClass(), getName());
super.runBare();
validateHistoryData();
} catch (AssertionFailedError e) {
log.error(EMPTY_LINE);
log.error("ASSERTION FAILED: {}", e, e);
exception = e;
throw e;
} catch (Throwable e) {
log.error(EMPTY_LINE);
log.error("EXCEPTION: {}", e, e);
exception = e;
throw e;
} finally {
if (deploymentIdFromDeploymentAnnotation != null) {
TestHelper.annotationDeploymentTearDown(processEngine, deploymentIdFromDeploymentAnnotation, getClass(), getName());
deploymentIdFromDeploymentAnnotation = null;
}
for (String autoDeletedDeploymentId : deploymentIdsForAutoCleanup) {
repositoryService.deleteDeployment(autoDeletedDeploymentId, true);
}
deploymentIdsForAutoCleanup.clear();
assertAndEnsureCleanDb();
processEngineConfiguration.getClock().reset();
// Can't do this in the teardown, as the teardown will be called as part of the super.runBare
closeDownProcessEngine();
}
}
protected void validateHistoryData() {
if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.AUDIT)) {
List historicProcessInstances =
historyService.createHistoricProcessInstanceQuery().finished().list();
for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) {
assertNotNull("Historic process instance has no process definition id", historicProcessInstance.getProcessDefinitionId());
assertNotNull("Historic process instance has no process definition key", historicProcessInstance.getProcessDefinitionKey());
assertNotNull("Historic process instance has no process definition version", historicProcessInstance.getProcessDefinitionVersion());
assertNotNull("Historic process instance has no process definition key", historicProcessInstance.getDeploymentId());
assertNotNull("Historic process instance has no start activiti id", historicProcessInstance.getStartActivityId());
assertNotNull("Historic process instance has no start time", historicProcessInstance.getStartTime());
assertNotNull("Historic process instance has no end time", historicProcessInstance.getEndTime());
String processInstanceId = historicProcessInstance.getId();
// tasks
List historicTaskInstances = historyService.createHistoricTaskInstanceQuery()
.processInstanceId(processInstanceId).list();
if (historicTaskInstances != null && historicTaskInstances.size() > 0) {
for (HistoricTaskInstance historicTaskInstance : historicTaskInstances) {
assertEquals(processInstanceId, historicTaskInstance.getProcessInstanceId());
if (historicTaskInstance.getClaimTime() != null) {
assertNotNull("Historic task " + historicTaskInstance.getTaskDefinitionKey() + " has no work time", historicTaskInstance.getWorkTimeInMillis());
}
assertNotNull("Historic task " + historicTaskInstance.getTaskDefinitionKey() + " has no id", historicTaskInstance.getId());
assertNotNull("Historic task " + historicTaskInstance.getTaskDefinitionKey() + " has no process instance id", historicTaskInstance.getProcessInstanceId());
assertNotNull("Historic task " + historicTaskInstance.getTaskDefinitionKey() + " has no execution id", historicTaskInstance.getExecutionId());
assertNotNull("Historic task " + historicTaskInstance.getTaskDefinitionKey() + " has no process definition id", historicTaskInstance.getProcessDefinitionId());
assertNotNull("Historic task " + historicTaskInstance.getTaskDefinitionKey() + " has no task definition key", historicTaskInstance.getTaskDefinitionKey());
assertNotNull("Historic task " + historicTaskInstance.getTaskDefinitionKey() + " has no create time", historicTaskInstance.getCreateTime());
assertNotNull("Historic task " + historicTaskInstance.getTaskDefinitionKey() + " has no start time", historicTaskInstance.getStartTime());
assertNotNull("Historic task " + historicTaskInstance.getTaskDefinitionKey() + " has no end time", historicTaskInstance.getEndTime());
}
}
// activities
List historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId).list();
if (historicActivityInstances != null && historicActivityInstances.size() > 0) {
for (HistoricActivityInstance historicActivityInstance : historicActivityInstances) {
assertEquals(processInstanceId, historicActivityInstance.getProcessInstanceId());
assertNotNull("Historic activity instance " + historicActivityInstance.getActivityId() + " has no activity id", historicActivityInstance.getActivityId());
assertNotNull("Historic activity instance " + historicActivityInstance.getActivityId() + " has no activity type", historicActivityInstance.getActivityType());
assertNotNull("Historic activity instance " + historicActivityInstance.getActivityId() + " has no process definition id", historicActivityInstance.getProcessDefinitionId());
assertNotNull("Historic activity instance " + historicActivityInstance.getActivityId() + " has no process instance id", historicActivityInstance.getProcessInstanceId());
assertNotNull("Historic activity instance " + historicActivityInstance.getActivityId() + " has no execution id", historicActivityInstance.getExecutionId());
assertNotNull("Historic activity instance " + historicActivityInstance.getActivityId() + " has no start time", historicActivityInstance.getStartTime());
assertNotNull("Historic activity instance " + historicActivityInstance.getActivityId() + " has no end time", historicActivityInstance.getEndTime());
}
}
}
}
}
/**
* Each test is assumed to clean up all DB content it entered. After a test method executed, this method scans all tables to see if the DB is completely clean. It throws AssertionFailed in case the
* DB is not clean. If the DB is not clean, it is cleaned by performing a create a drop.
*/
protected void assertAndEnsureCleanDb() throws Throwable {
log.debug("verifying that db is clean after test");
Map tableCounts = managementService.getTableCount();
StringBuilder outputMessage = new StringBuilder();
for (String tableName : tableCounts.keySet()) {
String tableNameWithoutPrefix = tableName.replace(processEngineConfiguration.getDatabaseTablePrefix(), "");
if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableNameWithoutPrefix)) {
Long count = tableCounts.get(tableName);
if (count != 0L) {
outputMessage.append(" ").append(tableName).append(": ").append(count).append(" record(s) ");
}
}
}
if (outputMessage.length() > 0) {
outputMessage.insert(0, "DB NOT CLEAN: \n");
log.error(EMPTY_LINE);
log.error(outputMessage.toString());
log.info("dropping and recreating db");
CommandExecutor commandExecutor = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor();
CommandConfig config = new CommandConfig().transactionNotSupported();
commandExecutor.execute(config, new Command
© 2015 - 2024 Weber Informatics LLC | Privacy Policy