]> source.dussan.org Git - vaadin-framework.git/commitdiff
Makes test stable and adds helper comparison methods
authorArtur Signell <artur@vaadin.com>
Mon, 7 Oct 2013 16:56:43 +0000 (19:56 +0300)
committerArtur Signell <artur@vaadin.com>
Tue, 8 Oct 2013 12:27:07 +0000 (15:27 +0300)
* Takes into account that the timer can be triggered multiple times before the initial request is done (especially if the server is slow)
* Adds assertLessThan, assertLessThanOrEqual, assertGreaterThan, assertGreaterThanOrEqual to ease test creation

Change-Id: I5ce1681e35d9c1de02a83b38528f17ee705331d7

uitest/src/com/vaadin/tests/push/PushConfigurationTest.java
uitest/src/com/vaadin/tests/tb3/AbstractTB3Test.java

index d23c48e14af0f42c2c24dd78f7b1e0dd06ce8355..1f8c4c0e38b5528a030c99b6b875a98137b7047f 100644 (file)
@@ -17,7 +17,9 @@ package com.vaadin.tests.push;
 
 import org.junit.Assert;
 import org.junit.Test;
+import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
+import org.openqa.selenium.support.ui.ExpectedCondition;
 import org.openqa.selenium.support.ui.Select;
 
 import com.vaadin.tests.tb3.WebsocketTest;
@@ -29,7 +31,9 @@ public class PushConfigurationTest extends WebsocketTest {
         setDebug(true);
         openTestURL();
         // Websocket
-        Assert.assertEquals(1, getServerCounter());
+        int counter = getServerCounter();
+        assertGreaterOrEqual("Counter should be >= 1. Was: " + counter,
+                counter, 1);
         new Select(getTransportSelect()).selectByVisibleText("WEBSOCKET");
         new Select(getPushModeSelect()).selectByVisibleText("AUTOMATIC");
         Assert.assertTrue(vaadinElement(
@@ -37,20 +41,15 @@ public class PushConfigurationTest extends WebsocketTest {
                 .getText()
                 .matches(
                         "^[\\s\\S]*fallbackTransport: streaming[\\s\\S]*transport: websocket[\\s\\S]*$"));
-        int counter = getServerCounter();
+        counter = getServerCounter();
+        final int waitCounter = counter + 2;
+        waitUntil(new ExpectedCondition<Boolean>() {
 
-        for (int second = 0;; second++) {
-            if (second >= 5) {
-                Assert.fail("timeout");
+            @Override
+            public Boolean apply(WebDriver input) {
+                return (getServerCounter() >= waitCounter);
             }
-            if (getServerCounter() >= (counter + 2)) {
-                break;
-            }
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException e) {
-            }
-        }
+        });
 
         // Use debug console to verify we used the correct transport type
         Assert.assertTrue(driver.getPageSource().contains(
index 5c4ee01d5f37c73a0c4f40d748a3244a8ae1ebff..38c5b29bf9245409e602b9f201978fa8fc730a3f 100644 (file)
@@ -324,6 +324,86 @@ public abstract class AbstractTB3Test extends TestBenchTestCase {
         return getLogRowElement(rowNr).getText();
     }
 
+    /**
+     * Asserts that {@literal a} is &gt;= {@literal b}
+     * 
+     * @param message
+     *            The message to include in the {@link AssertionError}
+     * @param a
+     * @param b
+     * @throws AssertionError
+     *             If comparison fails
+     */
+    public static final <T> void assertGreaterOrEqual(String message,
+            Comparable<T> a, T b) throws AssertionError {
+        if (a.compareTo(b) >= 0) {
+            return;
+        }
+
+        throw new AssertionError(decorate(message, a, b));
+    }
+
+    /**
+     * Asserts that {@literal a} is &gt; {@literal b}
+     * 
+     * @param message
+     *            The message to include in the {@link AssertionError}
+     * @param a
+     * @param b
+     * @throws AssertionError
+     *             If comparison fails
+     */
+    public static final <T> void assertGreater(String message, Comparable<T> a,
+            T b) throws AssertionError {
+        if (a.compareTo(b) > 0) {
+            return;
+        }
+        throw new AssertionError(decorate(message, a, b));
+    }
+
+    /**
+     * Asserts that {@literal a} is &lt;= {@literal b}
+     * 
+     * @param message
+     *            The message to include in the {@link AssertionError}
+     * @param a
+     * @param b
+     * @throws AssertionError
+     *             If comparison fails
+     */
+    public static final <T> void assertLessThanOrEqual(String message,
+            Comparable<T> a, T b) throws AssertionError {
+        if (a.compareTo(b) <= 0) {
+            return;
+        }
+
+        throw new AssertionError(decorate(message, a, b));
+    }
+
+    /**
+     * Asserts that {@literal a} is &lt; {@literal b}
+     * 
+     * @param message
+     *            The message to include in the {@link AssertionError}
+     * @param a
+     * @param b
+     * @throws AssertionError
+     *             If comparison fails
+     */
+    public static final <T> void assertLessThan(String message,
+            Comparable<T> a, T b) throws AssertionError {
+        if (a.compareTo(b) < 0) {
+            return;
+        }
+        throw new AssertionError(decorate(message, a, b));
+    }
+
+    private static <T> String decorate(String message, Comparable<T> a, T b) {
+        message = message.replace("{0}", a.toString());
+        message = message.replace("{1}", b.toString());
+        return message;
+    }
+
     /**
      * Returns the path that should be used for the test. The path contains the
      * full path (appended to hostname+port) and must start with a slash.