deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rafab...@apache.org
Subject svn commit: r1657379 - /deltaspike/site/trunk/content/documentation/test-control.html
Date Wed, 04 Feb 2015 19:18:13 GMT
Author: rafabene
Date: Wed Feb  4 19:18:13 2015
New Revision: 1657379

URL: http://svn.apache.org/r1657379
Log:
Site checkin for project Apache DeltaSpike Documentation

Modified:
    deltaspike/site/trunk/content/documentation/test-control.html

Modified: deltaspike/site/trunk/content/documentation/test-control.html
URL: http://svn.apache.org/viewvc/deltaspike/site/trunk/content/documentation/test-control.html?rev=1657379&r1=1657378&r2=1657379&view=diff
==============================================================================
--- deltaspike/site/trunk/content/documentation/test-control.html (original)
+++ deltaspike/site/trunk/content/documentation/test-control.html Wed Feb  4 19:18:13 2015
@@ -232,6 +232,7 @@ body {
 <li><a href="#_jsf_via_myfaces_test">JSF (via MyFaces-Test)</a></li>
 </ul>
 </li>
+<li><a href="#_using_jersey_test_with_test_control">Using jersey-test with test-control</a></li>
 <li><a href="#_mixed_tests">Mixed Tests</a></li>
 <li><a href="#_known_restrictions">Known Restrictions</a>
 <ul class="sectlevel3">
@@ -732,6 +733,80 @@ constructor) and specify the target-type
 </div>
 </div>
 </div>
+<div class="sect2">
+<h3 id="_using_jersey_test_with_test_control">Using jersey-test with test-control</h3>
+<div class="paragraph">
+<p>jersey-test starts jetty which answers requests in a separated thread. since ds
test-control just handles the thread of the test itself, it&#8217;s needed to integrate
jetty and jersey with the cdi-container. usually that&#8217;s done via a ServletRequestListener
- the following part describes an alternative approach for jersey-test:</p>
+</div>
+<div class="listingblock">
+<div class="content">
+<pre class="CodeRay highlight"><code data-lang="java"><span class="comment">//use:
-Djersey.config.test.container.factory=custom.CdiAwareJettyTestContainerFactory</span>
+
+<span class="annotation">@RunWith</span>(CdiTestRunner.class)
+<span class="directive">public</span> <span class="type">class</span>
<span class="class">SimpleCdiAndJaxRsTest</span> <span class="directive">extends</span>
JerseyTest {
+  <span class="comment">//...</span>
+}</code></pre>
+</div>
+</div>
+<div class="paragraph">
+<p>or</p>
+</div>
+<div class="listingblock">
+<div class="content">
+<pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span>
<span class="type">class</span> <span class="class">CdiAwareJerseyTest</span>
<span class="directive">extends</span> JerseyTest {
+    <span class="directive">static</span> {
+        <span class="predefined-type">System</span>.setProperty(<span class="string"><span
class="delimiter">&quot;</span><span class="content">jersey.config.test.container.factory</span><span
class="delimiter">&quot;</span></span>,CdiAwareJettyTestContainerFactory.class.getName());
+    }
+}
+
+<span class="annotation">@RunWith</span>(CdiTestRunner.class)
+<span class="directive">public</span> <span class="type">class</span>
<span class="class">SimpleCdiAndJaxRsTest</span> <span class="directive">extends</span>
CdiAwareJerseyTest {
+    <span class="comment">//...</span>
+}</code></pre>
+</div>
+</div>
+<div class="listingblock">
+<div class="content">
+<pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span>
<span class="type">class</span> <span class="class">CdiAwareJettyTestContainerFactory</span>
<span class="directive">implements</span> TestContainerFactory {
+    <span class="annotation">@Override</span>
+    <span class="directive">public</span> TestContainer create(<span class="directive">final</span>
<span class="predefined-type">URI</span> baseUri, <span class="directive">final</span>
DeploymentContext context) <span class="directive">throws</span> <span class="exception">IllegalArgumentException</span>
{
+        <span class="keyword">return</span> <span class="keyword">new</span>
CdiAwareJettyTestContainer(baseUri, context);
+    }
+}</code></pre>
+</div>
+</div>
+<div class="paragraph">
+<p>CdiAwareJettyTestContainer is a copy of JettyTestContainerFactory.JettyTestContainer
but with</p>
+</div>
+<div class="listingblock">
+<div class="content">
+<pre class="CodeRay highlight"><code data-lang="java">HandlerWrapper cdiHandlerWrapper
= <span class="keyword">new</span> CdiAwareHandlerWrapper();
+cdiHandlerWrapper.setHandler(<span class="local-variable">this</span>.server.getHandler());
+<span class="local-variable">this</span>.server.setHandler(cdiHandlerWrapper);</code></pre>
+</div>
+</div>
+<div class="paragraph">
+<p>after the line with JettyHttpContainerFactory#createServer</p>
+</div>
+<div class="listingblock">
+<div class="content">
+<pre class="CodeRay highlight"><code data-lang="java"><span class="comment">//activate
the request-context e.g. via:</span>
+<span class="directive">public</span> <span class="type">class</span>
<span class="class">CdiAwareHandlerWrapper</span> <span class="directive">extends</span>
HandlerWrapper {
+    <span class="annotation">@Override</span>
+    <span class="directive">public</span> <span class="type">void</span>
handle(<span class="predefined-type">String</span> target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response) <span class="directive">throws</span>
<span class="exception">IOException</span>, ServletException {
+        CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
+
+        <span class="keyword">try</span> {
+            cdiContainer.getContextControl().startContext(RequestScoped.class);
+            <span class="local-variable">super</span>.handle(target, baseRequest,
request, response);
+        } <span class="keyword">finally</span> {
+            cdiContainer.getContextControl().stopContext(RequestScoped.class);
+        }
+    }
+}</code></pre>
+</div>
+</div>
+</div>
 <div class="sect2">
 <h3 id="_mixed_tests">Mixed Tests</h3>
 <div class="paragraph">



Mime
View raw message