[ 
https://issues.apache.org/jira/browse/DELTASPIKE-829?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Gerhard Petracek updated DELTASPIKE-829:
----------------------------------------
    Description: 
jersey-test starts jetty which answers requests in a separated thread. since ds 
test-control just handles the thread of the test itself, it's needed to 
integrate jetty and jersey with the cdi-container. usually that's done via a 
ServletRequestListener - the following part describes an alternative approach 
for jersey-test:

{code}
//use: 
-Djersey.config.test.container.factory=custom.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}

  was:
alternative to a ServletRequestListener:

{code}
//use: 
-Djersey.config.test.container.factory=custom.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}


> 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
>
> jersey-test starts jetty which answers requests in a separated thread. since 
> ds test-control just handles the thread of the test itself, it's needed to 
> integrate jetty and jersey with the cdi-container. usually that's done via a 
> ServletRequestListener - the following part describes an alternative approach 
> for jersey-test:
> {code}
> //use: 
> -Djersey.config.test.container.factory=custom.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)

Reply via email to