View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.apache.myfaces.orchestra.conversation;
    20
    21  import org.apache.myfaces.orchestra.conversation.basic.LogConversationMessager;
    22  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
    23  import org.apache.myfaces.orchestra.frameworkAdapter.local.LocalFrameworkAdapter;
    24  import org.springframework.aop.scope.ScopedObject;
    25  import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
    26
    27  /**
    28   * Test various aspects of the conversation handling
    29   */
    30  public class TestConversation extends AbstractDependencyInjectionSpringContextTests implements ConversationBindingListener
    31  {
    32      protected String[] getConfigLocations()
    33      {
    34          return new String[]
    35              {
    36                  "classpath:testApplicationContext.xml"
    37              };
    38      }
    39
    40
    41      private int valueBoundCount = 0;
    42      private int valueUnboundCount = 0;
    43
    44      protected void onSetUp() throws Exception
    45      {
    46          super.onSetUp();
    47
    48          LocalFrameworkAdapter frameworkAdapter = new LocalFrameworkAdapter();
    49          frameworkAdapter.setApplicationContext(applicationContext);
    50          frameworkAdapter.setConversationMessager(new LogConversationMessager());
    51          FrameworkAdapter.setCurrentInstance(frameworkAdapter);
    52      }
    53
    54      public void testConversation() throws Exception
    55      {
    56          DummyBean.callback=this;
    57          final String BEAN_NAME = "dummyBean";
    58
    59          // The Spring configuration for dummyBean does not explicitly set a conversation name,
    60          // so conversation-name = bean-name
    61          final String CONVERSATION_NAME = BEAN_NAME;
    62
    63          valueBoundCount = 0;
    64          valueUnboundCount = 0;
    65
    66          /* simple create test */
    67          DummyBean bean = (DummyBean) applicationContext.getBean(BEAN_NAME);
    68
    69          assertTrue("should be a scoped object", bean instanceof ScopedObject);
    70
    71          assertFalse("conversation should not have been started yet", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
    72          assertEquals("value bound", 0, valueBoundCount);
    73          assertEquals("value unbound", 0, valueUnboundCount);
    74          bean.touch();
    75          assertEquals("value bound", 1, valueBoundCount);
    76          assertEquals("value unbound", 0, valueUnboundCount);
    77          assertTrue("conversation should have been started", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
    78
    79          /* check if correct conversation has been started */
    80          Conversation conversationHolder = bean.checkCurrentConversation();
    81          assertNotNull("current conversation", conversationHolder);
    82          assertEquals("conversation name", CONVERSATION_NAME, conversationHolder.getName());
    83
    84          /* invalidate conversation */
    85          bean.invalidateSelf();
    86
    87          assertFalse("conversation should not be running", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
    88          assertTrue("conversation should be marked invalid", conversationHolder.isInvalid());
    89          assertEquals("value bound", 1, valueBoundCount);
    90          assertEquals("value unbound", 1, valueUnboundCount);
    91
    92          bean.touch();
    93          assertTrue("conversation should have been started", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
    94          assertEquals("value bound", 2, valueBoundCount);
    95
    96
    97          /* check if a new conversation bean has been created */
    98          bean.setData("check");
    99
    100         bean.invalidateAndRestartSelf();
    101
    102         assertEquals("value bound", 2, valueBoundCount);
    103         assertEquals("value unbound", 2, valueUnboundCount);
    104
    105         assertTrue("conversation should still be running", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
    106
    107         DummyBean beanNew = (DummyBean) applicationContext.getBean(BEAN_NAME);
    108
    109         assertNotNull("should have got a new conversation", beanNew);
    110         assertNull("the conversation is not new", bean.getData());
    111
    112         beanNew.touch();
    113
    114         assertEquals("value bound", 3, valueBoundCount);
    115
    116         /* clear the whole conversation context */
    117
    118         ConversationManager.getInstance().clearCurrentConversationContext();
    119
    120         assertEquals("value unbound", 3, valueUnboundCount);
    121         assertFalse("conversation should not be running", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
    122     }
    123
    124     public void valueBound(ConversationBindingEvent event)
    125     {
    126         valueBoundCount++;
    127     }
    128
    129     public void valueUnbound(ConversationBindingEvent event)
    130     {
    131         valueUnboundCount++;
    132     }
    133 }