jetty learn

本文源自:https://github.com/springside/springside4/wiki/Jetty

jetty 是servlet容器,是一个嵌入式的servlet容器:

1,可以在eclipse中,通过java代码,直接启动此容器,例如:QuickstartServer.java、ShowcaseService.java

package org.springside.examples.quickstart.functional;
 
import org.eclipse.jetty.server.Server;
import org.springside.modules.test.jetty.JettyFactory;
 
/**
 * 使用Jetty运行调试Web应用, 在Console输入回车快速重新加载应用.
 * 
 * @author calvin
 */
public class QuickStartServer {
 
    public static final int PORT = 8080;
    public static final String CONTEXT = "/quickstart";
    public static final String BASE_URL = "http://localhost:8080/quickstart";
    public static final String[] TLD_JAR_NAMES = new String[] { "sitemesh", "spring-webmvc", "shiro-web",
            "springside-core" };
 
    public static void main(String[] args) throws Exception {
        // 设定Spring的profile
        System.setProperty("spring.profiles.active", "development");
 
        // 启动Jetty
        Server server = JettyFactory.createServerInSource(PORT, CONTEXT);
        JettyFactory.setTldJarNames(server, TLD_JAR_NAMES);
 
        try {
            server.start();
 
            System.out.println("Server running at " + BASE_URL);
            System.out.println("Hit Enter to reload the application");
 
            // 等待用户输入回车重载应用.
            while (true) {
                char c = (char) System.in.read();
                if (c == '\n') {
                    JettyFactory.reloadContext(server);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
}

2,直接在用例中 启动Jetty,而不是先将应用打包部署到容器。在各个项目的BaseFunctionalTest中有演示。

package org.springside.examples.quickstart.functional;
 
import java.net.URL;
import java.sql.Driver;
 
import org.eclipse.jetty.server.Server;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springside.modules.test.data.DataFixtures;
import org.springside.modules.test.jetty.JettyFactory;
import org.springside.modules.test.selenium.Selenium2;
import org.springside.modules.utils.PropertiesLoader;
 
/**
 * 功能测试基类.
 * 
 * 在整个测试期间启动一次Jetty Server和 Selenium,在JVM退出时关闭两者。 
 * 在每个TestCase Class执行前重新载入默认数据.
 *  
 * @author calvin
 */
public class BaseFunctionalTestCase {
 
    protected static String baseUrl;
 
    protected static Server jettyServer;
 
    protected static SimpleDriverDataSource dataSource;
 
    protected static Selenium2 s;
 
    protected static PropertiesLoader propertiesLoader = new PropertiesLoader("classpath:/application.properties",
            "classpath:/application.functional.properties", "classpath:/application.functional-local.properties");
 
    private static Logger logger = LoggerFactory.getLogger(BaseFunctionalTestCase.class);
 
    @BeforeClass
    public static void beforeClass() throws Exception {
 
        baseUrl = propertiesLoader.getProperty("baseUrl", QuickStartServer.BASE_URL);
 
        //如果是目标地址是localhost,则启动嵌入式jetty。如果指向远程地址,则不需要启动Jetty.
        Boolean isEmbedded = new URL(baseUrl).getHost().equals("localhost");
 
        if (isEmbedded) {
            startJettyOnce();
        }
 
        buildDataSourceOnce();
        reloadSampleData();
    }
 
    /**
     * 启动Jetty服务器, 仅启动一次.
     */
    protected static void startJettyOnce() throws Exception {
        if (jettyServer == null) {
            //设定Spring的profile
            System.setProperty("spring.profiles.active", "functional");
 
            jettyServer = JettyFactory.createServerInSource(new URL(baseUrl).getPort(), QuickStartServer.CONTEXT);
            JettyFactory.setTldJarNames(jettyServer, QuickStartServer.TLD_JAR_NAMES);
            jettyServer.start();
 
            logger.info("Jetty Server started");
        }
    }
 
    /**
     * 构造数据源,仅构造一次.
     * 连接参数从配置文件中读取,可指向本地的开发环境,也可以指向远程的测试服务器。
     */
    protected static void buildDataSourceOnce() throws ClassNotFoundException {
        if (dataSource == null) {
            dataSource = new SimpleDriverDataSource();
            dataSource.setDriverClass((Class) Class.forName(propertiesLoader
                    .getProperty("jdbc.driver")));
            dataSource.setUrl(propertiesLoader.getProperty("jdbc.url"));
            dataSource.setUsername(propertiesLoader.getProperty("jdbc.username"));
            dataSource.setPassword(propertiesLoader.getProperty("jdbc.password"));
        }
    }
 
    /**
     * 载入测试数据.
     */
    protected static void reloadSampleData() throws Exception {
        DataFixtures.executeScript(dataSource, "classpath:data/cleanup-data.sql", "classpath:data/import-data.sql");
    }
}

3,其他方面

转载于:https://www.cnblogs.com/yougyoum/archive/2012/10/08/2715844.html

你可能感兴趣的:(python,测试,开发工具)