    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.conversation.model.UserData;
    23  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
    24  import org.apache.myfaces.orchestra.frameworkAdapter.local.LocalFrameworkAdapter;
    25  import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
    26
    27  /**
    28   * Test various aspects of the conversation handling
    29   */
    30  public class TestConversationPersistence extends AbstractDependencyInjectionSpringContextTests
    31  {
    32      protected String[] getConfigLocations()
    33      {
    34          return new String[]
    35              {
    36                  "classpath:testApplicationContext.xml"
    37              };
    38      }
    39
    40      protected void onSetUp() throws Exception
    41      {
    42          super.onSetUp();
    43
    44          LocalFrameworkAdapter frameworkAdapter = new LocalFrameworkAdapter();
    45          frameworkAdapter.setApplicationContext(applicationContext);
    46          frameworkAdapter.setConversationMessager(new LogConversationMessager());
    47          FrameworkAdapter.setCurrentInstance(frameworkAdapter);
    48      }
    49
    50      public void testPersistence()
    51      {
    52          final String BEAN_NAME = "persistentBackingBean";
    53
    54          PersistentBackingBean conv = (PersistentBackingBean) applicationContext.getBean(BEAN_NAME);
    55
    56          /* create and reread a user */
    57          UserData user = conv.createUser();
    58          UserData user2 = conv.readUser(user.getId());
    59
    60          assertSame("has to be the same user",user , user2);
    61
    62          conv.invalidateConversation();
    63
    64          /* restart conversatin */
    65          UserData user3 = conv.readUser(user.getId());
    66
    67          assertNotSame("should not be the same user", user ,user3);
    68      }
    69
    70      public void testRestartConversation()
    71      {
    72          final String BEAN_NAME = "persistentBackingBean";
    73
    74          PersistentBackingBean bean = (PersistentBackingBean) applicationContext.getBean(BEAN_NAME);
    75
    76          UserData user = bean.createUser();
    77
    78          bean.invalidateAndRestartConversation();
    79
    80          /* here we access the new conversation */
    81          UserData restartedUser = bean.getRestartedUser();
    82
    83          assertNotNull("should have got a user", restartedUser);
    84          assertNotSame("should not be the same user", user, restartedUser);
    85          assertEquals("has to be the same user id", user.getId(), restartedUser.getId());
    86
    87          /* end all conversations*/
    88          ConversationManager.getInstance().clearCurrentConversationContext();
    89
    90          bean.updateUser(user.getId(), "test2");
    91
    92          /* invalidate conversation */
    93          bean.invalidateConversation();
    94
    95          UserData user4 = bean.readUser(user.getId());
    96
    97          assertNotNull(user4);
    98          assertEquals(user.getId(), user4.getId());
    99          assertEquals("test", user4.getUsername());
    100     }
    101 }