tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From freem...@apache.org
Subject svn commit: r493422 - in /tapestry/tapestry5/tapestry-core/trunk/src: main/java/org/apache/tapestry/test/pagelevel/PageTester.java site/apt/guide/unit-testing-pages.apt site/apt/index.apt site/site.xml
Date Sat, 06 Jan 2007 06:11:37 GMT
Author: freemant
Date: Fri Jan  5 22:11:36 2007
New Revision: 493422

URL: http://svn.apache.org/viewvc?view=rev&rev=493422
Log:
1) Add a chapter in the user guide for PageTester.
2) Update some javadoc in PageTester.

Added:
    tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/unit-testing-pages.apt
Modified:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/pagelevel/PageTester.java
    tapestry/tapestry5/tapestry-core/trunk/src/site/apt/index.apt
    tapestry/tapestry5/tapestry-core/trunk/src/site/site.xml

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/pagelevel/PageTester.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/pagelevel/PageTester.java?view=diff&rev=493422&r1=493421&r2=493422
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/pagelevel/PageTester.java
(original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/pagelevel/PageTester.java
Fri Jan  5 22:11:36 2007
@@ -61,17 +61,29 @@
     private final String _contextPath;
 
     /**
-     * Initializes a PageTester without overriding any services.
+     * Initializes a PageTester without overriding any services and assuming that the context
root
+     * is in src/main/webapp.
      * 
      * @see #PageTester(String, String, String, Map)
      */
-    @SuppressWarnings("unchecked")
     public PageTester(String appPackage, String appName)
     {
-        this(appPackage, appName, DEFAULT_CONTEXT_PATH, Collections.EMPTY_MAP);
+        this(appPackage, appName, DEFAULT_CONTEXT_PATH);
     }
 
     /**
+     * @see #PageTester(String, String, String, Map)
+     */
+    @SuppressWarnings("unchecked")
+    public PageTester(String appPackage, String appName, String contextPath)
+    {
+        this(appPackage, appName, contextPath, Collections.EMPTY_MAP);
+    }
+
+    /**
+     * Initializes a PageTester that acts as a browser and a servlet container to test drive
your
+     * Tapestry pages.
+     * 
      * @param appPackage
      *            The same value you would specify using the tapestry.app-package context
parameter.
      *            As this testing environment is not run in a servlet container, you need
to specify
@@ -79,6 +91,9 @@
      * @param appName
      *            The same value you would specify as the filter name. It is used to form
the name
      *            of the module builder for your app. If you don't have one, pass an empty
string.
+     * @param contextPath
+     *            The path to the context root so that Tapestry can find the templates (if
they're
+     *            put there).
      * @param serviceOverrides
      *            The mock implementation (value) for some services (key).
      */

Added: tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/unit-testing-pages.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/unit-testing-pages.apt?view=auto&rev=493422
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/unit-testing-pages.apt (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/unit-testing-pages.apt Fri Jan
 5 22:11:36 2007
@@ -0,0 +1,168 @@
+ ----
+  Unit testing pages or components
+ ----
+
+Unit testing pages or components
+
+  You can easily unit test a certain page or a component. Follow the simple 
+  tasks below.
+ 
+* Setting up a driving environment
+
+  In order to unit test a page, you'll need to create an instance of 
+  {{{../apidocs/org/apache/tapestry/test/pagelevel/PageTester.html}PageTester}}.
+  It acts as both the browser and the servlet container so that you can
+  use it to drive your page. As it is not a real servlet container, you need 
+  to tell it the same information as you would in web.xml:
+  
+  [[1]] Your application package.
+
+  [[2]] Your filter name. This is used to load your Tapestry IoC module only.
+  If you have none, you can pass an empty string or anything to it.
+  
+  [[3]] The folder acting as your context root. This is used to locate your
+  templates (if they're put there).
+
+  Here is an example (using TestNG, but you're free to use JUnit or anything else):
+
++---+
+public class MyTest extends Assert
+{
+    @Test
+    public void test1()
+    {
+        String appPackage = "org.example.app";
+        String appName = "App1"; // App1Module.java has configured some services.
+        PageTester tester = new PageTester(appPackage, appName, "src/main/webapp");
+    }
+}
++---+
+
+* Testing the rendering of a page
+
+  To test if a page renders properly (optionally with context), you can tell
+  the PageTester to render it and then assert against the 
+   {{{../apidocs/org/apache/tapestry/dom/Document.html}DOM Document}} returned.
+  
+  Here is an example. Let's assuming the page being tested is named "MyPage"
+  and it should return a page containing an HTML element whose id is "id1" 
+  and whose text content should be "hello":
+  
++---+
+public class MyTest extends Assert
+{
+    @Test
+    public void test1()
+    {
+        String appPackage = "org.example.app";
+        String appName = "LocaleApp";
+        PageTester tester = new PageTester(appPackage, appName, "src/main/webapp");
+        Document doc = tester.renderPage("MyPage");
+        assertEquals(doc.getElementById("id1").getChildText(), "hello");
+    }
+}
++---+
+
+  If the page requires a context, you can pass it this way:
+  
++---+
+public class MyTest extends Assert
+{
+    @Test
+    public void test1()
+    {
+        String appPackage = "org.example.app";
+        String appName = "LocaleApp";
+        PageTester tester = new PageTester(appPackage, appName, "src/main/webapp");
+        Object[] context = new Object[]{ "abc", 123 };
+        Document doc = tester.invoke(new ComponentInvocation(new PageLinkTarget("MyPage"),
context));
+        assertEquals(doc.getElementById("id1").getChildText(), "hello");
+    }
+}
++---+
+
+* Testing an action link
+
+  After rendering a page, you may want to "click" on an action link and then
+  assert against the resulting page. You can do it this way:
+
++---+
+public class MyTest extends Assert
+{
+    @Test
+    public void test1()
+    {
+        String appPackage = "org.example.app";
+        String appName = "LocaleApp";
+        PageTester tester = new PageTester(appPackage, appName, "src/main/webapp");
+        Document doc = tester.renderPage("MyPage");
+        Element link = doc.getElementById("link1");
+        doc = tester.clickLink(link);
+        assertTrue(doc.toString().contains("abc"));
+    }
+}
++---+
+
+* Testing a form submission
+
+  After rendering a page, you may want to fill out a form, submit it and
+  then inspect the resulting page. You can do it this way:
+
++---+
+public class MyTest extends Assert
+{
+    @Test
+    public void test1()
+    {
+        String appPackage = "org.example.app";
+        String appName = "LocaleApp";
+        PageTester tester = new PageTester(appPackage, appName, "src/main/webapp");
+        Document doc = tester.renderPage("MyPage");
+        Element form = doc.getElementById("form1");
+        Map<String, String> fieldValues = new HashMap<String, String>();
+        fieldValues.put("field1", "hello");
+        fieldValues.put("field2", "100");
+        doc = tester.submitForm(form, fieldValues);
+        assertTrue(doc.toString().contains("abc"));
+    }
+}
++---+
+
+* Unit testing a component
+
+  To unit test a component, just create a test page containing that component. Then 
+  unit test that page.
+  
+* Providing mock implementations for your service layer
+
+  Your page typically will invoke your business logic and will ultimately reach
+  the database. In unit testing, it is highly recommended that you not invoke
+  them as you're unit testing the UI (page). To do that, you should put 
+  your transaction control code, business logic and etc into Tapestry IoC
+  services. Then you should override their implementations using mock objects:
+
++---+
+public class MyTest extends Assert
+{
+    @Test
+    public void test1()
+    {
+        String appPackage = "org.example.app";
+        String appName = "LocaleApp";
+        Map<String, Object> serviceOverrides = new HashMap<String, Object>();
+        Service1 service1Mock = new Service1() {
+            public Object transform(Object input) {
+                //assert against the input and return some mock value
+            }
+        }
+        Service2 service2Mock = new Service2() {
+            public void perform(Object cmd) {
+                //assert against the cmd
+            }
+        }
+        serviceOverrides.put("org.example.app.service1", service1Mock);
+        serviceOverrides.put("org.example.app.service2", service2Mock);
+        PageTester tester = new PageTester(appPackage, appName, "src/main/webapp", serviceOverrides);
+    }
+}
++---+

Modified: tapestry/tapestry5/tapestry-core/trunk/src/site/apt/index.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/site/apt/index.apt?view=diff&rev=493422&r1=493421&r2=493422
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/site/apt/index.apt (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/site/apt/index.apt Fri Jan  5 22:11:36 2007
@@ -31,6 +31,8 @@
   
   * Super-duper Ajax integration built on {{{http://dojotoolkit.org} Dojo}}
   
+  * Easy & fast unit testing of individual pages or components
+  
 New And Of Note
 
   Progress on Tapestry 5 is really taking off. This space lists some cool new features that
have been added

Modified: tapestry/tapestry5/tapestry-core/trunk/src/site/site.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/site/site.xml?view=diff&rev=493422&r1=493421&r2=493422
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/site/site.xml (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/site/site.xml Fri Jan  5 22:11:36 2007
@@ -67,6 +67,7 @@
             <item name="Request Processing" href="guide/request.html"/>
             <item name="DOM" href="guide/dom.html"/>
             <item name="Class Reloading" href="guide/reload.html"/>
+            <item name="Unit testing pages/components" href="guide/unit-testing-pages.html"/>
         </menu> 
                         
         <menu ref="reports"/>



Mime
View raw message