/** * Returns the instance of proxy to thread local context of configuration * * @return the instance of proxy to thread local context of configuration */ public static RichFacesPageFragmentsConfiguration getProxy() { return GrapheneProxy.getProxyForHandler(new GrapheneProxyHandler(TARGET) { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(TARGET.getTarget(), args); } }, RichFacesPageFragmentsConfiguration.class); }
/** * Sets the configuration context for current thread * * @param configuration the configuration instance * @throws IllegalArgumentException when provided configuration instance is null */ public static void set(FundamentalTestConfiguration configuration) { if (configuration == null) { throw new IllegalArgumentException("configuration instance can't be null"); } if (GrapheneProxy.isProxyInstance(configuration)) { throw new IllegalArgumentException("instance of the proxy can't be set to the configuration"); } REFERENCE.set(configuration); }
protected final <T> List<T> createPageFragmentList(final Class<T> clazz, final SearchContext searchContext, final By rootBy) { GrapheneContext grapheneContext = ((GrapheneProxyInstance) searchContext).getGrapheneContext(); List<T> result = GrapheneProxy.getProxyForFutureTarget(grapheneContext, new FutureTarget() { @Override public Object getTarget() { List<WebElement> elements = searchContext.findElements(rootBy); List<T> fragments = new ArrayList<T>(); for (int i = 0; i < elements.size(); i++) { fragments.add(createPageFragment(clazz, WebElementUtils.findElementLazily(rootBy, searchContext, i))); } return fragments; } }, List.class); return result; }
/** * <p> * Wraps the given target instance in the proxy. * </p> * * <p> * The list of interfaces which should be implemented by the proxy is automatically computer from provided instance. * </p> * * * @param target the target instance to be wrapped * @return the proxy wrapping the target */ @SuppressWarnings("unchecked") public static <T> T getProxyForTarget(GrapheneContext context, T target) { if (Modifier.isFinal(target.getClass().getModifiers())) { if (target.getClass().getInterfaces().length > 0) { return GrapheneProxy.getProxyForTargetWithInterfaces(context, target, target.getClass().getInterfaces()); } else { throw new IllegalStateException("Can't create a proxy for " + target.getClass() + ", it's final and id doesn't implement any interface."); } } GrapheneProxyHandler handler = GrapheneContextualHandler.forTarget(context, target); return (T) createProxy(handler, target.getClass()); }
if (baseType != null && !baseType.isInterface() && Modifier.isFinal(baseType.getModifiers())) { if (additionalInterfaces.length > 0) { return GrapheneProxy.getProxyForFutureTarget(context, futureTarget, additionalInterfaces[0], additionalInterfaces); } else { throw new IllegalStateException("Can't create a proxy for " + baseType return getProxyForHandler(handler, baseType, additionalInterfaces);
if (GrapheneProxy.isProxyInstance(target)) { proxy = (GrapheneProxyInstance) ((GrapheneProxyInstance) target).copy(); } else { proxy = (GrapheneProxyInstance) GrapheneProxy.getProxyForTarget(context, target);
@Test public void test() throws Throwable { InterceptorBuilder builder = new InterceptorBuilder(); builder.interceptInvocation(WebDriver.class, interceptor1).findElement(Interceptors.any(By.class)); builder.interceptInvocation(WebDriver.class, interceptor2).findElement(Interceptors.any(By.class)); Interceptor builtInterceptor = builder.build(); WebDriver driverProxy = GrapheneProxy.getProxyForTargetWithInterfaces(context, driver, WebDriver.class); GrapheneProxyInstance proxy = (GrapheneProxyInstance) driverProxy; proxy.registerInterceptor(builtInterceptor); driverProxy.findElement(by); Mockito.inOrder(interceptor1, interceptor2); verify(interceptor1).intercept(Mockito.any(InvocationContext.class)); verify(interceptor2).intercept(Mockito.any(InvocationContext.class)); } }
clone = (GrapheneProxyInstance) GrapheneProxy.getProxyForTarget(context, getTarget()); for (Interceptor interceptor : getSortedInterceptorsByPrecedence()) { clone.registerInterceptor(interceptor);
@SuppressWarnings("unchecked") public static <T> T getProxyForHandler(GrapheneProxyHandler handler, Class<?> baseType, Class<?>... additionalInterfaces) { return (T) createProxy(handler, baseType, additionalInterfaces); }
if (baseType != null && !baseType.isInterface() && Modifier.isFinal(baseType.getModifiers())) { if (additionalInterfaces.length > 0) { return GrapheneProxy.getProxyForFutureTarget(context, futureTarget, additionalInterfaces[0], additionalInterfaces); } else { throw new IllegalStateException("Can't create a proxy for " + baseType return getProxyForHandler(handler, baseType, additionalInterfaces);
if (GrapheneProxy.isProxyInstance(target)) { proxy = (GrapheneProxyInstance) ((GrapheneProxyInstance) target).copy(); } else { proxy = (GrapheneProxyInstance) GrapheneProxy.getProxyForTarget(context, target);
/** * <p> * Wraps the given target instance in the proxy. * </p> * * <p> * The list of interfaces which should be implemented by the proxy is automatically computer from provided instance. * </p> * * * @param target the target instance to be wrapped * @return the proxy wrapping the target */ @SuppressWarnings("unchecked") public static <T> T getProxyForTarget(GrapheneContext context, T target) { if (Modifier.isFinal(target.getClass().getModifiers())) { if (target.getClass().getInterfaces().length > 0) { return GrapheneProxy.getProxyForTargetWithInterfaces(context, target, target.getClass().getInterfaces()); } else { throw new IllegalStateException("Can't create a proxy for " + target.getClass() + ", it's final and id doesn't implement any interface."); } } GrapheneProxyHandler handler = GrapheneContextualHandler.forTarget(context, target); return (T) createProxy(handler, target.getClass()); }
Object newProxy = GrapheneProxy.getProxyForTargetWithInterfaces(context, result, interfaces);
clone = (GrapheneProxyInstance) GrapheneProxy.getProxyForTarget(context, getTarget()); for (Interceptor interceptor : getSortedInterceptorsByPrecedence()) { clone.registerInterceptor(interceptor);
@SuppressWarnings("unchecked") public static <T> T getProxyForHandler(GrapheneProxyHandler handler, Class<?> baseType, Class<?>... additionalInterfaces) { return (T) createProxy(handler, baseType, additionalInterfaces); }
/** * Returns the instance of proxy to thread local context of configuration * * @return the instance of proxy to thread local context of configuration */ public static FundamentalTestConfiguration getProxy() { return GrapheneProxy.getProxyForHandler(new GrapheneProxyHandler(TARGET) { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(TARGET.getTarget(), args); } }, FundamentalTestConfiguration.class); }
/** * Sets the configuration context for current thread * * @param configuration the configuration instance * @throws IllegalArgumentException when provided configuration instance is null */ public static void set(RichFacesPageFragmentsConfiguration configuration) { if (configuration == null) { throw new IllegalArgumentException("configuration instance can't be null"); } if (GrapheneProxy.isProxyInstance(configuration)) { throw new IllegalArgumentException("instance of the proxy can't be set to the configuration"); } REFERENCE.set(configuration); }
protected final <T> List<T> createPageFragmentList(final Class<T> clazz, final SearchContext searchContext, final By rootBy) { GrapheneContext grapheneContext = ((GrapheneProxyInstance) searchContext).getGrapheneContext(); List<T> result = GrapheneProxy.getProxyForFutureTarget(grapheneContext, new FutureTarget() { @Override public Object getTarget() { List<WebElement> elements = searchContext.findElements(rootBy); List<T> fragments = new ArrayList<T>(); for (int i = 0; i < elements.size(); i++) { fragments.add(createPageFragment(clazz, WebElementUtils.findElementLazily(rootBy, searchContext, i))); } return fragments; } }, List.class); return result; }
Object newProxy = GrapheneProxy.getProxyForTargetWithInterfaces(context, result, interfaces);
@Test public void testInterceptorCalling() { // having MyObject target = new MyObject(); MyObject proxy = GrapheneProxy.getProxyForTarget(null, target); // when ((GrapheneProxyInstance) proxy).registerInterceptor(new Interceptor() { @Override public Object intercept(InvocationContext context) throws Throwable { return context.invoke(); } @Override public int getPrecedence() { return 1; } }); proxy.someMethod(); }