diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /jdisc_core/src/test/java/com/yahoo/jdisc/test/TestDriverTestCase.java |
Publish
Diffstat (limited to 'jdisc_core/src/test/java/com/yahoo/jdisc/test/TestDriverTestCase.java')
-rw-r--r-- | jdisc_core/src/test/java/com/yahoo/jdisc/test/TestDriverTestCase.java | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/test/TestDriverTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/test/TestDriverTestCase.java new file mode 100644 index 00000000000..bea231d19d9 --- /dev/null +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/test/TestDriverTestCase.java @@ -0,0 +1,163 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.jdisc.test; + +import com.yahoo.jdisc.Request; +import com.yahoo.jdisc.Response; +import com.yahoo.jdisc.application.Application; +import com.yahoo.jdisc.application.ContainerBuilder; +import com.yahoo.jdisc.handler.AbstractRequestHandler; +import com.yahoo.jdisc.handler.CompletionHandler; +import com.yahoo.jdisc.handler.ContentChannel; +import com.yahoo.jdisc.handler.RequestDeniedException; +import com.yahoo.jdisc.handler.ResponseHandler; +import com.yahoo.jdisc.service.ContainerNotReadyException; +import org.junit.Test; + +import java.nio.ByteBuffer; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class TestDriverTestCase { + + @Test + public void requireThatFactoryMethodsWork() { + TestDriver.newInjectedApplicationInstance(MyApplication.class).close(); + TestDriver.newInjectedApplicationInstanceWithoutOsgi(MyApplication.class).close(); + TestDriver.newInjectedApplicationInstance(new MyApplication()).close(); + TestDriver.newInjectedApplicationInstanceWithoutOsgi(new MyApplication()).close(); + TestDriver.newSimpleApplicationInstance().close(); + TestDriver.newSimpleApplicationInstanceWithoutOsgi().close(); + } + + @Test + public void requireThatAccessorsWork() { + TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(); + assertNotNull(driver.bootstrapLoader()); + assertTrue(driver.close()); + } + + @Test + public void requireThatConnectRequestWorks() { + TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(); + MyRequestHandler requestHandler = new MyRequestHandler(new MyContentChannel()); + ContainerBuilder builder = driver.newContainerBuilder(); + builder.serverBindings().bind("scheme://host/path", requestHandler); + driver.activateContainer(builder); + ContentChannel content = driver.connectRequest("scheme://host/path", new MyResponseHandler()); + assertNotNull(content); + content.close(null); + assertNotNull(requestHandler.handler); + requestHandler.handler.handleResponse(new Response(Response.Status.OK)).close(null); + assertTrue(driver.close()); + } + + @Test + public void requireThatDispatchRequestWorks() { + TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(); + MyRequestHandler requestHandler = new MyRequestHandler(new MyContentChannel()); + ContainerBuilder builder = driver.newContainerBuilder(); + builder.serverBindings().bind("scheme://host/path", requestHandler); + driver.activateContainer(builder); + driver.dispatchRequest("scheme://host/path", new MyResponseHandler()); + assertNotNull(requestHandler.handler); + assertTrue(requestHandler.content.closed); + requestHandler.handler.handleResponse(new Response(Response.Status.OK)).close(null); + assertTrue(driver.close()); + } + + @Test + public void requireThatFailedRequestCreateDoesNotBlockClose() { + TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(); + try { + driver.connectRequest("scheme://host/path", new MyResponseHandler()); + fail(); + } catch (ContainerNotReadyException e) { + + } + assertTrue(driver.close()); + } + + @Test + public void requireThatFailedRequestConnectDoesNotBlockClose() { + TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(); + ContainerBuilder builder = driver.newContainerBuilder(); + builder.serverBindings().bind("scheme://host/path", new MyRequestHandler(null)); + driver.activateContainer(builder); + try { + driver.connectRequest("scheme://host/path", new MyResponseHandler()); + fail(); + } catch (RequestDeniedException e) { + + } + assertTrue(driver.close()); + } + + private static class MyApplication implements Application { + + @Override + public void start() { + + } + + @Override + public void stop() { + + } + + @Override + public void destroy() { + + } + } + + private static class MyRequestHandler extends AbstractRequestHandler { + + final MyContentChannel content; + ResponseHandler handler; + + MyRequestHandler(MyContentChannel content) { + this.content = content; + } + + @Override + public ContentChannel handleRequest(Request request, ResponseHandler handler) { + this.handler = handler; + if (content == null) { + throw new RequestDeniedException(request); + } + return content; + } + } + + private static class MyResponseHandler implements ResponseHandler { + + final MyContentChannel content = new MyContentChannel(); + + @Override + public ContentChannel handleResponse(Response response) { + return content; + } + } + + private static class MyContentChannel implements ContentChannel { + + boolean closed = false; + + @Override + public void write(ByteBuffer buf, CompletionHandler handler) { + throw new UnsupportedOperationException(); + } + + @Override + public void close(CompletionHandler handler) { + closed = true; + handler.completed(); + } + } +} |