Request:java
public interface Request { String getName(); }
Response:ide
public interface Response { String getName(); }
RequestHandler:測試
public interface RequestHandler { Response process(Request request) throws Exception; }
Controller:this
public interface Controller { Response processRequest(Request request); void addHandler(Request request, RequestHandler requestHandler); }
DefaultController:spa
import java.util.HashMap; import java.util.Map; public class DefaultController implements Controller{ /* 若是這個地方指明瞭RequestHandler的,可能會遇到一些泛型方面的問題。 指明瞭RequestHandler,我可以將RequestHandler的子類放進這個map 中麼? 這個地方我忘了,估計那部分還須要再閱讀一遍 */ private Map<String,RequestHandler> requestHandlers = new HashMap<String, RequestHandler>(); protected RequestHandler getHander(Request request) { if (!this.requestHandlers.containsKey(request.getName())) { String msg = "Can't find handler for request name."; throw new RuntimeException(msg); } return requestHandlers.get(request.getName()); } public Response processRequest(Request request) { Response response; try{ response = getHander(request).process(request); } catch (Exception e) { response = new ErrorResponse(request, e); } return response; } public void addHandler(Request request, RequestHandler requestHandler) { if (this.requestHandlers.containsKey(request.getName())) { String msg = "A request handler handler has "+ "already been registered for request "+ "["+request.getName()+"]."; throw new RuntimeException(msg); }else { requestHandlers.put(request.getName(), requestHandler); } } }
ErrorResponse:設計
public class ErrorResponse implements Response { private String name = "ErrorResponse"; private Request originalRequest; private Exception originalException; public ErrorResponse(Request originalRequest, Exception originalException) { this.originalRequest = originalRequest; this.originalException = originalException; } public Request getOriginalRequest() { return originalRequest; } public Exception getOriginalException() { return originalException; } public String getName() { return name; } }
DefaultControllerTest:code
import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import static org.junit.Assert.*; public class DefaultControllerTest { private DefaultController controller; private Request request; private RequestHandler handler; @Before public void instantiate() throws Exception{ controller = new DefaultController(); request = new SimpleRequest(); handler = new SimpleRequestHandler(); controller.addHandler(request, handler); } @Test public void testAddHandler(){ RequestHandler handler2 = controller.getHander(request); assertSame("Should be same.",handler,handler2); } @Test public void testProcessRequest() { Response response = controller.processRequest(request); assertNotNull("Should not be null",response); assertEquals("shoue be same", new SimpleResponse(), response); } /* 書上這部分有點亂,我只能本身來設計各個測試了,前面已經完成了 診斷測試,如今主要完成各個異常測試: 1.測試addHandler方法拋出的異常 2.測試getHandler方法拋出的異常 3.測試process方法拋出的異常,因爲process方法的異常 被DefaultController的processRequest方法給 捕獲了,因此須要測試processRequest返回的錯誤 消息。 */ @Test public void testProcessAnswerErrorResponse(){ SimpleRequest request = new SimpleRequest("testError"); SimpleExceptionHandler handler = new SimpleExceptionHandler(); controller.addHandler(request,handler); Response response = controller.processRequest(request); assertNotNull("Should not be noll", response); assertEquals("Should be the same.", ErrorResponse.class,response.getClass()); } @Test(expected = RuntimeException.class) public void testGetHandlerNotDefined() { SimpleRequest request = new SimpleRequest("testNotDefined"); controller.getHander(request); } @Test(expected = RuntimeException.class) public void testAddHandlerDuplicateName() { SimpleRequest request = new SimpleRequest(); SimpleRequestHandler handler = new SimpleRequestHandler(); controller.addHandler(request,handler); } /* 測試超時 */ @Test(timeout = 130) @Ignore(value = "no need not.") public void testProcessMultipleRequestsTimeout() { Request request; Response simpleResponse = new SimpleResponse(); SimpleRequestHandler handler = new SimpleRequestHandler(); for (int i = 0; i < 99999; i++) { request = new SimpleRequest(); controller.addHandler(request, handler); simpleResponse = controller.processRequest(request); assertNotNull(simpleResponse); assertNotSame(ErrorResponse.class,simpleResponse.getClass()); } } private class SimpleRequest implements Request { private static final String NAME = "Test"; private String name; public SimpleRequest(String name) { this.name = name; } public SimpleRequest() { this(NAME); } public String getName() { return name; } } private class SimpleResponse implements Response { private static final String NAME = "Test"; public String getName() { return NAME; } @Override public boolean equals(Object obj) { boolean result = false; if (obj instanceof SimpleResponse) { result = ((SimpleResponse) obj).getName().equals(getName()); } return result; } @Override public int hashCode() { return NAME.hashCode(); } } private class SimpleRequestHandler implements RequestHandler { public Response process(Request request) throws Exception { return new SimpleResponse(); } } private class SimpleExceptionHandler implements RequestHandler { public Response process(Request request) throws Exception { String msg = "error processing request"; throw new Exception(msg); } } }
《Junit實戰》筆記blog