/*
* 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.ajax;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.MockPageWithLinkAndComponent;
import org.apache.wicket.MockPageWithOneComponent;
import org.apache.wicket.Page;
import org.apache.wicket.WicketTestCase;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.protocol.http.WebRequestCycle;
import org.apache.wicket.util.tester.ITestPageSource;
import org.apache.wicket.util.time.Duration;
/**
* Tests that an AbstractAjaxTimerBehavior injects itself into the markup once
* and only once. Also tests the callback URL to make sure the timer reinjects
* itself
*
* @author Jim McLaughlin
*/
public class AjaxTimerBehaviorTest extends WicketTestCase
{
private static final Log log = LogFactory.getLog(AjaxTimerBehaviorTest.class);
/**
* Construct.
*
* @param name
*/
public AjaxTimerBehaviorTest(String name)
{
super(name);
}
/**
* Tests timer behavior in a component added to an AjaxRequestTarget
*/
public void testAddToAjaxUpdate()
{
Duration dur = Duration.seconds(20);
final MyAjaxSelfUpdatingTimerBehavior timer = new MyAjaxSelfUpdatingTimerBehavior(dur);
final MockPageWithLinkAndComponent page = new MockPageWithLinkAndComponent();
page.add(new WebComponent(MockPageWithLinkAndComponent.COMPONENT_ID)
.setOutputMarkupId(true));
page.add(new AjaxLink(MockPageWithLinkAndComponent.LINK_ID)
{
private static final long serialVersionUID = 1L;
public void onClick(AjaxRequestTarget target)
{
WebMarkupContainer wmc = new WebMarkupContainer(
MockPageWithLinkAndComponent.COMPONENT_ID);
wmc.setOutputMarkupId(true);
wmc.add(timer);
page.replace(wmc);
target.addComponent(wmc);
}
});
tester.startPage(new ITestPageSource()
{
private static final long serialVersionUID = 1L;
public Page getTestPage()
{
return page;
}
});
tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
validate(timer, false);
}
/**
* tests timer behavior in a WebPage.
*/
public void testAddToWebPage()
{
Duration dur = Duration.seconds(20);
final MyAjaxSelfUpdatingTimerBehavior timer = new MyAjaxSelfUpdatingTimerBehavior(dur);
final MockPageWithOneComponent page = new MockPageWithOneComponent();
Label label = new Label(MockPageWithOneComponent.COMPONENT_ID, "Hello");
page.add(label);
label.setOutputMarkupId(true);
label.add(timer);
tester.startPage(new ITestPageSource()
{
private static final long serialVersionUID = 1L;
public Page getTestPage()
{
return page;
}
});
validate(timer, true);
}
/**
* Validates the reponse, then makes sure the timer injects itself again
* when called.
*
* @param timer
* @param inBodyOnLoad
*/
private void validate(MyAjaxSelfUpdatingTimerBehavior timer, boolean inBodyOnLoad)
{
String document = tester.getServletResponse().getDocument();
String updateScript = timer.getUpdateScript();
String bodyOnLoadUpdateScript = "Wicket.Event.add(window, \"load\", function() { " + updateScript + ";});";
if (inBodyOnLoad)
{
validateTimerScript(document, bodyOnLoadUpdateScript);
}
else
{
validateTimerScript(document, updateScript);
}
tester.setupRequestAndResponse();
WebRequestCycle cycle = tester.createRequestCycle();
String url = timer.getCallbackUrl().toString();
tester.getServletRequest().setRequestToRedirectString(url);
tester.processRequestCycle(cycle);
// Validate the document
document = tester.getServletResponse().getDocument();
validateTimerScript(document, updateScript);
}
/**
* Checks that the timer javascript is in the document once and only once
*
* @param document
* the response from the Application
* @param updateScript
* the timer script
*/
private void validateTimerScript(String document, String updateScript)
{
log.debug(document);
String quotedRegex;
quotedRegex = quote(updateScript);
Pattern pat = Pattern.compile(quotedRegex, Pattern.DOTALL);
Matcher mat = pat.matcher(document);
int count = 0;
while (mat.find())
{
++count;
}
// make sure there is only one match
assertEquals("There should be 1 and only 1 script in the markup for this behavior,"
+ "but " + count + " were found", 1, count);
}
// quick fix for JDK 5 method
private static final String quote(String s) {
int slashEIndex = s.indexOf("\\E");
if (slashEIndex == -1)
return "\\Q" + s + "\\E";
StringBuffer sb = new StringBuffer(s.length() * 2);
sb.append("\\Q");
slashEIndex = 0;
int current = 0;
while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
sb.append(s.substring(current, slashEIndex));
current = slashEIndex + 2;
sb.append("\\E\\\\E\\Q");
}
sb.append(s.substring(current, s.length()));
sb.append("\\E");
return sb.toString();
}
static class MyAjaxSelfUpdatingTimerBehavior extends AjaxSelfUpdatingTimerBehavior
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Duration duration;
String updateScript;
/**
* Construct.
* @param updateInterval
*/
public MyAjaxSelfUpdatingTimerBehavior(Duration updateInterval)
{
super(updateInterval);
this.duration = updateInterval;
}
protected void onComponentRendered()
{
super.onComponentRendered();
updateScript = getJsTimeoutCall(duration);
}
/**
* @return Update script
*/
public String getUpdateScript()
{
return updateScript;
}
}
}