码迷,mamicode.com
首页 > 编程语言 > 详细

嵌入式jetty启动spring(java配置方式),junit测试用

时间:2014-12-07 12:38:20      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:jetty   嵌入式jetty   

package com.doctor.embeddedjetty;

import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;


public class EmbeddedJettyServer {
	private int port ;
	private Class<?> springConfiguration = null;
	
	private Server server;
	
	public EmbeddedJettyServer(Class<?> springConfiguration){
		this(8080, springConfiguration);
	}
	public EmbeddedJettyServer(int port,Class<?> springConfiguration){
		this.port = port;
		this.springConfiguration = springConfiguration;
		init();
	}
	
	public void init(){
		server = new Server(port);
		ServletContextHandler context = new ServletContextHandler();
		context.setContextPath("/");
		server.setHandler(context);
		
		AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
		webApplicationContext.register(springConfiguration);
		DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
		context.addServlet(new ServletHolder(dispatcherServlet), "/*");

	}
	public void start() throws Exception{
		if (server!= null) {
			if (server.isStarting() || server.isStarted() || server.isRunning() ) {
				return;
			}
		}
		TimeUnit.SECONDS.sleep(3);
		server.start();
	}
	
	public void stop() throws Exception{
		if (server != null) {
			if (server.isRunning()) {
				server.stop();
			}
		}
	}
	
	public void join() throws InterruptedException{
		if (server!=null) {
			server.join();
		}
	}
}
测试代码:
package com.doctor.embeddedjetty;


import static org.junit.Assert.*;
import static org.hamcrest.core.IsEqual.*;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.junit.Test;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;




public class EmbeddedJettyForSpringMvcTest {
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>@Test
<span style="white-space:pre">	</span>public void test() throws Throwable{
<span style="white-space:pre">		</span>EmbeddedJettyServer jettyServer = new EmbeddedJettyServer(SpringConfiguration.class);
<span style="white-space:pre">		</span>jettyServer.start();
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>Response response = Request.Get("http://localhost:8080/").execute();
<span style="white-space:pre">		</span>assertThat(response.returnContent().asString(), equalTo("hello"));
<span style="white-space:pre">		</span>jettyServer.stop();
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>@Configuration
<span style="white-space:pre">	</span>@ComponentScan("com.doctor.embeddedjetty")
<span style="white-space:pre">	</span>static class SpringConfiguration{
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
}

嵌入式jetty启动spring(java配置方式),junit测试用

标签:jetty   嵌入式jetty   

原文地址:http://blog.csdn.net/doctor_who2004/article/details/41786115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!