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

📄 mockwebapplication.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.protocol.http;import java.io.File;import java.util.Enumeration;import java.util.HashMap;import java.util.Map;import javax.servlet.FilterConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import org.apache.wicket.Application;import org.apache.wicket.Component;import org.apache.wicket.IRequestTarget;import org.apache.wicket.Page;import org.apache.wicket.PageParameters;import org.apache.wicket.Session;import org.apache.wicket.markup.html.pages.ExceptionErrorPage;import org.apache.wicket.request.target.component.BookmarkablePageRequestTarget;import org.apache.wicket.request.target.component.IBookmarkablePageRequestTarget;import org.apache.wicket.request.target.component.IPageRequestTarget;import org.apache.wicket.settings.IRequestCycleSettings;import org.apache.wicket.util.file.WebApplicationPath;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * This class provides a mock implementation of a Wicket HTTP based tester that can be used for * testing. It emulates all of the functionality of an HttpServlet in a controlled, single-threaded * environment. It is supported with mock objects for WebSession, HttpServletRequest, * HttpServletResponse and ServletContext. * <p> * In its most basic usage you can just create a new MockWebApplication and provide your Wicket * Application object. This should be sufficient to allow you to construct components and pages and * so on for testing. To use certain features such as localization you must also call * setupRequestAndResponse(). * <p> * The tester takes an optional path attribute that defines a directory on the disk which will * correspond to the root of the WAR bundle. This can then be used for locating non-tester * resources. * <p> * To actually test the processing of a particular page or component you can also call * processRequestCycle() to do all the normal work of a Wicket request. * <p> * Between calling setupRequestAndResponse() and processRequestCycle() you can get hold of any of * the objects for initialization. The servlet request object has some handy convenience methods for * Initializing the request to invoke certain types of pages and components. * <p> * After completion of processRequestCycle() you will probably just be testing component states. * However, you also have full access to the response document (or binary data) and result codes via * the servlet response object. * <p> * IMPORTANT NOTES * <ul> * <li>This harness is SINGLE THREADED - there is only one global session. For multi-threaded * testing you must do integration testing with a full tester server. * </ul> *  * @author Chris Turner */public class MockWebApplication{	/** Logging */	private static final Logger log = LoggerFactory.getLogger(MockWebApplication.class);	/** The last rendered page. */	private Page lastRenderedPage;	/** The previously rendered page */	private Page previousRenderedPage;	/** Mock http servlet request. */	private final MockHttpServletRequest servletRequest;	/** Mock http servlet response. */	private final MockHttpServletResponse servletResponse;	/** Mock http servlet session. */	private final MockHttpSession servletSession;	/** Request. */	private WebRequest wicketRequest;	/** Parameters to be set on the next request. */	private Map parametersForNextRequest = new HashMap();	/** Response. */	private WebResponse wicketResponse;	/** Session. */	private WebSession wicketSession;	/** The tester object */	private final WebApplication application;	private final ServletContext context;	private final WicketFilter filter;	/**	 * Create the mock http tester that can be used for testing.	 * 	 * @param application	 *            The wicket application object	 * @param path	 *            The absolute path on disk to the web tester contents (e.g. war root) - may be null	 * @see org.apache.wicket.protocol.http.MockServletContext	 */	public MockWebApplication(final WebApplication application, final String path)	{		this.application = application;		context = newServletContext(path);		filter = new WicketFilter()		{			protected IWebApplicationFactory getApplicationFactory()			{				return new IWebApplicationFactory()				{					public WebApplication createApplication(WicketFilter filter)					{						return application;					};				};			}		};		try		{			filter.init(new FilterConfig()			{				public ServletContext getServletContext()				{					return context;				}				public Enumeration getInitParameterNames()				{					return null;				}				public String getInitParameter(String name)				{					if (name.equals(WicketFilter.FILTER_MAPPING_PARAM))					{						return WicketFilter.SERVLET_PATH_HOLDER;						// return "/" + MockWebApplication.this.getName() +						// "/*";					}					return null;				}				public String getFilterName()				{					return "WicketMockServlet";				}			});		}		catch (ServletException e)		{			throw new RuntimeException(e);		}		Application.set(this.application);		// Construct mock session, request and response		servletSession = new MockHttpSession(context);		servletRequest = new MockHttpServletRequest(this.application, servletSession, context);		servletResponse = new MockHttpServletResponse(servletRequest);		// Construct request and response using factories		wicketRequest = this.application.newWebRequest(servletRequest);		wicketResponse = this.application.newWebResponse(servletResponse);		// Create request cycle		createRequestCycle();		this.application.getRequestCycleSettings().setRenderStrategy(			IRequestCycleSettings.ONE_PASS_RENDER);		// Don't buffer the response, as this can break ajax tests: see WICKET-1264		this.application.getRequestCycleSettings().setBufferResponse(false);		this.application.getResourceSettings().setResourceFinder(new WebApplicationPath(context));		this.application.getPageSettings().setAutomaticMultiWindowSupport(false);		// Since the purpose of MockWebApplication is singlethreaded		// programmatic testing it doesn't make much sense to have a		// modification watcher thread started to watch for changes in the		// markup.		// Disabling this also helps test suites with many test cases		// (problems has been noticed with >~300 test cases). The problem		// is that even if the wicket tester is GC'ed the modification		// watcher still runs, taking up file handles and memory, leading		// to "Too many files opened" or a regular OutOfMemoryException		this.application.getResourceSettings().setResourcePollFrequency(null);	}	/**	 * Used to create a new mock servlet context.	 * 	 * @param path	 *            The absolute path on disk to the web tester contents (e.g. war root) - may be null	 * @return ServletContext	 */	public ServletContext newServletContext(final String path)	{		return new MockServletContext(application, path);	}	/**	 * Gets the application object.	 * 	 * @return Wicket application	 */	public final WebApplication getApplication()	{		return application;	}	/**	 * Get the page that was just rendered by the last request cycle processing.	 * 	 * @return The last rendered page	 */	public Page getLastRenderedPage()	{		return lastRenderedPage;	}	/**	 * Get the page that was previously	 * 	 * @return The last rendered page	 */	public Page getPreviousRenderedPage()	{		return previousRenderedPage;	}	/**	 * Get the request object so that we can apply configurations to it.	 * 	 * @return The request object	 */	public MockHttpServletRequest getServletRequest()	{		return servletRequest;	}	/**	 * Get the response object so that we can apply configurations to it.	 * 	 * @return The response object	 */	public MockHttpServletResponse getServletResponse()	{		return servletResponse;	}	/**	 * Get the session object so that we can apply configurations to it.	 * 	 * @return The session object	 */	public MockHttpSession getServletSession()	{		return servletSession;	}	/**	 * Get the wicket request object.	 * 	 * @return The wicket request object	 */	public WebRequest getWicketRequest()	{		return wicketRequest;	}	/**

⌨️ 快捷键说明

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