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

📄 basewickettester.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * 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.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.wicket.Component;import org.apache.wicket.Page;import org.apache.wicket.PageParameters;import org.apache.wicket.RequestCycle;import org.apache.wicket.Session;import org.apache.wicket.WicketRuntimeException;import org.apache.wicket.ajax.AjaxEventBehavior;import org.apache.wicket.ajax.AjaxRequestTarget;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.IBehavior;import org.apache.wicket.feedback.FeedbackMessage;import org.apache.wicket.feedback.FeedbackMessages;import org.apache.wicket.feedback.IFeedbackMessageFilter;import org.apache.wicket.markup.html.basic.Label;import org.apache.wicket.markup.html.form.Button;import org.apache.wicket.markup.html.form.CheckGroup;import org.apache.wicket.markup.html.form.Form;import org.apache.wicket.markup.html.form.FormComponent;import org.apache.wicket.markup.html.form.RadioGroup;import org.apache.wicket.markup.html.link.AbstractLink;import org.apache.wicket.markup.html.link.BookmarkablePageLink;import org.apache.wicket.markup.html.link.IPageLink;import org.apache.wicket.markup.html.link.Link;import org.apache.wicket.markup.html.link.PageLink;import org.apache.wicket.markup.html.list.ListView;import org.apache.wicket.markup.html.panel.Panel;import org.apache.wicket.protocol.http.HttpSessionStore;import org.apache.wicket.protocol.http.MockHttpServletRequest;import org.apache.wicket.protocol.http.MockHttpServletResponse;import org.apache.wicket.protocol.http.MockWebApplication;import org.apache.wicket.protocol.http.WebApplication;import org.apache.wicket.protocol.http.WebRequestCycle;import org.apache.wicket.session.ISessionStore;import org.apache.wicket.util.diff.DiffUtil;import org.apache.wicket.util.lang.Classes;import org.apache.wicket.util.string.Strings;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. See javadoc of <code>WicketTester</code> for example usage. This class can be used * as is, but JUnit users should use derived class <code>WicketTester</code>. *  * @see WicketTester *  * @author Ingram Chen * @author Juergen Donnerstag * @author Frank Bille * @since 1.2.6 */public class BaseWicketTester extends MockWebApplication{	/** log. */	private static final Logger log = LoggerFactory.getLogger(BaseWicketTester.class);	/**	 * @author jcompagner	 */	private static final class TestPageSource implements ITestPageSource	{		private final Page page;		private static final long serialVersionUID = 1L;		/**		 * Constructor.		 * 		 * @param page		 */		private TestPageSource(Page page)		{			this.page = page;		}		public Page getTestPage()		{			return page;		}	}	/**	 * @author frankbille	 */	public static class DummyWebApplication extends WebApplication	{		public Class getHomePage()		{			return DummyHomePage.class;		}		protected void outputDevelopmentModeWarning()		{			// Do nothing.		}		protected ISessionStore newSessionStore()		{			// Don't use a filestore, or we spawn lots of threads, which makes			// things slow.			return new HttpSessionStore(this);		}	}	/**	 * Creates <code>WicketTester</code> and automatically create a <code>WebApplication</code>,	 * but the tester will have no home page.	 */	public BaseWicketTester()	{		this(new DummyWebApplication(), null);	}	/**	 * Creates <code>WicketTester</code> and automatically creates a <code>WebApplication</code>.	 * 	 * @param homePage	 *            a home page <code>Class</code>	 */	public BaseWicketTester(final Class homePage)	{		this(new WebApplication()		{			/**			 * @see org.apache.wicket.Application#getHomePage()			 */			public Class getHomePage()			{				return homePage;			}			protected void outputDevelopmentModeWarning()			{				// Do nothing.			}			protected ISessionStore newSessionStore()			{				// Don't use a filestore, or we spawn lots of threads, which				// makes things slow.				return new HttpSessionStore(this);			}		}, null);	}	/**	 * Creates a <code>WicketTester</code>.	 * 	 * @param application	 *            a <code>WicketTester</code> <code>WebApplication</code> object	 */	public BaseWicketTester(final WebApplication application)	{		this(application, null);	}	/**	 * Creates a <code>WicketTester</code> for unit testing.	 * 	 * @param application	 *            a <code>WicketTester</code> <code>WebApplication</code> object	 * @param path	 *            the absolute path on disk to the <code>WebApplication</code>'s contents (e.g.	 *            war root) - may be <code>null</code>	 * 	 * @see org.apache.wicket.protocol.http.MockWebApplication#MockWebApplication(	 *      org.apache.wicket.protocol.http.WebApplication, String)	 */	public BaseWicketTester(final WebApplication application, final String path)	{		super(application, path);	}	/**	 * Renders a <code>Page</code> defined in <code>TestPageSource</code>. This is usually used	 * when a page does not have default constructor. For example, a <code>ViewBook</code> page	 * requires a <code>Book</code> instance:	 * 	 * <pre>	 * tester.startPage(new TestPageSource()	 * {	 * 	public Page getTestPage()	 * 	{	 * 		Book mockBook = new Book(&quot;myBookName&quot;);	 * 		return new ViewBook(mockBook);	 * 	}	 * });	 * </pre>	 * 	 * @param testPageSource	 *            a <code>Page</code> factory that creates a test page instance	 * @return the rendered Page	 */	public final Page startPage(final ITestPageSource testPageSource)	{		startPage(DummyHomePage.class);		DummyHomePage page = (DummyHomePage)getLastRenderedPage();		page.setTestPageSource(testPageSource);		executeListener(page.getTestPageLink());		return getLastRenderedPage();	}	/**	 * Builds and processes a request suitable for invoking a listener. The <code>Component</code>	 * must implement any of the known <code>IListener</code> interfaces.	 * 	 * @param component	 *            the listener to invoke	 */	public void executeListener(Component component)	{		setupRequestAndResponse();		getServletRequest().setRequestToComponent(component);		processRequestCycle();	}	/**	 * Builds and processes a request suitable for executing an <code>AbstractAjaxBehavior</code>.	 * 	 * @param behavior	 *            an <code>AbstractAjaxBehavior</code> to execute	 */	public void executeBehavior(final AbstractAjaxBehavior behavior)	{		// setupRequestAndResponse();		WebRequestCycle cycle = createRequestCycle();		CharSequence url = behavior.getCallbackUrl(false);		setupRequestAndResponse(true);		cycle = createRequestCycle();		getServletRequest().setRequestToRedirectString(url.toString());		processRequestCycle(cycle);	}	/**	 * Renders the <code>Page</code>.	 * 	 * @param page	 *            a <code>Page</code> to render	 * @return the rendered <code>Page</code>	 */	public final Page startPage(final Page page)	{		return startPage(new TestPageSource(page));	}	/**	 * Renders a <code>Page</code> from its default constructor.	 * 	 * @param pageClass	 *            a test <code>Page</code> class with default constructor	 * @return the rendered <code>Page</code>	 */	public final Page startPage(Class pageClass)	{		processRequestCycle(pageClass);		return getLastRenderedPage();	}	/**	 * Renders a <code>Page</code> from its default constructor.	 * 	 * @param pageClass	 *            a test <code>Page</code> class with default constructor	 * @param parameters	 *            the parameters to use for the class.	 * @return the rendered <code>Page</code>	 */	public final Page startPage(Class pageClass, PageParameters parameters)	{		processRequestCycle(pageClass, parameters);		return getLastRenderedPage();	}	/**	 * Creates a {@link FormTester} for the <code>Form</code> at a given path, and fills all child	 * {@link org.apache.wicket.markup.html.form.FormComponent}s with blank <code>String</code>s.	 * 	 * @param path	 *            path to <code>FormComponent</code>	 * @return a <code>FormTester</code> instance for testing the <code>Form</code>	 * @see #newFormTester(String, boolean)	 */	public FormTester newFormTester(String path)	{		return newFormTester(path, true);	}	/**	 * Creates a {@link FormTester} for the <code>Form</code> at a given path.	 * 	 * @param path	 *            path to <code>FormComponent</code>	 * @param fillBlankString	 *            specifies whether to fill all child <code>FormComponent</code>s with blank	 *            <code>String</code>s	 * @return a <code>FormTester</code> instance for testing the <code>Form</code>	 * @see FormTester	 */	public FormTester newFormTester(String path, boolean fillBlankString)	{		return new FormTester(path, (Form)getComponentFromLastRenderedPage(path), this,			fillBlankString);	}	/**	 * Renders a <code>Panel</code> defined in <code>TestPanelSource</code>. The usage is	 * similar to {@link #startPage(ITestPageSource)}. Please note that testing <code>Panel</code>	 * must use the supplied <code>panelId<code> as a <code>Component</code> id.	 *	 * <pre>	 * tester.startPanel(new TestPanelSource()	 * {	 * 	public Panel getTestPanel(String panelId)	 * 	{	 * 		MyData mockMyData = new MyData();	 * 		return new MyPanel(panelId, mockMyData);	 * 	}	 * });	 * </pre>	 *	 * @param testPanelSource	 *            a <code>Panel</code> factory that creates test <code>Panel</code> instances	 * @return a rendered <code>Panel</code>	 */	public final Panel startPanel(final TestPanelSource testPanelSource)	{		return (Panel)startPage(new ITestPageSource()		{			private static final long serialVersionUID = 1L;			public Page getTestPage()			{				return new DummyPanelPage(testPanelSource);			}		}).get(DummyPanelPage.TEST_PANEL_ID);	}	/**	 * Renders a <code>Panel</code> from a <code>Panel(String id)</code> constructor.	 * 	 * @param panelClass	 *            a test <code>Panel</code> class with <code>Panel(String id)</code> constructor	 * @return a rendered <code>Panel</code>	 */	public final Panel startPanel(final Class panelClass)	{		return (Panel)startPage(new ITestPageSource()		{			private static final long serialVersionUID = 1L;			public Page getTestPage()			{				return new DummyPanelPage(new TestPanelSource()				{					private static final long serialVersionUID = 1L;					public Panel getTestPanel(String panelId)					{						try						{							Constructor c = panelClass.getConstructor(new Class[] { String.class });							return (Panel)c.newInstance(new Object[] { panelId });						}						catch (SecurityException e)						{							throw convertoUnexpect(e);						}						catch (NoSuchMethodException e)						{							throw convertoUnexpect(e);						}						catch (InstantiationException e)						{							throw convertoUnexpect(e);						}						catch (IllegalAccessException e)						{							throw convertoUnexpect(e);						}						catch (InvocationTargetException e)						{							throw convertoUnexpect(e);						}					}				});			}		}).get(DummyPanelPage.TEST_PANEL_ID);	}	/**	 * A helper method for starting a component for a test without attaching it to a Page.	 * 	 * Components which are somehow dependent on the page structure can not be currently tested with	 * this method.	 * 	 * Example:	 * 	 * UserDataView view = new UserDataView("view", new ListDataProvider(userList));	 * tester.startComponent(view); assertEquals(4, view.size());	 * 	 * @param component	 */	public void startComponent(Component component)	{		if (component instanceof FormComponent)		{			((FormComponent)component).processInput();		}		component.beforeRender();	}

⌨️ 快捷键说明

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