    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.spring;
    20
    21  import org.apache.myfaces.orchestra.conversation.Conversation;
    22  import org.apache.myfaces.orchestra.conversation.ConversationManager;
    23  import org.apache.myfaces.orchestra.conversation.SimpleBean;
    24  import org.apache.myfaces.orchestra.conversation.basic.LogConversationMessager;
    25  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
    26  import org.apache.myfaces.orchestra.frameworkAdapter.local.LocalFrameworkAdapter;
    27  import org.springframework.aop.SpringProxy;
    28  import org.springframework.aop.scope.ScopedObject;
    29  import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
    30
    31
    32  /**
    33   * Test various methods on the _SpringUtils class.
    34   */
    35  public class TestSpringUtils extends AbstractDependencyInjectionSpringContextTests
    36  {
    37      protected String[] getConfigLocations()
    38      {
    39          return new String[]
    40              {
    41                  "classpath:org/apache/myfaces/orchestra/conversation/spring/TestSpringUtils.xml"
    42              };
    43      }
    44
    45      protected void onSetUp() throws Exception
    46      {
    47          super.onSetUp();
    48
    49          LocalFrameworkAdapter frameworkAdapter = new LocalFrameworkAdapter();
    50          frameworkAdapter.setApplicationContext(applicationContext);
    51          frameworkAdapter.setConversationMessager(new LogConversationMessager());
    52          FrameworkAdapter.setCurrentInstance(frameworkAdapter);
    53      }
    54
    55      protected void onTearDown() throws Exception
    56      {
    57          FrameworkAdapter.setCurrentInstance(null);
    58          super.onTearDown();
    59      }
    60
    61      public void testConversation() throws Exception
    62      {
    63          final String BEAN_NAME = "simpleBean";
    64
    65          // The Spring configuration for dummyBean does not explicitly set a conversation name,
    66          // so conversation-name = bean-name
    67          final String CONVERSATION_NAME = BEAN_NAME;
    68
    69          // create a scoped-proxy (but not the underlying bean)
    70          SimpleBean scopedProxy = (SimpleBean) applicationContext.getBean(BEAN_NAME);
    71
    72          assertTrue("should be a scoped object", scopedProxy instanceof ScopedObject);
    73          assertTrue("should be a proxy", scopedProxy instanceof SpringProxy);
    74          assertFalse("conversation should not have been started yet", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
    75
    76          // Invoke a method on the scoped-proxy, forcing the conversation-proxy to be created, which will in turn
    77          // force the real bean to be created. The name "conversation-proxy" means the proxy to which the advices
    78          // are attached, including the CurrentConversationAdvice which sets up the conversation before the real
    79          // bean is invoked.
    80          scopedProxy.getData();
    81          assertTrue("conversation should have been started", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
    82
    83          // Obtain a reference to the conversation-proxy bean. The scoped-proxy and conversation-proxy will be different objects.
    84          SimpleBean convProxy = (SimpleBean) _SpringUtils.getTargetObject(scopedProxy);
    85          assertTrue("should be a proxy", convProxy instanceof SpringProxy);
    86          assertTrue(convProxy != scopedProxy);
    87
    88          // Just for fun, obtain a reference to the real actual bean too.
    89          SimpleBean realBean = (SimpleBean) _SpringUtils.getTargetObject(convProxy);
    90          assertFalse("should not be a proxy", realBean instanceof SpringProxy);
    91          assertTrue(realBean != convProxy);
    92
    93          // Check that the scoped-proxy maps all method-calls to the conversation-proxy which in turn maps the
    94          // real object by modifying data on the underlying object then reading  via the indirect reference, and
    95          // vice-versa
    96          assertEquals(null, convProxy.getData());
    97          scopedProxy.setData("proxy");
    98          assertEquals("proxy", realBean.getData());
    99          realBean.setData("real");
    100         assertEquals("real", scopedProxy.getData());
    101
    102         // After invalidating the conversation and recreating the bean, the scopedProxy no longer refers
    103         // to the same object.
    104         Conversation conv = ConversationManager.getInstance().getConversation(CONVERSATION_NAME);
    105         conv.invalidate();
    106         assertEquals(null, scopedProxy.getData()); // this is a new object with reset data property
    107         SimpleBean newConvProxy = (SimpleBean) _SpringUtils.getTargetObject(scopedProxy);
    108         assertNotSame(convProxy, newConvProxy);
    109
    110         // Writing via the proxy no longer touches the original realBean
    111         scopedProxy.setData("proxy");
    112         assertTrue("proxy".equals(scopedProxy.getData()));
    113         assertFalse("proxy".equals(realBean.getData()));
    114
    115         // And accessing the old proxy is no longer allowed. Note that this condition isn't normally
    116         // possible in user code as users don't use non-public api _SpringUtils.getTargetObject to
    117         // unwrap proxies.
    118         try
    119         {
    120             convProxy.getData();
    121             fail("old proxy still accessable after conversation has terminated");
    122         }
    123         catch(IllegalStateException e)
    124         {
    125             // ok, expected
    126         }
    127     }
    128 }