Skip to content

Commit

Permalink
WELD-348
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Feb 4, 2010
1 parent 0021b3e commit 3c8cf8d
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 13 deletions.
Expand Up @@ -18,6 +18,7 @@

import static org.jboss.weld.logging.Category.CONVERSATION;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BeanManagerMessage.CONTEXT_NOT_ACTIVE;
import static org.jboss.weld.logging.messages.ConversationMessage.BEGIN_CALLED_ON_LONG_RUNNING_CONVERSATION;
import static org.jboss.weld.logging.messages.ConversationMessage.DEMOTED_LRC;
import static org.jboss.weld.logging.messages.ConversationMessage.END_CALLED_ON_TRANSIENT_CONVERSATION;
Expand All @@ -32,6 +33,9 @@
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.weld.Container;
import org.jboss.weld.context.ContextLifecycle;
import org.jboss.weld.context.ContextNotActiveException;
import org.jboss.weld.exceptions.ForbiddenStateException;
import org.slf4j.cal10n.LocLogger;

Expand Down Expand Up @@ -66,8 +70,14 @@ public class ConversationImpl implements Conversation, Serializable
/**
* Creates a new conversation
*/
public ConversationImpl()
public ConversationImpl() {}

protected void checkConversationActive()
{
if (!Container.instance().services().get(ContextLifecycle.class).getConversationContext().isActive())
{
throw new ContextNotActiveException(CONTEXT_NOT_ACTIVE, "@ConversationScoped");
}
}

/**
Expand Down Expand Up @@ -98,6 +108,7 @@ public void init(ConversationIdGenerator conversationIdGenerator, @ConversationI

public void begin()
{
checkConversationActive();
if (!isTransient())
{
throw new ForbiddenStateException(BEGIN_CALLED_ON_LONG_RUNNING_CONVERSATION);
Expand All @@ -108,6 +119,7 @@ public void begin()

public void begin(String id)
{
checkConversationActive();
// Store away the (first) change to the conversation ID. If the original
// conversation was long-running,
// we might have to place it back for termination once the request is
Expand All @@ -122,6 +134,7 @@ public void begin(String id)

public void end()
{
checkConversationActive();
if (isTransient())
{
throw new ForbiddenStateException(END_CALLED_ON_TRANSIENT_CONVERSATION);
Expand All @@ -132,6 +145,7 @@ public void end()

public String getId()
{
checkConversationActive();
if (!isTransient())
{
return id;
Expand All @@ -155,11 +169,13 @@ public String getUnderlyingId()

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

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

Expand All @@ -172,9 +188,9 @@ public void setTimeout(long timeout)
public void switchTo(ConversationImpl conversation)
{
log.debug(SWITCHED_CONVERSATION, this, conversation);
id = conversation.getUnderlyingId();
this._transient = conversation.isTransient();
timeout = conversation.getTimeout();
id = conversation.id;
this._transient = conversation._transient;
timeout = conversation.timeout;
}

@Override
Expand Down Expand Up @@ -215,6 +231,9 @@ public int hashCode()

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


}
36 changes: 36 additions & 0 deletions jboss-tck-runner/src/test/resources/tck-tests.xml
Expand Up @@ -57,6 +57,42 @@
</methods>
</class>

<!-- CDITCK-109 -->
<class name="org.jboss.jsr299.tck.tests.context.conversation.ConversationBeginTest">
<methods>
<exclude name="testBeginAlreadyLongRunningConversationThrowsException" />
<exclude name="testConversationBeginMakesConversationLongRunning" />
</methods>
</class>
<class name="org.jboss.jsr299.tck.tests.context.conversation.ConversationContextTest">
<methods>
<exclude name="testDefaultConversationIsTransient" />
<exclude name="testTransientConversationHasNullId" />
</methods>
</class>
<class name="org.jboss.jsr299.tck.tests.context.conversation.ConversationEndTest">
<methods>
<exclude name="testConversationEndMakesConversationTransient" />
<exclude name="testEndTransientConversationThrowsException" />
</methods>
</class>
<class name="org.jboss.jsr299.tck.tests.context.conversation.ConversationIdSetByApplicationTest">
<methods>
<exclude name="testConversationIdMayBeSetByApplication" />
</methods>
</class>
<class name="org.jboss.jsr299.tck.tests.context.conversation.ConversationIdSetByContainerTest">
<methods>
<exclude name="testConversationBeginMakesConversationLongRunning" />
</methods>
</class>
<class name="org.jboss.jsr299.tck.tests.context.conversation.ConversationTimeoutTest">
<methods>
<exclude name="testConversationHasDefaultTimeout" />
<exclude name="testSetConversationTimeoutOverride" />
</methods>
</class>

<!-- Issues in Weld (the RI) -->

<!-- WELD-390 fixed, but TCK test is broken in 1.0.1-CR1 -->
Expand Down
9 changes: 9 additions & 0 deletions tests/src/main/java/org/jboss/weld/mock/TestContainer.java
Expand Up @@ -21,6 +21,7 @@
import java.util.Collection;

import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.context.ConversationContext;
import org.jboss.weld.manager.BeanManagerImpl;

/**
Expand Down Expand Up @@ -129,6 +130,10 @@ public TestContainer ensureRequestActive()
{
getLifecycle().beginSession();
}
if (!getLifecycle().isConversationActive())
{
((ConversationContext) getLifecycle().getConversationContext()).setActive(true);
}
if (!getLifecycle().isRequestActive())
{
getLifecycle().beginRequest();
Expand All @@ -146,6 +151,10 @@ public TestContainer stopContainer()
{
getLifecycle().endRequest();
}
if (getLifecycle().isConversationActive())
{
((ConversationContext) getLifecycle().getConversationContext()).setActive(false);
}
if (getLifecycle().isSessionActive())
{
getLifecycle().endSession();
Expand Down
Expand Up @@ -44,8 +44,7 @@ public boolean deploy(Collection<Class<?>> classes, Collection<URL> beansXml)
this.deploymentException = new DeploymentException("Error deploying beans", e);
return false;
}
testContainer.getLifecycle().beginSession();
testContainer.getLifecycle().beginRequest();
testContainer.ensureRequestActive();
return true;
}

Expand Down
125 changes: 118 additions & 7 deletions tests/src/test/java/org/jboss/weld/tests/contexts/ContextTest.java
Expand Up @@ -16,21 +16,132 @@
*/
package org.jboss.weld.tests.contexts;

import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.Conversation;

import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.IntegrationTest;
import org.jboss.weld.Container;
import org.jboss.weld.context.ContextLifecycle;
import org.jboss.weld.test.AbstractWeldTest;
import org.testng.annotations.Test;

@Artifact
@IntegrationTest
public class ContextTest extends AbstractWeldTest
{
// WBRI-155
@Test(description="WBRI155", groups="stub")
public void testSessionContextActiveForMultipleSimultaneousThreads()

@Test(description = "WELD-348")
public void testCallToConversationWithContextNotActive()
{
// TODO impl
assert false;
boolean alreadyActive = false;
try
{
alreadyActive = Container.instance().services().get(ContextLifecycle.class).isConversationActive();
if (alreadyActive)
{
Container.instance().services().get(ContextLifecycle.class).getConversationContext().setActive(false);
}
try
{
getReference(Conversation.class).getId();
assert false;
}
catch (ContextNotActiveException e)
{
// Expected
}
catch (Exception e)
{
assert false;
}
try
{
getReference(Conversation.class).getTimeout();
assert false;
}
catch (ContextNotActiveException e)
{
// Expected
}
catch (Exception e)
{
assert false;
}
try
{
getReference(Conversation.class).begin();
assert false;
}
catch (ContextNotActiveException e)
{
// Expected
}
catch (Exception e)
{
assert false;
}
try
{
getReference(Conversation.class).begin("foo");
assert false;
}
catch (ContextNotActiveException e)
{
// Expected
}
catch (Exception e)
{
assert false;
}
try
{
getReference(Conversation.class).end();
assert false;
}
catch (ContextNotActiveException e)
{
// Expected
}
catch (Exception e)
{
assert false;
}
try
{
getReference(Conversation.class).isTransient();
assert false;
}
catch (ContextNotActiveException e)
{
// Expected
}
catch (Exception e)
{
assert false;
}
try
{
getReference(Conversation.class).setTimeout(0);
assert false;
}
catch (ContextNotActiveException e)
{
// Expected
}
catch (Exception e)
{
assert false;
}
}
finally
{
if (alreadyActive)
{
Container.instance().services().get(ContextLifecycle.class).getConversationContext().setActive(true);
}
}

}



}

0 comments on commit 3c8cf8d

Please sign in to comment.