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

📄 webxmltest.java

📁 这是远程web服务调用的一个包,可以将JSP直接作为服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.meterware.servletunit;/********************************************************************************************************************* $Id: WebXMLTest.java,v 1.25 2006/03/24 19:59:12 russgold Exp $** Copyright (c) 2001-2004,2006, Russell Gold** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated* documentation files (the "Software"), to deal in the Software without restriction, including without limitation* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and* to permit persons to whom the Software is furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in all copies or substantial portions* of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER* DEALINGS IN THE SOFTWARE.********************************************************************************************************************/import com.meterware.httpunit.*;import java.io.*;import java.net.URL;import java.util.Properties;import java.util.List;import java.util.Arrays;import javax.servlet.*;import javax.servlet.http.*;import org.w3c.dom.Document;import org.xml.sax.SAXException;import junit.framework.TestSuite;import junit.framework.TestCase;public class WebXMLTest extends TestCase {    public static void main(String args[]) {        junit.textui.TestRunner.run( suite() );    }    public static TestSuite suite() {        return new TestSuite( WebXMLTest.class );    }    public WebXMLTest( String name ) {        super( name );    }    public void testBasicAccess() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.addServlet( "/SimpleServlet", SimpleGetServlet.class );        File webXml = createWebXml( wxs );        ServletRunner sr = new ServletRunner( webXml );        WebRequest request   = new GetMethodWebRequest( "http://localhost/SimpleServlet" );        WebResponse response = sr.getResponse( request );        assertNotNull( "No response received", response );        assertEquals( "content type", "text/html", response.getContentType() );        assertEquals( "requested resource", SimpleGetServlet.RESPONSE_TEXT, response.getText() );    }    public void testRealPath() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.addServlet( "/SimpleServlet", SimpleGetServlet.class );        File webXml = createWebXml( new File( "build/base" ), wxs );        assertRealPath( "path with no context", new ServletRunner( webXml ), new File( "something.txt" ), "/something.txt" );        assertRealPath( "path with context", new ServletRunner( webXml, "/testing" ), new File( "build/base/something.txt" ), "/something.txt" );        assertRealPath( "path with no context, no slash", new ServletRunner( webXml ), new File( "something.txt" ), "something.txt" );        assertRealPath( "path with context, no slash", new ServletRunner( webXml, "/testing" ), new File( "build/base/something.txt" ), "something.txt" );    }    private void assertRealPath( String comment, ServletRunner sr, File expectedFile, String relativePath ) {        String realPath = sr.getSession(true).getServletContext().getRealPath( relativePath );        assertEquals( comment, expectedFile.getAbsolutePath(), realPath );    }    private File createWebXml( WebXMLString wxs ) throws IOException {        return createWebXml( new File("build"), wxs );    }    private File createWebXml( File parent, WebXMLString wxs ) throws IOException {        File dir = new File( parent, "META-INF" );        dir.mkdirs();        File webXml = new File( dir, "web.xml" );        FileOutputStream fos = new FileOutputStream( webXml );        fos.write( wxs.asText().getBytes() );        fos.close();        return webXml;    }    public void testBasicAuthenticationConfig() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.requireBasicAuthentication( "SampleRealm" );        WebApplication app = new WebApplication( newDocument( wxs.asText() ) );        assertTrue( "Did not detect basic authentication", app.usesBasicAuthentication() );        assertEquals( "Realm name", "SampleRealm", app.getAuthenticationRealm() );    }    public void testFormAuthenticationConfig() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.requireFormAuthentication( "SampleRealm", "/Login", "/Error" );        WebApplication app = new WebApplication( newDocument( wxs.asText() ) );        assertTrue( "Did not detect form-based authentication", app.usesFormAuthentication() );        assertEquals( "Realm name", "SampleRealm", app.getAuthenticationRealm() );        assertEquals( "Login path", "/Login", app.getLoginURL().getFile() );        assertEquals( "Error path", "/Error", app.getErrorURL().getFile() );    }    public void testSecurityConstraint() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.addSecureURL( "SecureArea1", "/SimpleServlet" );        wxs.addAuthorizedRole( "SecureArea1", "supervisor" );        WebApplication app = new WebApplication( newDocument( wxs.asText() ) );        assertTrue( "Did not require authorization", app.requiresAuthorization( new URL( "http://localhost/SimpleServlet" ) ) );        assertTrue( "Should not require authorization", !app.requiresAuthorization( new URL( "http://localhost/FreeServlet" ) ) );        List roles = Arrays.asList( app.getPermittedRoles( new URL( "http://localhost/SimpleServlet" ) ) );        assertTrue( "Should have access", roles.contains( "supervisor" ) );        assertTrue( "Should not have access", !roles.contains( "peon" ) );    }    /**     * Verifies that the default display name is null.     */    public void testDefaultContextNameConfiguration() throws Exception {        WebXMLString wxs = new WebXMLString();        WebApplication app = new WebApplication( newDocument( wxs.asText() ) );        assertNull( "Context name should default to null", app.getDisplayName() );    }    /**     * Verifies that a web application can read its display name from the configuration.     * @throws Exception     */    public void testContextNameConfiguration() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.setDisplayName( "samples" );        wxs.addServlet( "simple", "/SimpleServlet", SimpleGetServlet.class );        WebApplication app = new WebApplication( newDocument( wxs.asText() ) );        assertEquals( "Display name", "samples", app.getDisplayName() );        ServletRunner sr = new ServletRunner( wxs.asInputStream() );        ServletUnitClient client = sr.newClient();        InvocationContext ic = client.newInvocation( "http://localhost/SimpleServlet" );        ServletContext servletContext = ic.getServlet().getServletConfig().getServletContext();        assertEquals( "Context name", "samples", servletContext.getServletContextName() );    }    public void testServletParameters() throws Exception {        WebXMLString wxs = new WebXMLString();        Properties params = new Properties();        params.setProperty( "color", "red" );        params.setProperty( "age", "12" );        wxs.addServlet( "simple", "/SimpleServlet", SimpleGetServlet.class, params );        ServletRunner sr = new ServletRunner( wxs.asInputStream() );        ServletUnitClient client = sr.newClient();        InvocationContext ic = client.newInvocation( "http://localhost/SimpleServlet" );        ServletConfig servletConfig = ic.getServlet().getServletConfig();        assertEquals( "Servlet name", "simple", servletConfig.getServletName() );        assertNull( "init parameter 'gender' should be null", servletConfig.getInitParameter( "gender" ) );        assertEquals( "init parameter via config", "red", ic.getServlet().getServletConfig().getInitParameter( "color" ) );        assertEquals( "init parameter directly", "12", ((HttpServlet) ic.getServlet()).getInitParameter( "age" ) );    }    public void testContextParameters() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.addServlet( "/SimpleServlet", SimpleGetServlet.class );        wxs.addContextParam( "icecream", "vanilla" );        wxs.addContextParam( "cone", "waffle" );        wxs.addContextParam( "topping", "" );        ServletRunner sr = new ServletRunner( wxs.asInputStream() );        ServletUnitClient client = sr.newClient();        assertEquals( "Context parameter 'icecream'", "vanilla", sr.getContextParameter( "icecream" ) );        InvocationContext ic = client.newInvocation( "http://localhost/SimpleServlet" );        javax.servlet.ServletContext sc = ((HttpServlet) ic.getServlet()).getServletContext();        assertNotNull( "ServletContext should not be null", sc );        assertEquals( "ServletContext.getInitParameter()", "vanilla", sc.getInitParameter( "icecream" ) );        assertEquals( "init parameter: cone", "waffle", sc.getInitParameter( "cone" ) );        assertEquals( "init parameter: topping", "", sc.getInitParameter( "topping" ) );        assertNull( "ServletContext.getInitParameter() should be null", sc.getInitParameter( "shoesize" ) );    }    private Document newDocument( String contents ) throws UnsupportedEncodingException, SAXException, IOException {       return HttpUnitUtils.newParser().parse( toInputStream( contents ) );    }    private ByteArrayInputStream toInputStream( String contents ) throws UnsupportedEncodingException {        return new ByteArrayInputStream( contents.getBytes( "UTF-8" ) );    }    public void testBasicAuthorization() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.addServlet( "/SimpleServlet", SimpleGetServlet.class );        wxs.requireBasicAuthentication( "Sample Realm" );        wxs.addSecureURL( "SecureArea1", "/SimpleServlet" );        wxs.addAuthorizedRole( "SecureArea1", "supervisor" );        ServletRunner sr = new ServletRunner( wxs.asInputStream() );        ServletUnitClient  wc = sr.newClient();        try {            wc.getResponse( "http://localhost/SimpleServlet" );            fail( "Did not insist on validation for access to servlet" );        } catch (AuthorizationRequiredException e) {            assertEquals( "Realm", "Sample Realm", e.getAuthenticationParameter( "realm" ) );

⌨️ 快捷键说明

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