Package org.apache.wicket

Source Code of org.apache.wicket.BehaviorRequestTest$TestCallbackBehavior

/*
* 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;

import junit.framework.TestCase;

import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.behavior.IBehaviorListener;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.IMarkupResourceStreamProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.handler.ListenerInterfaceRequestHandler;
import org.apache.wicket.request.handler.PageAndComponentProvider;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.StringResourceStream;
import org.apache.wicket.util.tester.WicketTester;


/**
* @see https://issues.apache.org/jira/browse/WICKET-3098
*/
public class BehaviorRequestTest extends TestCase
{
  private WicketTester tester;
  private TestPage page;

  @Override
  protected void setUp() throws Exception
  {
    tester = new WicketTester();
    page = new TestPage();
    tester.startPage(page);
  }

  public void testEnabledBehaviorRequest()
  {
    tester.executeUrl(urlForBehavior(page.enabledBehavior));
    assertTrue(page.enabledBehavior.isCalled());
  }

  public void testDisabledBehaviorRequest()
  {
    tester.executeUrl(urlForBehavior(page.disabledBehavior));
    assertTrue(!page.disabledBehavior.isCalled());
  }

  private String urlForBehavior(IBehavior behaviorUnderTest)
  {
    int index = page.container.getBehaviorId(behaviorUnderTest);
    String enabledBehaviorUrl = tester.urlFor(
      new ListenerInterfaceRequestHandler(new PageAndComponentProvider(page, page.container),
        IBehaviorListener.INTERFACE, index)).toString();
    return enabledBehaviorUrl;
  }

  public static class TestPage extends WebPage implements IMarkupResourceStreamProvider
  {
    private WebMarkupContainer container;
    private TestCallbackBehavior enabledBehavior;
    private TestCallbackBehavior disabledBehavior;

    public TestPage()
    {
      enabledBehavior = new TestCallbackBehavior();
      enabledBehavior.setEnabled(true);
      disabledBehavior = new TestCallbackBehavior();
      disabledBehavior.setEnabled(false);
      container = new WebMarkupContainer("container");
      container.add(enabledBehavior);
      container.add(disabledBehavior);
      add(container);
    }

    public IResourceStream getMarkupResourceStream(MarkupContainer container,
      Class<?> containerClass)
    {
      return new StringResourceStream("<html><a wicket:id=\"container\">container</a></html>");
    }
  }

  private static class TestCallbackBehavior extends AbstractBehavior implements IBehaviorListener
  {
    private boolean enabled;
    private boolean called;

    @Override
    public void onComponentTag(Component component, ComponentTag tag)
    {
      super.onComponentTag(component, tag);
      tag.put("href", component.urlFor(this, IBehaviorListener.INTERFACE));
    }

    public void onRequest()
    {
      called = true;
    }

    public void setEnabled(boolean enabled)
    {
      this.enabled = enabled;
    }

    @Override
    public boolean isEnabled(Component component)
    {
      return component.isEnabledInHierarchy() && enabled;
    }

    public boolean isCalled()
    {
      return called;
    }

  }
}
TOP

Related Classes of org.apache.wicket.BehaviorRequestTest$TestCallbackBehavior

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.