You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltaspike.apache.org by "Gerhard Petracek (JIRA)" <ji...@apache.org> on 2015/02/04 18:32:35 UTC

[jira] [Created] (DELTASPIKE-829) add hints for using jersey-test with test-control

Gerhard Petracek created DELTASPIKE-829:
-------------------------------------------

             Summary: add hints for using jersey-test with test-control
                 Key: DELTASPIKE-829
                 URL: https://issues.apache.org/jira/browse/DELTASPIKE-829
             Project: DeltaSpike
          Issue Type: Improvement
          Components: Documentation, TestControl
            Reporter: Gerhard Petracek
            Assignee: Rafael Benevides


alternative to a ServletRequestListener:

{code}
//use: -Djersey.config.test.container.factory=my.CdiAwareJettyTestContainerFactory

@RunWith(CdiTestRunner.class)
public class SimpleCdiAndJaxRsTest extends JerseyTest {
  //...
}
{code}

or

{code}
public class CdiAwareJerseyTest extends JerseyTest
{
    static
    {
        System.setProperty("jersey.config.test.container.factory", CdiAwareJettyTestContainerFactory.class.getName());
    }
}

@RunWith(CdiTestRunner.class)
public class SimpleCdiAndJaxRsTest extends CdiAwareJerseyTest
{
    //...
}
{code}

{code}
public class CdiAwareJettyTestContainerFactory
  implements TestContainerFactory
{
    @Override
    public TestContainer create(final URI baseUri, final DeploymentContext context) throws IllegalArgumentException
    {
        return new CdiAwareJettyTestContainer(baseUri, context);
    }
}
{code}

CdiAwareJettyTestContainer is a copy of JettyTestContainerFactory.JettyTestContainer but with

{code}
HandlerWrapper cdiHandlerWrapper = new CdiAwareHandlerWrapper();        cdiHandlerWrapper.setHandler(this.server.getHandler());        this.server.setHandler(cdiHandlerWrapper);
{code}
after the line with JettyHttpContainerFactory#createServer

{code}
//activate the request-context e.g. via:
public class CdiAwareHandlerWrapper extends HandlerWrapper
{
    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();

        try
        {
            cdiContainer.getContextControl().startContext(RequestScoped.class);
            super.handle(target, baseRequest, request, response);
        }
        finally
        {
            cdiContainer.getContextControl().stopContext(RequestScoped.class);
        }
    }
}
{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)