/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.myfaces.orchestra.conversation.spring;
import org.apache.myfaces.orchestra.conversation.Conversation;
import org.apache.myfaces.orchestra.conversation.ConversationManager;
import org.apache.myfaces.orchestra.conversation.SimpleBean;
import org.apache.myfaces.orchestra.conversation.basic.LogConversationMessager;
import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
import org.apache.myfaces.orchestra.frameworkAdapter.local.LocalFrameworkAdapter;
import org.springframework.aop.scope.ScopedObject;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
/**
* Test various methods on the _SpringUtils class.
*/
public class TestSpringUtils extends AbstractDependencyInjectionSpringContextTests
{
protected String[] getConfigLocations()
{
return new String[]
{
"classpath:org/apache/myfaces/orchestra/conversation/spring/TestSpringUtils.xml"
};
}
protected void onSetUp() throws Exception
{
super.onSetUp();
LocalFrameworkAdapter frameworkAdapter = new LocalFrameworkAdapter();
frameworkAdapter.setApplicationContext(applicationContext);
frameworkAdapter.setConversationMessager(new LogConversationMessager());
FrameworkAdapter.setCurrentInstance(frameworkAdapter);
}
protected void onTearDown() throws Exception
{
FrameworkAdapter.setCurrentInstance(null);
super.onTearDown();
}
public void testConversation() throws Exception
{
final String BEAN_NAME = "simpleBean";
// The Spring configuration for dummyBean does not explicitly set a conversation name,
// so conversation-name = bean-name
final String CONVERSATION_NAME = BEAN_NAME;
// create a proxy (but not the underlying bean)
SimpleBean proxy = (SimpleBean) applicationContext.getBean(BEAN_NAME);
assertTrue("should be a scoped object", proxy instanceof ScopedObject);
assertFalse("conversation should not have been started yet", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
// Invoke a method on the proxy, forcing the real object to be created
proxy.getData();
assertTrue("conversation should have been started", ConversationManager.getInstance().hasConversation(CONVERSATION_NAME));
// Obtain a reference to the real bean. The proxy and real bean will be different objects.
SimpleBean real = (SimpleBean) _SpringUtils.getRealBean(proxy);
assertTrue(real != proxy);
// Check that the proxy maps to the real object by modifying via the proxy and reading via
// the direct reference, and vice-versa
assertEquals(null, real.getData());
proxy.setData("proxy");
assertEquals("proxy", real.getData());
real.setData("real");
assertEquals("real", proxy.getData());
// After invalidating the conversation and recreating the bean, the proxy and
// real are no longer the same object.
Conversation conv = ConversationManager.getInstance().getConversation(CONVERSATION_NAME);
conv.invalidate();
proxy.setData("proxy");
assertEquals("real", real.getData());
real.setData("real");
assertEquals("proxy", proxy.getData());
}
}