Author: ozizka(a)redhat.com
Date: 2009-06-11 12:16:17 -0400 (Thu, 11 Jun 2009)
New Revision: 518
Modified:
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/HelloJoprTest.java
Log:
HelloJoprTest updated to handle paginated apps list.
Modified: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/HelloJoprTest.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/HelloJoprTest.java 2009-06-11
15:53:00 UTC (rev 517)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/HelloJoprTest.java 2009-06-11
16:16:17 UTC (rev 518)
@@ -19,7 +19,6 @@
* 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.jopr.jsfunit;
import com.gargoylesoftware.htmlunit.BrowserVersion;
@@ -41,6 +40,7 @@
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.cactus.ServletTestCase;
+import org.jboss.jopr.jsfunit.exceptions.EmbJoprTestException;
import org.jboss.jopr.jsfunit.exceptions.HtmlElementNotFoundException;
import org.jboss.jopr.jsfunit.util.EmbJoprTestToolkit;
import org.jboss.jsfunit.framework.WebClientSpec;
@@ -55,186 +55,186 @@
*
* @author Stan Silvert
*/
-public class HelloJoprTest extends ServletTestCase
-{
- private boolean isJBoss4;
+public class HelloJoprTest extends ServletTestCase {
- private JSFClientSession client;
- private JSFServerSession server;
-
- /**
- * Start a JSFUnit session by logging in to the main page. Note that
- * because setUp() is called before each test, a new HttpSession will be
- * created each time a test is run.
- */
- public void setUp() throws IOException
- {
- isJBoss4 = Package.getPackage("org.jboss.system.server")
- .getImplementationVersion()
- .startsWith("4");
-
- // Initial JSF request
- WebClientSpec wcSpec = new WebClientSpec("/", BrowserVersion.FIREFOX_3);
-
- // This is temporary because embedded Jopr can't find /js/rhq.js
- wcSpec.getWebClient().setThrowExceptionOnFailingStatusCode(false);
-
- // Always press OK for confirm dialogs
- wcSpec.getWebClient().setConfirmHandler(new SimpleConfirmHandler(true));
-
- wcSpec.setInitialRequestStrategy(new JoprLoginStrategy()); // logs in
-
- JSFSession jsfSession = new JSFSession(wcSpec);
- this.client = jsfSession.getJSFClientSession();
- this.server = jsfSession.getJSFServerSession();
- }
-
- /**
- * @return the suite of tests being tested
- */
- public static Test suite()
- {
- return new TestSuite( HelloJoprTest.class );
- }
-
- public void testDeployWAR() throws IOException, HtmlElementNotFoundException
- {
+
+ private boolean isJBoss4;
+ private JSFClientSession client;
+ private JSFServerSession server;
- EmbJoprTestToolkit ejtt = new EmbJoprTestToolkit(client, server);
- // Click the nav tree.
- String linkLabel = "WARs";
- //HtmlAnchor warLink = getNavTreeLink(linkLabel);
- HtmlAnchor warLink = ejtt.navTree.getNodeLink(linkLabel);
- warLink.click();
-
- // click on the "Add new resource" button
- DebugUtils.writeFile("target/testDeployWAR-beforeAdd.html",
client.getPageAsText());///
- client.click("actionHeaderForm:addNewContent"); // 404 if
setThrowExceptionOnFailingStatusCode(true) above
-
- // upload hellothere.war
- HtmlFileInput fileInput =
(HtmlFileInput)client.getElement("createContentForm:file");
- fileInput.setContentType("application/war");
- fileInput.setValueAttribute(System.getProperty("jsfunit.testdata") +
"/war/hellothere.war");
- client.click("createContentForm:addButton");
-
- // assert that the success message appeared on the client side
- assertTrue(client.getPageAsText().contains("hellothere.war created
successfully"));
-
- // assert text and sevrity level for FacesMessage on server side
- assertTrue(server.getFacesMessages().hasNext());
- FacesMessage successMessage = server.getFacesMessages().next();
- assertTrue(FacesMessage.SEVERITY_INFO.equals(successMessage.getSeverity()));
- assertTrue(successMessage.getDetail().contains("hellothere.war created
successfully"));
-
- // Use JMX to assert that the WAR really did deploy successfully
- assertTrue(isWarDeployed("hellothere.war"));
-
- // use HtmlUnit to test the newly deployed war in a new WebClient session
- // note that I have full access to the FacesContext and the previous request
- HttpServletRequest request =
(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
- int port = request.getLocalPort();
- WebClient webClient = new WebClient();
- HtmlPage page = (HtmlPage)webClient.getPage("http://localhost:" + port +
"/hellothere/hello.jsp");
- assertTrue(page.asText().contains("HELLO WORLD"));
-
- // Undeploy the WAR
- HtmlButtonInput deleteButton = getDeleteButton("hellothere.war");
- deleteButton.click();
-
- assertFalse(isWarDeployed("hellothere.war"));
- }
-
- private boolean isWarDeployed(String warName)
- {
- if (isJBoss4)
- {
- return checkWarDeployment_4_2(warName);
- }
- else
- {
- return checkWarDeployment_5_0(warName);
- }
- }
-
- // Query the MBean server to find if WAR is deployed
- private boolean checkWarDeployment_4_2(String warName)
- {
- try {
- MBeanServer jmxServer = MBeanServerLocator.locateJBoss();
- ObjectName objName = new ObjectName("jboss.web.deployment:war=" +
warName + ",*");
- Set mBeans = jmxServer.queryNames(objName, null);
- if (mBeans.size() != 1) return false;
- ObjectName deploymentMBean = (ObjectName)mBeans.iterator().next();
- String state = (String)jmxServer.getAttribute(deploymentMBean,
"StateString");
- return "Started".equals(state);
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
-
- // Query the MBean server to find if WAR is deployed
- private boolean checkWarDeployment_5_0(String warName)
- {
- if (warName.endsWith(".war"))
- {
- warName = warName.substring(0, warName.lastIndexOf(".war"));
- }
- else
- {
- warName = warName.substring(0, warName.lastIndexOf(".WAR"));
- }
-
- try {
- MBeanServer jmxServer = MBeanServerLocator.locateJBoss();
- ObjectName objName = new
ObjectName("jboss.deployment:id=\"jboss.web.deployment:war=/" + warName +
"\",*");
- Set mBeans = jmxServer.queryNames(objName, null);
- if (mBeans.size() != 1) return false;
- ObjectName deploymentMBean = (ObjectName)mBeans.iterator().next();
-
- // returns org.jboss.deployers.spi.DeploymentState
- Object state = jmxServer.getAttribute(deploymentMBean, "State");
- return "DEPLOYED".equals(state.toString());
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
+ /**
+ * Start a JSFUnit session by logging in to the main page. Note that
+ * because setUp() is called before each test, a new HttpSession will be
+ * created each time a test is run.
+ */
+ public void setUp() throws IOException {
+ isJBoss4 =
Package.getPackage("org.jboss.system.server").getImplementationVersion().startsWith("4");
- // need a standard JSFUnit API to replace this code
- private HtmlAnchor getNavTreeLink(String linkLabel)
- {
- return getLinkInsideForm("navTreeForm", linkLabel);
- }
-
- private HtmlButtonInput getDeleteButton(String resourceName)
- {
- HtmlAnchor link = getLinkInsideForm("resourceSummaryForm",
resourceName);
- // The id will look like "resourceSummaryForm:dataTable:2:resourceName"
- // I need the row number. (2 in the above example)
- String id = link.getIdAttribute();
- String[] idElements = id.split(":");
- String row = idElements[idElements.length - 2];
- return (HtmlButtonInput)client.getElement(row + ":removeButton");
- }
-
- // finds a <a> tag inside a form that has a particular label
- private HtmlAnchor getLinkInsideForm(String formId, String linkLabel)
- {
- HtmlForm form = (HtmlForm)client.getElement(formId);
- List links = form.getByXPath(".//a"); // get all <a> tags inside
form
+ // Initial JSF request
+ WebClientSpec wcSpec = new WebClientSpec("/", BrowserVersion.FIREFOX_3);
- for (Iterator i = links.iterator(); i.hasNext();)
- {
- HtmlAnchor link = (HtmlAnchor)i.next();
- String linkText = link.getTextContent();
- if (linkText.contains(linkLabel)) return link;
- }
-
- throw new IllegalStateException("Nav Tree link for '" + linkLabel +
"' not found.");
- }
-
-}
+ // This is temporary because embedded Jopr can't find /js/rhq.js
+ wcSpec.getWebClient().setThrowExceptionOnFailingStatusCode(false);
+
+ // Always press OK for confirm dialogs
+ wcSpec.getWebClient().setConfirmHandler(new SimpleConfirmHandler(true));
+
+ wcSpec.setInitialRequestStrategy(new JoprLoginStrategy()); // logs in
+
+ JSFSession jsfSession = new JSFSession(wcSpec);
+ this.client = jsfSession.getJSFClientSession();
+ this.server = jsfSession.getJSFServerSession();
+ }
+
+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite() {
+ return new TestSuite(HelloJoprTest.class);
+ }
+
+ public void testDeployWAR() throws IOException, HtmlElementNotFoundException,
EmbJoprTestException {
+
+ final String DEPLOYABLE_NAME = "hellothere.war";
+
+ final EmbJoprTestToolkit ejtt = new EmbJoprTestToolkit(client, server);
+
+
+ // Click the nav tree.
+ String linkLabel = "WARs";
+ //HtmlAnchor warLink = getNavTreeLink(linkLabel);
+ HtmlAnchor warLink = ejtt.navTree.getNodeLink(linkLabel);
+ warLink.click();
+
+ // click on the "Add new resource" button
+ DebugUtils.writeFile("target/testDeployWAR-beforeAdd.html",
client.getPageAsText());///
+ client.click("actionHeaderForm:addNewContent"); // 404 if
setThrowExceptionOnFailingStatusCode(true) above
+
+ // upload hellothere.war
+ HtmlFileInput fileInput = (HtmlFileInput)
client.getElement("createContentForm:file");
+ fileInput.setContentType("application/war");
+ fileInput.setValueAttribute(System.getProperty("jsfunit.testdata") +
"/war/hellothere.war");
+ client.click("createContentForm:addButton");
+
+ // assert that the success message appeared on the client side
+ assertTrue(client.getPageAsText().contains("hellothere.war created
successfully"));
+
+ // assert text and sevrity level for FacesMessage on server side
+ assertTrue(server.getFacesMessages().hasNext());
+ FacesMessage successMessage = server.getFacesMessages().next();
+ assertTrue(FacesMessage.SEVERITY_INFO.equals(successMessage.getSeverity()));
+ assertTrue(successMessage.getDetail().contains("hellothere.war created
successfully"));
+
+ // Use JMX to assert that the WAR really did deploy successfully
+ assertTrue(isWarDeployed(DEPLOYABLE_NAME));
+
+ // use HtmlUnit to test the newly deployed war in a new WebClient session
+ // note that I have full access to the FacesContext and the previous request
+ HttpServletRequest request = (HttpServletRequest)
FacesContext.getCurrentInstance().getExternalContext().getRequest();
+ int port = request.getLocalPort();
+ WebClient webClient = new WebClient();
+ HtmlPage page = (HtmlPage) webClient.getPage("http://localhost:" + port +
"/hellothere/hello.jsp");
+ assertTrue(page.asText().contains("HELLO WORLD"));
+
+ /* Doesn't work when the WAR is not on first page of listing.
+ // Undeploy the WAR
+ HtmlButtonInput deleteButton = getDeleteButton(DEPLOYABLE_NAME);
+ deleteButton.click();*/
+
+ ejtt.deployment.undeployViaEmbJopr(AppConstants.DeployableTypes.WAR, DEPLOYABLE_NAME);
+
+ assertFalse(isWarDeployed(DEPLOYABLE_NAME));
+ }
+
+
+
+ private boolean isWarDeployed(String warName) {
+ if (isJBoss4) {
+ return checkWarDeployment_4_2(warName);
+ } else {
+ return checkWarDeployment_5_0(warName);
+ }
+ }
+
+
+ /** Query the MBean server to find if WAR is deployed. */
+ private boolean checkWarDeployment_4_2(String warName) {
+ try {
+ MBeanServer jmxServer = MBeanServerLocator.locateJBoss();
+ ObjectName objName = new ObjectName("jboss.web.deployment:war=" + warName +
",*");
+ Set mBeans = jmxServer.queryNames(objName, null);
+ if (mBeans.size() != 1) {
+ return false;
+ }
+ ObjectName deploymentMBean = (ObjectName) mBeans.iterator().next();
+ String state = (String) jmxServer.getAttribute(deploymentMBean,
"StateString");
+ return "Started".equals(state);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ /** Query the MBean server to find if WAR is deployed. */
+ private boolean checkWarDeployment_5_0(String warName) {
+ if (warName.endsWith(".war")) {
+ warName = warName.substring(0, warName.lastIndexOf(".war"));
+ } else {
+ warName = warName.substring(0, warName.lastIndexOf(".WAR"));
+ }
+
+ try {
+ MBeanServer jmxServer = MBeanServerLocator.locateJBoss();
+ ObjectName objName = new
ObjectName("jboss.deployment:id=\"jboss.web.deployment:war=/" + warName +
"\",*");
+ Set mBeans = jmxServer.queryNames(objName, null);
+ if (mBeans.size() != 1) {
+ return false;
+ }
+ ObjectName deploymentMBean = (ObjectName) mBeans.iterator().next();
+
+ // returns org.jboss.deployers.spi.DeploymentState
+ Object state = jmxServer.getAttribute(deploymentMBean, "State");
+ return "DEPLOYED".equals(state.toString());
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ // need a standard JSFUnit API to replace this code
+ private HtmlAnchor getNavTreeLink(String linkLabel) {
+ return getLinkInsideForm("navTreeForm", linkLabel);
+ }
+
+
+ private HtmlButtonInput getDeleteButton(String resourceName) {
+ HtmlAnchor link = getLinkInsideForm("resourceSummaryForm", resourceName);
+ // The id will look like "resourceSummaryForm:dataTable:2:resourceName"
+ // I need the row number. (2 in the above example)
+ String id = link.getIdAttribute();
+ String[] idElements = id.split(":");
+ String row = idElements[idElements.length - 2];
+ return (HtmlButtonInput) client.getElement(row + ":removeButton");
+ }
+
+
+ /** Finds a <a> tag inside a form that has a particular label. */
+ private HtmlAnchor getLinkInsideForm(String formId, String linkLabel) {
+ HtmlForm form = (HtmlForm) client.getElement(formId);
+ List links = form.getByXPath(".//a"); // get all <a> tags inside form
+
+ for (Iterator i = links.iterator(); i.hasNext();) {
+ HtmlAnchor link = (HtmlAnchor) i.next();
+ String linkText = link.getTextContent();
+ if (linkText.contains(linkLabel)) {
+ return link;
+ }
+ }
+
+ throw new IllegalStateException("Nav Tree link for '" + linkLabel +
"' not found.");
+ }
+
+
+}// class