stub粗粒度测试(六)

stub粗粒度测试

一、实验目的

二、实验环境(本实验的硬件和软件环境及使用仪器等)

三、实验实现过程

四、实验结果分析与总结


一、实验目的

1.掌握stub工作模式

2.掌握使用嵌入式服务器代替真正的网络服务器

3.掌握使用stub单元测试

二、实验环境(本实验的硬件和软件环境及使用仪器等)

硬件:PC电脑一台;

配置: window系统,内存大于4G  硬盘250G及以上

软件:eclipse、 jdk15

三、实验实现过程

实验内容:

任务1、Jetty的工作模式

右键构建路径添加库

选择用户库

再点击用户库

新建库

创建好库后点击此处,随后导入准备好的jar包即可

创建文件

创建html文件

编写

package lab7;

import org.eclipse.jetty.server.Server;

import org.eclipse.jetty.server.handler.ContextHandler;

import org.eclipse.jetty.server.handler.ResourceHandler;

public class JettySample {

    public static void main(String[] args) throws Exception {

       Server server = new Server(8081);

       ContextHandler context = new ContextHandler();

       context.setContextPath("/");

       context.setResourceBase("./HelloWorld.html");

       server.setHandler(context);

       context.setHandler(new ResourceHandler());

       server.start();

       server.join();

    }

}

编写HelloWorld.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

helloworld!

</body>

</html>

在浏览器中输入地址

观察结果

(未能访问成功)

任务2、stub粗粒度测试

创建类

编写

package lab7;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

public class WebClient{

public String getContent( URL url )

{ StringBuffer content = new StringBuffer();

try

{

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setDoInput(true);

InputStream is = connection.getInputStream();

int count;

while ( -1 != ( count = is.read() ) )

{content.append( new String( Character.toChars( count ) ) );}

}

catch ( IOException e )

{ return null;}

return content.toString();

}

}

创建测试用例

编写测试用例

package lab7;

import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;

import java.net.URL;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Server;

import org.eclipse.jetty.server.handler.AbstractHandler;

import org.eclipse.jetty.server.handler.ContextHandler;

import org.junit.AfterClass;

import org.junit.BeforeClass;

import org.junit.jupiter.api.Test;

import org.eclipse.jetty.server.Request;

class WebClientTest {

@BeforeClass

public static void setUp() throws Exception {

Server server = new Server(8082);

WebClientTest t = new WebClientTest();

ContextHandler contentOkContext = new ContextHandler();

server.setHandler(contentOkContext);

contentOkContext.setContextPath("/testGetContentOk");

contentOkContext.setHandler(t.new TestGetContentOkHandler());

server.setStopAtShutdown(true);

server.start();

}

@Test

public void testGetContentOk() throws Exception {

WebClient client = new WebClient();

String result = client.getContent(new

URL("http://localhost:8082/testGetContentOk"));

assertEquals("It works", result);

}

@AfterClass

public static void tearDown() throws Exception {

//server.stop();

}

private class TestGetContentOkHandler extends AbstractHandler {

@Override

public void handle(String target, Request baseRequest,

HttpServletRequest request, HttpServletResponse response)

throws IOException,ServletException {

response.setContentType("text/html;charset=utf-8");

response.setStatus(HttpServletResponse.SC_OK);

baseRequest.setHandled(true);

response.getWriter().write("It works");

response.getWriter().flush();

}

}

}

查看测试结果

(没能运行成功)

任务3、stub替换连接

创建测试用例

编写测试用例

package lab7;

import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;

import java.net.URL;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.server.Request;

import org.eclipse.jetty.server.Server;

import org.eclipse.jetty.server.handler.AbstractHandler;

import org.eclipse.jetty.server.handler.ContextHandler;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.Test;

import static org.eclipse.jetty.server.Response.*;

public class TestWebClient2 {

private WebClient client;

Server server;

@Before

public void init() throws Exception {

client = new WebClient();

server = new Server(8084);

TestWebClient2 t = new TestWebClient2();

// content正常的情说

ContextHandler contentOkContext = new ContextHandler();

contentOkContext.setContextPath("/testGetContentOk");

server.setHandler(contentOkContext);

contentOkContext.setHandler(t.new TestGetContentOkHandler());

//ContentError的情况

ContextHandler contentErrorContext = new ContextHandler();

contentErrorContext.setContextPath("/testGetContentError");

contentErrorContext.setHandler(t.new TestGetContentServerErrorHandler());

//ContentNotFound的情况

ContextHandler contentNotFoundContext = new ContextHandler();

contentNotFoundContext.setContextPath("/testGetContentNotFound");

contentNotFoundContext.setHandler(t.new TestGetContentNotFoundHandler());

server.setStopAtShutdown(true);

server.start();

}

@After

public void tearDown1() throws Exception {

server.stop();

}

@Test

public void testGetContentOk() throws Exception {

String result = client.getContent(new URL("http://localhost:8084/testGetContentOk"));

assertEquals("It works", result);

}

@Test

public void testGetContentError() throws Exception {

String result = client.getContent(new URL("http://localhost:8084/testGetContentError"));

assertNull(result);

}

@Test

public void testGetContentNotFound() throws Exception {

String result = client.getContent(new URL("http://localhost:8084/testGetContentNotFound"));

assertNull(result);

}

@AfterClass

public static void tearDown() throws Exception {

//server.stop();

}

private class TestGetContentOkHandler extends AbstractHandler {

@Override

public void handle(String target, Request baseRequest, HttpServletRequest request,HttpServletResponse response)

throws IOException, ServletException {

response.setContentType("text/html;charst=utf-8");

response.setStatus(HttpServletResponse.SC_OK);

baseRequest.setHandled(true);

response.getWriter().write("It works");

response.getWriter().flush();

}

}

//ContentNotFoundContentError相对应的Handler

// Handler to handle bad requests to the server

private class TestGetContentServerErrorHandler extends AbstractHandler {

public void handle(String target, Request baseRequest, HttpServletRequest request,HttpServletResponse response)

throws IOException, ServletException {

response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);

}

}

// Handler to handle requests that request unavailable content.

private class TestGetContentNotFoundHandler extends AbstractHandler {

public void handle(String target, Request baseRequest, HttpServletRequest request,HttpServletResponse response)

throws IOException, ServletException {

response.sendError(HttpServletResponse.SC_NOT_FOUND);

}

}

}

编写测试用例

package lab7;

import static org.junit.jupiter.api. Assertions.assertEquals;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.ProtocolException;

import java.net.URL;

import java.net.URLConnection;

import java.net.URLStreamHandler;

import java.net.URLStreamHandlerFactory;

import org.junit.BeforeClass;

import org.junit.Test;

public class TestWebClient3 {

@BeforeClass

public static void setUp() {

TestWebClient3 t = new TestWebClient3();

URL.setURLStreamHandlerFactory(t.new StubStreamHandlerFactory());

}

@Test

public void testGetContentOk()throws Exception {

WebClient client = new WebClient ();

String result = client.getContent (new URL("http://1ocalhost/sdfsdfsdfsdfsdfsdfsdfsdf"));

assertEquals("It works", result);

}

private class StubStreamHandlerFactory implements URLStreamHandlerFactory {

public URLStreamHandler createURLStreamHandler(String protocol) {

return new StubHttpURLStreamHandler();

}

}

private class StubHttpURLStreamHandler extends URLStreamHandler {

protected URLConnection openConnection(URL url) throws IOException {

return new StubHttpURLConnection(url);

}

}

private class StubHttpURLConnection extends HttpURLConnection {

private boolean isInput = true;

protected StubHttpURLConnection(URL url) {

super (url);

}

public InputStream getInputStream() throws IOException {

if (!isInput){

throw new ProtocolException(

"Cannot read from URLConnection"+

"if doInput=false (call setDoInput(true))");

}

ByteArrayInputStream bais = new ByteArrayInputStream(new String("It works").getBytes());

return bais;

}

public void disconnect() {

}

public void connect() throws IOException {

}

public boolean usingProxy(){

return false;

}

}

}

分别查看测试结果

四、实验结果分析与总结

在粗粒度实验的过程中不是很顺利,检查了代码无误并且目录也没有错,猜测可能是创建的文件有问题,但也是按步骤创建的,不过跟参考文件的小图标有区别,我的;参考的,找了许久没有找到解决办法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值