[weld-commits] Weld SVN: r5546 - in ftest/numberguess/trunk/src/main/java/org/jboss/weld/example/numberguess: clustertest and 1 other directories.
weld-commits at lists.jboss.org
weld-commits at lists.jboss.org
Thu Jan 21 10:19:48 EST 2010
Author: mgencur at redhat.com
Date: 2010-01-21 10:19:48 -0500 (Thu, 21 Jan 2010)
New Revision: 5546
Added:
ftest/numberguess/trunk/src/main/java/org/jboss/weld/example/numberguess/clustertest/
ftest/numberguess/trunk/src/main/java/org/jboss/weld/example/numberguess/clustertest/selenium/
ftest/numberguess/trunk/src/main/java/org/jboss/weld/example/numberguess/clustertest/selenium/NumberGuessClusteringTest.java
Log:
WELD-396 functional test for numberguess example in cluster
Added: ftest/numberguess/trunk/src/main/java/org/jboss/weld/example/numberguess/clustertest/selenium/NumberGuessClusteringTest.java
===================================================================
--- ftest/numberguess/trunk/src/main/java/org/jboss/weld/example/numberguess/clustertest/selenium/NumberGuessClusteringTest.java (rev 0)
+++ ftest/numberguess/trunk/src/main/java/org/jboss/weld/example/numberguess/clustertest/selenium/NumberGuessClusteringTest.java 2010-01-21 15:19:48 UTC (rev 5546)
@@ -0,0 +1,253 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, 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.weld.example.numberguess.clustertest.selenium;
+
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.fail;
+
+import java.io.IOException;
+
+import org.jboss.weld.example.common.test.selenium.WeldSelenium;
+import org.jboss.weld.example.common.test.selenium.WeldSeleniumTest;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * This class tests Weld numberguess example in a cluster. Two instances of JBoss AS are
+ * being used. First part of test is executed at first (master) instance. Then the first
+ * instance is killed and a second (slave) instance takes over executing of the application.
+ * This behaviour simulates recovery from breakdown and session replication.
+ *
+ * The first version of application server that can be used is JBoss 6.0.0.M1, nevertheless this
+ * version of AS has to be updated with current version of Weld core.
+ *
+ * Prior to executing this test it is needed to start both JBoss AS instances manually.
+ * For example (assuming you have created second "all" configuration ("all2")):
+ * ./run.sh -c all -g DocsPartition -u 239.255.101.101 -b localhost -Djboss.messaging.ServerPeerID=1
+ * -Djboss.service.binding.set=ports-default
+ * ./run.sh -c all2 -g DocsPartition -u 239.255.101.101 -b localhost -Djboss.messaging.ServerPeerID=2
+ * -Djboss.service.binding.set=ports-01
+ * The configuration all is considered to be master jboss instance (related to
+ * jboss.service.binding.set=ports-default) and the application is deployed to farm directory under
+ * chosen jboss configuration directory specified with jboss.master.configuration property.
+ *
+ *
+ * @author mgencur
+ * @author kpiwko
+ *
+ */
+public class NumberGuessClusteringTest extends WeldSeleniumTest
+{
+
+ protected String MAIN_PAGE = "/home.jsf";
+ protected String GUESS_MESSAGES = "id=numberGuess:messages";
+ protected String GUESS_STATUS = "xpath=//div[contains(text(),'I'm thinking of ')]";
+
+ protected String GUESS_FIELD = "id=numberGuess:inputGuess";
+ protected String GUESS_FIELD_WITH_VALUE = "xpath=//input[@id='numberGuess:inputGuess'][@value=3]";
+
+ protected String GUESS_SUBMIT = "id=numberGuess:guessButton";
+ protected String GUESS_RESTART = "id=numberGuess:restartButton";
+ protected String GUESS_SMALLEST = "id=numberGuess:smallest";
+ protected String GUESS_BIGGEST = "id=numberGuess:biggest";
+
+ protected String WIN_MSG = "Correct!";
+ protected String LOSE_MSG = "No guesses left!";
+ protected String HIGHER_MSG = "Higher!";
+
+ private final String SECOND_INSTANCE_BROWSER_URL = "http://localhost:8180";
+ private final long JBOSS_SHUTDOWN_TIMEOUT = 20000;
+
+ private WeldSelenium browser2;
+
+ @BeforeMethod(dependsOnGroups = "seleniumSetUp")
+ public void open()
+ {
+ browser.open(contextPath + MAIN_PAGE);
+ browser.waitForPageToLoad();
+ deleteCookies(browser);
+ browser2 = startSecondBrowser();
+ deleteCookies(browser2);
+ }
+
+ @Test
+ public void guessingWithFailoverTest()
+ {
+ preFailurePart(browser);
+
+ String newAddress = getAddressForSecondInstance(browser);
+
+ shutdownMasterJBossInstance();
+
+ browser2.open(newAddress);
+
+ assertTrue(browser2.isTextPresent(HIGHER_MSG), "Page should contain message Higher!");
+ assertEquals(Integer.parseInt(browser.getText(GUESS_SMALLEST)),4, "Page should contain smallest number equal to 4");
+ assertEquals(Integer.parseInt(browser.getText(GUESS_BIGGEST)),100, "Page should contain biggest number equal to 100");
+ assertTrue(browser2.isElementPresent(GUESS_FIELD_WITH_VALUE), "Page should contain input field with value of 3");
+
+ postFailurePart(browser2);
+
+ assertTrue(isOnWinPage(browser2), "Win page expected after playing smart.");
+ }
+
+ protected void preFailurePart(WeldSelenium browser)
+ {
+ int numberOfGuesses = 3;
+ int guess = 0;
+
+ //enter several guesses
+ while (true){
+ while (isOnGuessPage(browser) && guess < numberOfGuesses)
+ {
+ enterGuess(browser, ++guess);
+ }
+
+ //we always want to enter at least 3 guesses so that we can continue in the other browser window with expected results
+ if (guess < numberOfGuesses)
+ {
+ resetForm(browser);
+ guess = 0;
+ }
+ else
+ {
+ break;
+ }
+ }
+ }
+
+ protected void postFailurePart(WeldSelenium browser)
+ {
+ int min, max, guess;
+ int i = 0;
+
+ while (isOnGuessPage(browser))
+ {
+ deleteCookies(browser);
+ if (i > 7)
+ {
+ fail("Game should not be longer than 7 guesses in the second browser after failover");
+ }
+
+ assertTrue(browser.isElementPresent(GUESS_SMALLEST), "Expected smallest number on page");
+ assertTrue(browser.isElementPresent(GUESS_BIGGEST), "Expected biggest number on page");
+
+ min = Integer.parseInt(browser.getText(GUESS_SMALLEST));
+ max = Integer.parseInt(browser.getText(GUESS_BIGGEST));
+ guess = min + ((max - min) / 2);
+ enterGuess(browser, guess);
+ i++;
+ }
+ }
+
+ protected void resetForm(WeldSelenium browser)
+ {
+ browser.click(GUESS_RESTART);
+ browser.waitForPageToLoad();
+ }
+
+ protected void enterGuess(WeldSelenium browser, int guess)
+ {
+ browser.type(GUESS_FIELD, String.valueOf(guess));
+ browser.click(GUESS_SUBMIT);
+ browser.waitForPageToLoad();
+ }
+
+ protected boolean isOnGuessPage(WeldSelenium browser)
+ {
+ return !(isOnWinPage(browser) || isOnLosePage(browser));
+ }
+
+ protected boolean isOnWinPage(WeldSelenium browser)
+ {
+ String text = browser.getText(GUESS_MESSAGES);
+ return WIN_MSG.equals(text);
+ }
+
+ protected boolean isOnLosePage(WeldSelenium browser)
+ {
+ String text = browser.getText(GUESS_MESSAGES);
+ return LOSE_MSG.equals(text);
+ }
+
+ public WeldSelenium startSecondBrowser()
+ {
+ String url = SECOND_INSTANCE_BROWSER_URL;
+ return super.startBrowser(super.host, super.port, super.browserType, url);
+ }
+
+ public String getAddressForSecondInstance(WeldSelenium browser)
+ {
+ String loc = browser.getLocation();
+ String[] parsedStrings = loc.split("/");
+ StringBuilder sb = new StringBuilder();
+ for (int i = 3; i != parsedStrings.length; i++){
+ sb.append("/").append(parsedStrings[i]);
+ }
+
+ String sid;
+ if (browser.isCookiePresent("JSESSIONID"))
+ {
+ sid = browser.getCookieByName("JSESSIONID");
+ }
+ else
+ {
+ //get sessionid directly from browser URL if JSESSIONID cookie is not present
+ sid = loc.substring(loc.indexOf("jsessionid=") + "jsessionid=".length(), loc.length());
+ }
+
+ String newAddress = sb.toString();
+ String firstPart = newAddress.substring(0, newAddress.indexOf(";"));
+
+ //construct a new address for second browser
+ newAddress = firstPart + ";jsessionid=" + sid;
+
+ return newAddress;
+ }
+
+ private void deleteCookies(WeldSelenium browser)
+ {
+ if (browser.isCookiePresent("JSESSIONID"))
+ {
+ browser.deleteCookie("JSESSIONID", "path=" + contextPath + ", domain=localhost, recurse=true");
+ }
+ }
+
+ public void shutdownMasterJBossInstance()
+ {
+ String command = super.jbossConfig + "/../../bin/shutdown.sh -s localhost:1099 -S";
+ try
+ {
+ Process process = Runtime.getRuntime().exec(command);
+ process.waitFor();
+ Thread.sleep(JBOSS_SHUTDOWN_TIMEOUT);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e.getCause());
+ }
+ catch (InterruptedException e)
+ {
+ }
+ }
+}
More information about the weld-commits
mailing list