You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2014/10/13 16:04:03 UTC

svn commit: r1631398 - in /sling/site/trunk/content/documentation/development: jcr-mock.mdtext osgi-mock.mdtext resourceresolver-mock.mdtext sling-mock.mdtext

Author: sseifert
Date: Mon Oct 13 14:04:03 2014
New Revision: 1631398

URL: http://svn.apache.org/r1631398
Log:
SLING-4042 mock documentation

Modified:
    sling/site/trunk/content/documentation/development/jcr-mock.mdtext
    sling/site/trunk/content/documentation/development/osgi-mock.mdtext
    sling/site/trunk/content/documentation/development/resourceresolver-mock.mdtext
    sling/site/trunk/content/documentation/development/sling-mock.mdtext

Modified: sling/site/trunk/content/documentation/development/jcr-mock.mdtext
URL: http://svn.apache.org/viewvc/sling/site/trunk/content/documentation/development/jcr-mock.mdtext?rev=1631398&r1=1631397&r2=1631398&view=diff
==============================================================================
--- sling/site/trunk/content/documentation/development/jcr-mock.mdtext (original)
+++ sling/site/trunk/content/documentation/development/jcr-mock.mdtext Mon Oct 13 14:04:03 2014
@@ -5,12 +5,11 @@ Mock implementation of selected JCR APIs
 
 ## Maven Dependency
 
-```xml
-<dependency>
-  <groupId>org.apache.sling</groupId>
-  <artifactId>org.apache.sling.testing.jcr-mock</artifactId>
-</dependency>
-```
+    #!xml
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.testing.jcr-mock</artifactId>
+    </dependency>
 
 See latest version on the [downloads page]({{ refs.downloads.path }}).
 
@@ -43,12 +42,11 @@ The factory class `MockJcr` allows to in
 
 Example:
 
-```java
-// get session
-Session session = MockJcr.newSession();
-
-// get repository
-Repository repository = MockJcr.newRepository();
-```
+    #!java
+    // get session
+    Session session = MockJcr.newSession();
+
+    // get repository
+    Repository repository = MockJcr.newRepository();
 
 The repository is empty and contains only the root node. You can use the JCR API to fill it with content.

Modified: sling/site/trunk/content/documentation/development/osgi-mock.mdtext
URL: http://svn.apache.org/viewvc/sling/site/trunk/content/documentation/development/osgi-mock.mdtext?rev=1631398&r1=1631397&r2=1631398&view=diff
==============================================================================
--- sling/site/trunk/content/documentation/development/osgi-mock.mdtext (original)
+++ sling/site/trunk/content/documentation/development/osgi-mock.mdtext Mon Oct 13 14:04:03 2014
@@ -5,12 +5,11 @@ Mock implementation of selected OSGi API
 
 ## Maven Dependency
 
-```xml
-<dependency>
-  <groupId>org.apache.sling</groupId>
-  <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
-</dependency>
-```
+    #!xml
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
+    </dependency>
 
 See latest version on the [downloads page]({{ refs.downloads.path }}).
 
@@ -40,26 +39,24 @@ The factory class `MockOsgi` allows to i
 
 Example:
 
-```java
-// get bundle context
-BundleContext bundleContext = MockOsgi.newBundleContext();
-
-// get component context
-Dictionary<String,Object> properties = new Hashtable<>();
-properties.put("prop1", "value1");
-BundleContext bundleContext = MockOsgi.newComponentContext(properties);
-```
+    #!java
+    // get bundle context
+    BundleContext bundleContext = MockOsgi.newBundleContext();
+
+    // get component context
+    Dictionary<String,Object> properties = new Hashtable<>();
+    properties.put("prop1", "value1");
+    BundleContext bundleContext = MockOsgi.newComponentContext(properties);
 
 It is possible to simulate registering of OSGi services (backed by a simple hash map internally):
 
-```java
-// register service
-bundleContext.registerService(MyClass.class, myService, properties);
-
-// get service instance
-ServiceReference ref = bundleContext.getServiceReference(MyClass.class.getName());
-MyClass service = bundleContext.getService(ref);
-```
+    #!java
+    // register service
+    bundleContext.registerService(MyClass.class, myService, properties);
+
+    // get service instance
+    ServiceReference ref = bundleContext.getServiceReference(MyClass.class.getName());
+    MyClass service = bundleContext.getService(ref);
 
 ### Activation and Dependency Injection
 
@@ -68,24 +65,23 @@ tries to to its best to execute all as e
 
 Example:
 
-```java
-// get bundle context
-BundleContext bundleContext = MockOsgi.newBundleContext();
+    #!java
+    // get bundle context
+    BundleContext bundleContext = MockOsgi.newBundleContext();
 
-// create service instance manually
-MyService service = new MyService();
+    // create service instance manually
+    MyService service = new MyService();
 
-// inject dependencies
-MockOsgi.injectServices(service, bundleContext);
+    // inject dependencies
+    MockOsgi.injectServices(service, bundleContext);
 
-// activate service
-MockOsgi.activate(service, props);
+    // activate service
+    MockOsgi.activate(service, props);
 
-// operate with service...
+    // operate with service...
 
-// deactivate service
-MockOsgi.deactivate(service);
-```
+    // deactivate service
+    MockOsgi.deactivate(service);
 
 Please note: The injectServices, activate and deactivate Methods can only work properly when the SCR XML metadata files
 are preset in the classpath at `/OSGI-INF`. They are generated automatically by the Maven SCR plugin, but might be

Modified: sling/site/trunk/content/documentation/development/resourceresolver-mock.mdtext
URL: http://svn.apache.org/viewvc/sling/site/trunk/content/documentation/development/resourceresolver-mock.mdtext?rev=1631398&r1=1631397&r2=1631398&view=diff
==============================================================================
--- sling/site/trunk/content/documentation/development/resourceresolver-mock.mdtext (original)
+++ sling/site/trunk/content/documentation/development/resourceresolver-mock.mdtext Mon Oct 13 14:04:03 2014
@@ -5,12 +5,11 @@ Mock for the resource resolver / factory
 
 ## Maven Dependency
 
-```xml
-<dependency>
-  <groupId>org.apache.sling</groupId>
-  <artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
-</dependency>
-```
+    #!xml
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
+    </dependency>
 
 See latest version on the [downloads page]({{ refs.downloads.path }}).
 

Modified: sling/site/trunk/content/documentation/development/sling-mock.mdtext
URL: http://svn.apache.org/viewvc/sling/site/trunk/content/documentation/development/sling-mock.mdtext?rev=1631398&r1=1631397&r2=1631398&view=diff
==============================================================================
--- sling/site/trunk/content/documentation/development/sling-mock.mdtext (original)
+++ sling/site/trunk/content/documentation/development/sling-mock.mdtext Mon Oct 13 14:04:03 2014
@@ -5,12 +5,11 @@ Mock implementation of selected Sling AP
 
 ## Maven Dependency
 
-```xml
-<dependency>
-  <groupId>org.apache.sling</groupId>
-  <artifactId>org.apache.sling.testing.sling-mock</artifactId>
-</dependency>
-```
+    #!xml
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.testing.sling-mock</artifactId>
+    </dependency>
 
 See latest version on the [downloads page]({{ refs.downloads.path }}).
 
@@ -58,21 +57,19 @@ independently (and in parallel, if requi
 
 Example:
 
-```java
-public class ExampleTest {
+    #!java
+    public class ExampleTest {
 
-  @Rule
-  public final SlingContext context = new SlingContext();
+      @Rule
+      public final SlingContext context = new SlingContext();
 
-  @Test
-  public void testSomething() {
-    Resource resource = context.resourceResolver().getResource("/content/sample/en");
-    // further testing
-  }
-
-}
+      @Test
+      public void testSomething() {
+        Resource resource = context.resourceResolver().getResource("/content/sample/en");
+        // further testing
+      }
 
-```
+    }
 
 It is possible to combine such a unit test with a `@RunWith` annotation e.g. for
 [Mockito JUnit Runner][mockito-testrunner].
@@ -97,15 +94,13 @@ Additionally it supports:
 
 The Sling mock context supports different resource resolver types. Example:
 
-```java
-public class ExampleTest {
+    #!java
+    public class ExampleTest {
 
-  @Rule
-  public final SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
+      @Rule
+      public final SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
 
-}
-
-```
+    }
 
 Different resource resolver mock types are supported with pros and cons, see next chapter for details.
 
@@ -149,13 +144,12 @@ _Warnings/Remarks_
 
 To use this type you have to declare an additional dependency in your test project:
 
-```xml
-<dependency>
-  <groupId>org.apache.sling</groupId>
-  <artifactId>org.apache.sling.testing.sling-mock-jackrabbit</artifactId>
-  <scope>test</scope>
-</dependency>
-```
+    #!xml
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.testing.sling-mock-jackrabbit</artifactId>
+      <scope>test</scope>
+    </dependency>
 
 See latest version on the [downloads page]({{ refs.downloads.path }}).
 
@@ -164,13 +158,12 @@ See latest version on the [downloads pag
 
 Example:
 
-```java
-// get a resource resolver
-ResourceResolver resolver = MockSling.newResourceResolver();
-
-// get a resource resolver backed by a specific repository type
-ResourceResolver resolver = MockSling.newResourceResolver(ResourceResolverType.JCR_MOCK);
-```
+    #!java
+    // get a resource resolver
+    ResourceResolver resolver = MockSling.newResourceResolver();
+
+    // get a resource resolver backed by a specific repository type
+    ResourceResolver resolver = MockSling.newResourceResolver(ResourceResolverType.JCR_MOCK);
 
 ### Adapter Factories
 
@@ -178,18 +171,17 @@ You can register your own or existing ad
 
 Example:
 
-```java
-// register adapter factory
-BundleContext bundleContext = MockOsgi.newBundleContext();
-MockSling.setAdapterManagerBundleContext(bundleContext);
-bundleContext.registerService(myAdapterFactory);
-
-// test adaption
-MyClass object = resource.adaptTo(MyClass.class);
-
-// cleanup after unit test
-MockSling.clearAdapterManagerBundleContext();
-```
+    #!java
+    // register adapter factory
+    BundleContext bundleContext = MockOsgi.newBundleContext();
+    MockSling.setAdapterManagerBundleContext(bundleContext);
+    bundleContext.registerService(myAdapterFactory);
+
+    // test adaption
+    MyClass object = resource.adaptTo(MyClass.class);
+
+    // cleanup after unit test
+    MockSling.clearAdapterManagerBundleContext();
 
 Make sure you clean up the adapter manager bundle association after running the unit test otherwise it can 
 interfere with the following tests. If you use the `SlingContext` Junit rule this is done automatically for you.
@@ -199,16 +191,15 @@ interfere with the following tests. If y
 
 Example:
 
-```java
-// get script helper
-SlingScriptHelper scriptHelper = MockSling.newSlingScriptHelper();
-
-// get request
-SlingHttpServletRequest request = scriptHelper.getRequest();
-
-// get service
-MyService object = scriptHelper.getService(MyService.class);
-```
+    #!java
+    // get script helper
+    SlingScriptHelper scriptHelper = MockSling.newSlingScriptHelper();
+
+    // get request
+    SlingHttpServletRequest request = scriptHelper.getRequest();
+
+    // get service
+    MyService object = scriptHelper.getService(MyService.class);
 
 To support getting OSGi services you have to register them via the `BundleContext` interface of the
 [JCR Mocks][jcr-mock] before. You can use an alternative factory method for the `SlingScriptHelper` providing
@@ -219,69 +210,67 @@ existing instances of request, response 
 
 Example for preparing a sling request with custom request data:
 
-```java
-// prepare sling request
-ResourceResolver resourceResolver = MockSling.newResourceResolver();
-MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(resourceResolver);
-
-// simulate query string
-request.setQueryString("param1=aaa&param2=bbb");
-
-// alternative - set query parameters as map
-request.setParameterMap(ImmutableMap.<String,Object>builder()
-    .put("param1", "aaa")
-    .put("param2", "bbb")
-    .build());
-
-// set current resource
-request.setResource(resourceResolver.getResource("/content/sample"));
-
-// set sling request path info properties
-MockRequestPathInfo requestPathInfo = (MockRequestPathInfo)request.getRequestPathInfo();
-requestPathInfo.setSelectorString("selector1.selector2");
-requestPathInfo.setExtension("html");
-
-// set method
-request.setMethod(HttpConstants.METHOD_POST);
-
-// set attributes
-request.setAttribute("attr1", "value1");
-
-// set headers
-request.addHeader("header1", "value1");
-
-// set cookies
-request.addCookie(new Cookie("cookie1", "value1"));
-```
+    #!java
+    // prepare sling request
+    ResourceResolver resourceResolver = MockSling.newResourceResolver();
+    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(resourceResolver);
+
+    // simulate query string
+    request.setQueryString("param1=aaa&param2=bbb");
+
+    // alternative - set query parameters as map
+    request.setParameterMap(ImmutableMap.<String,Object>builder()
+        .put("param1", "aaa")
+        .put("param2", "bbb")
+        .build());
+
+    // set current resource
+    request.setResource(resourceResolver.getResource("/content/sample"));
+
+    // set sling request path info properties
+    MockRequestPathInfo requestPathInfo = (MockRequestPathInfo)request.getRequestPathInfo();
+    requestPathInfo.setSelectorString("selector1.selector2");
+    requestPathInfo.setExtension("html");
+
+    // set method
+    request.setMethod(HttpConstants.METHOD_POST);
+
+    // set attributes
+    request.setAttribute("attr1", "value1");
+
+    // set headers
+    request.addHeader("header1", "value1");
+
+    // set cookies
+    request.addCookie(new Cookie("cookie1", "value1"));
 
 ### SlingHttpServletResponse
 
 Example for preparing a sling response which can collect the data that was written to it:
 
-```java
-// prepare sling response
-MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
-
-// execute your unit test code that writes to the response...
-
-// validate status code
-assertEquals(HttpServletResponse.SC_OK, response.getStatus());
-
-// validate content type and content length
-assertEquals("text/plain;charset=UTF-8", response.getContentType());
-assertEquals(CharEncoding.UTF_8, response.getCharacterEncoding());
-assertEquals(55, response.getContentLength());
-
-// validate headers
-assertTrue(response.containsHeader("header1"));
-assertEquals("5", response.getHeader("header2"));
-
-// validate response body as string
-assertEquals(TEST_CONTENT, response.getOutputAsString());
-
-// validate response body as binary data
-assertArrayEquals(TEST_DATA, response.getOutput());
-```
+    #!java
+    // prepare sling response
+    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
+
+    // execute your unit test code that writes to the response...
+
+    // validate status code
+    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+
+    // validate content type and content length
+    assertEquals("text/plain;charset=UTF-8", response.getContentType());
+    assertEquals(CharEncoding.UTF_8, response.getCharacterEncoding());
+    assertEquals(55, response.getContentLength());
+
+    // validate headers
+    assertTrue(response.containsHeader("header1"));
+    assertEquals("5", response.getHeader("header2"));
+
+    // validate response body as string
+    assertEquals(TEST_CONTENT, response.getOutputAsString());
+
+    // validate response body as binary data
+    assertArrayEquals(TEST_DATA, response.getOutput());
 
 
 ### Import resource data from JSON file in classpath
@@ -291,36 +280,34 @@ in the classpath beneath the unit tests.
 
 Example JSON data:
 
-```json
-{
-  "jcr:primaryType": "app:Page",
-  "jcr:content": {
-    "jcr:primaryType": "app:PageContent",
-    "jcr:title": "English",
-    "app:template": "/apps/sample/templates/homepage",
-    "sling:resourceType": "sample/components/homepage",
-    "jcr:createdBy": "admin",
-    "jcr:created": "Thu Aug 07 2014 16:32:59 GMT+0200",
-    "par": {
-      "jcr:primaryType": "nt:unstructured",
-      "sling:resourceType": "foundation/components/parsys",
-      "colctrl": {
-        "jcr:primaryType": "nt:unstructured",
-        "layout": "2;colctrl-lt0",
-        "sling:resourceType": "foundation/components/parsys/colctrl"
+    #!json
+    {
+      "jcr:primaryType": "app:Page",
+      "jcr:content": {
+        "jcr:primaryType": "app:PageContent",
+        "jcr:title": "English",
+        "app:template": "/apps/sample/templates/homepage",
+        "sling:resourceType": "sample/components/homepage",
+        "jcr:createdBy": "admin",
+        "jcr:created": "Thu Aug 07 2014 16:32:59 GMT+0200",
+        "par": {
+          "jcr:primaryType": "nt:unstructured",
+          "sling:resourceType": "foundation/components/parsys",
+          "colctrl": {
+            "jcr:primaryType": "nt:unstructured",
+            "layout": "2;colctrl-lt0",
+            "sling:resourceType": "foundation/components/parsys/colctrl"
+          }
+        }
       }
     }
-  }
-}
-```
 
 Example code to import the JSON data:
 
-```java
-ResourceResolver resolver = MockSling.newResourceResolver();
-ContentLoader contentLoader = new ContentLoader(resolver);
-contentLoader.json("/sample-data.json", "/content/sample/en");
-```
+    #!java
+    ResourceResolver resolver = MockSling.newResourceResolver();
+    ContentLoader contentLoader = new ContentLoader(resolver);
+    contentLoader.json("/sample-data.json", "/content/sample/en");
 
 This codes creates a new resource at `/content/sample/en` (and - if not existent - the parent resources) and
 imports the JSON data to this node. It can be accessed using the Sling Resource or JCR API afterwards.
@@ -333,11 +320,10 @@ The data is stored usig a nt:file/nt:res
 
 Example code to import a binary file:
 
-```java
-ResourceResolver resolver = MockSling.newResourceResolver();
-ContentLoader contentLoader = new ContentLoader(resolver);
-contentLoader.binaryFile("/sample-file.gif", "/content/binary/sample-file.gif");
-```
+    #!java
+    ResourceResolver resolver = MockSling.newResourceResolver();
+    ContentLoader contentLoader = new ContentLoader(resolver);
+    contentLoader.binaryFile("/sample-file.gif", "/content/binary/sample-file.gif");
 
 This codes creates a new resource at `/content/binary/sample-file.gif` (and - if not existent - the parent 
 resources) and imports the binary data to a jcr:content subnode.