Author: lfryc(a)redhat.com
Date: 2010-10-28 12:01:29 -0400 (Thu, 28 Oct 2010)
New Revision: 19734
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/jboss/test/selenium/RequestTypeModelGuard.java
Modified:
modules/tests/metamer/trunk/ftest-source/pom.xml
Log:
added request type guard for non-atomic selenium operations over model
Modified: modules/tests/metamer/trunk/ftest-source/pom.xml
===================================================================
--- modules/tests/metamer/trunk/ftest-source/pom.xml 2010-10-28 15:26:57 UTC (rev 19733)
+++ modules/tests/metamer/trunk/ftest-source/pom.xml 2010-10-28 16:01:29 UTC (rev 19734)
@@ -60,6 +60,11 @@
<version>${project.version}</version>
<classifier>classes</classifier>
</dependency>
+ <dependency>
+ <groupId>javassist</groupId>
+ <artifactId>javassist</artifactId>
+ <version>3.12.1.GA</version>
+ </dependency>
</dependencies>
<build>
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/jboss/test/selenium/RequestTypeModelGuard.java
===================================================================
---
modules/tests/metamer/trunk/ftest-source/src/main/java/org/jboss/test/selenium/RequestTypeModelGuard.java
(rev 0)
+++
modules/tests/metamer/trunk/ftest-source/src/main/java/org/jboss/test/selenium/RequestTypeModelGuard.java 2010-10-28
16:01:29 UTC (rev 19734)
@@ -0,0 +1,129 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.test.selenium;
+
+import java.lang.reflect.Method;
+
+import javassist.util.proxy.MethodHandler;
+import javassist.util.proxy.ProxyFactory;
+import javassist.util.proxy.ProxyObject;
+
+import org.jboss.test.selenium.guard.request.RequestTypeGuard;
+import org.jboss.test.selenium.request.RequestType;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class RequestTypeModelGuard implements MethodHandler {
+ private RequestTypeGuard guard;
+
+ private RequestTypeModelGuard(RequestTypeGuard guard) {
+ this.guard = guard;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <T extends Model> T guard(T model, RequestType requestExpected,
boolean interlayed) {
+ RequestTypeGuard guard = new RequestTypeGuard(requestExpected, interlayed);
+
+ ProxyFactory f = new ProxyFactory();
+ f.setSuperclass(model.getClass());
+ Class<T> c = f.createClass();
+
+ T newInstance;
+ try {
+ newInstance = (T) c.newInstance();
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+ ((ProxyObject) newInstance).setHandler(new RequestTypeModelGuard(guard));
+ return newInstance;
+ }
+
+ @Override
+ public Object invoke(Object self, Method method, Method proceed, Object[] args)
throws Throwable {
+ guard.doBeforeCommand();
+ Object result = proceed.invoke(self, args);
+ guard.doAfterCommand();
+
+ return result;
+ }
+
+ /**
+ * Shortcut for registering a XMLHttpRequest on given selenium object.
+ *
+ * @param selenium
+ * where should be registered XMLHttpRequest guard
+ * @return the selenium guarded to use XMLHttpRequest
+ */
+ public static <T extends Model> T guardXhr(T model) {
+ return guard(model, RequestType.XHR, false);
+ }
+
+ /**
+ * Shortcut for registering a regular HTTP request on given selenium object.
+ *
+ * @param selenium
+ * where should be registered regular HTTP request guard
+ * @return the selenium guarded to use regular HTTP requests
+ */
+ public static <T extends Model> T guardHttp(T selenium) {
+ return guard(selenium, RequestType.HTTP, false);
+ }
+
+ /**
+ * Shortcut for registering a guard for no request on given selenium object.
+ *
+ * @param selenium
+ * where should be registered no request guard
+ * @return the selenium guarded to use no request during interaction
+ */
+ public static <T extends Model> T guardNoRequest(T selenium) {
+ return guard(selenium, RequestType.NONE, false);
+ }
+
+ /**
+ * Shortcut for registering guard waiting for interception of XHR type request
+ *
+ * @param selenium
+ * where should be the guard registered
+ * @return the selenium waiting for interception of XHR type request
+ */
+ public static <T extends Model> T waitXhr(T selenium) {
+ return guard(selenium, RequestType.XHR, true);
+ }
+
+ /**
+ * Shortcut for registering guard waiting for interception of HTTP type request
+ *
+ * @param selenium
+ * selenium where should be the guard registered
+ * @return the selenium waitinf for interception of HTTP type request
+ */
+ public static <T extends Model> T waitHttp(T selenium) {
+ return guard(selenium, RequestType.HTTP, true);
+ }
+
+ public interface Model {
+ }
+
+}