You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2007/11/30 13:43:08 UTC

svn commit: r599803 - in /myfaces/orchestra/trunk/core/src/test: java/org/apache/myfaces/orchestra/conversation/ resources/org/ resources/org/apache/ resources/org/apache/myfaces/ resources/org/apache/myfaces/orchestra/ resources/org/apache/myfaces/orc...

Author: skitching
Date: Fri Nov 30 04:43:06 2007
New Revision: 599803

URL: http://svn.apache.org/viewvc?rev=599803&view=rev
Log:
Add unit tests for the "auto scope proxying" feature: see ORCHESTRA-10

Added:
    myfaces/orchestra/trunk/core/src/test/java/org/apache/myfaces/orchestra/conversation/SimpleBean.java
    myfaces/orchestra/trunk/core/src/test/java/org/apache/myfaces/orchestra/conversation/TestScope.java
    myfaces/orchestra/trunk/core/src/test/resources/org/
    myfaces/orchestra/trunk/core/src/test/resources/org/apache/
    myfaces/orchestra/trunk/core/src/test/resources/org/apache/myfaces/
    myfaces/orchestra/trunk/core/src/test/resources/org/apache/myfaces/orchestra/
    myfaces/orchestra/trunk/core/src/test/resources/org/apache/myfaces/orchestra/conversation/
    myfaces/orchestra/trunk/core/src/test/resources/org/apache/myfaces/orchestra/conversation/TestScope.xml

Added: myfaces/orchestra/trunk/core/src/test/java/org/apache/myfaces/orchestra/conversation/SimpleBean.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/test/java/org/apache/myfaces/orchestra/conversation/SimpleBean.java?rev=599803&view=auto
==============================================================================
--- myfaces/orchestra/trunk/core/src/test/java/org/apache/myfaces/orchestra/conversation/SimpleBean.java (added)
+++ myfaces/orchestra/trunk/core/src/test/java/org/apache/myfaces/orchestra/conversation/SimpleBean.java Fri Nov 30 04:43:06 2007
@@ -0,0 +1,59 @@
+/*
+ * 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;
+
+public class SimpleBean
+{
+	private String data;
+
+	SimpleBean[] thisRefHolder;
+	SimpleBean thisRef;
+	
+	public SimpleBean()
+	{
+	}
+
+	public SimpleBean getThis()
+	{
+		thisRefHolder = new SimpleBean[1];
+		thisRefHolder[0] = this;
+		thisRef = this;
+		return this;
+	}
+	
+	public SimpleBean getThisRef()
+	{
+		return thisRef;
+	}
+
+	public SimpleBean[] getThisRefHolder()
+	{
+		return thisRefHolder;
+	}
+
+	public String getData()
+	{
+		return data;
+	}
+
+	public void setData(String data)
+	{
+		this.data = data;
+	}
+}

Added: myfaces/orchestra/trunk/core/src/test/java/org/apache/myfaces/orchestra/conversation/TestScope.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/test/java/org/apache/myfaces/orchestra/conversation/TestScope.java?rev=599803&view=auto
==============================================================================
--- myfaces/orchestra/trunk/core/src/test/java/org/apache/myfaces/orchestra/conversation/TestScope.java (added)
+++ myfaces/orchestra/trunk/core/src/test/java/org/apache/myfaces/orchestra/conversation/TestScope.java Fri Nov 30 04:43:06 2007
@@ -0,0 +1,84 @@
+/*
+ * 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;
+
+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 aspects of the conversation handling
+ */
+public class TestScope extends AbstractDependencyInjectionSpringContextTests
+{
+	protected String[] getConfigLocations()
+	{
+		return new String[]
+			{
+				"classpath:org/apache/myfaces/orchestra/conversation/TestScope.xml"
+			};
+	}
+
+	protected void onSetUp() throws Exception
+	{
+		super.onSetUp();
+    }
+	
+	public void testFoo() throws Exception {
+
+		// Set up the FrameworkAdapter. This is needed by any ConversationManager operation.
+		LocalFrameworkAdapter frameworkAdapter = new LocalFrameworkAdapter();
+		frameworkAdapter.setApplicationContext(applicationContext);
+		frameworkAdapter.setConversationMessager(new LogConversationMessager());
+        FrameworkAdapter.setCurrentInstance(frameworkAdapter);
+
+		// Get the object from spring. Orchestra should wrap it in a proxy that implements ScopedObject 
+        SimpleBean b1 = (SimpleBean) applicationContext.getBean("unscopedBean");
+		assertNotNull(b1);
+		assertTrue(b1 instanceof ScopedObject);
+
+		// The proxy also checks any return values, and modifies them so that methods on the target
+		// that return the target object actually return the proxy. Tricky! This means that the
+		// "method chaining" pattern can work, eg foo.doX().doY().doZ() and all invocations pass
+		// through the proxy.
+		SimpleBean b1a = b1.getThis();
+		assertTrue(b1 == b1a);
+		assertTrue(b1 == b1.getThisRef());
+		
+		// However the proxy cannot completely hide itself. The most obvious way is that the proxy
+		// has fields (because it subclasses SimpleBean) but they are not initialised by the call
+		// to getThis(), because it is the target object that ran that method, not the proxy.
+		assertNull(b1.thisRef);
+		assertNull(b1.thisRefHolder);
+		
+		// And it cannot perform its "ref replacement" trick when the ref is nested inside some
+		// more complicated object. Note that this means that when the target object passes its
+		// "this" parameter to another object, it is the raw unproxied this that gets passed.
+		SimpleBean[] refHolder = b1.getThisRefHolder();
+		assertNotNull(refHolder);
+		assertFalse(b1 == refHolder[0]);
+		
+		b1.setData("hello, world");
+		String s1 = b1.getData();
+		assertEquals("hello, world", s1);
+		
+	}
+}

Added: myfaces/orchestra/trunk/core/src/test/resources/org/apache/myfaces/orchestra/conversation/TestScope.xml
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/test/resources/org/apache/myfaces/orchestra/conversation/TestScope.xml?rev=599803&view=auto
==============================================================================
--- myfaces/orchestra/trunk/core/src/test/resources/org/apache/myfaces/orchestra/conversation/TestScope.xml (added)
+++ myfaces/orchestra/trunk/core/src/test/resources/org/apache/myfaces/orchestra/conversation/TestScope.xml Fri Nov 30 04:43:06 2007
@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+  -->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	   xmlns:tx="http://www.springframework.org/schema/tx"
+	   xmlns:aop="http://www.springframework.org/schema/aop"
+	   xsi:schemaLocation="
+			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
+			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
+			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
+
+	<!-- register our custom spring scope -->
+	<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
+		<property name="scopes">
+			<map>
+				<entry key="conversation.manual">
+					<bean class="org.apache.myfaces.orchestra.conversation.spring.SpringConversationScope">
+					  <property name="timeout" value="30"/>
+					</bean>
+				</entry>
+				<entry key="conversation.flash">
+					<bean class="org.apache.myfaces.orchestra.conversation.spring.SpringConversationScope">
+					  <property name="lifetime" value="flash"/>
+					</bean>
+				</entry>
+			</map>
+		</property>
+	</bean>
+
+
+	<!-- our beans -->
+
+	<bean
+		name="scopedBean"
+		class="org.apache.myfaces.orchestra.conversation.SimpleBean"
+		scope="conversation.manual"
+		autowire="byName">
+
+		<aop:scoped-proxy/>
+	</bean>
+
+
+	<bean
+		name="unscopedBean"
+		class="org.apache.myfaces.orchestra.conversation.SimpleBean"
+		scope="conversation.manual"
+		autowire="byName">
+	</bean>
+
+</beans>
\ No newline at end of file