⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wickettester.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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.util.tester;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;import javax.servlet.http.HttpServletResponse;import junit.framework.Assert;import junit.framework.AssertionFailedError;import org.apache.wicket.Component;import org.apache.wicket.Page;import org.apache.wicket.ajax.AjaxRequestTarget;import org.apache.wicket.feedback.FeedbackMessage;import org.apache.wicket.markup.html.basic.Label;import org.apache.wicket.markup.html.list.ListView;import org.apache.wicket.protocol.http.HttpSessionStore;import org.apache.wicket.protocol.http.MockHttpServletResponse;import org.apache.wicket.protocol.http.SecondLevelCacheSessionStore;import org.apache.wicket.protocol.http.UnitTestSettings;import org.apache.wicket.protocol.http.WebApplication;import org.apache.wicket.protocol.http.WebResponse;import org.apache.wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore;import org.apache.wicket.session.ISessionStore;import org.apache.wicket.util.diff.DiffUtil;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * A helper class to ease unit testing of Wicket applications without the need for a servlet * container. To start a test, either use <code>startPage</code> or <code>startPanel</code>: *  * <pre> * // production page * public class MyPage extends WebPage * { * 	public MyPage() * 	{ * 		add(new Label(&quot;myMessage&quot;, &quot;Hello!&quot;)); * 		add(new Link(&quot;toYourPage&quot;) * 		{ * 			public void onClick() * 			{ * 				setResponsePage(new YourPage(&quot;Hi!&quot;)); * 			} * 		}); * 	} * } * </pre> *  * <pre> * // test code * private WicketTester tester; *  * public void setUp() * { * 	tester = new WicketTester(); * } *  * public void testRenderMyPage() * { * 	//start and render the test page * 	tester.startPage(MyPage.class); * 	//assert rendered page class * 	tester.assertRenderedPage(MyPage.class); * 	//assert rendered label component * 	tester.assertLabel(&quot;myMessage&quot;, &quot;Hello!&quot;); * } * </pre> *  * The above example is straight forward: start <code>MyPage.class</code> and assert * <code>Label</code> it rendered. Next, we try to navigate through a <code>Link</code>: *  * <pre> * // production page * public class YourPage extends WebPage * { * 	public YourPage(String message) * 	{ * 		add(new Label(&quot;yourMessage&quot;, message)); * 		info(&quot;Wicket Rocks ;-)&quot;); * 	} * } *  * //test code * public void testLinkToYourPage() * { * 	tester.startPage(MyPage.class); * 	//click link and render * 	tester.clickLink(&quot;toYourPage&quot;); * 	tester.assertRenderedPage(YourPage.class); * 	tester.assertLabel(&quot;yourMessage&quot;, &quot;Hi!&quot;); * } * </pre> *  * <code>tester.clickLink(path);</code> will simulate user click on the component (in this case, * it's a <code>Link</code>) and render the response page <code>YourPage</code>. Ok, unit test * of <code>MyPage</code> is completed. Now we test <code>YourPage</code> standalone: *  * <pre> * //test code * public void testRenderYourPage() * { * 	// provide page instance source for WicketTester * 	tester.startPage(new TestPageSource() * 	{ * 		public Page getTestPage() * 		{ * 			return new YourPage(&quot;mock message&quot;); * 		} * 	}); * 	tester.assertRenderedPage(YourPage.class); * 	tester.assertLabel(&quot;yourMessage&quot;, &quot;mock message&quot;); * 	// assert feedback messages in INFO Level * 	tester.assertInfoMessages(new String[] { &quot;Wicket Rocks ;-)&quot; }); * } * </pre> *  * Instead of <code>tester.startPage(pageClass)</code>, we define a * {@link org.apache.wicket.util.tester.ITestPageSource} to provide testing page instance for * <code>WicketTester</code>. This is necessary because <code>YourPage</code> uses a custom * constructor, which is very common for transferring model data, but cannot be instantiated by * reflection. Finally, we use <code>assertInfoMessages</code> to assert there is a feedback * message "Wicket Rocks ;-)" at the INFO level. *  * TODO General: Example usage of FormTester *  * @author Ingram Chen * @author Juergen Donnerstag * @author Frank Bille * @since 1.2.6 */public class WicketTester extends BaseWicketTester{	/**	 * Default dummy web application for testing. Uses {@link HttpSessionStore} to store pages and	 * the <code>Session</code>.	 */	public static class DummyWebApplication extends WebApplication	{		/**		 * @see org.apache.wicket.Application#getHomePage()		 */		public Class getHomePage()		{			return DummyHomePage.class;		}		protected ISessionStore newSessionStore()		{			// Don't use a filestore, or we spawn lots of threads, which makes			// things slow.			return new HttpSessionStore(this);		}		/**		 * @see org.apache.wicket.protocol.http.WebApplication#newWebResponse(javax.servlet.http.HttpServletResponse)		 */		protected WebResponse newWebResponse(final HttpServletResponse servletResponse)		{			return new WebResponse(servletResponse);		}		protected void outputDevelopmentModeWarning()		{			// do nothing		}	}	/**	 * Dummy web application that does not support back button support but is cheaper to use for	 * unit tests. Uses {@link SecondLevelCacheSessionStore} with a noop {@link IPageStore}.	 */	public static class NonPageCachingDummyWebApplication extends DummyWebApplication	{		protected ISessionStore newSessionStore()		{			return new SecondLevelCacheSessionStore(this, new IPageStore()			{				public void destroy()				{				}				public Page getPage(String sessionId, String pagemap, int id, int versionNumber,					int ajaxVersionNumber)				{					return null;				}				public void pageAccessed(String sessionId, Page page)				{				}				public void removePage(String sessionId, String pagemap, int id)				{				}				public void storePage(String sessionId, Page page)				{				}				public void unbind(String sessionId)				{				}				public boolean containsPage(String sessionId, String pageMapName, int pageId,					int pageVersion)				{					return false;				}			});		}	}	/** log. */	private static final Logger log = LoggerFactory.getLogger(WicketTester.class);	/**	 * Creates a <code>WicketTester</code> and automatically creates a <code>WebApplication</code>,	 * but the tester will have no home page.	 */	public WicketTester()	{		this(new DummyWebApplication());	}	/**	 * Creates a <code>WicketTester</code> and automatically creates a <code>WebApplication</code>.	 * 	 * @param homePage	 *            a home page <code>Class</code>	 */	public WicketTester(final Class homePage)	{		this(new WebApplication()		{			/**			 * @see org.apache.wicket.Application#getHomePage()			 */			public Class getHomePage()			{				return homePage;			}			protected ISessionStore newSessionStore()			{				// Don't use a filestore, or we spawn lots of threads, which				// makes things slow.				return new HttpSessionStore(this);			}			protected WebResponse newWebResponse(final HttpServletResponse servletResponse)			{				return new WebResponse(servletResponse);			}			protected void outputDevelopmentModeWarning()			{				// Do nothing.			}		});	}	/**	 * Creates a <code>WicketTester</code>.	 * 	 * @param application	 *            a <code>WicketTester</code> <code>WebApplication</code> object	 */	public WicketTester(final WebApplication application)	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -