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

📄 servlettestbase.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
字号:
package org.rapla;

import java.io.File;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;

import junit.framework.TestCase;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.rapla.components.util.IOUtil;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaLocale;

public abstract class ServletTestBase extends TestCase
{
    MainServlet mainServlet = new MainServlet();
    Server jettyServer;
    final public static String WEBAPP_FOLDER_NAME = RaplaTestCase.TEST_FOLDER_NAME  + "/webapp";
    final public static String WEBAPP_INF_FOLDER_NAME = WEBAPP_FOLDER_NAME + "/WEB-INF";
  
    public ServletTestBase( String name )
    {
        super( name );
        new File("temp").mkdir();
        File testFolder =new File(RaplaTestCase.TEST_FOLDER_NAME);
        testFolder.mkdir();
    }

    protected void setUp() throws Exception
    {
        super.setUp();
        mainServlet.setServerId( getStorageName() );
        new File(WEBAPP_FOLDER_NAME).mkdir();
        new File(WEBAPP_INF_FOLDER_NAME).mkdir();
        
        IOUtil.copy( "test-src/test.xconf", WEBAPP_INF_FOLDER_NAME + "/raplaserver.xconf" );
        IOUtil.copy( "test-src/test.xlog", WEBAPP_INF_FOLDER_NAME + "/raplaserver.xlog" );
        IOUtil.copy( "test-src/testdefault.xml", WEBAPP_INF_FOLDER_NAME + "/test.xml" );
        IOUtil.copy( "webapp/WEB-INF/web.xml", WEBAPP_INF_FOLDER_NAME + "/web.xml" );
        
        jettyServer =new Server(8051);
        jettyServer.setAttribute( "org.mortbay.jetty.Request.maxFormContentSize",new Integer(64000000) );
        Context context = new Context( jettyServer,"/",Context.SESSIONS );
        context.setResourceBase( WEBAPP_FOLDER_NAME );
        context.addServlet( new ServletHolder(mainServlet), "/*" );
        jettyServer.start();
        
        URL server = new URL("http://127.0.0.1:8051/rapla/ping");
        HttpURLConnection connection = (HttpURLConnection)server.openConnection();
        int timeout = 10000;
        int interval = 200;
        for ( int i=0;i<timeout / interval;i++)
        {
            try
            {
                connection.connect();
            } 
            catch (ConnectException ex) {
                Thread.sleep(interval);
            }
        }
    }
    
    protected RaplaContext getContext()
    {
        return mainServlet.getContext();
    }
    
    protected RaplaLocale getRaplaLocale() throws Exception {
        return (RaplaLocale) getContext().lookup(RaplaLocale.ROLE);
    }

    
    protected void tearDown() throws Exception
    {
        jettyServer.stop();
        super.tearDown();
    }
    
    protected String getStorageName() {
        return "storage-file";
    }
 }

⌨️ 快捷键说明

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