WICKET-5452 Make Wicket-Atmosphere testable - AtmosphereTester

Reuse the already installed EventBus in MyApp#init. Just set TesterBroadcaster 
for testing purposes.

Add APIs to make it possible to assert on the suspended (by Atmosphere) http 
response

(cherry picked from commit 77d0a46ef018a1bb35282fbb374d9de56ac51c39)


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

Branch: refs/heads/master
Commit: 16fca60fee3ade5c2522f838f6ac4919bda71871
Parents: e6b59d5
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Thu Aug 14 12:05:00 2014 +0200
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Fri Aug 15 10:14:39 2014 +0200

----------------------------------------------------------------------
 .../wicket/util/tester/BaseWicketTester.java    |  10 +-
 .../org/apache/wicket/atmosphere/EventBus.java  |  27 ++-
 .../atmosphere/tester/AtmosphereTester.java     | 183 +++++++++++++++----
 .../tester/TesterAtmosphereBehavior.java        |  65 +++++++
 .../atmosphere/tester/TesterBroadcaster.java    |   6 +-
 .../atmosphere/tester/TesterEventBus.java       |  54 ------
 .../wicket/atmosphere/AtmosphereTest.java       |  68 -------
 .../wicket/atmosphere/AtmosphereTesterTest.java | 108 +++++++++++
 8 files changed, 358 insertions(+), 163 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/16fca60f/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 aa5b7b2..79c1a08 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
@@ -514,6 +514,14 @@ public class BaseWicketTester
        }
 
        /**
+        * @param response
+        */
+       public void setLastResponse(final MockHttpServletResponse response)
+       {
+               this.lastResponse = response;
+       }
+
+       /**
         * @return session
         */
        public Session getSession()
@@ -797,7 +805,7 @@ public class BaseWicketTester
        private void recordRequestResponse()
        {
                lastRequest = request;
-               lastResponse = response;
+               setLastResponse(response);
 
                previousRequests.add(request);
                previousResponses.add(response);

http://git-wip-us.apache.org/repos/asf/wicket/blob/16fca60f/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/EventBus.java
----------------------------------------------------------------------
diff --git 
a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/EventBus.java
 
b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/EventBus.java
index e4dd4a5..6dd5065 100644
--- 
a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/EventBus.java
+++ 
b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/EventBus.java
@@ -101,9 +101,16 @@ public class EventBus implements UnboundListener
                return eventBus;
        }
 
-       private final WebApplication application;
+       /**
+        * @param application
+        * @return {@code true} if there is an installed EventBus to the given 
application
+        */
+       public static boolean isInstalled(Application application)
+       {
+               return application.getMetaData(EVENT_BUS_KEY) != null;
+       }
 
-       private final Broadcaster broadcaster;
+       private final WebApplication application;
 
        private final Multimap<PageKey, EventSubscription> subscriptions = 
HashMultimap.create();
 
@@ -113,6 +120,8 @@ public class EventBus implements UnboundListener
 
        private final AtmosphereParameters parameters = new 
AtmosphereParameters();
 
+       private Broadcaster broadcaster;
+
        /**
         * A flag indicating whether to be notified about Atmosphere internal 
events
         *
@@ -131,7 +140,7 @@ public class EventBus implements UnboundListener
         */
        public EventBus(WebApplication application)
        {
-               this(application, lookupDefaultBroadcaster());
+               this(application, null);
        }
 
        private static Broadcaster lookupDefaultBroadcaster()
@@ -193,7 +202,13 @@ public class EventBus implements UnboundListener
         */
        public Broadcaster getBroadcaster()
        {
-               return broadcaster;
+               return broadcaster != null ? broadcaster : 
lookupDefaultBroadcaster();
+       }
+
+       public EventBus setBroadcaster(Broadcaster broadcaster)
+       {
+               this.broadcaster = broadcaster;
+               return this;
        }
 
        /**
@@ -413,7 +428,7 @@ public class EventBus implements UnboundListener
                        subscriptionsForPage = 
Iterables.filter(eventSubscriptions, new EventFilter(event));
                }
                if (key == null)
-                       broadcaster.removeAtmosphereResource(resource);
+                       getBroadcaster().removeAtmosphereResource(resource);
                else
                {
                        Iterator<EventSubscription> iterator = 
subscriptionsForPage.iterator();
@@ -444,7 +459,7 @@ public class EventBus implements UnboundListener
                        subscriptionsForPage, event);
                Response response = new 
AtmosphereWebResponse(resource.getResponse());
                if (application.createRequestCycle(request, 
response).processRequestAndDetach())
-                       broadcaster.broadcast(response.toString(), resource);
+                       getBroadcaster().broadcast(response.toString(), 
resource);
        }
 
        @Override

http://git-wip-us.apache.org/repos/asf/wicket/blob/16fca60f/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/AtmosphereTester.java
----------------------------------------------------------------------
diff --git 
a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/AtmosphereTester.java
 
b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/AtmosphereTester.java
index 91ea64e..eb1cf1b 100644
--- 
a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/AtmosphereTester.java
+++ 
b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/AtmosphereTester.java
@@ -16,63 +16,182 @@
  */
 package org.apache.wicket.atmosphere.tester;
 
+import java.util.List;
+
 import org.apache.wicket.Page;
 import org.apache.wicket.atmosphere.AtmosphereBehavior;
+import org.apache.wicket.atmosphere.EventBus;
 import org.apache.wicket.protocol.http.WebApplication;
+import org.apache.wicket.protocol.http.mock.MockHttpServletResponse;
 import org.apache.wicket.util.tester.WicketTester;
-import org.atmosphere.cpr.AtmosphereRequest;
+import org.atmosphere.cpr.AtmosphereConfig;
+import org.atmosphere.cpr.AtmosphereFramework;
 import org.atmosphere.cpr.AtmosphereResource;
-import org.atmosphere.cpr.AtmosphereResourceImpl;
-import org.atmosphere.cpr.AtmosphereResponse;
 import org.atmosphere.cpr.HeaderConfig;
-import org.atmosphere.handler.AtmosphereHandlerAdapter;
 
 /**
- *
+ * A helper for testing Atmosphere enabled pages
  */
 public class AtmosphereTester
 {
-       private final TesterEventBus eventBus;
+       /**
+        * The EventBus that will be used to post/push messages
+        * to the suspended http response
+        */
+       private final EventBus eventBus;
+
+       private final WicketTester wicketTester;
+       
+       /**
+        * The response which will be suspended by Atmosphere and
+        * all pushes/post will go to
+        */
+       private MockHttpServletResponse suspendedResponse;
 
+       private MockHttpServletResponse lastResponse;
+       
+       /**
+        * Constructor.
+        *
+        * @param wicketTester
+        *          The testing helper
+        * @param page
+        *          The page to test
+        */
        public AtmosphereTester(final WicketTester wicketTester, Page page)
        {
+               this.wicketTester = wicketTester;
+
                WebApplication application = wicketTester.getApplication();
-               this.eventBus = new TesterEventBus(application);
 
-               AtmosphereBehavior atmosphereBehavior = new AtmosphereBehavior()
+               TesterBroadcaster broadcaster = createBroadcaster();
+
+               if (EventBus.isInstalled(application))
+               {
+                       this.eventBus = EventBus.get(application);
+                       this.eventBus.setBroadcaster(broadcaster);
+               }
+               else
                {
-                       @Override
-                       public void onRequest()
-                       {
-                               TesterBroadcaster broadcaster = 
eventBus.getBroadcaster();
-
-                               AtmosphereResource atmosphereResource = new 
AtmosphereResourceImpl();
-                               AtmosphereRequest atmosphereRequest = 
AtmosphereRequest.wrap(wicketTester.getRequest());
-                               AtmosphereResponse atmosphereResponse = 
AtmosphereResponse.wrap(wicketTester.getResponse());
-                               TesterAsyncSupport asyncSupport = new 
TesterAsyncSupport();
-                               
atmosphereResource.initialize(broadcaster.getApplicationConfig(), broadcaster, 
atmosphereRequest, atmosphereResponse,
-                                               asyncSupport, new 
AtmosphereHandlerAdapter());
-
-                               atmosphereResource.setBroadcaster(broadcaster);
-                               
broadcaster.addAtmosphereResource(atmosphereResource);
-
-                               String uuid = atmosphereResource.uuid();
-                               Page page = getComponent().getPage();
-
-                               page.setMetaData(ATMOSPHERE_UUID, uuid);
-                               eventBus.registerPage(uuid, page);
-                       }
-               };
+                       this.eventBus = new EventBus(application, broadcaster);
+               }
+
+               initialize(wicketTester, page);
+       }
+
+       private void initialize(final WicketTester wicketTester, Page page)
+       {
+               // remove any already installed AtmosphereBehaviors on the page
+               List<AtmosphereBehavior> behaviors = 
page.getBehaviors(AtmosphereBehavior.class);
+               page.remove(behaviors.toArray(new 
AtmosphereBehavior[behaviors.size()]));
+
+               // install AtmosphereBehavior that doesn't use Meteor
+               AtmosphereBehavior atmosphereBehavior = new 
TesterAtmosphereBehavior(wicketTester, eventBus);
                page.add(atmosphereBehavior);
 
+               // start the page to collect all @Subscribe methods in the 
component hierarchy
                wicketTester.startPage(page);
+
+               // pretend it is a websocket connection
                
wicketTester.getRequest().setHeader(HeaderConfig.X_ATMOSPHERE_TRANSPORT, 
AtmosphereResource.TRANSPORT.WEBSOCKET.name());
+
+               // start the "upgrade" connection
+               suspendedResponse = wicketTester.getResponse();
                wicketTester.executeBehavior(atmosphereBehavior);
        }
 
-       public AtmosphereTester post(Object payload)
+       private TesterBroadcaster createBroadcaster()
+       {
+               TesterBroadcaster broadcaster = new TesterBroadcaster();
+
+               AtmosphereFramework framework = new AtmosphereFramework();
+               AtmosphereConfig config = new AtmosphereConfig(framework);
+
+               TesterBroadcasterFactory broadcasterFactory = new 
TesterBroadcasterFactory(config, broadcaster);
+               framework.setBroadcasterFactory(broadcasterFactory);
+
+               broadcaster.initialize("wicket-atmosphere-tester", config);
+
+               return broadcaster;
+       }
+
+       /**
+        * @return The collected so far pushed data in the suspended response
+        */
+       public String getPushedResponse()
        {
-               eventBus.post(payload);
+               return suspendedResponse != null ? 
suspendedResponse.getDocument() : null;
+       }
+
+       /**
+        * Resets the suspended response to be empty
+        * @return this instance, for chaining
+        */
+       public AtmosphereTester resetResponse()
+       {
+               if (suspendedResponse != null)
+               {
+                       suspendedResponse.reset();
+               }
+               return this;
+       }
+
+       /**
+        * Posts a message to all suspended responses
+        *
+        * @param message
+        *          The message to push
+        * @return this instance, for chaining
+        */
+       public AtmosphereTester post(Object message)
+       {
+               eventBus.post(message);
+               return this;
+       }
+
+       /**
+        * Posts a message to the suspended response with the given uuid
+        *
+        * @param message
+        *          The message to push
+        * @param resourceUuid
+        *          The identifier of the suspended http response
+        * @return this instance, for chaining
+        */
+       public AtmosphereTester post(Object message, String resourceUuid)
+       {
+               eventBus.post(message, resourceUuid);
+               return this;
+       }
+
+       /**
+        * Switches the current <em>lastResponse</em> with the 
<em>suspendedResponse</em>
+        * so the application test can use WicketTester's assert methods.
+        *
+        * Note: Make sure to call {@linkplain #switchOffTestMode()} later to 
be able to
+        * assert on non-Atmosphere related responses
+        *
+        * @return this instance, for chaining
+        */
+       public AtmosphereTester switchOnTestMode()
+       {
+               lastResponse = wicketTester.getLastResponse();
+               wicketTester.setLastResponse(suspendedResponse);
+               return this;
+       }
+
+       /**
+        * Sets back the <em>lastResponse</em> with the saved on by {@link 
#switchOnTestMode()}.
+        *
+        * @return this instance, for chaining
+        */
+       public AtmosphereTester switchOffTestMode()
+       {
+               if (lastResponse != null)
+               {
+                       wicketTester.setLastResponse(lastResponse);
+                       lastResponse = null;
+               }
                return this;
        }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/16fca60f/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterAtmosphereBehavior.java
----------------------------------------------------------------------
diff --git 
a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterAtmosphereBehavior.java
 
b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterAtmosphereBehavior.java
new file mode 100644
index 0000000..d6632fe
--- /dev/null
+++ 
b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterAtmosphereBehavior.java
@@ -0,0 +1,65 @@
+/*
+ * 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.wicket.atmosphere.tester;
+
+import org.apache.wicket.Page;
+import org.apache.wicket.atmosphere.AtmosphereBehavior;
+import org.apache.wicket.atmosphere.EventBus;
+import org.apache.wicket.util.tester.WicketTester;
+import org.atmosphere.cpr.AtmosphereRequest;
+import org.atmosphere.cpr.AtmosphereResource;
+import org.atmosphere.cpr.AtmosphereResourceImpl;
+import org.atmosphere.cpr.AtmosphereResponse;
+import org.atmosphere.handler.AtmosphereHandlerAdapter;
+
+/**
+ * A specialization that doesn't use Meteor to create AtmosphereResource
+ * but creates it manually by using the WicketTester's http request and 
response
+ */
+class TesterAtmosphereBehavior extends AtmosphereBehavior
+{
+       private final EventBus eventBus;
+       private final WicketTester wicketTester;
+
+       TesterAtmosphereBehavior(WicketTester wicketTester, EventBus eventBus)
+       {
+               this.wicketTester = wicketTester;
+               this.eventBus = eventBus;
+       }
+
+       @Override
+       public void onRequest()
+       {
+               TesterBroadcaster broadcaster = (TesterBroadcaster) 
eventBus.getBroadcaster();
+
+               AtmosphereResource atmosphereResource = new 
AtmosphereResourceImpl();
+               AtmosphereRequest atmosphereRequest = 
AtmosphereRequest.wrap(wicketTester.getRequest());
+               AtmosphereResponse atmosphereResponse = 
AtmosphereResponse.wrap(wicketTester.getResponse());
+               TesterAsyncSupport asyncSupport = new TesterAsyncSupport();
+               
atmosphereResource.initialize(broadcaster.getApplicationConfig(), broadcaster, 
atmosphereRequest, atmosphereResponse,
+                               asyncSupport, new AtmosphereHandlerAdapter());
+
+               atmosphereResource.setBroadcaster(broadcaster);
+               broadcaster.addAtmosphereResource(atmosphereResource);
+
+               String uuid = atmosphereResource.uuid();
+               Page page = getComponent().getPage();
+
+               page.setMetaData(ATMOSPHERE_UUID, uuid);
+               eventBus.registerPage(uuid, page);
+       }
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/16fca60f/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterBroadcaster.java
----------------------------------------------------------------------
diff --git 
a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterBroadcaster.java
 
b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterBroadcaster.java
index 4fae2da..7452d6a 100644
--- 
a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterBroadcaster.java
+++ 
b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterBroadcaster.java
@@ -19,6 +19,8 @@ package org.apache.wicket.atmosphere.tester;
 import org.atmosphere.cpr.AtmosphereConfig;
 import org.atmosphere.cpr.AtmosphereResource;
 import org.atmosphere.cpr.AtmosphereResponse;
+import org.atmosphere.cpr.BroadcasterConfig;
+import org.atmosphere.cpr.DefaultBroadcaster;
 import org.atmosphere.cpr.Entry;
 import org.atmosphere.util.SimpleBroadcaster;
 
@@ -33,9 +35,9 @@ class TesterBroadcaster extends SimpleBroadcaster
        }
 
        @Override
-       protected void executeBlockingWrite(AtmosphereResource resource, Entry 
entry)
+       protected void push(Entry entry)
        {
-               AtmosphereResponse response = resource.getResponse();
+               AtmosphereResponse response = entry.resource.getResponse();
                String message = entry.message.toString();
                response.write(message);
        }

http://git-wip-us.apache.org/repos/asf/wicket/blob/16fca60f/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterEventBus.java
----------------------------------------------------------------------
diff --git 
a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterEventBus.java
 
b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterEventBus.java
deleted file mode 100644
index 48e97cb..0000000
--- 
a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterEventBus.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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.wicket.atmosphere.tester;
-
-import org.apache.wicket.atmosphere.EventBus;
-import org.apache.wicket.protocol.http.WebApplication;
-import org.atmosphere.cpr.AtmosphereConfig;
-import org.atmosphere.cpr.AtmosphereFramework;
-
-/**
- *
- */
-class TesterEventBus extends EventBus
-{
-       public TesterEventBus(WebApplication application)
-       {
-               super(application, createBroadcaster());
-       }
-
-       private static TesterBroadcaster createBroadcaster()
-       {
-               TesterBroadcaster broadcaster = new TesterBroadcaster();
-
-               AtmosphereFramework framework = new AtmosphereFramework();
-               AtmosphereConfig config = new AtmosphereConfig(framework);
-
-               TesterBroadcasterFactory broadcasterFactory = new 
TesterBroadcasterFactory(config, broadcaster);
-               framework.setBroadcasterFactory(broadcasterFactory);
-
-               broadcaster.initialize("wicket-atmosphere-tester", config);
-
-               return broadcaster;
-       }
-
-       @Override
-       public TesterBroadcaster getBroadcaster()
-       {
-               return (TesterBroadcaster) super.getBroadcaster();
-       }
-}

http://git-wip-us.apache.org/repos/asf/wicket/blob/16fca60f/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTest.java
 
b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTest.java
deleted file mode 100644
index 368367d..0000000
--- 
a/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTest.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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.wicket.atmosphere;
-
-import java.util.Date;
-
-import org.apache.wicket.ajax.AjaxRequestTarget;
-import org.apache.wicket.atmosphere.tester.AtmosphereTester;
-import org.apache.wicket.request.mapper.parameter.PageParameters;
-import org.apache.wicket.util.tester.WicketTester;
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- *
- */
-public class AtmosphereTest extends Assert
-{
-       @Test
-       public void atmospherePush()
-       {
-               final String updateTimeIsExecuted = "updateTime is executed!";
-
-               WicketTester tester = new WicketTester();
-               HomePage page = new HomePage(new PageParameters())
-               {
-                       @Subscribe
-                       public void updateTime(AjaxRequestTarget target, Date 
event)
-                       {
-                               super.updateTime(target, event);
-
-                               target.appendJavaScript(updateTimeIsExecuted);
-                       }
-
-                       @Subscribe(contextAwareFilter = ReceiverFilter.class)
-                       public void receiveMessage(AjaxRequestTarget target, 
ChatMessage message)
-                       {
-                               super.receiveMessage(target, message);
-
-                               System.err.println("receiveMessage");
-                       }
-               };
-
-               AtmosphereTester waTester = new AtmosphereTester(tester, page);
-
-               Date payload = new Date();
-               waTester.post(payload);
-
-//             System.err.println("Ajax response:\n" + 
tester.getLastResponseAsString());
-               tester.assertContains(updateTimeIsExecuted);
-
-               tester.destroy();
-       }
-}

http://git-wip-us.apache.org/repos/asf/wicket/blob/16fca60f/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTesterTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTesterTest.java
 
b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTesterTest.java
new file mode 100644
index 0000000..c8f2773
--- /dev/null
+++ 
b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTesterTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.wicket.atmosphere;
+
+import static org.hamcrest.CoreMatchers.is;
+
+import java.util.Date;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.atmosphere.tester.AtmosphereTester;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.apache.wicket.util.tester.FormTester;
+import org.apache.wicket.util.tester.WicketTester;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class AtmosphereTesterTest extends Assert
+{
+       final AtomicBoolean updateTimeCalled = new AtomicBoolean(false);
+       final AtomicBoolean receiveMessageCalled = new AtomicBoolean(false);
+
+       @Test
+       public void atmospherePush()
+       {
+               final String updateTimeIsExecuted = "updateTime is executed!";
+
+               WicketTester tester = new WicketTester();
+               HomePage page = new HomePage(new PageParameters())
+               {
+                       @Subscribe
+                       public void updateTime(AjaxRequestTarget target, Date 
event)
+                       {
+                               super.updateTime(target, event);
+
+                               updateTimeCalled.set(true);
+
+                               target.appendJavaScript(updateTimeIsExecuted);
+                       }
+
+                       @Subscribe(contextAwareFilter = ReceiverFilter.class)
+                       public void receiveMessage(AjaxRequestTarget target, 
ChatMessage message)
+                       {
+                               super.receiveMessage(target, message);
+                               receiveMessageCalled.set(true);
+                       }
+               };
+
+               AtmosphereTester waTester = new AtmosphereTester(tester, page);
+
+               assertThat(updateTimeCalled.get(), is(false));
+               assertThat(receiveMessageCalled.get(), is(false));
+
+               Date payload = new Date();
+               waTester.post(payload);
+
+               assertThat(updateTimeCalled.get(), is(true));
+               assertThat(receiveMessageCalled.get(), is(false));
+
+               tester.assertContains(updateTimeIsExecuted);
+
+               final FormTester form = tester.newFormTester("form");
+
+               form.setValue("input", "Atmosphere rocks!");
+
+               form.submit("send");
+
+               assertThat(updateTimeCalled.get(), is(true));
+               assertThat(receiveMessageCalled.get(), is(true));
+
+               // get the the collected so far content of the suspended 
response
+               // Note: it may contain several <ajax-response>s.
+               // use waTester.resetResponse() to remove the collected data
+               String atmosphereResponse = waTester.getPushedResponse();
+//             System.out.println("RES:" + atmosphereResponse);
+
+               // assert
+               Assert.assertFalse("<?xml version=\"1.0\" 
encoding=\"UTF-8\"?><ajax-response></ajax-response>"
+                               .equals(atmosphereResponse));
+
+               waTester.switchOnTestMode();
+               // now the assertions are against the Atmosphere's suspended 
response data
+               tester.assertComponentOnAjaxResponse("message");
+               waTester.switchOffTestMode();
+               // now the assertions will be the real last response
+
+               tester.assertLabel("message", "Atmosphere rocks!");
+
+               tester.destroy();
+       }
+}

Reply via email to