You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bu...@apache.org on 2014/11/14 22:56:47 UTC

svn commit: r929262 - in /websites/staging/sling/trunk/content: ./ documentation/development/osgi-mock.html documentation/development/sling-mock.html

Author: buildbot
Date: Fri Nov 14 21:56:47 2014
New Revision: 929262

Log:
Staging update by buildbot for sling

Modified:
    websites/staging/sling/trunk/content/   (props changed)
    websites/staging/sling/trunk/content/documentation/development/osgi-mock.html
    websites/staging/sling/trunk/content/documentation/development/sling-mock.html

Propchange: websites/staging/sling/trunk/content/
------------------------------------------------------------------------------
--- cms:source-revision (original)
+++ cms:source-revision Fri Nov 14 21:56:47 2014
@@ -1 +1 @@
-1639681
+1639795

Modified: websites/staging/sling/trunk/content/documentation/development/osgi-mock.html
==============================================================================
--- websites/staging/sling/trunk/content/documentation/development/osgi-mock.html (original)
+++ websites/staging/sling/trunk/content/documentation/development/osgi-mock.html Fri Nov 14 21:56:47 2014
@@ -96,9 +96,9 @@
 <li><a href="#maven-dependency">Maven Dependency</a></li>
 <li><a href="#implemented-mock-features">Implemented mock features</a></li>
 <li><a href="#usage">Usage</a><ul>
+<li><a href="#osgi-context-junit-rule">OSGi Context JUnit Rule</a></li>
 <li><a href="#getting-osgi-mock-objects">Getting OSGi mock objects</a></li>
 <li><a href="#activation-and-dependency-injection">Activation and Dependency Injection</a></li>
-<li><a href="#usage-with-slingcontext-junit-rule">Usage with SlingContext JUnit rule</a></li>
 </ul>
 </li>
 </ul>
@@ -119,18 +119,73 @@
 <p>The mock implementation supports:</p>
 <ul>
 <li>Instantiating OSGi <code>Bundle</code>, <code>BundleContext</code> and <code>ComponentContext</code> objects and navigate between them.</li>
-<li>Read and write properties on them.</li>
-<li>Register OSGi services and get references to service instances</li>
+<li>Register OSGi SCR services and get references to service instances</li>
+<li>Supports reading OSGi SCR metadata from <code>/OSGI-INF/&lt;pid&gt;.xml</code></li>
+<li>Apply service properties/component configuration provided in unit test and from SCR metadata</li>
+<li>Inject SCR dependencies</li>
+<li>Call lifecyle methods for activating, deactivating or modifying SCR components</li>
 <li>Service and bundle listener implementation</li>
-<li>When adding services to BundleContext OSGi metadata from <code>/OSGI-INF/&lt;pid&gt;.xml</code> is read (e.g. for service ranking property)</li>
 <li>Mock implementation of <code>LogService</code> which logs to SLF4J in JUnit context</li>
 </ul>
 <p>The following features are <em>not supported</em>:</p>
 <ul>
-<li>Activation and deactivation methods of services are not called automatically (but helper methods exist)</li>
-<li>Dependency injection does not take place automatically (but helper methods exist)</li>
+<li>Dependencies are only injected and lifecycle methods are only called when initiated by the unit test</li>
 </ul>
 <h2 id="usage">Usage</h2>
+<h3 id="osgi-context-junit-rule">OSGi Context JUnit Rule</h3>
+<p>The OSGi mock context can be injected into a JUnit test using a custom JUnit rule named <code>OsgiContext</code>.
+This rules takes care of all initialization and cleanup tasks required to make sure all unit tests can run 
+independently (and in parallel, if required).</p>
+<p>Example:</p>
+<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
+16
+17
+18</pre></div></td><td class="code"><div class="codehilite"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ExampleTest</span> <span class="o">{</span>
+
+  <span class="nd">@Rule</span>
+  <span class="kd">public</span> <span class="kd">final</span> <span class="n">OsgiContext</span> <span class="n">context</span> <span class="o">=</span> <span class="k">new</span> <span class="n">OsgiContext</span><span class="o">();</span>
+
+  <span class="nd">@Test</span>
+  <span class="kd">public</span> <span class="kt">void</span> <span class="nf">testSomething</span><span class="o">()</span> <span class="o">{</span>
+
+    <span class="c1">// register and activate service</span>
+    <span class="n">MyService</span> <span class="n">service1</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="na">registerInjectActivateService</span><span class="o">(</span><span class="k">new</span> <span class="n">MyService</span><span class="o">(),</span>
+        <span class="n">ImmutableMap</span><span class="o">.&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">Object</span><span class="o">&gt;</span><span class="n">of</span><span class="o">(</span><span class="s">&quot;prop1&quot;</span><span class="o">,</span> <span class="s">&quot;value1&quot;</span><span class="o">));</span>
+
+    <span class="c1">// get service instance</span>
+    <span class="n">OtherService</span> <span class="n">service2</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="na">getService</span><span class="o">(</span><span class="n">OtherService</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
+
+  <span class="o">}</span>
+
+<span class="o">}</span>
+</pre></div>
+</td></tr></table>
+
+<p>It is possible to combine such a unit test with a <code>@RunWith</code> annotation e.g. for
+<a href="http://mockito.github.io/mockito/docs/current/org/mockito/runners/MockitoJUnitRunner.html">Mockito JUnit Runner</a>.</p>
+<p>The <code>OsgiContext</code> object provides access to mock implementations of:</p>
+<ul>
+<li>OSGi Component Context</li>
+<li>OSGi Bundle Context</li>
+</ul>
+<p>Additionally it supports:</p>
+<ul>
+<li>Registering and activating OSGi services and inject dependencies</li>
+</ul>
 <h3 id="getting-osgi-mock-objects">Getting OSGi mock objects</h3>
 <p>The factory class <code>MockOsgi</code> allows to instantiate the different mock implementations.</p>
 <p>Example:</p>
@@ -139,14 +194,12 @@
 3
 4
 5
-6
-7</pre></div></td><td class="code"><div class="codehilite"><pre><span class="c1">// get bundle context</span>
+6</pre></div></td><td class="code"><div class="codehilite"><pre><span class="c1">// get bundle context</span>
 <span class="n">BundleContext</span> <span class="n">bundleContext</span> <span class="o">=</span> <span class="n">MockOsgi</span><span class="o">.</span><span class="na">newBundleContext</span><span class="o">();</span>
 
 <span class="c1">// get component context</span>
-<span class="n">Dictionary</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span><span class="n">Object</span><span class="o">&gt;</span> <span class="n">properties</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Hashtable</span><span class="o">&lt;&gt;();</span>
-<span class="n">properties</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">&quot;prop1&quot;</span><span class="o">,</span> <span class="s">&quot;value1&quot;</span><span class="o">);</span>
-<span class="n">BundleContext</span> <span class="n">bundleContext</span> <span class="o">=</span> <span class="n">MockOsgi</span><span class="o">.</span><span class="na">newComponentContext</span><span class="o">(</span><span class="n">properties</span><span class="o">);</span>
+<span class="n">BundleContext</span> <span class="n">bundleContext</span> <span class="o">=</span> <span class="n">MockOsgi</span><span class="o">.</span><span class="na">newComponentContext</span><span class="o">(</span><span class="n">properties</span><span class="o">,</span>
+    <span class="n">ImmutableMap</span><span class="o">.&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">Object</span><span class="o">&gt;</span><span class="n">of</span><span class="o">(</span><span class="s">&quot;prop1&quot;</span><span class="o">,</span> <span class="s">&quot;value1&quot;</span><span class="o">));</span>
 </pre></div>
 </td></tr></table>
 
@@ -211,21 +264,8 @@ are preset in the classpath at <code>/OS
 missing if your clean and build the project within your IDE (e.g. Eclipse). In this case you have to compile the
 project again with maven and can run the tests - or use a Maven IDE Integration like m2eclipse.</li>
 </ul>
-<h3 id="usage-with-slingcontext-junit-rule">Usage with <code>SlingContext</code> JUnit rule</h3>
-<p>If you are using the <code>SlingContext</code> JUnit rule from <a href="/documentation/development/sling-mock.html">Sling Mocks</a> it is even easier asseccing and registering OSGi services:</p>
-<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>1
-2
-3
-4
-5</pre></div></td><td class="code"><div class="codehilite"><pre><span class="c1">// register service, inject dependencies and call activate method</span>
-<span class="n">context</span><span class="o">.</span><span class="na">registerInjectActivateService</span><span class="o">(</span><span class="n">myService</span><span class="o">);</span>
-
-<span class="c1">// get service instance</span>
-<span class="n">MyClass</span> <span class="n">service</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="na">getService</span><span class="o">(</span><span class="n">MyClass</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
-</pre></div>
-</td></tr></table>
       <div class="timestamp" style="margin-top: 30px; font-size: 80%; text-align: right;">
-        Rev. 1631433 by sseifert on Mon, 13 Oct 2014 15:01:38 +0000
+        Rev. 1639795 by sseifert on Fri, 14 Nov 2014 21:56:35 +0000
       </div>
       <div class="trademarkFooter"> 
         Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project

Modified: websites/staging/sling/trunk/content/documentation/development/sling-mock.html
==============================================================================
--- websites/staging/sling/trunk/content/documentation/development/sling-mock.html (original)
+++ websites/staging/sling/trunk/content/documentation/development/sling-mock.html Fri Nov 14 21:56:47 2014
@@ -198,8 +198,8 @@ independently (and in parallel, if requi
 </pre></div>
 </td></tr></table>
 
-<p>It is possible to combine such a unit test with a <code>@RunWith</code> annotation e.g. for
-[Mockito JUnit Runner][mockito-testrunner].</p>
+<p>It is possible to combine such a unit test rule with a <code>@RunWith</code> annotation e.g. for
+<a href="http://mockito.github.io/mockito/docs/current/org/mockito/runners/MockitoJUnitRunner.html">Mockito JUnit Runner</a>.</p>
 <p>The <code>SlingContext</code> object provides access to mock implementations of:</p>
 <ul>
 <li>OSGi Component Context</li>
@@ -549,7 +549,7 @@ resources) and imports the binary data t
 
 <p>If you use the <code>SlingContext</code> JUnit rule you case just use <code>context.create()</code>.</p>
       <div class="timestamp" style="margin-top: 30px; font-size: 80%; text-align: right;">
-        Rev. 1631697 by sseifert on Tue, 14 Oct 2014 09:34:19 +0000
+        Rev. 1639795 by sseifert on Fri, 14 Nov 2014 21:56:35 +0000
       </div>
       <div class="trademarkFooter"> 
         Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project