Repository: wicket
Updated Branches:
  refs/heads/wicket-6.x 2c4133145 -> 899d72777


WICKET-5926 Arquillian Support with Container ServletContext in 
BaseWicketTester/WicketTester.

Cherry pick the improvements to (Base)WicketTester from 7.x.
The Arquillian test will be only in 7.x.


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/899d7277
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/899d7277
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/899d7277

Branch: refs/heads/wicket-6.x
Commit: 899d72777021e05a2610a07f5c2f37c25a19ef3b
Parents: 2c41331
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Thu Jun 25 11:18:08 2015 +0300
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Thu Jun 25 11:18:08 2015 +0300

----------------------------------------------------------------------
 .../wicket/util/tester/BaseWicketTester.java    | 79 +++++++++++++++-----
 .../apache/wicket/util/tester/WicketTester.java | 28 +++++++
 2 files changed, 90 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/899d7277/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java 
b/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
index 8b230b8..2661c87 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
@@ -259,20 +259,58 @@ public class BaseWicketTester
         */
        public BaseWicketTester(final WebApplication application, final 
ServletContext servletCtx)
        {
+               this(application, servletCtx, true);
+       }
+
+       /**
+        * Creates a <code>WicketTester</code>.
+        *
+        * @param application
+        *            a <code>WicketTester</code> <code>WebApplication</code> 
object
+        * @param init
+        *            force the application to be initialized (default = true)
+        */
+       public BaseWicketTester(final WebApplication application, boolean init)
+       {
+               this(application, null, init);
+       }
+
+       /**
+        * Creates a <code>WicketTester</code>.
+        *
+        * @param application
+        *            a <code>WicketTester</code> <code>WebApplication</code> 
object
+        * @param servletCtx
+        *            the servlet context used as backend
+        * @param init
+        *            force the application to be initialized (default = true)
+        */
+       public BaseWicketTester(final WebApplication application, final 
ServletContext servletCtx, boolean init)
+       {
+               if (!init)
+               {
+                       Args.notNull(application, "application");
+               }
+
                servletContext = servletCtx != null ? servletCtx
-                       : new MockServletContext(application, null);
+                               // If it's provided from the container it's not 
necessary to mock.
+                               : !init && application.getServletContext() != 
null ? application.getServletContext()
+                               : new MockServletContext(application, null);
 
-               final FilterConfig filterConfig = new TestFilterConfig();
-               WicketFilter filter = new WicketFilter()
+               // If using Arquillian and it's configured in a web.xml it'll 
be provided. If not, mock it.
+               if(application.getWicketFilter() == null)
                {
-                       @Override
-                       public FilterConfig getFilterConfig()
+                       final FilterConfig filterConfig = new 
TestFilterConfig();
+                       WicketFilter filter = new WicketFilter()
                        {
-                               return filterConfig;
-                       }
-               };
-
-               application.setWicketFilter(filter);
+                               @Override
+                               public FilterConfig getFilterConfig()
+                               {
+                                       return filterConfig;
+                               }
+                       };
+                       application.setWicketFilter(filter);
+               }
 
                httpSession = new MockHttpSession(servletContext);
 
@@ -280,15 +318,22 @@ public class BaseWicketTester
 
                this.application = application;
 
-               // FIXME some tests are leaking applications by not calling 
destroy on them or overriding
-               // teardown() without calling super, for now we work around by 
making each name unique
-               application.setName("WicketTesterApplication-" + 
UUID.randomUUID());
+               // If it's provided from the container it's not necessary to 
set again.
+               if (init)
+               {
+                       // FIXME some tests are leaking applications by not 
calling destroy on them or overriding
+                       // teardown() without calling super, for now we work 
around by making each name unique
+                       application.setName("WicketTesterApplication-" + 
UUID.randomUUID());
+               }
                ThreadContext.setApplication(application);
 
-               application.setServletContext(servletContext);
-
-               // initialize the application
-               application.initApplication();
+               // If it's provided from the container it's not necessary to 
set again and init.
+               if (init)
+               {
+                       application.setServletContext(servletContext);
+                       // initialize the application
+                       application.initApplication();
+               }
 
                // We don't expect any changes during testing. In addition we 
avoid creating
                // ModificationWatcher threads tests.

http://git-wip-us.apache.org/repos/asf/wicket/blob/899d7277/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java 
b/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java
index b6ebcf6..bc61eea 100644
--- a/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java
+++ b/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java
@@ -228,6 +228,34 @@ public class WicketTester extends BaseWicketTester
        }
 
        /**
+        * Creates a <code>WicketTester</code> to help unit testing.
+        *
+        * @param application
+        *            a <code>WicketTester</code> <code>WebApplication</code> 
object
+        * @param init
+        *            force the application to be initialized (default = true)
+        */
+       public WicketTester(WebApplication application, boolean init)
+       {
+               super(application, init);
+       }
+
+       /**
+        * Creates a <code>WicketTester</code> to help unit testing.
+        *
+        * @param application
+        *            a <code>WicketTester</code> <code>WebApplication</code> 
object
+        * @param servletCtx
+        *            the servlet context used as backend
+        * @param init
+        *            force the application to be initialized (default = true)
+        */
+       public WicketTester(WebApplication application, ServletContext 
servletCtx, boolean init)
+       {
+               super(application, servletCtx, init);
+       }
+
+       /**
         * Asserts that the Ajax location header is present.
         */
        public void assertAjaxLocation()

Reply via email to