/*
 * 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.markup.html.internal;

import org.apache.wicket.Page;
import org.apache.wicket.WicketTestCase;
import org.apache.wicket.markup.parser.filter.InlineEnclosureHandler;
import org.apache.wicket.protocol.http.WebApplication;
import org.junit.Test;


/**
 * Test for {@link InlineEnclosure} and {@link InlineEnclosureHandler}.
 * 
 * @author Joonas Hamalainen
 */
public class InlineEnclosureTest extends WicketTestCase
{
	/**
	 * WICKET-5085: Since {@link InlineEnclosure}s are not removed as other auto-Components, they must be
	 * resolved once only.
	 * 
	 * @throws Exception
	 */
	@Test
	public void inlineEnclosuresAreResolvedOnceOnly() throws Exception
	{
		InlineEnclosurePanelPage page = new InlineEnclosurePanelPage();

		tester.startPage(page);

		assertEquals(2, page.visitChildren(InlineEnclosure.class).toList().size());

		tester.startPage(page);

		assertEquals(2, page.visitChildren(InlineEnclosure.class).toList().size());

		tester.startPage(page);

		assertEquals(2, page.visitChildren(InlineEnclosure.class).toList().size());
	}


	/**
	 * @throws Exception
	 */
	@Test
	public void inlineEnclosurePage_1() throws Exception
	{
		executeTest(InlineEnclosurePage_1.class, "InlineEnclosurePageExpectedResult_1.html");
	}

	/**
	 * @throws Exception
	 */
	@Test
	public void inlineEnclosurePage_2() throws Exception
	{
		executeTest(InlineEnclosurePage_2.class, "InlineEnclosurePageExpectedResult_2.html");
	}

	/**
	 * @throws Exception
	 */
	@Test
	public void inlineEnclosurePanelPage() throws Exception
	{
		executeTest(InlineEnclosurePanelPage.class, "InlineEnclosurePanelPageExpectedResult.html");
	}

	@Test
	public void inlineEnclosurePageDifferentNamespace() throws Exception
	{
		executeTest(InlineEnclosureDifferentNamespacePage.class,
			"InlineEnclosureDifferentNamespaceExpectedResult.html");
	}

	/**
	 * https://issues.apache.org/jira/browse/WICKET-4520
	 * 
	 * Tests that wicket:enclosure and wicket:message attributes can co-exist
	 * 
	 * @throws Exception
	 */
	@Test
	public void inlineEnclosureWithWicketMessageVisible() throws Exception
	{
		executeTest(new InlineEnclosureWithWicketMessagePage(true),
			"InlineEnclosureWithWicketMessagePage_visible_expected.html");
	}

	/**
	 * 
	 * https://issues.apache.org/jira/browse/WICKET-4520
	 * 
	 * Tests that wicket:enclosure and wicket:message attributes can co-exist
	 * 
	 * @throws Exception
	 */
	@Test
	public void inlineEnclosureWithWicketMessageInvisible() throws Exception
	{
		executeTest(new InlineEnclosureWithWicketMessagePage(false),
			"InlineEnclosureWithWicketMessagePage_invisible_expected.html");
	}

	@Override
	protected WebApplication newApplication()
	{
		return new WebApplication()
		{
			@Override
			public Class<? extends Page> getHomePage()
			{
				return InlineEnclosurePage_1.class;
			}

			@Override
			protected void init()
			{
				getMarkupSettings().setStripWicketTags(true);
			}
		};
	}

}
@@ -863,16 +863,149 @@
 nal;%0D%0A%0D%0A
+import java.util.concurrent.atomic.AtomicInteger;%0D%0A%0D%0Aimport org.apache.wicket.Component;%0D%0Aimport org.apache.wicket.MarkupContainer;%0D%0A
 import o
@@ -1197,16 +1197,108 @@
 ation;%0D%0A
+import org.apache.wicket.util.visit.IVisit;%0D%0Aimport org.apache.wicket.util.visit.IVisitor;%0D%0A
 import o
@@ -1862,35 +1862,37 @@
 uals(2, 
-page.visitChildren(
+childrenByType(page, 
 InlineEn
@@ -1905,32 +1905,16 @@
 e.class)
-.toList().size()
 );%0D%0A%0D%0A%09%09
@@ -1958,35 +1958,37 @@
 uals(2, 
-page.visitChildren(
+childrenByType(page, 
 InlineEn
@@ -2001,32 +2001,16 @@
 e.class)
-.toList().size()
 );%0D%0A%0D%0A%09%09
@@ -2058,27 +2058,29 @@
 (2, 
-page.visitChildren(
+childrenByType(page, 
 Inli
@@ -2101,32 +2101,372 @@
 ass)
-.toList().size()
+);%0D%0A%09%7D%0D%0A%0D%0A%09private %3CT%3E int childrenByType(MarkupContainer parent, Class%3CT%3E filter)%0D%0A%09%7B%0D%0A%09%09final AtomicInteger count = new AtomicInteger(0);%0D%0A%09%09parent.visitChildren(filter, new IVisitor%3CComponent, Void%3E()%0D%0A%09%09%7B%0D%0A%09%09%09@Override%0D%0A%09%09%09public void component(Component object, IVisit%3CVoid%3E visit)%0D%0A%09%09%09%7B%0D%0A%09%09%09%09count.incrementAndGet(
 );%0D%0A
+%09%09
 %09%7D%0D%0A
+%09%09%7D);%0D%0A%0D%0A%09%09return count.get();%0D%0A%09%7D
 %0D%0A%0D%0A
@@ -4412,8 +4412,10 @@
 %0A%09%7D%0D%0A%0D%0A%7D
+%0D%0A

Use non-deprecated MarkupContainer#visitChildren(Class, IVisitor)