Author: dan.j.allen
Date: 2009-05-11 17:44:16 -0400 (Mon, 11 May 2009)
New Revision: 10867
Added:
examples/trunk/servlet-permalink/src/test/
examples/trunk/servlet-permalink/src/test/java/
examples/trunk/servlet-permalink/src/test/java/org/
examples/trunk/servlet-permalink/src/test/java/org/jboss/
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/examples/
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/examples/permalink/
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/examples/permalink/WhenNavigatingToCategoryPage.java
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/examples/permalink/WhenNavigatingToHomePage.java
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/test/
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/test/AbstractScenario.java
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/test/FacesTesterHolder.java
examples/trunk/servlet-permalink/src/test/resources/
examples/trunk/servlet-permalink/src/test/resources/unit-test-suite.xml
Log:
experimental use of facestester
Added:
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/examples/permalink/WhenNavigatingToCategoryPage.java
===================================================================
---
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/examples/permalink/WhenNavigatingToCategoryPage.java
(rev 0)
+++
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/examples/permalink/WhenNavigatingToCategoryPage.java 2009-05-11
21:44:16 UTC (rev 10867)
@@ -0,0 +1,37 @@
+package org.jboss.seam.examples.permalink;
+
+import static org.testng.Assert.*;
+
+import java.util.List;
+
+import org.jboss.seam.test.AbstractScenario;
+import org.testng.annotations.Test;
+
+/**
+ * @author Dan Allen
+ */
+public class WhenNavigatingToCategoryPage extends AbstractScenario
+{
+ private static final String CATEGORY_PAGE = "/category.xhtml";
+
+ @Test(groups = {"integration", "scenario"})
+ public void shouldSetCategoryFromViewParameter() throws Exception
+ {
+ tester.requestPage(CATEGORY_PAGE + "?name=General");
+ String category = getValue("blog.category", String.class);
+ assertEquals(category, "General");
+ }
+
+ @Test(groups = {"integration", "scenario"})
+ public void shouldDisplayEntriesInCategory() throws Exception
+ {
+ tester.requestPage(CATEGORY_PAGE + "?name=JSF 2");
+ List<BlogEntry> entriesForPage = getValue("blog.entriesForPage",
List.class);
+ assertNotNull(entriesForPage);
+ assertEquals(entriesForPage.size(), 3);
+ for (BlogEntry entry : entriesForPage)
+ {
+ assertEquals(entry.getCategory(), "JSF 2");
+ }
+ }
+}
Added:
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/examples/permalink/WhenNavigatingToHomePage.java
===================================================================
---
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/examples/permalink/WhenNavigatingToHomePage.java
(rev 0)
+++
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/examples/permalink/WhenNavigatingToHomePage.java 2009-05-11
21:44:16 UTC (rev 10867)
@@ -0,0 +1,55 @@
+package org.jboss.seam.examples.permalink;
+
+import static org.testng.Assert.*;
+
+import com.steeplesoft.jsf.facestester.FacesComponent;
+import com.steeplesoft.jsf.facestester.FacesPage;
+import java.util.List;
+import javax.faces.component.UICommand;
+
+import org.jboss.seam.test.AbstractScenario;
+import org.testng.annotations.Test;
+
+/**
+ * @author Dan Allen
+ */
+public class WhenNavigatingToHomePage extends AbstractScenario
+{
+ private static final String HOME_PAGE = "/home.xhtml";
+
+ @Test(groups = {"integration", "scenario"})
+ public void shouldLoadBlogEntries() throws Exception
+ {
+ tester.requestPage(HOME_PAGE);
+ List<BlogEntry> entriesForPage = getValue("blog.entriesForPage",
List.class);
+ assertNotNull(entriesForPage);
+ assertEquals(entriesForPage.size(), 3);
+ }
+
+ @Test(groups = {"integration", "scenario"})
+ public void shouldHaveLinksDisabledToFirstAndPreviousPage() throws Exception
+ {
+ FacesPage homePage = tester.requestPage(HOME_PAGE);
+ FacesComponent first = homePage.getComponentWithId("first");
+ assertEquals(first.getValueAsString(), "First Page");
+
assertTrue(Boolean.valueOf(String.valueOf(first.getWrappedComponent().getAttributes().get("disabled"))));
+ FacesComponent previous = homePage.getComponentWithId("previous");
+ assertEquals(previous.getValueAsString(), "Newer Entries");
+
assertTrue(Boolean.valueOf(String.valueOf(previous.getWrappedComponent().getAttributes().get("disabled"))));
+ }
+
+ @Test
+ public void shouldDisplaySearchForm() throws Exception
+ {
+ FacesPage homePage = tester.requestPage(HOME_PAGE);
+ FacesComponent searchForm = homePage.getComponentWithId("search");
+ assertTrue(searchForm.isRendered());
+ FacesComponent queryInput = homePage.getComponentWithId("search:q");
+ assertEquals(queryInput.getWrappedComponent().getClientId(), "q",
"The prependId attribute on form should be false and the parent id not prepended to
the child id");
+ assertEquals(queryInput.getValueAsString(), null);
+ FacesComponent searchButton = homePage.getComponentWithId("search:s");
+ assertEquals(searchButton.getWrappedComponent().getClientId(), "s",
"The prependId attribute on form should be false and the parent id not prepended to
the child id");
+ assertTrue(searchButton.getWrappedComponent() instanceof UICommand);
+ assertEquals(((UICommand)
searchButton.getWrappedComponent()).getActionExpression().getExpressionString(),
"#{blog.search}");
+ }
+}
Added:
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/test/AbstractScenario.java
===================================================================
---
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/test/AbstractScenario.java
(rev 0)
+++
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/test/AbstractScenario.java 2009-05-11
21:44:16 UTC (rev 10867)
@@ -0,0 +1,78 @@
+package org.jboss.seam.test;
+
+import com.steeplesoft.jsf.facestester.FacesTester;
+import javax.el.ELContext;
+import javax.faces.context.FacesContext;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import org.jboss.webbeans.context.RequestContext;
+import org.jboss.webbeans.context.SessionContext;
+import org.jboss.webbeans.context.api.BeanStore;
+import org.jboss.webbeans.context.api.helpers.ConcurrentHashMapBeanStore;
+import org.jboss.webbeans.environment.servlet.Listener;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+
+/**
+ * A base class for scenarios that execute JSF pages
+ * in a JCDI environment.
+ *
+ * TODO move me to a Seam module (perhaps the test module)
+ *
+ * @author Dan Allen
+ */
+public class AbstractScenario
+{
+ protected FacesTester tester;
+
+ protected Listener wbListener;
+
+ @AfterClass(alwaysRun = true)
+ public void afterClass() throws Exception
+ {
+ ServletContext sc = (ServletContext)
tester.getFacesContext().getExternalContext().getContext();
+ wbListener.contextDestroyed(new ServletContextEvent(sc));
+ }
+
+ @AfterMethod(alwaysRun = true)
+ public void afterMethod()
+ {
+ SessionContext.instance().destroy();
+ SessionContext.instance().setActive(false);
+ RequestContext.instance().destroy();
+ RequestContext.instance().setActive(false);
+ }
+
+ @BeforeClass
+ public void beforeClass() throws Exception
+ {
+ tester = FacesTesterHolder.instance();
+ ServletContext sc = (ServletContext)
tester.getFacesContext().getExternalContext().getContext();
+ wbListener = new Listener();
+ wbListener.contextInitialized(new ServletContextEvent(sc));
+ }
+
+ @BeforeMethod
+ public void beforeMethod()
+ {
+ BeanStore reqBS = new ConcurrentHashMapBeanStore();
+ RequestContext.instance().setBeanStore(reqBS);
+ RequestContext.instance().setActive(true);
+ BeanStore sessBS = new ConcurrentHashMapBeanStore();
+ SessionContext.instance().setBeanStore(sessBS);
+ SessionContext.instance().setActive(true);
+ }
+
+ protected <T> T getValue(String expression, Class<T> expectedClass)
+ {
+ if (!expression.startsWith("#{"))
+ {
+ expression = "#{" + expression + "}";
+ }
+ FacesContext context = FacesContext.getCurrentInstance();
+ ELContext elContext = context.getELContext();
+ return (T)
context.getApplication().getExpressionFactory().createValueExpression(elContext,
expression, expectedClass).getValue(elContext);
+ }
+}
Added:
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/test/FacesTesterHolder.java
===================================================================
---
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/test/FacesTesterHolder.java
(rev 0)
+++
examples/trunk/servlet-permalink/src/test/java/org/jboss/seam/test/FacesTesterHolder.java 2009-05-11
21:44:16 UTC (rev 10867)
@@ -0,0 +1,21 @@
+package org.jboss.seam.test;
+
+import com.steeplesoft.jsf.facestester.FacesTester;
+
+/**
+ * @author Dan Allen
+ */
+public class FacesTesterHolder
+{
+ private static FacesTester tester;
+
+ public static synchronized FacesTester instance()
+ {
+ if (tester == null)
+ {
+ tester = new FacesTester();
+ }
+
+ return tester;
+ }
+}
Added: examples/trunk/servlet-permalink/src/test/resources/unit-test-suite.xml
===================================================================
--- examples/trunk/servlet-permalink/src/test/resources/unit-test-suite.xml
(rev 0)
+++ examples/trunk/servlet-permalink/src/test/resources/unit-test-suite.xml 2009-05-11
21:44:16 UTC (rev 10867)
@@ -0,0 +1,8 @@
+<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
+<suite name="Permalink Servlet Example Unit Tests"
verbose="1">
+ <test name="Permalink Servlet Example Unit Tests">
+ <packages>
+ <package name="org.jboss.seam.examples.permalink"/>
+ </packages>
+ </test>
+</suite>
Show replies by date