From 8adb9a88d0ec3ac67ea77a7a367a3536405d2f44 Mon Sep 17 00:00:00 2001 From: Micha Kiener Date: Tue, 12 Apr 2011 19:51:50 +0000 Subject: [PATCH] SPR-6416, adding basic conversation object tests, improving the access time of the conversation object --- .../manager/DefaultConversation.java | 4 + .../conversation/DefaultConversationTest.java | 99 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 org.springframework.context/src/test/java/org/springframework/conversation/DefaultConversationTest.java diff --git a/org.springframework.context/src/main/java/org/springframework/conversation/manager/DefaultConversation.java b/org.springframework.context/src/main/java/org/springframework/conversation/manager/DefaultConversation.java index 79267bea29..4cb6d08c12 100644 --- a/org.springframework.context/src/main/java/org/springframework/conversation/manager/DefaultConversation.java +++ b/org.springframework.context/src/main/java/org/springframework/conversation/manager/DefaultConversation.java @@ -118,6 +118,7 @@ public class DefaultConversation implements MutableConversation, Serializable { public void clear() { attributes.clear(); + touch(); } public void setId(String id) { @@ -157,6 +158,8 @@ public class DefaultConversation implements MutableConversation, Serializable { public void addChildConversation(MutableConversation conversation, boolean isIsolated) { checkValidity(); + touch(); + if (conversation instanceof DefaultConversation) { // set this conversation as the parent within the given child conversation ((DefaultConversation)conversation).setParentConversation(this, isIsolated); @@ -170,6 +173,7 @@ public class DefaultConversation implements MutableConversation, Serializable { } public void removeChildConversation(MutableConversation conversation) { + touch(); if (children != null) { children.remove(conversation); if (children.size() == 0) { diff --git a/org.springframework.context/src/test/java/org/springframework/conversation/DefaultConversationTest.java b/org.springframework.context/src/test/java/org/springframework/conversation/DefaultConversationTest.java new file mode 100644 index 0000000000..e75ec3a5b1 --- /dev/null +++ b/org.springframework.context/src/test/java/org/springframework/conversation/DefaultConversationTest.java @@ -0,0 +1,99 @@ +/* + * Copyright 2002-2011 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; + +import org.junit.Test; + +import org.springframework.conversation.manager.DefaultConversation; +import org.springframework.conversation.manager.MutableConversation; + +import static org.junit.Assert.*; + +/** + * @author Micha Kiener + */ +public class DefaultConversationTest { + + @Test + public void testConversationAttribute() { + Conversation conversation = new DefaultConversation(); + + String test = new String("Test"); + conversation.setAttribute("test", test); + + assertSame("attribute must be accessible through conversation object", test, conversation.getAttribute("test")); + } + + @Test + public void testConversationAttributeInheritance() { + MutableConversation conversation = new DefaultConversation(); + + String test = new String("Test"); + conversation.setAttribute("test", test); + + MutableConversation childConversation = new DefaultConversation(); + conversation.addChildConversation(childConversation, false); + + assertSame("attribute must be accessible through child conversation object", test, + childConversation.getAttribute("test")); + } + + @Test + public void testConversationAttributeIsolation() { + MutableConversation conversation = new DefaultConversation(); + + String test = new String("Test"); + conversation.setAttribute("test", test); + + MutableConversation childConversation = new DefaultConversation(); + conversation.addChildConversation(childConversation, true); + + assertNull("attribute must not be accessible from within an isolated conversation", + childConversation.getAttribute("test")); + } + + @Test + public void testNewConversationAttributeIsolation() { + MutableConversation conversation = new DefaultConversation(); + + String test = new String("Test"); + conversation.setAttribute("test", test); + + MutableConversation childConversation = new DefaultConversation(); + conversation.addChildConversation(childConversation, true); + + String test2 = new String("Test2"); + childConversation.setAttribute("test2", test2); + + assertNull("newly added attribute on isolated conversation must not be accessible within its parent", + conversation.getAttribute("test2")); + } + + @Test + public void testConversationAttributeRemoval() { + Conversation conversation = new DefaultConversation(); + + String test = new String("Test"); + conversation.setAttribute("test", test); + + String test2 = (String)conversation.removeAttribute("test"); + assertNotNull("removed attribute must be returned", test2); + assertSame("removed attribute must be the same as being added", test, test2); + + assertNull("removed attribute must not be accessible after removal", conversation.getAttribute("test")); + } +}