Author: ivaynberg
Date: Thu Jun 18 16:21:11 2009
New Revision: 786145
URL: http://svn.apache.org/viewvc?rev=786145&view=rev
Log:
WICKET-2306 testing support for ajaxlazyloadpanel
Issue: WICKET-2306
Added:
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxLazyLoadPanelTester.java
(with props)
wicket/trunk/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/AjaxLazyLoadPanelTesterTest.java
(with props)
wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/BehaviorsUtil.java
(with props)
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
Added:
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxLazyLoadPanelTester.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxLazyLoadPanelTester.java?rev=786145&view=auto
==============================================================================
---
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxLazyLoadPanelTester.java
(added)
+++
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxLazyLoadPanelTester.java
Thu Jun 18 16:21:11 2009
@@ -0,0 +1,84 @@
+/*
+ * 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.extensions.ajax.markup.html;
+
+import java.util.List;
+
+import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.Component.IVisitor;
+import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
+import org.apache.wicket.behavior.AbstractAjaxBehavior;
+import org.apache.wicket.behavior.BehaviorsUtil;
+import org.apache.wicket.behavior.IBehavior;
+import org.apache.wicket.util.tester.WicketTester;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Class to help test {...@link AjaxLazyLoadPanel}
+ *
+ * @author Antony Stubbs
+ */
+public class AjaxLazyLoadPanelTester
+{
+
+ private static final Logger logger =
LoggerFactory.getLogger(AjaxLazyLoadPanelTester.class);
+
+ /**
+ * Searches the {...@link MarkupContainer}, looking for and triggering
{...@link AjaxLazyLoadPanel}s
+ * to fetch their contents. Very useful for testing pages / panels that
use
+ * {...@link AjaxLazyLoadPanel}s.
+ *
+ * @param wt
+ * the {...@link WicketTester} to execute the behaviour (
+ * {...@link WicketTester#executeBehavior} ).
+ * @param container
+ * contains the {...@link AjaxLazyLoadPanel} to trigger
+ */
+ public static void executeAjaxLazyLoadPanel(final WicketTester wt,
MarkupContainer container)
+ {
+ container.visitChildren(AjaxLazyLoadPanel.class, new
IVisitor<AjaxLazyLoadPanel>()
+ {
+ public Object component(AjaxLazyLoadPanel component)
+ {
+ // get the AbstractAjaxBehaviour which is
responsible for
+ // getting the contents of the lazy panel
+ List<IBehavior> behaviors =
BehaviorsUtil.getBehaviors(component,
+ AbstractAjaxBehavior.class);
+ if (behaviors.size() == 0)
+ {
+ logger.warn("AjaxLazyLoadPanel child
found, but no attached AbstractAjaxBehaviors found. A curious situation...");
+ }
+ for (IBehavior b : behaviors)
+ {
+ if (b instanceof AbstractAjaxBehavior &&
+ !(b instanceof
AjaxSelfUpdatingTimerBehavior))
+ {
+ // tell wicket tester to
execute it :)
+ logger.debug("Triggering lazy
panel: " + component.getClassRelativePath());
+ AbstractAjaxBehavior
abstractAjaxBehaviour = (AbstractAjaxBehavior)b;
+
wt.executeBehavior(abstractAjaxBehaviour);
+ }
+ }
+ // continue looking for other AjazLazyLoadPanel
+ return CONTINUE_TRAVERSAL;
+ }
+ });
+ }
+
+
+}
Propchange:
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxLazyLoadPanelTester.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
wicket/trunk/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/AjaxLazyLoadPanelTesterTest.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/AjaxLazyLoadPanelTesterTest.java?rev=786145&view=auto
==============================================================================
---
wicket/trunk/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/AjaxLazyLoadPanelTesterTest.java
(added)
+++
wicket/trunk/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/AjaxLazyLoadPanelTesterTest.java
Thu Jun 18 16:21:11 2009
@@ -0,0 +1,78 @@
+/*
+ * 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.extensions.markup.html;
+
+import junit.framework.TestCase;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.Page;
+import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
+import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanelTester;
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.panel.Panel;
+import org.apache.wicket.util.tester.DummyPanelPage;
+import org.apache.wicket.util.tester.TestPanelSource;
+import org.apache.wicket.util.tester.WicketTester;
+
+/**
+ *
+ * Tests {...@link AjaxLazyLoadPanelTester}
+ *
+ * @author Antony Stubbs
+ */
+
+public class AjaxLazyLoadPanelTesterTest extends TestCase
+{
+ private MarkupContainer ajaxLazyLoadPanel;
+
+ /**
+ * Test
+ */
+ public void test()
+ {
+ WicketTester wt = new WicketTester();
+ final Page dummyPanelPage = new DummyPanelPage(new
TestPanelSource()
+ {
+ private static final long serialVersionUID = 1L;
+
+ public Panel getTestPanel(String panelId)
+ {
+ AjaxLazyLoadPanel ajaxLazyLoadPanel1 = new
AjaxLazyLoadPanel(panelId)
+ {
+ private static final long
serialVersionUID = 1L;
+
+ @Override
+ public Component
getLazyLoadComponent(String markupId)
+ {
+ return new Label(markupId,
"lazy panel test");
+ }
+ };
+ ajaxLazyLoadPanel = ajaxLazyLoadPanel1;
+ return (Panel)ajaxLazyLoadPanel;
+ }
+
+ });
+ wt.startPage(dummyPanelPage);
+ wt.assertLabel("panel:content",
+ "<img
src=\"resources/org.apache.wicket.ajax.AbstractDefaultAjaxBehavior/indicator.gif\"/>");
+ AjaxLazyLoadPanelTester.executeAjaxLazyLoadPanel(wt,
dummyPanelPage);
+ wt.debugComponentTrees();
+ wt.assertLabel("panel:content", "lazy panel test");
+ }
+
+}
Propchange:
wicket/trunk/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/AjaxLazyLoadPanelTesterTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/BehaviorsUtil.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/BehaviorsUtil.java?rev=786145&view=auto
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/BehaviorsUtil.java
(added)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/BehaviorsUtil.java
Thu Jun 18 16:21:11 2009
@@ -0,0 +1,62 @@
+package org.apache.wicket.behavior;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.wicket.Component;
+
+/**
+ * This class contains a convenience method neccesary only until 1.5 when we
can make
+ * {...@link Component#getBehaviors(Class)} public
+ *
+ * @author Antony Stubbs
+ * @author igor.vaynberg
+ *
+ * @since 1.4
+ *
+ */
+// FIXME 1.5: remove
+public class BehaviorsUtil
+{
+
+ /**
+ * Returns all behaviors attached to a component which implement a
given type. A workaround
+ * until WICKET-2115 is resolved.
+ *
+ * @param <M>
+ * The type of behavior
+ * @param component
+ * target componet
+ * @param type
+ * type of behaviors to look for, <code>null</code> for all
+ * @return unmodifiable list of behaviors
+ */
+ public static <M extends IBehavior> List<IBehavior>
getBehaviors(Component component,
+ Class<M> type)
+ {
+ List<IBehavior> behaviors = component.getBehaviors();
+ if (behaviors == null)
+ {
+ return Collections.emptyList();
+ }
+
+ List<IBehavior> subset = new
ArrayList<IBehavior>(behaviors.size());
+ for (IBehavior behavior : behaviors)
+ {
+ if (behavior != null)
+ {
+ if (type == null)
+ {
+ subset.add(behavior);
+ }
+ else if
(type.isAssignableFrom(behavior.getClass()))
+ {
+ subset.add(type.cast(behavior));
+ }
+ }
+ }
+ return Collections.unmodifiableList(subset);
+ }
+
+}
Propchange:
wicket/trunk/wicket/src/main/java/org/apache/wicket/behavior/BehaviorsUtil.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java?rev=786145&r1=786144&r2=786145&view=diff
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
(original)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
Thu Jun 18 16:21:11 2009
@@ -26,6 +26,7 @@
import java.util.regex.Pattern;
import org.apache.wicket.Component;
+import org.apache.wicket.MarkupContainer;
import org.apache.wicket.Page;
import org.apache.wicket.PageParameters;
import org.apache.wicket.RequestCycle;
@@ -34,11 +35,13 @@
import org.apache.wicket.Component.IVisitor;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.behavior.AbstractAjaxBehavior;
+import org.apache.wicket.behavior.BehaviorsUtil;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.feedback.FeedbackMessages;
@@ -1107,6 +1110,38 @@
}
/**
+ * Simulates the firing of all ajax timer behaviors on the page
+ *
+ * @param wt
+ * @param container
+ */
+ public void executeAllTimerBehaviors(MarkupContainer container)
+ {
+ container.visitChildren(MarkupContainer.class, new
IVisitor<MarkupContainer>()
+ {
+ public Object component(MarkupContainer component)
+ {
+ // get the AbstractAjaxBehaviour which is
responsible for
+ // getting the contents of the lazy panel
+ List<IBehavior> behaviors =
BehaviorsUtil.getBehaviors(component,
+ AjaxSelfUpdatingTimerBehavior.class);
+ for (IBehavior b : behaviors)
+ {
+ if (b instanceof
AjaxSelfUpdatingTimerBehavior)
+ {
+ log.debug("Triggering
AjaxSelfUpdatingTimerBehavior: " +
+
component.getClassRelativePath());
+ AjaxSelfUpdatingTimerBehavior
abstractAjaxBehaviour = (AjaxSelfUpdatingTimerBehavior)b;
+
executeBehavior(abstractAjaxBehaviour);
+ }
+ }
+ return CONTINUE_TRAVERSAL;
+ }
+ });
+ }
+
+
+ /**
* Simulates the firing of an Ajax event. You add an Ajax event to a
<code>Component</code> by
* using:
*