Skip to content

Commit

Permalink
Fix failing test after Weld 2.4.1.Final upgrade
Browse files Browse the repository at this point in the history
Weld 2.4.1.Final behaves somewhat different and is initializing the beans
during discovery. That leads to a call
CustomTaskPersistenceContextManager#beginCommandScopedEntityManager
which throws the exception. The bean is thus not initialized and the CDI
container is not able to inject some members (as they were not
initialized and thus are not available for injection). Using the
CommandBasedTaskService mock should lead to a same result - we do assert
that the custom bean is being injected as the execute() methods throws
exception.
  • Loading branch information
Petr Široký authored and psiroky committed Jan 31, 2017
1 parent 6c667c2 commit 0b6955d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
15 changes: 13 additions & 2 deletions jbpm-services/jbpm-services-cdi/pom.xml
Expand Up @@ -104,7 +104,6 @@
<artifactId>dashbuilder-dataset-api</artifactId>
</dependency>


<!-- test -->
<dependency>
<groupId>org.jboss.weld</groupId>
Expand Down Expand Up @@ -132,11 +131,23 @@
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.1_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.1_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
Expand Down Expand Up @@ -175,7 +186,7 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
Expand Up @@ -15,45 +15,34 @@
*/
package org.jbpm.services.cdi.test.humantaskservice;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.Produces;
import javax.inject.Qualifier;

import org.drools.core.impl.EnvironmentFactory;
import org.drools.persistence.jta.JtaTransactionManager;
import org.jbpm.services.cdi.producer.HumanTaskServiceProducer;
import org.jbpm.services.task.HumanTaskConfigurator;
import org.jbpm.services.task.impl.command.CommandBasedTaskService;
import org.jbpm.services.task.persistence.JPATaskPersistenceContextManager;
import org.kie.api.runtime.Environment;
import org.kie.api.runtime.EnvironmentName;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;

/**
*
*
*/
public class CustomHumanTaskServiceProducer extends HumanTaskServiceProducer {

@Produces
@CustomHumanTaskService
@Override
public CommandBasedTaskService produceTaskService() {
return super.produceTaskService();
CommandBasedTaskService taskServiceMock = Mockito.mock(CommandBasedTaskService.class);
Mockito.when(taskServiceMock.execute(Mockito.any())).thenAnswer((InvocationOnMock invocation) -> {
throw new CustomTaskServiceInUse();
});
return taskServiceMock;
}

@Override
protected void configureHumanTaskConfigurator(HumanTaskConfigurator configurator) {
Environment environment = EnvironmentFactory.newEnvironment();
environment.set(EnvironmentName.TASK_PERSISTENCE_CONTEXT_MANAGER, new CustomTaskPersistenceContextManager());
environment.set(EnvironmentName.TRANSACTION_MANAGER, new CustomTransactionManager());
super.configureHumanTaskConfigurator(configurator.environment(environment));
}
Expand All @@ -65,21 +54,10 @@ public CustomTransactionManager() {
}


public static class CustomTaskPersistenceContextManager extends JPATaskPersistenceContextManager {
public CustomTaskPersistenceContextManager() {
super(EnvironmentFactory.newEnvironment());
}

@Override
public void beginCommandScopedEntityManager() {
throw new CustomTaskPersistenceContextManagerInUse();
}
}

/**
* Exception throw to show the CustomTaskPersistenceContextManager is in use.
* Exception throw to show the custom service task is in use.
*/
@SuppressWarnings("serial")
public static class CustomTaskPersistenceContextManagerInUse extends RuntimeException {}
public static class CustomTaskServiceInUse extends RuntimeException {}

}
Expand Up @@ -23,7 +23,7 @@
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jbpm.kie.test.util.AbstractKieServicesBaseTest;
import org.jbpm.services.cdi.test.humantaskservice.CustomHumanTaskServiceProducer.CustomTaskPersistenceContextManagerInUse;
import org.jbpm.services.cdi.test.humantaskservice.CustomHumanTaskServiceProducer.CustomTaskServiceInUse;
import org.jbpm.services.task.commands.TaskCommand;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -147,13 +147,12 @@ public void testDefaultTaskService() throws Exception {
}

/**
* The CustomHumanTaskService is configured with a TaskPersistenceContextManager
* implementation that throws an exception to prove that the custom environment
* is in use.
* The CustomHumanTaskService is configured as a mock which throws an exception
* when the the execute() method is called.
*
* @throws Exception
*/
@Test(expected=CustomTaskPersistenceContextManagerInUse.class)
@Test(expected=CustomTaskServiceInUse.class)
public void testCustomTaskService() throws Exception {
customTaskService.execute(new TestCommand());
}
Expand Down

0 comments on commit 0b6955d

Please sign in to comment.