Skip to content

Commit

Permalink
SPR-6416, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Micha Kiener committed Jan 18, 2010
1 parent bede7e9 commit 11284ed
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 18 deletions.
Expand Up @@ -33,6 +33,9 @@
* @since 3.1
*/
public class BasicConversationTests {
private static final String TEST_BEAN1_NAME = "testBean1";
private static final String TEST_BEAN2_NAME = "testBean2";

private static ConfigurableApplicationContext context;
private static ConversationManager manager;
private static ConversationStore store;
Expand All @@ -56,15 +59,15 @@ public void testContext() {

@Test
public void testTemporaryConversation() {
ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
assertNotNull(bean);
assertNull(bean.getName());
String id = resolver.getCurrentConversationId();
assertNotNull(id);
Conversation conversation = store.getConversation(id);
assertNotNull(conversation);
assertTrue(conversation.isTemporary());
Object attribute = conversation.getAttribute("testBean");
Object attribute = conversation.getAttribute(TEST_BEAN1_NAME);
assertNotNull(attribute);
assertSame(bean, attribute);

Expand All @@ -80,7 +83,7 @@ public void testNewConversation() {
assertFalse(conversation.isTemporary());
assertSame(conversation, manager.getCurrentConversation());

ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
assertNotNull(bean);

conversation.end(ConversationEndingType.SUCCESS);
Expand All @@ -96,7 +99,7 @@ public void testRootConversation() {
assertFalse(conversation.isTemporary());
assertSame(conversation, manager.getCurrentConversation());

ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
assertNotNull(bean);

conversation.end(ConversationEndingType.SUCCESS);
Expand Down Expand Up @@ -135,7 +138,7 @@ public void testNestedConversation() {
assertFalse(conversation.isNested());
assertFalse(((MutableConversation) conversation).isParent());

ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
assertNotNull(bean);

Conversation nestedConversation = manager.beginConversation(false, JoinMode.NESTED);
Expand All @@ -145,7 +148,7 @@ public void testNestedConversation() {
assertTrue(nestedConversation.isNested());
assertTrue(((MutableConversation) conversation).isParent());

assertSame(bean, context.getBean("testBean"));
assertSame(bean, context.getBean(TEST_BEAN1_NAME));

nestedConversation.end(ConversationEndingType.SUCCESS);
assertSame(conversation, manager.getCurrentConversation());
Expand Down Expand Up @@ -199,7 +202,7 @@ public void testIsolatedConversation() {
assertFalse(conversation.isNested());
assertFalse(((MutableConversation) conversation).isParent());

ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
assertNotNull(bean);

Conversation nestedConversation = manager.beginConversation(false, JoinMode.ISOLATED);
Expand All @@ -210,12 +213,12 @@ public void testIsolatedConversation() {
assertTrue(nestedConversation.isNested());
assertTrue(((MutableConversation) conversation).isParent());

assertNotSame(bean, context.getBean("testBean"));
assertNotSame(bean, context.getBean(TEST_BEAN1_NAME));

nestedConversation.end(ConversationEndingType.SUCCESS);
assertSame(conversation, manager.getCurrentConversation());

assertSame(bean, context.getBean("testBean"));
assertSame(bean, context.getBean(TEST_BEAN1_NAME));

conversation.end(ConversationEndingType.SUCCESS);
assertTrue(conversation.isEnded());
Expand All @@ -234,7 +237,7 @@ public void testJoinedConversation() {
assertFalse(conversation.isNested());
assertFalse(((MutableConversation) conversation).isParent());

ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
assertNotNull(bean);

Conversation joinedConversation = manager.beginConversation(false, JoinMode.JOINED);
Expand All @@ -245,12 +248,12 @@ public void testJoinedConversation() {
assertFalse(joinedConversation.isNested());
assertFalse(((MutableConversation) joinedConversation).isParent());

assertSame(bean, context.getBean("testBean"));
assertSame(bean, context.getBean(TEST_BEAN1_NAME));

joinedConversation.end(ConversationEndingType.SUCCESS);
assertSame(conversation, manager.getCurrentConversation());

assertSame(bean, context.getBean("testBean"));
assertSame(bean, context.getBean(TEST_BEAN1_NAME));

conversation.end(ConversationEndingType.SUCCESS);
assertTrue(conversation.isEnded());
Expand All @@ -269,7 +272,7 @@ public void testSwitchedConversation() {
assertFalse(conversation.isNested());
assertFalse(((MutableConversation) conversation).isParent());

ConversationalBean bean = (ConversationalBean) context.getBean("testBean");
ConversationalBean bean = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
assertNotNull(bean);

Conversation switchedConversation = manager.beginConversation(false, JoinMode.SWITCHED);
Expand All @@ -280,15 +283,15 @@ public void testSwitchedConversation() {
assertFalse(switchedConversation.isNested());
assertFalse(((MutableConversation) switchedConversation).isParent());

ConversationalBean bean2 = (ConversationalBean) context.getBean("testBean");
ConversationalBean bean2 = (ConversationalBean) context.getBean(TEST_BEAN1_NAME);
assertNotSame(bean, bean2);

manager.switchConversation(conversation.getId());
assertSame(conversation, manager.getCurrentConversation());
assertSame(bean, context.getBean("testBean"));
assertSame(bean, context.getBean(TEST_BEAN1_NAME));

manager.switchConversation(switchedConversation.getId());
assertSame(bean2, context.getBean("testBean"));
assertSame(bean2, context.getBean(TEST_BEAN1_NAME));

switchedConversation.end(ConversationEndingType.SUCCESS);
conversation.end(ConversationEndingType.SUCCESS);
Expand Down Expand Up @@ -345,7 +348,7 @@ public void testImplicitConversationEnding() {
}

@Test
public void testConversationAnnotation() {
public void testConversationAnnotation1() {
ConversationalServiceBean serviceBean = context.getBean(ConversationalServiceBean.class);
assertNotNull(serviceBean);

Expand Down Expand Up @@ -378,6 +381,87 @@ public void testConversationAnnotation() {
serviceBean.clean();
}

@Test
public void testConversationAnnotation2() {
ConversationalServiceBean serviceBean = context.getBean(ConversationalServiceBean.class);
assertNotNull(serviceBean);

serviceBean.startConversation();
Conversation conversation = manager.getCurrentConversation();
assertNotNull(conversation);

ConversationEventAwareBean bean = (ConversationEventAwareBean) context.getBean(TEST_BEAN2_NAME);
assertNotNull(bean);
assertNull(bean.getConversation1());
assertNull(bean.getConversation2());
assertNull(bean.getActivationType());
assertNull(bean.getDeactivationType());
assertNull(bean.getEndingType());

serviceBean.endConversation();

assertNotNull(bean.getEndingType());
assertSame(ConversationEndingType.SUCCESS, bean.getEndingType());
assertNotNull(bean.getConversation1());
assertSame(conversation, bean.getConversation1());

serviceBean.clean();
}

@Test
public void testConversationAnnotation3() {
ConversationalServiceBean serviceBean = context.getBean(ConversationalServiceBean.class);
assertNotNull(serviceBean);
serviceBean.setFailureFlag(true);

serviceBean.startConversation();
Conversation conversation = manager.getCurrentConversation();
assertNotNull(conversation);

ConversationEventAwareBean bean = (ConversationEventAwareBean) context.getBean(TEST_BEAN2_NAME);
assertNotNull(bean);

try {
serviceBean.endConversationSuccess();
} catch (RuntimeException e) {
// must happen
}

assertNotNull(bean.getEndingType());
assertSame(ConversationEndingType.FAILURE_SUCCESS, bean.getEndingType());
assertNotNull(bean.getConversation1());
assertSame(conversation, bean.getConversation1());

serviceBean.clean();
}

@Test
public void testConversationAnnotation4() {
ConversationalServiceBean serviceBean = context.getBean(ConversationalServiceBean.class);
assertNotNull(serviceBean);
serviceBean.setFailureFlag(true);

serviceBean.startConversation();
Conversation conversation = manager.getCurrentConversation();
assertNotNull(conversation);

ConversationEventAwareBean bean = (ConversationEventAwareBean) context.getBean(TEST_BEAN2_NAME);
assertNotNull(bean);

try {
serviceBean.endConversationCancel();
} catch (RuntimeException e) {
// must happen
}

assertNotNull(bean.getEndingType());
assertSame(ConversationEndingType.FAILURE_CANCEL, bean.getEndingType());
assertNotNull(bean.getConversation1());
assertSame(conversation, bean.getConversation1());

serviceBean.clean();
}

protected static String getContextLocation() {
return "org/springframework/conversation/conversationTestContext.xml";
}
Expand Down
@@ -0,0 +1,81 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* 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.springframework.conversation;

/**
* @author Micha Kiener
* @since 3.1
*/
public class ConversationEventAwareBean implements ConversationListener {
private Conversation conversation1;
private Conversation conversation2;
private ConversationActivationType activationType;
private ConversationDeactivationType deactivationType;
private ConversationEndingType endingType;

/**
* @see org.springframework.conversation.ConversationListener#conversationActivated(org.springframework.conversation.Conversation,
* org.springframework.conversation.Conversation,
* org.springframework.conversation.ConversationActivationType)
*/
public void conversationActivated(Conversation conversation, Conversation oldCurrentConversation,
ConversationActivationType activationType) {
conversation1 = conversation;
conversation2 = oldCurrentConversation;
this.activationType = activationType;
}

/**
* @see org.springframework.conversation.ConversationListener#conversationDeactivated(org.springframework.conversation.Conversation,
* org.springframework.conversation.Conversation,
* org.springframework.conversation.ConversationDeactivationType)
*/
public void conversationDeactivated(Conversation conversation, Conversation newCurrentConversation,
ConversationDeactivationType deactivationType) {
conversation1 = conversation;
conversation2 = newCurrentConversation;
this.deactivationType = deactivationType;
}

/**
* @see org.springframework.conversation.ConversationListener#conversationEnded(org.springframework.conversation.Conversation,
* org.springframework.conversation.ConversationEndingType)
*/
public void conversationEnded(Conversation conversation, ConversationEndingType endingType) {
conversation1 = conversation;
this.endingType = endingType;
}

public Conversation getConversation1() {
return conversation1;
}

public Conversation getConversation2() {
return conversation2;
}

public ConversationActivationType getActivationType() {
return activationType;
}

public ConversationDeactivationType getDeactivationType() {
return deactivationType;
}

public ConversationEndingType getEndingType() {
return endingType;
}
}
Expand Up @@ -25,6 +25,10 @@ public interface ConversationalServiceBean {

public void endConversation();

public void endConversationSuccess();

public void endConversationCancel();

public void doInConversation();

public Conversation getStartingConversation();
Expand All @@ -34,4 +38,6 @@ public interface ConversationalServiceBean {
public Conversation getConversationalConversation();

public void clean();

public void setFailureFlag(boolean failureFlag);
}
Expand Up @@ -28,6 +28,7 @@
*/
public class ConversationalServiceBeanImpl implements ConversationalServiceBean {
private ConversationManager manager;
private boolean failureFlag;

private Conversation startingConversation;
private Conversation endingConversation;
Expand All @@ -38,6 +39,22 @@ public void startConversation() {
startingConversation = manager.getCurrentConversation();
}

@EndConversation(ConversationEndingType.SUCCESS)
public void endConversationSuccess() {
endingConversation = manager.getCurrentConversation();
if (failureFlag) {
throw new RuntimeException("Test failure while ending conversation...");
}
}

@EndConversation(ConversationEndingType.CANCEL)
public void endConversationCancel() {
endingConversation = manager.getCurrentConversation();
if (failureFlag) {
throw new RuntimeException("Test failure while ending conversation...");
}
}

@EndConversation
public void endConversation() {
endingConversation = manager.getCurrentConversation();
Expand All @@ -64,6 +81,11 @@ public void clean() {
startingConversation = null;
endingConversation = null;
conversationalConversation = null;
failureFlag = false;
}

public void setFailureFlag(boolean failureFlag) {
this.failureFlag = failureFlag;
}

public void setConversationManager(ConversationManager manager) {
Expand Down
Expand Up @@ -33,9 +33,11 @@
<property name="conversationResolver" ref="conversationResolver"/>
</bean>

<bean id="testBean" class="org.springframework.conversation.ConversationalBean" scope="conversation"/>
<bean id="testBean1" class="org.springframework.conversation.ConversationalBean" scope="conversation"/>
<bean id="testBean2" class="org.springframework.conversation.ConversationEventAwareBean" scope="conversation"/>

<bean id="testService" class="org.springframework.conversation.ConversationalServiceBeanImpl">
<property name="conversationManager" ref="conversationManager"/>
</bean>

</beans>

0 comments on commit 11284ed

Please sign in to comment.