You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by de...@apache.org on 2018/05/03 18:26:47 UTC

[myfaces-test] 19/24: MYFACESTEST-14 Update myfaces-test to use JUnit 4.x

This is an automated email from the ASF dual-hosted git repository.

deki pushed a commit to branch 1_0_0_beta_4
in repository https://gitbox.apache.org/repos/asf/myfaces-test.git

commit be6fa62b1ec87888f7e7dc63a173d997a8646005
Author: Leonardo Uribe <lu...@apache.org>
AuthorDate: Thu Jul 1 20:38:30 2010 +0000

    MYFACESTEST-14 Update myfaces-test to use JUnit 4.x
---
 test12/pom.xml                                     |  12 +-
 .../myfaces/test/base/AbstractJsfTestCase.java     |   5 +
 .../AbstractJsfConfigurableMockTestCase.java}      |  50 ++--
 .../base/{ => junit4}/AbstractJsfTestCase.java     |  25 +-
 .../htmlunit/junit4/AbstractHtmlUnitTestCase.java  | 320 +++++++++++++++++++++
 .../test/mock/{TestMockBean.java => MockBean.java} |   2 +-
 .../myfaces/test/mock/MockObjectsTestCase.java     |   4 +-
 test20/pom.xml                                     |   2 +-
 8 files changed, 386 insertions(+), 34 deletions(-)

diff --git a/test12/pom.xml b/test12/pom.xml
index 3ffa3bb..4484902 100644
--- a/test12/pom.xml
+++ b/test12/pom.xml
@@ -67,7 +67,7 @@
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <version>3.8.1</version>
+            <version>4.5</version>
             <scope>compile</scope>
         </dependency>
 
@@ -97,6 +97,13 @@
     <build>
         <plugins>
             <plugin>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.5</source>
+                    <target>1.5</target>
+                </configuration>
+            </plugin>
+            <plugin>
                 <artifactId>maven-source-plugin</artifactId>
                 <executions>
                     <execution>
@@ -112,7 +119,7 @@
     <!-- Allow building with JDK 1.4 as well as JDK 1.5,
          the 1.4 profile caters only to JSF 1.1 -->
     <profiles>
-
+        <!-- 
         <profile>
             <id>myfaces-test-jdk14</id>
             <activation>
@@ -162,6 +169,7 @@
                 </plugins>
             </build>
         </profile>
+         -->
 
         <profile>
             <id>myfaces-test-jdk15</id>
diff --git a/test12/src/main/java/org/apache/myfaces/test/base/AbstractJsfTestCase.java b/test12/src/main/java/org/apache/myfaces/test/base/AbstractJsfTestCase.java
index 4c11900..fbe30cb 100644
--- a/test12/src/main/java/org/apache/myfaces/test/base/AbstractJsfTestCase.java
+++ b/test12/src/main/java/org/apache/myfaces/test/base/AbstractJsfTestCase.java
@@ -108,6 +108,11 @@ public abstract class AbstractJsfTestCase extends TestCase {
 
         setFactories();
 
+        setUpJSFObjects();
+    }
+    
+    protected void setUpJSFObjects()  throws Exception
+    {
         setUpExternalContext();
         setUpLifecycle();
         setUpFacesContext();
diff --git a/test12/src/main/java/org/apache/myfaces/test/base/AbstractJsfTestCase.java b/test12/src/main/java/org/apache/myfaces/test/base/junit4/AbstractJsfConfigurableMockTestCase.java
similarity index 86%
copy from test12/src/main/java/org/apache/myfaces/test/base/AbstractJsfTestCase.java
copy to test12/src/main/java/org/apache/myfaces/test/base/junit4/AbstractJsfConfigurableMockTestCase.java
index 4c11900..f8cbe11 100644
--- a/test12/src/main/java/org/apache/myfaces/test/base/AbstractJsfTestCase.java
+++ b/test12/src/main/java/org/apache/myfaces/test/base/junit4/AbstractJsfConfigurableMockTestCase.java
@@ -15,20 +15,23 @@
  * limitations under the License.
  */
 
-package org.apache.myfaces.test.base;
+package org.apache.myfaces.test.base.junit4;
 
 import java.net.URL;
 import java.net.URLClassLoader;
 
 import javax.faces.FactoryFinder;
+import javax.faces.application.Application;
 import javax.faces.application.ApplicationFactory;
 import javax.faces.component.UIViewRoot;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.faces.context.FacesContextFactory;
+import javax.faces.lifecycle.Lifecycle;
 import javax.faces.lifecycle.LifecycleFactory;
+import javax.faces.render.RenderKit;
 import javax.faces.render.RenderKitFactory;
 
-import junit.framework.TestCase;
-
-import org.apache.myfaces.test.mock.MockApplication;
 import org.apache.myfaces.test.mock.MockExternalContext;
 import org.apache.myfaces.test.mock.MockFacesContext;
 import org.apache.myfaces.test.mock.MockFacesContextFactory;
@@ -40,9 +43,11 @@ import org.apache.myfaces.test.mock.MockServletConfig;
 import org.apache.myfaces.test.mock.MockServletContext;
 import org.apache.myfaces.test.mock.lifecycle.MockLifecycle;
 import org.apache.myfaces.test.mock.lifecycle.MockLifecycleFactory;
+import org.junit.After;
+import org.junit.Before;
 
 /**
- * <p>Abstract JUnit test case base class, which sets up the JavaServer Faces
+ * <p>Abstract JUnit 4.5 test case base class, which sets up the JavaServer Faces
  * mock object environment for a particular simulated request.  The following
  * protected variables are initialized in the <code>setUp()</code> method, and
  * cleaned up in the <code>tearDown()</code> method:</p>
@@ -71,7 +76,7 @@ import org.apache.myfaces.test.mock.lifecycle.MockLifecycleFactory;
  * the test methods for your test case.</p>
  */
 
-public abstract class AbstractJsfTestCase extends TestCase {
+public abstract class AbstractJsfConfigurableMockTestCase {
 
 
     // ------------------------------------------------------------ Constructors
@@ -82,8 +87,8 @@ public abstract class AbstractJsfTestCase extends TestCase {
      *
      * @param name Name of this test case
      */
-    public AbstractJsfTestCase(String name) {
-        super(name);
+    public AbstractJsfConfigurableMockTestCase()
+    {
     }
 
 
@@ -93,7 +98,8 @@ public abstract class AbstractJsfTestCase extends TestCase {
     /**
      * <p>Set up instance variables required by this test case.</p>
      */
-    protected void setUp() throws Exception {
+    @Before
+    public void setUp() throws Exception {
 
         // Set up a new thread context class loader
         threadContextClassLoader = Thread.currentThread().getContextClassLoader();
@@ -108,6 +114,11 @@ public abstract class AbstractJsfTestCase extends TestCase {
 
         setFactories();
 
+        setUpJSFObjects();
+    }
+    
+    protected void setUpJSFObjects()  throws Exception
+    {
         setUpExternalContext();
         setUpLifecycle();
         setUpFacesContext();
@@ -177,8 +188,8 @@ public abstract class AbstractJsfTestCase extends TestCase {
     {
         ApplicationFactory applicationFactory = (ApplicationFactory)
         FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
-        application = (MockApplication) applicationFactory.getApplication();
-        facesContext.setApplication(application);
+        application = applicationFactory.getApplication();
+        ((MockFacesContext)facesContext).setApplication(application);
     }
     
     protected void setUpRenderKit() throws Exception
@@ -192,7 +203,8 @@ public abstract class AbstractJsfTestCase extends TestCase {
     /**
      * <p>Tear down instance variables required by this test case.</p>
      */
-    protected void tearDown() throws Exception {
+    @After
+    public void tearDown() throws Exception {
 
         application = null;
         config = null;
@@ -218,14 +230,14 @@ public abstract class AbstractJsfTestCase extends TestCase {
 
 
     // Mock object instances for our tests
-    protected MockApplication         application = null;
+    protected Application             application = null;
     protected MockServletConfig       config = null;
-    protected MockExternalContext     externalContext = null;
-    protected MockFacesContext        facesContext = null;
-    protected MockFacesContextFactory facesContextFactory = null;
-    protected MockLifecycle           lifecycle = null;
-    protected MockLifecycleFactory    lifecycleFactory = null;
-    protected MockRenderKit           renderKit = null;
+    protected ExternalContext         externalContext = null;
+    protected FacesContext            facesContext = null;
+    protected FacesContextFactory     facesContextFactory = null;
+    protected Lifecycle               lifecycle = null;
+    protected LifecycleFactory        lifecycleFactory = null;
+    protected RenderKit               renderKit = null;
     protected MockHttpServletRequest  request = null;
     protected MockHttpServletResponse response = null;
     protected MockServletContext      servletContext = null;
diff --git a/test12/src/main/java/org/apache/myfaces/test/base/AbstractJsfTestCase.java b/test12/src/main/java/org/apache/myfaces/test/base/junit4/AbstractJsfTestCase.java
similarity index 94%
copy from test12/src/main/java/org/apache/myfaces/test/base/AbstractJsfTestCase.java
copy to test12/src/main/java/org/apache/myfaces/test/base/junit4/AbstractJsfTestCase.java
index 4c11900..2734ee2 100644
--- a/test12/src/main/java/org/apache/myfaces/test/base/AbstractJsfTestCase.java
+++ b/test12/src/main/java/org/apache/myfaces/test/base/junit4/AbstractJsfTestCase.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.myfaces.test.base;
+package org.apache.myfaces.test.base.junit4;
 
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -26,8 +26,6 @@ import javax.faces.component.UIViewRoot;
 import javax.faces.lifecycle.LifecycleFactory;
 import javax.faces.render.RenderKitFactory;
 
-import junit.framework.TestCase;
-
 import org.apache.myfaces.test.mock.MockApplication;
 import org.apache.myfaces.test.mock.MockExternalContext;
 import org.apache.myfaces.test.mock.MockFacesContext;
@@ -40,9 +38,11 @@ import org.apache.myfaces.test.mock.MockServletConfig;
 import org.apache.myfaces.test.mock.MockServletContext;
 import org.apache.myfaces.test.mock.lifecycle.MockLifecycle;
 import org.apache.myfaces.test.mock.lifecycle.MockLifecycleFactory;
+import org.junit.After;
+import org.junit.Before;
 
 /**
- * <p>Abstract JUnit test case base class, which sets up the JavaServer Faces
+ * <p>Abstract JUnit 4.5 test case base class, which sets up the JavaServer Faces
  * mock object environment for a particular simulated request.  The following
  * protected variables are initialized in the <code>setUp()</code> method, and
  * cleaned up in the <code>tearDown()</code> method:</p>
@@ -71,7 +71,7 @@ import org.apache.myfaces.test.mock.lifecycle.MockLifecycleFactory;
  * the test methods for your test case.</p>
  */
 
-public abstract class AbstractJsfTestCase extends TestCase {
+public abstract class AbstractJsfTestCase {
 
 
     // ------------------------------------------------------------ Constructors
@@ -82,8 +82,8 @@ public abstract class AbstractJsfTestCase extends TestCase {
      *
      * @param name Name of this test case
      */
-    public AbstractJsfTestCase(String name) {
-        super(name);
+    public AbstractJsfTestCase()
+    {
     }
 
 
@@ -93,7 +93,8 @@ public abstract class AbstractJsfTestCase extends TestCase {
     /**
      * <p>Set up instance variables required by this test case.</p>
      */
-    protected void setUp() throws Exception {
+    @Before
+    public void setUp() throws Exception {
 
         // Set up a new thread context class loader
         threadContextClassLoader = Thread.currentThread().getContextClassLoader();
@@ -108,6 +109,11 @@ public abstract class AbstractJsfTestCase extends TestCase {
 
         setFactories();
 
+        setUpJSFObjects();
+    }
+    
+    protected void setUpJSFObjects()  throws Exception
+    {
         setUpExternalContext();
         setUpLifecycle();
         setUpFacesContext();
@@ -192,7 +198,8 @@ public abstract class AbstractJsfTestCase extends TestCase {
     /**
      * <p>Tear down instance variables required by this test case.</p>
      */
-    protected void tearDown() throws Exception {
+    @After
+    public void tearDown() throws Exception {
 
         application = null;
         config = null;
diff --git a/test12/src/main/java/org/apache/myfaces/test/htmlunit/junit4/AbstractHtmlUnitTestCase.java b/test12/src/main/java/org/apache/myfaces/test/htmlunit/junit4/AbstractHtmlUnitTestCase.java
new file mode 100644
index 0000000..7151773
--- /dev/null
+++ b/test12/src/main/java/org/apache/myfaces/test/htmlunit/junit4/AbstractHtmlUnitTestCase.java
@@ -0,0 +1,320 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.myfaces.test.htmlunit.junit4;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Iterator;
+
+import org.junit.After;
+import org.junit.Before;
+
+import com.gargoylesoftware.htmlunit.ElementNotFoundException;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
+import com.gargoylesoftware.htmlunit.html.HtmlBody;
+import com.gargoylesoftware.htmlunit.html.HtmlElement;
+import com.gargoylesoftware.htmlunit.html.HtmlForm;
+import com.gargoylesoftware.htmlunit.html.HtmlHead;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
+
+
+
+/**
+ * <p>Abstract base class for system integration tests based on HtmlUnit.
+ * These tests will expect a system property named <code>url</code> to be
+ * present, which will define the URL (including the context path, but
+ * without a trailing slash) of the application to be tested.</p>
+ */
+
+public abstract class AbstractHtmlUnitTestCase {
+
+
+    // ------------------------------------------------------------ Constructors
+
+
+    /**
+     * <p>Construct a new instance of this test case.</p>
+     *
+     * @param name Name of the new test case
+     */
+    public AbstractHtmlUnitTestCase()
+    {
+    }
+
+
+    // ------------------------------------------------------ Instance Variables
+
+
+    /**
+     * <p>The most recently retrieved page from the server.</p>
+     */
+    protected HtmlPage page = null;
+
+
+    /**
+     * <p>The calculated URL for the installed "systest" web application.
+     * This value is based on a system property named <code>url</code>,
+     * which must be defined as part of the command line that executes
+     * each test case.</p>
+     */
+    protected URL url = null;
+
+
+    /**
+     * <p>The web client for this test case.</p>
+     */
+    protected WebClient webClient = null;
+
+
+    // ------------------------------------------------------ Test Setup Methods
+
+
+    /**
+     * <p>Set up the instance variables required for this test case.</p>
+     *
+     * @exception Exception if an error occurs
+     */
+    @Before
+    protected void setUp() throws Exception {
+
+        // Calculate the URL for the installed "systest" web application
+        String url = System.getProperty("url");
+        this.url = new URL(url + "/");
+
+        // Initialize HtmlUnit constructs for this test case
+        webClient = new WebClient();
+
+    }
+
+    /**
+     * <p>Tear down instance variables required by this test case.</p>
+     */
+    @After
+    protected void tearDown() throws Exception {
+
+        page = null;
+        url = null;
+        webClient = null;
+
+    }
+
+
+
+    // ------------------------------------------------------- Protected Methods
+
+
+    /**
+     * <p>Return the body element for the most recently retrieved page.
+     * If there is no such element, return <code>null</code>.</p>
+     *
+     * @exception Exception if an error occurs
+     */
+    protected HtmlBody body() throws Exception {
+
+        Iterator elements = page.getAllHtmlChildElements();
+        while (elements.hasNext()) {
+            HtmlElement element = (HtmlElement) elements.next();
+            if (element instanceof HtmlBody) {
+                return ((HtmlBody) element);
+            }
+        }
+        return (null);
+
+    }
+
+
+    /**
+     * <p>Return the HTML element with the specified <code>id</code> from the
+     * most recently retrieved page.  If there is no such element, return
+     * <code>null</code>.</p>
+     *
+     * @param id Identifier of the requested element.
+     *
+     * @exception Exception if an error occurs
+     */
+    protected HtmlElement element(String id) throws Exception {
+
+        try {
+            return (page.getHtmlElementById(id));
+        } catch (ElementNotFoundException e) {
+            return (null);
+        }
+
+    }
+
+
+    /**
+     * <p>Return the form with the specified <code>id</code> from the most
+     * recently retrieved page.  If there is no such form, return
+     * <code>null</code>.<p>
+     *
+     * @param id Identifier of the requested form.
+     *
+     * @exception Exception if an error occurs
+     */
+    protected HtmlForm form(String id) throws Exception {
+
+        Iterator forms = page.getForms().iterator();
+        while (forms.hasNext()) {
+            HtmlForm form = (HtmlForm) forms.next();
+            if (id.equals(form.getAttributeValue("id"))) {
+                return (form);
+            }
+        }
+        return (null);
+
+    }
+
+
+    /**
+     * <p>Return the head element for the most recently retrieved page.
+     * If there is no such element, return <code>null</code>.</p>
+     *
+     * @exception Exception if an error occurs
+     */
+    protected HtmlHead head() throws Exception {
+
+        Iterator elements = page.getAllHtmlChildElements();
+        while (elements.hasNext()) {
+            HtmlElement element = (HtmlElement) elements.next();
+            if (element instanceof HtmlHead) {
+                return ((HtmlHead) element);
+            }
+        }
+        return (null);
+
+    }
+
+
+    /**
+     * <p>Click the specified hyperlink, and retrieve the subsequent page,
+     * saving a reference so that other utility methods may be used to
+     * retrieve information from it.</p>
+     *
+     * @param anchor Anchor component to click
+     *
+     * @exception IOException if an input/output error occurs
+     */
+    protected HtmlPage link(HtmlAnchor anchor) throws IOException {
+
+        HtmlPage page = (HtmlPage) anchor.click();
+        this.page = page;
+        return page;
+
+    }
+
+
+    /**
+     * <p>Return the currently stored page reference.</p>
+     */
+    protected HtmlPage page() {
+
+        return this.page;
+
+    }
+
+
+    /**
+     * <p>Retrieve and return the page at the specified context relative path.
+     * Save a reference to this page so that other utility methods may be used
+     * to retrieve information from it.</p>
+     *
+     * @param path Context relative path
+     *
+     * @exception IllegalArgumentException if the context relative path
+     *  does not begin with a '/' character
+     * @exception Exception if a different error occurs
+     */
+    protected HtmlPage page(String path) throws Exception {
+
+        HtmlPage page = (HtmlPage) webClient.getPage(url(path));
+        this.page = page;
+        return (page);
+
+    }
+
+
+    /**
+     * <p>Reset the stored page reference to the specified value.  This is
+     * useful for scenarios testing resubmit of the same page (simulating the
+     * user pressing the back button and then submitting again).</p>
+     *
+     * @param page Previously saved page to which to reset
+     */
+    protected void reset(HtmlPage page) {
+
+        this.page = page;
+
+    }
+
+
+    /**
+     * <p>Submit the current page, using the specified component, and retrieve
+     * the subsequent page, saving a reference so that other utility methods
+     * may be used to retrieve information from it.</p>
+     *
+     * @param submit Submit button component to click
+     *
+     * @exception IOException if an input/output error occurs
+     */
+    protected HtmlPage submit(HtmlSubmitInput submit) throws IOException {
+
+        HtmlPage page = (HtmlPage) submit.click();
+        this.page = page;
+        return page;
+
+    }
+
+
+    /**
+     * <p>Return the page title from the most recently retrieved page.
+     * Any leading and trailing whitespace will be trimmed.</p>
+     *
+     * @exception Exception if an error occurs
+     */
+    protected String title() throws Exception {
+
+        return (page.getTitleText().trim());
+
+    }
+
+
+    /**
+     * <p>Calculate and return an absolute URL for the specified context
+     * relative path, which must begin with a '/' character.</p>
+     *
+     * @param path Context relative path
+     *
+     * @exception IllegalArgumentException if the context relative path
+     *  does not begin with a '/' character
+     * @exception Exception if a different error ocurs
+     */
+    protected URL url(String path) throws Exception {
+
+        if (path.charAt(0) != '/') {
+            throw new IllegalArgumentException("Context path '" + path
+                                               + "' does not start with '/'");
+        }
+        return new URL(url, path.substring(1));
+
+    }
+
+
+}
diff --git a/test12/src/test/java/org/apache/myfaces/test/mock/TestMockBean.java b/test12/src/test/java/org/apache/myfaces/test/mock/MockBean.java
similarity index 97%
rename from test12/src/test/java/org/apache/myfaces/test/mock/TestMockBean.java
rename to test12/src/test/java/org/apache/myfaces/test/mock/MockBean.java
index cd4acf5..2e05dda 100644
--- a/test12/src/test/java/org/apache/myfaces/test/mock/TestMockBean.java
+++ b/test12/src/test/java/org/apache/myfaces/test/mock/MockBean.java
@@ -24,7 +24,7 @@ import java.io.Serializable;
  *
  * $Id$
  */
-public class TestMockBean implements Serializable {
+public class MockBean implements Serializable {
 
     private static final long serialVersionUID = 8879968751506858610L;
     private String command;
diff --git a/test12/src/test/java/org/apache/myfaces/test/mock/MockObjectsTestCase.java b/test12/src/test/java/org/apache/myfaces/test/mock/MockObjectsTestCase.java
index c8755c7..7721a75 100644
--- a/test12/src/test/java/org/apache/myfaces/test/mock/MockObjectsTestCase.java
+++ b/test12/src/test/java/org/apache/myfaces/test/mock/MockObjectsTestCase.java
@@ -57,7 +57,7 @@ public class MockObjectsTestCase extends AbstractJsfTestCase {
         session.setAttribute("sameKey", "sameKeySesValue");
         request.setAttribute("reqScopeName", "reqScopeValue");
         request.setAttribute("sameKey", "sameKeyReqValue");
-        request.setAttribute("test", new TestMockBean());
+        request.setAttribute("test", new MockBean());
 
     }
 
@@ -103,7 +103,7 @@ public class MockObjectsTestCase extends AbstractJsfTestCase {
 
     public void testMethodBindingInvokePositive() throws Exception {
 
-        TestMockBean bean = (TestMockBean) request.getAttribute("test");
+        MockBean bean = (MockBean) request.getAttribute("test");
         MethodBinding mb = null;
         Class argsString[] = new Class[] { String.class };
         Class argsNone[] = new Class[0];
diff --git a/test20/pom.xml b/test20/pom.xml
index 8021996..86c5c99 100644
--- a/test20/pom.xml
+++ b/test20/pom.xml
@@ -67,7 +67,7 @@
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <version>3.8.1</version>
+            <version>4.5</version>
             <scope>compile</scope>
         </dependency>
 

-- 
To stop receiving notification emails like this one, please contact
deki@apache.org.