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.scope.ScopedObject;
28  import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
29  
30  
31  /**
32   * Test various methods on the _SpringUtils class.
33   */
34  public class TestSpringUtils extends AbstractDependencyInjectionSpringContextTests
35  {
36      protected String[] getConfigLocations()
37      {
38          return new String[]
39              {
40                  "classpath:org/apache/myfaces/orchestra/conversation/spring/TestSpringUtils.xml"
41              };
42      }
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      protected void onTearDown() throws Exception
55      {
56          FrameworkAdapter.setCurrentInstance(null);
57          super.onTearDown();
58      }
59  
60      public void testConversation() throws Exception
61      {
62          final String BEAN_NAME = "simpleBean";
63  
64          // The Spring configuration for dummyBean does not explicitly set a conversation name,
65          // so conversation-name = bean-name
66          final String CONVERSATION_NAME = BEAN_NAME;
67  
68          // create a proxy (but not the underlying bean)
69          SimpleBean proxy = (SimpleBean) applicationContext.getBean(BEAN_NAME);
70  
71          assertTrue("should be a scoped object", proxy instanceof ScopedObject);
72          assertFalse("conversation should not have been started yet", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
73  
74          // Invoke a method on the proxy, forcing the real object to be created
75          proxy.getData();
76          assertTrue("conversation should have been started", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
77  
78          // Obtain a reference to the real bean. The proxy and real bean will be different objects.
79          SimpleBean real = (SimpleBean) _SpringUtils.getRealBean(proxy);
80          assertTrue(real != proxy);
81          
82          // Check that the proxy maps to the real object by modifying via the proxy and reading via
83          // the direct reference, and vice-versa
84          assertEquals(null, real.getData());
85          proxy.setData("proxy");
86          assertEquals("proxy", real.getData());
87          real.setData("real");
88          assertEquals("real", proxy.getData());
89  
90          // After invalidating the conversation and recreating the bean, the proxy and
91          // real are no longer the same object.
92          Conversation conv = ConversationManager.getInstance().getConversation(CONVERSATION_NAME);
93          conv.invalidate();
94          proxy.setData("proxy");
95          assertEquals("real", real.getData());
96          real.setData("real");
97          assertEquals("proxy", proxy.getData());
98      }
99  }