Author: lfryc(a)redhat.com
Date: 2010-07-10 17:11:41 -0400 (Sat, 10 Jul 2010)
New Revision: 17907
Added:
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/AbstractPushTestCase.java
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/IntervalTestCase.java
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/PushAttributes.java
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/SimpleTestCase.java
Modified:
root/tests/metamer/trunk/ftest/test-source/pom.xml
Log:
added 2 test cases and utility classes for testing a4j:push
Modified: root/tests/metamer/trunk/ftest/test-source/pom.xml
===================================================================
--- root/tests/metamer/trunk/ftest/test-source/pom.xml 2010-07-10 21:11:05 UTC (rev
17906)
+++ root/tests/metamer/trunk/ftest/test-source/pom.xml 2010-07-10 21:11:41 UTC (rev
17907)
@@ -18,6 +18,11 @@
<groupId>org.jboss.test</groupId>
<artifactId>richfaces-selenium</artifactId>
</dependency>
+ <dependency>
+ <groupId>commons-httpclient</groupId>
+ <artifactId>commons-httpclient</artifactId>
+ <version>3.1</version>
+ </dependency>
</dependencies>
<build>
Added:
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/AbstractPushTestCase.java
===================================================================
---
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/AbstractPushTestCase.java
(rev 0)
+++
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/AbstractPushTestCase.java 2010-07-10
21:11:41 UTC (rev 17907)
@@ -0,0 +1,116 @@
+/*******************************************************************************
+ * 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.richfaces.tests.testapp.a4jPush;
+
+import static org.testng.Assert.*;
+import static org.jboss.test.selenium.utils.URLUtils.buildUrl;
+import static org.jboss.test.selenium.SeleniumGetter.*;
+
+import java.io.IOException;
+import java.net.URL;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.jboss.test.selenium.SystemProperties;
+import org.jboss.test.selenium.locator.ElementLocator;
+import org.jboss.test.selenium.request.RequestType;
+import org.richfaces.tests.testapp.AbstractTestappTestCase;
+import org.testng.annotations.BeforeClass;
+
+/**
+ * Abstract test case for testing a4j:push component.
+ *
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public abstract class AbstractPushTestCase extends AbstractTestappTestCase {
+
+ private static final int STATUS_CODE_OK = 200;
+
+ protected PushAttributes pushAttributes = new PushAttributes();
+
+ private ElementLocator outputCounter = pjq("span[id$=outputCounter]");
+
+ private HttpClient httpClient;
+ private HttpMethod pushMethod;
+
+ @Override
+ public URL getTestUrl() {
+ URL contextPath = SystemProperties.getContextPath();
+ return buildUrl(contextPath, "faces/components/a4jPush/simple.xhtml");
+ }
+
+ /**
+ * Initializes the HttpClient which triggers events in {@link #generatePushEvent()}.
+ */
+ @BeforeClass
+ public void initializeHttpClient() {
+ httpClient = new HttpClient();
+ URL eventProducerUrl = buildUrl(getTestUrl(), "event-producer.xhtml");
+ pushMethod = new GetMethod(eventProducerUrl.toString() +
"?pushEnabled=true");
+ }
+
+ /**
+ * Returns the value of counter as pushed value
+ *
+ * @return the value of counter as pushed value
+ */
+ protected int getCounter() {
+ return getText(outputCounter).asInteger();
+ }
+
+ /**
+ * Push the event specified number times and then waits for observation of event by
client.
+ *
+ * @param numberOfPushes
+ * the number of events should be generated
+ */
+ protected void pushAndWait(int numberOfPushes) throws HttpException, IOException {
+ selenium.getPageExtensions().install();
+ selenium.getRequestInterceptor().clearRequestTypeDone();
+
+ for (int i = 0; i < numberOfPushes; i++) {
+ generatePushEvent();
+ }
+
+ selenium.getRequestInterceptor().waitForRequestTypeChange();
+ RequestType requestDone = selenium.getRequestInterceptor().getRequestTypeDone();
+
+ assertEquals(requestDone, RequestType.XHR);
+ }
+
+ /**
+ * <p>
+ * Generates the push event for all registered listeners from a4j:push test Simple
page.
+ * </p>
+ *
+ * <p>
+ * Internally use HttpClient to ping URL triggering push event in preRenderView
phase.
+ * </p>
+ */
+ private void generatePushEvent() throws HttpException, IOException {
+ httpClient.executeMethod(pushMethod);
+ assertEquals(pushMethod.getStatusCode(), STATUS_CODE_OK);
+ }
+}
Added:
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/IntervalTestCase.java
===================================================================
---
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/IntervalTestCase.java
(rev 0)
+++
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/IntervalTestCase.java 2010-07-10
21:11:41 UTC (rev 17907)
@@ -0,0 +1,151 @@
+/*******************************************************************************
+ * 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.richfaces.tests.testapp.a4jPush;
+
+import static java.lang.System.currentTimeMillis;
+import static org.jboss.test.selenium.utils.text.SimplifiedFormat.format;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+
+import org.apache.commons.httpclient.HttpException;
+import org.testng.annotations.Test;
+
+/**
+ * Tests the interval attribute for a4j:push
+ *
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class IntervalTestCase extends AbstractPushTestCase {
+
+ private static final int[] INTERVAL_VALUES = new int[]{1000, 5000, 500};
+ private static final long MAX_DEVIATION = 300;
+ private static final long MAX_AVERAGE_DEVIATION = 100;
+ private static final int DEFAULT_COUNTER_STEP = 2;
+ private static final int ITERATION_COUNT = 3;
+ private static final int MULTIPLE_PUSH_COUNT = 5;
+
+ private long startTime;
+ private int counter;
+ private int counterStep;
+
+ private long deviationTotal = 0;
+ private long deviationCount = 0;
+
+ /**
+ * <p>
+ * For 3 different intervals, test that the interval between push event triggered and
event observered by client
+ * haven't greater deviation than the specified interval.
+ * </p>
+ *
+ * <p>
+ * First set the given interval into the interval attribute.
+ * </p>
+ *
+ * <p>
+ * Then push and wait for result of update triggered by observation of push event.
+ * </p>
+ *
+ * <p>
+ * Then repeat push and wait 6 times with 1 event triggered and 5 events triggered in
sequence.
+ * </p>
+ */
+ @Test
+ public void testInterval() throws HttpException, IOException {
+ for (int interval : INTERVAL_VALUES) {
+ counterStep = DEFAULT_COUNTER_STEP;
+ pushAttributes.setInterval(interval);
+
+ pushAndWait(1);
+ for (int i = 0; i < ITERATION_COUNT; i++) {
+ startIntervalAndCounter();
+ pushAndWait(1);
+ validateIntervalAndCounter(interval);
+
+ startIntervalAndCounter();
+ pushAndWait(MULTIPLE_PUSH_COUNT);
+ validateIntervalAndCounter(interval);
+ }
+ }
+
+ validateAverageDeviation();
+ }
+
+ /**
+ * Remembers start of the time frame and current value of counter.
+ */
+ private void startIntervalAndCounter() {
+ startTime = currentTimeMillis();
+ counter = getCounter();
+ }
+
+ /**
+ * <p>
+ * Obtains current value of counter and end of the time frame to compute the run
time.
+ * </p>
+ *
+ * <p>
+ * Validates that run time haven't greater deviation from given maximum {@link
#MAX_DEVIATION}.
+ * </p>
+ *
+ * <p>
+ * Remembers the deviation as part of total deviation for computing average value.
+ * </p>
+ *
+ * <p>
+ * Validates that counter have been increased by right value.
+ * </p>
+ */
+ private void validateIntervalAndCounter(int interval) {
+ long runTime = currentTimeMillis() - startTime;
+ long deviation = Math.abs(interval - runTime);
+ assertTrue(deviation <= MAX_DEVIATION, format("Deviation ({0}) is greater
than defined maximum {1}", deviation,
+ MAX_DEVIATION));
+
+ if (seleniumDebug) {
+ System.out.println("deviation: " + deviation);
+ }
+
+ deviationTotal += deviation;
+ deviationCount += 1;
+
+ int newCounter = getCounter();
+ assertEquals(newCounter, counter + counterStep);
+ counter = newCounter;
+
+ }
+
+ /**
+ * Validates the average deviations from intervals.
+ */
+ private void validateAverageDeviation() {
+ long averageDeviation = deviationTotal / deviationCount;
+ if (seleniumDebug) {
+ System.out.println("averageDeviation: " + averageDeviation);
+ }
+ assertTrue(averageDeviation <= MAX_AVERAGE_DEVIATION, format(
+ "Average deviation for all the intervals ({0}) should not be greater
than defined maximum {1}",
+ averageDeviation, MAX_AVERAGE_DEVIATION));
+ }
+}
Added:
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/PushAttributes.java
===================================================================
---
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/PushAttributes.java
(rev 0)
+++
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/PushAttributes.java 2010-07-10
21:11:41 UTC (rev 17907)
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ * 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.richfaces.tests.testapp.a4jPush;
+
+import static org.jboss.test.selenium.locator.LocatorFactory.id;
+import static org.jboss.test.selenium.SeleniumGetter.*;
+
+import org.jboss.test.selenium.framework.AjaxSelenium;
+import org.jboss.test.selenium.framework.AjaxSeleniumProxy;
+import org.jboss.test.selenium.locator.ElementLocator;
+
+/**
+ * The control point for setting different attributes for a4j:push test page.
+ *
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class PushAttributes {
+
+ private AjaxSelenium selenium = AjaxSeleniumProxy.getInstance();
+
+ private ElementLocator attributeInterval =
id("form:attributes:intervalInput");
+
+ public void setInterval(long miliseconds) {
+ selenium.type(attributeInterval, String.valueOf(miliseconds));
+ selenium.waitForPageToLoad();
+ }
+
+ public long getInterval() {
+ return getValue(attributeInterval).asLong();
+ }
+}
Added:
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/SimpleTestCase.java
===================================================================
---
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/SimpleTestCase.java
(rev 0)
+++
root/tests/metamer/trunk/ftest/test-source/src/main/java/org/richfaces/tests/testapp/a4jPush/SimpleTestCase.java 2010-07-10
21:11:41 UTC (rev 17907)
@@ -0,0 +1,65 @@
+/*******************************************************************************
+ * 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.richfaces.tests.testapp.a4jPush;
+
+import static org.testng.Assert.assertNotSame;
+import static org.jboss.test.selenium.utils.text.SimplifiedFormat.format;
+
+import java.io.IOException;
+
+import org.apache.commons.httpclient.HttpException;
+import org.testng.annotations.Test;
+
+/**
+ * Test case for simple usage of a4j:push component.
+ *
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class SimpleTestCase extends AbstractPushTestCase {
+ private static final long SIMPLE_INTERVAL = 2500;
+
+ /**
+ * <p>
+ * Sets the interval to value {@link #SIMPLE_INTERVAL}.
+ * </p>
+ *
+ * <p>
+ * Then try to generate push event, wait for client observation.
+ * </p>
+ *
+ * <p>
+ * Validates that counter is changed between iterations.
+ * </p>
+ */
+ @Test
+ public void testSimple() throws HttpException, IOException {
+ pushAttributes.setInterval(SIMPLE_INTERVAL);
+
+ int beginCounter = getCounter();
+ pushAndWait(1);
+ int endCounter = getCounter();
+
+ assertNotSame(beginCounter < endCounter, format(
+ "The counter before push is greater {0} or equal the end counter
{1}", beginCounter, endCounter));
+ }
+}