You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ja...@apache.org on 2010/09/14 18:01:36 UTC
svn commit: r996967 - in
/myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl:
ProxyInterceptor.java WebappTestConfiguratorImpl.java WebappTesterImpl.java
Author: jakobk
Date: Tue Sep 14 16:01:36 2010
New Revision: 996967
URL: http://svn.apache.org/viewvc?rev=996967&view=rev
Log:
MYFACESTEST-33 WebappTesterImpl does not need to be proxied
Modified:
myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/ProxyInterceptor.java
myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/WebappTestConfiguratorImpl.java
myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/WebappTesterImpl.java
Modified: myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/ProxyInterceptor.java
URL: http://svn.apache.org/viewvc/myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/ProxyInterceptor.java?rev=996967&r1=996966&r2=996967&view=diff
==============================================================================
--- myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/ProxyInterceptor.java (original)
+++ myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/ProxyInterceptor.java Tue Sep 14 16:01:36 2010
@@ -22,115 +22,80 @@ import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.myfaces.test.webapp.api.ResourceFinder;
-import org.apache.myfaces.test.webapp.api.tester.ServerSideCode;
-import org.apache.myfaces.test.webapp.impl.metadata.assertion.AssertableELExpression;
import org.apache.myfaces.test.webapp.impl.metadata.assertion.AssertableProxyMethod;
-import org.apache.myfaces.test.webapp.impl.metadata.assertion.AssertableServerSideCode;
-import org.apache.myfaces.test.webapp.impl.metadata.assertion.AssertableValue;
-import sun.reflect.Reflection;
-import javax.faces.context.FacesContext;
import java.lang.reflect.Method;
import java.util.logging.Logger;
/**
* @author Cosmin Martinconi, 14.07.2010
*/
-public class ProxyInterceptor implements MethodInterceptor {
+public class ProxyInterceptor implements MethodInterceptor
+{
private static final Logger log = Logger.getLogger(ProxyInterceptor.class.getName());
+ public static ProxyInterceptor getCurrentInstance()
+ {
+ return (ProxyInterceptor) ResourceFinder.getResource(ProxyInterceptor.class.getName());
+ }
+
private AssertableProxyMethod methodInvoked;
private Object methodReturnValue;
private Class testClass;
- public ProxyInterceptor() {
- methodInvoked = new AssertableProxyMethod();
+ public ProxyInterceptor()
+ {
}
- public Object newInstance(Class clazz) {
- try {
+ public Object newInstance(Class clazz)
+ {
+ try
+ {
Enhancer e = new Enhancer();
e.setSuperclass(clazz);
e.setCallback(this);
return e.create();
- } catch (Throwable e) {
+ }
+ catch (Throwable e)
+ {
log.severe("Could not generate ne instance of class " + clazz.getName());
throw new Error(e.getMessage());
}
}
- public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
-
- // if assertThat() or expectCall() method is called an AssertableValue will be injected as argument
- if (WebappTesterImpl.class.isInstance(obj) && ( method.getName().equals("assertThat")
- || method.getName().equals("expectCall"))) {
- AssertableValue assertValue = getAssertableValue(args[0]);
-
- if (assertValue == null)
- throw new IllegalArgumentException(args[0] + " argument of " + method + " method is not supported.");
- else
- return proxy.invokeSuper(obj, new Object[]{assertValue});
- }
-
- if (testClass.equals(Reflection.getCallerClass(3))) {
- saveMethodInvoked(obj, args, proxy);
-
- // calls are not supported on FacesContext since it has no implementation attached yet
- if (FacesContext.class.isAssignableFrom(obj.getClass()))
- return methodReturnValue = null;
-
- methodReturnValue = methodInvoked.evaluate();
- return methodReturnValue;
- }
-
- return proxy.invokeSuper(obj, args);
- }
+ public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable
+ {
+ // save the method invocation
+ saveMethodInvoked(obj, args, proxy);
- public static ProxyInterceptor getCurrentInstance() {
- return (ProxyInterceptor) ResourceFinder.getResource(ProxyInterceptor.class.getName());
+ // return value not needed
+ return null;
}
- public void setTestClass(Class tClass) {
+ public void setTestClass(Class tClass)
+ {
testClass = tClass;
}
- public AssertableProxyMethod getSavedMethod() {
- return new AssertableProxyMethod(methodInvoked.getMethodObj(),
- methodInvoked.getMethodArgs(),
- methodInvoked.getMethodProxy());
- }
+ public AssertableProxyMethod getSavedMethod()
+ {
+ AssertableProxyMethod localMethodInvoked = methodInvoked;
- private void saveMethodInvoked(Object obj, Object[] args, MethodProxy proxy) {
- methodInvoked.setMethodObj(obj);
- methodInvoked.setMethodArgs(args);
- methodInvoked.setMethodProxy(proxy);
- }
-
- private AssertableValue getAssertableValue(Object arg) {
-
- if (arg == methodReturnValue || (arg != null && arg.equals(methodReturnValue))) {
- return getSavedMethod();
- }
+ // clear the saved method
+ methodInvoked = null;
- if (String.class.isInstance(arg)) {
- String elValue = (String) arg;
+ return localMethodInvoked;
+ }
- if (elValue.matches("#\\{.*}")) {
- AssertableELExpression elExpression = new AssertableELExpression();
- elExpression.setELValue(elValue);
- return elExpression;
- }
- else {
- throw new IllegalArgumentException("String value " + elValue + " is not a valid EL expression!");
- }
- }
- else if (arg instanceof ServerSideCode)
- {
- return new AssertableServerSideCode((ServerSideCode) arg);
- }
+ public boolean hasSavedMethod()
+ {
+ return methodInvoked != null;
+ }
- return null;
+ private void saveMethodInvoked(Object obj, Object[] args, MethodProxy proxy)
+ {
+ methodInvoked = new AssertableProxyMethod(obj, args, proxy);
}
}
Modified: myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/WebappTestConfiguratorImpl.java
URL: http://svn.apache.org/viewvc/myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/WebappTestConfiguratorImpl.java?rev=996967&r1=996966&r2=996967&view=diff
==============================================================================
--- myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/WebappTestConfiguratorImpl.java (original)
+++ myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/WebappTestConfiguratorImpl.java Tue Sep 14 16:01:36 2010
@@ -44,8 +44,6 @@ public class WebappTestConfiguratorImpl
private static final String PROXY_INTERCEPTOR = ProxyInterceptor.class.getName();
private static final String TEST_ASSERT_ERRORS = TestErrorManager.class.getName();
- public static final Class DEFAULT_WEBAPP_TESTER = WebappTesterImpl.class;
-
public void configure(Class testClass) {
configureResources();
@@ -123,7 +121,7 @@ public class WebappTestConfiguratorImpl
ResourceFinder.setResource(TEST_ASSERT_ERRORS, new TestErrorManager());
// add API dependent resources
- ResourceFinder.setResource(ResourceFinder.WEBAPP_TESTER, createProxy(DEFAULT_WEBAPP_TESTER ));
+ ResourceFinder.setResource(ResourceFinder.WEBAPP_TESTER, new WebappTesterImpl());
}
Modified: myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/WebappTesterImpl.java
URL: http://svn.apache.org/viewvc/myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/WebappTesterImpl.java?rev=996967&r1=996966&r2=996967&view=diff
==============================================================================
--- myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/WebappTesterImpl.java (original)
+++ myfaces/gsoc/webapptest/impl/src/main/java/org/apache/myfaces/test/webapp/impl/WebappTesterImpl.java Tue Sep 14 16:01:36 2010
@@ -22,6 +22,7 @@ import com.gargoylesoftware.htmlunit.Pag
import com.gargoylesoftware.htmlunit.WebClient;
import org.apache.myfaces.test.webapp.api.ResourceFinder;
+import org.apache.myfaces.test.webapp.api.tester.ServerSideCode;
import org.apache.myfaces.test.webapp.api.tester.WebappTester;
import org.apache.myfaces.test.webapp.impl.metadata.TestCaseMetadata;
import org.apache.myfaces.test.webapp.impl.metadata.action.TestActionClick;
@@ -173,46 +174,73 @@ public class WebappTesterImpl implements
Begin methods, that verify expected behavior or values
---------------------------------------------------------------*/
- public Assertable assertThat(Object injectedAssertableValue) {
+ public Assertable assertThat(Object value)
+ {
+ AssertableValue assertableValue = _getAssertableValue(value);
TestAssertCondition assertCondition = new TestAssertCondition();
-
- if ((AssertableValue.class).isAssignableFrom(injectedAssertableValue.getClass())) {
- assertCondition.setAssertableValue((AssertableValue) injectedAssertableValue);
- } else {
- throw new IllegalArgumentException("No " + AssertableValue.class.getName() + " instance found!");
- }
+ assertCondition.setAssertableValue(assertableValue);
getCurrentMetadata().addAssertion(assertCondition);
return new AssertableImpl(assertCondition);
}
- public Call expectCall(Object injectedAssertableValue) {
+ public Call expectCall(Object value)
+ {
+ AssertableValue assertableValue = _getAssertableValue(value);
TestAssertExpectCall assertExpectCall = new TestAssertExpectCall();
-
- if ((AssertableValue.class).isAssignableFrom(injectedAssertableValue.getClass())) {
- assertExpectCall.setAssertableValue((AssertableValue) injectedAssertableValue);
- } else {
- throw new IllegalArgumentException("No " + AssertableValue.class.getName() + " instance found!");
- }
+ assertExpectCall.setAssertableValue(assertableValue);
getCurrentMetadata().addAssertion(assertExpectCall);
return new CallImpl(assertExpectCall);
}
- public final Call expectLastCall() {
- TestAssertExpectCall assertExpectLastCall = new TestAssertExpectCall();
+ public final Call expectLastCall()
+ {
AssertableProxyMethod proxyMethod = ProxyInterceptor.getCurrentInstance().getSavedMethod();
+ TestAssertExpectCall assertExpectLastCall = new TestAssertExpectCall();
assertExpectLastCall.setAssertableValue(proxyMethod);
+
getCurrentMetadata().addAssertion(assertExpectLastCall);
return new CallImpl(assertExpectLastCall);
}
+ private AssertableValue _getAssertableValue(Object value)
+ {
+ // check ProxyInterceptor for method invocation on Assertable bean
+ ProxyInterceptor proxyInterceptor = ProxyInterceptor.getCurrentInstance();
+ if (proxyInterceptor.hasSavedMethod())
+ {
+ return proxyInterceptor.getSavedMethod();
+ }
+ else if (value instanceof String)
+ {
+ String elValue = (String) value;
+
+ if (elValue.matches("#\\{.*}"))
+ {
+ AssertableELExpression elExpression = new AssertableELExpression();
+ elExpression.setELValue(elValue);
+ return elExpression;
+ }
+ else
+ {
+ throw new IllegalArgumentException("String value " + elValue + " is not a valid EL expression!");
+ }
+ }
+ else if (value instanceof ServerSideCode)
+ {
+ return new AssertableServerSideCode((ServerSideCode) value);
+ }
+
+ return null;
+ }
+
/*--------------------------------------------------------------
Begin classes needed for fluent API
---------------------------------------------------------------*/