Skip to content

Commit

Permalink
Example of Conversation mock.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernard Labno committed May 30, 2012
1 parent 7ec9d5d commit f6f3fc2
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 3 deletions.
7 changes: 7 additions & 0 deletions test/pom.xml
Expand Up @@ -82,5 +82,12 @@
<version>${arquillian.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.shrinkwrap.descriptors</groupId>
<artifactId>shrinkwrap-descriptors-impl-javaee</artifactId>
<version>2.0.0-alpha-2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -1,12 +1,17 @@
package pl.com.it_crowd.arquillian.mock_contexts.test;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import java.io.Serializable;

@ConversationScoped
public class ConversationalComponent implements Serializable {
// ------------------------------ FIELDS ------------------------------

@Inject
private Conversation conversation;

private int index;

// --------------------- GETTER / SETTER METHODS ---------------------
Expand All @@ -20,4 +25,13 @@ public void setIndex(int index)
{
this.index = index;
}

// -------------------------- OTHER METHODS --------------------------

public void init()
{
if (conversation.isTransient()) {
conversation.begin();
}
}
}
@@ -0,0 +1,147 @@
package pl.com.it_crowd.arquillian.mock_contexts.test;

import pl.com.it_crowd.arquillian.mock_contexts.MockContextsManager;

import javax.annotation.PreDestroy;
import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

@Alternative
@ConversationScoped
public class MockConversation implements Conversation, Serializable {
// ------------------------------ FIELDS ------------------------------

private static Set<String> ACTIVE_CONVERSATIONS = new HashSet<String>();

private static long CONVERSATION_ID_COUNTER = 1;

private static final Logger log = Logger.getLogger(MockContextsManager.class.getName());

private boolean _transient;

private BeanManager beanManager;

private String id;

private long timeout;

// --------------------------- CONSTRUCTORS ---------------------------

public MockConversation()
{
}

@Inject
public MockConversation(BeanManager beanManager)
{
this.beanManager = beanManager;
this._transient = true;
this.timeout = 0;
}

// --------------------- GETTER / SETTER METHODS ---------------------

public long getTimeout()
{
verifyConversationContextActive();
return timeout;
}

public void setTimeout(long timeout)
{
verifyConversationContextActive();
this.timeout = timeout;
}

// ------------------------ CANONICAL METHODS ------------------------

@Override
public String toString()
{
if (_transient) {
return "Transient conversation";
} else {
return "Conversation with id: " + id;
}
}

// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface Conversation ---------------------

public void begin()
{
verifyConversationContextActive();
if (!_transient) {
throw new IllegalStateException("BEGIN_CALLED_ON_LONG_RUNNING_CONVERSATION");
}
_transient = false;
if (this.id == null) {
// This a conversation that was made transient previously in this request
this.id = "" + CONVERSATION_ID_COUNTER++;
ACTIVE_CONVERSATIONS.add(this.id);
}
}

public void begin(String id)
{
verifyConversationContextActive();
if (!_transient) {
throw new IllegalStateException("BEGIN_CALLED_ON_LONG_RUNNING_CONVERSATION");
}
if (ACTIVE_CONVERSATIONS.contains(id)) {
throw new IllegalStateException("CONVERSATION_ID_ALREADY_IN_USE:" + id);
}
_transient = false;
this.id = id;
}

public void end()
{
if (_transient) {
throw new IllegalStateException("END_CALLED_ON_TRANSIENT_CONVERSATION");
}
_transient = true;
ACTIVE_CONVERSATIONS.remove(this.id);
}

public String getId()
{
verifyConversationContextActive();
if (!_transient) {
return id;
} else {
return null;
}
}

public boolean isTransient()
{
verifyConversationContextActive();
return _transient;
}

@PreDestroy
private void onDestroy()
{
ACTIVE_CONVERSATIONS.remove(this.id);
}

private void verifyConversationContextActive()
{
try {
beanManager.getContext(ConversationScoped.class);
} catch (ContextNotActiveException e) {
throw new ContextNotActiveException("Conversation Context not active when method called on conversation " + this, e);
}
}
}
Expand Up @@ -4,20 +4,26 @@
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.beans10.BeansDescriptor;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import pl.com.it_crowd.arquillian.mock_contexts.ConversationScopeRequired;
import pl.com.it_crowd.arquillian.mock_contexts.ViewScopeRequired;

import javax.enterprise.context.Conversation;
import javax.inject.Inject;

@RunWith(Arquillian.class)
public class SampleTest {
// ------------------------------ FIELDS ------------------------------

@Inject
private Conversation conversation;

@Inject
private ConversationalComponent conversationalComponent;

Expand All @@ -29,10 +35,16 @@ public class SampleTest {
@Deployment
public static Archive getArchive()
{
final String beansDescriptor = Descriptors.create(BeansDescriptor.class)
.createAlternatives()
.clazz(MockConversation.class.getCanonicalName())
.up()
.exportAsString();
return ShrinkWrap.create(WebArchive.class, SampleTest.class.getSimpleName() + ".war")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource(new StringAsset(beansDescriptor), "beans.xml")
.addClass(ConversationalComponent.class)
.addClass(ViewScopedComponent.class);
.addClass(ViewScopedComponent.class)
.addClass(MockConversation.class);
}

// -------------------------- OTHER METHODS --------------------------
Expand All @@ -44,6 +56,9 @@ public void conversationScopedBeanTest()
Assert.assertEquals(0, conversationalComponent.getIndex());
conversationalComponent.setIndex(1);
Assert.assertEquals(1, conversationalComponent.getIndex());
Assert.assertTrue(conversation.isTransient());
conversationalComponent.init();
Assert.assertFalse(conversation.isTransient());
}

@ViewScopeRequired
Expand Down

0 comments on commit f6f3fc2

Please sign in to comment.