Package org.apache.sling.testing.mock.sling.context

# Class SlingContextImpl

java.lang.Object
org.apache.sling.testing.mock.osgi.context.OsgiContextImpl
org.apache.sling.testing.mock.sling.context.SlingContextImpl

@ConsumerType public class SlingContextImpl extends org.apache.sling.testing.mock.osgi.context.OsgiContextImpl
Defines Sling context objects with lazy initialization. Should not be used directly but via the SlingContext JUnit rule or extension.

## Field Summary

Fields
Modifier and Type
Field
Description
`protected ContentBuilder`
`contentBuilder`
Content builder
`protected ContentLoader`
`contentLoader`
Content loader
`protected ContentLoader`
`contentLoaderAutoCommit`
Content loader with auto commit after loading content
`protected MockSlingJakartaHttpServletRequest`
`jakartaRequest`
Sling jakarta request
`protected MockSlingJakartaHttpServletResponse`
`jakartaResponse`
Sling response
`protected MockSlingHttpServletRequest`
`request`
Deprecated.
Use jakartaRequest instead.
`protected org.apache.sling.resourcebuilder.api.ResourceBuilder`
`resourceBuilder`
Resource builder
`protected org.apache.sling.api.resource.ResourceResolver`
`resourceResolver`
Resource resolver
`protected org.apache.sling.api.resource.ResourceResolverFactory`
`resourceResolverFactory`
Resource resolver factory
`protected ResourceResolverType`
`resourceResolverType`
Resource resolver type
`protected MockSlingHttpServletResponse`
`response`
Deprecated.
Use jakartaResponse instead.
`protected org.apache.sling.api.scripting.SlingScriptHelper`
`slingScriptHelper`
Sling script helper
`protected UniqueRoot`
`uniqueRoot`
Unique root helper

### Fields inherited from class org.apache.sling.testing.mock.osgi.context.OsgiContextImpl

`componentContext`

## Constructor Summary

Constructors
Constructor
Description
`SlingContextImpl()`

## Method Summary

Modifier and Type
Method
Description
`final void`
`addModelsForClasses(@NotNull Class<?> @NotNull ... classes)`
Search classpath for given class names to scan for and register all classes with @Model annotation.
`final void`
`addModelsForClasses(@NotNull String @NotNull ... classNames)`
Search classpath for given class names to scan for and register all classes with @Model annotation.
`final void`
`addModelsForPackage(@NotNull String packageName)`
Search classpath for given java package names (and sub packages) to scan for and register all classes with @Model annotation.
`final void`
`addModelsForPackage(@NotNull String @NotNull ... packageNames)`
Search classpath for given java package names (and sub packages) to scan for and register all classes with @Model annotation.
`@NotNull org.apache.sling.resourcebuilder.api.ResourceBuilder`
`build()`
Creates a `ResourceBuilder` object for easily creating test content.
`@NotNull ContentBuilder`
`create()`
Creates a `ContentBuilder` object for easily creating test content.
`final @Nullable org.apache.sling.api.resource.Resource`
`currentResource()`
`final @Nullable org.apache.sling.api.resource.Resource`
`currentResource(@Nullable String resourcePath)`
Set current resource in request.
`@Nullable org.apache.sling.api.resource.Resource`
`currentResource(@Nullable org.apache.sling.api.resource.Resource resource)`
Set current resource in request.
`final @NotNull MockSlingJakartaHttpServletRequest`
`jakartaRequest()`
`final @NotNull MockSlingJakartaHttpServletResponse`
`jakartaResponse()`
`@NotNull ContentLoader`
`load()`
`@NotNull ContentLoader`
`load(boolean autoCommit)`
`protected @NotNull org.apache.sling.api.resource.ResourceResolverFactory`
`newResourceResolverFactory()`
Initialize mocked resource resolver factory.
`final <T1, T2> void`
`registerAdapter(@NotNull Class<T1> adaptableClass, @NotNull Class<T2> adapterClass, @NotNull Function<T1,T2> adaptHandler)`
Create a Sling AdapterFactory on the fly which can adapt from `adaptableClass` to `adapterClass` and delegates the adapter mapping to the given `adaptHandler` function.
`final <T1, T2> void`
`registerAdapter(@NotNull Class<T1> adaptableClass, @NotNull Class<T2> adapterClass, T2 adapter)`
Create a Sling AdapterFactory on the fly which can adapt from `adaptableClass` to `adapterClass` and just returns the given value as result.
`protected void`
`registerDefaultServices()`
Default services that should be available for every unit test
`final MockSlingHttpServletRequest`
`request()`
Deprecated.
Use `jakartaRequest()` instead.
`final @NotNull MockRequestPathInfo`
`requestPathInfo()`
`protected @Nullable Object`
`resolveSlingBindingProperty(@NotNull String property)`
Deprecated.
Please use `resolveSlingBindingProperty(String, SlingJakartaHttpServletRequest)`
`protected @Nullable Object`
`resolveSlingBindingProperty(@NotNull String property, @NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request)`
Dynamically resolve property request for current request `SlingBindings`.
`protected @Nullable Object`
`resolveSlingBindingProperty(@NotNull String property, org.apache.sling.api.SlingHttpServletRequest request)`
Deprecated.
use `resolveSlingBindingProperty(String, SlingJakartaHttpServletRequest)` instead
`final @NotNull org.apache.sling.api.resource.ResourceResolver`
`resourceResolver()`
Returns the singleton resource resolver bound to this context.
`final @NotNull ResourceResolverType`
`resourceResolverType()`
`final MockSlingHttpServletResponse`
`response()`
Deprecated.
Use `jakartaResponse()` instead.
`final void`
`runMode(@NotNull String @NotNull ... runModes)`
Set current run mode(s).
`protected void`
`setRegisterSlingModelsFromClassPath(boolean registerSlingModelsFromClassPath)`
`protected void`
`setResourceResolverFactoryActivatorProps(@Nullable Map<String,Object> props)`
`protected void`
`setResourceResolverType(@Nullable ResourceResolverType resourceResolverType)`
`protected void`
`setUp()`
Setup actions before test method execution
`final @NotNull org.apache.sling.api.scripting.SlingScriptHelper`
`slingScriptHelper()`
`protected void`
`tearDown()`
Teardown actions after test method execution
`@NotNull UniqueRoot`
`uniqueRoot()`
Create unique root paths for unit tests (and clean them up after the test run automatically).

### Methods inherited from class org.apache.sling.testing.mock.osgi.context.OsgiContextImpl

`bundleContext, componentContext, getService, getServices, registerInjectActivateService, registerInjectActivateService, registerInjectActivateService, registerInjectActivateService, registerInjectActivateService, registerInjectActivateService, registerService, registerService, registerService, registerService`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### resourceResolverFactory

protected org.apache.sling.api.resource.ResourceResolverFactory resourceResolverFactory
Resource resolver factory

### resourceResolverType

protected ResourceResolverType resourceResolverType
Resource resolver type

### resourceResolver

protected org.apache.sling.api.resource.ResourceResolver resourceResolver
Resource resolver

### request

@Deprecated(since="4.1.0") protected MockSlingHttpServletRequest request
Deprecated.
Use jakartaRequest instead.
Sling request

### response

@Deprecated(since="4.1.0") protected MockSlingHttpServletResponse response
Deprecated.
Use jakartaResponse instead.
Sling response

### jakartaRequest

protected MockSlingJakartaHttpServletRequest jakartaRequest
Sling jakarta request

### jakartaResponse

protected MockSlingJakartaHttpServletResponse jakartaResponse
Sling response

### slingScriptHelper

protected org.apache.sling.api.scripting.SlingScriptHelper slingScriptHelper
Sling script helper

### contentLoader

protected ContentLoader contentLoader
Content loader

### contentLoaderAutoCommit

protected ContentLoader contentLoaderAutoCommit
Content loader with auto commit after loading content

### contentBuilder

protected ContentBuilder contentBuilder
Content builder

### resourceBuilder

protected org.apache.sling.resourcebuilder.api.ResourceBuilder resourceBuilder
Resource builder

### uniqueRoot

protected UniqueRoot uniqueRoot
Unique root helper

## Constructor Details

### SlingContextImpl

public SlingContextImpl()

## Method Details

### setResourceResolverType

protected void setResourceResolverType(@Nullable @Nullable ResourceResolverType resourceResolverType)

Parameters:
  `resourceResolverType` - Resource resolver type

### setResourceResolverFactoryActivatorProps

protected void setResourceResolverFactoryActivatorProps(@Nullable @Nullable Map<String,Object> props)

Parameters:
  `props` - Resolver resolver factory activator properties

### setRegisterSlingModelsFromClassPath

protected void setRegisterSlingModelsFromClassPath(boolean registerSlingModelsFromClassPath)

Parameters:
  `registerSlingModelsFromClassPath` - Register sling models from classpath

### setUp

protected void setUp()
Setup actions before test method execution

Overrides:
  `setUp` in class `org.apache.sling.testing.mock.osgi.context.OsgiContextImpl`

### newResourceResolverFactory

@NotNull protected @NotNull org.apache.sling.api.resource.ResourceResolverFactory newResourceResolverFactory()
Initialize mocked resource resolver factory.

Returns:
  Resource resolver factory

### registerDefaultServices

protected void registerDefaultServices()
Default services that should be available for every unit test

### tearDown

protected void tearDown()
Teardown actions after test method execution

Overrides:
  `tearDown` in class `org.apache.sling.testing.mock.osgi.context.OsgiContextImpl`

### resourceResolverType

@NotNull public final @NotNull ResourceResolverType resourceResolverType()

Returns:
  Resource resolver type

### resourceResolver

@NotNull public final @NotNull org.apache.sling.api.resource.ResourceResolver resourceResolver()
Returns the singleton resource resolver bound to this context. It is automatically closed after the test.

Returns:
  Resource resolver

### request

@Deprecated(since="4.1.0") @NotNull public final MockSlingHttpServletRequest request()
Deprecated.
Use `jakartaRequest()` instead.

Returns:
  Sling request

### jakartaRequest

@NotNull public final @NotNull MockSlingJakartaHttpServletRequest jakartaRequest()

Returns:
  Sling request

### resolveSlingBindingProperty

@Deprecated(since="4.0.0") @Nullable protected @Nullable Object resolveSlingBindingProperty(@NotNull @NotNull String property, @NotNull org.apache.sling.api.SlingHttpServletRequest request)
Deprecated.
use `resolveSlingBindingProperty(String, SlingJakartaHttpServletRequest)` instead
Dynamically resolve property request for current request `SlingBindings`.

Parameters:
  `property` - Property key
  `request` - Context request
Returns:
  Resolved object or null if no result found

### resolveSlingBindingProperty

@Nullable protected @Nullable Object resolveSlingBindingProperty(@NotNull @NotNull String property, @NotNull @NotNull org.apache.sling.api.SlingJakartaHttpServletRequest request)
Dynamically resolve property request for current request `SlingBindings`.

Parameters:
  `property` - Property key
  `request` - Context request
Returns:
  Resolved object or null if no result found

### resolveSlingBindingProperty

@Deprecated @Nullable protected @Nullable Object resolveSlingBindingProperty(@NotNull @NotNull String property)
Deprecated.
Please use `resolveSlingBindingProperty(String, SlingJakartaHttpServletRequest)`
Dynamically resolve property request for current request `SlingBindings`.

Parameters:
  `property` - Property key
Returns:
  Resolved object or null if no result found

### requestPathInfo

@NotNull public final @NotNull MockRequestPathInfo requestPathInfo()

Returns:
  Request path info

### response

@Deprecated(since="4.0.0") @NotNull public final MockSlingHttpServletResponse response()
Deprecated.
Use `jakartaResponse()` instead.

Returns:
  Sling response

### jakartaResponse

@NotNull public final @NotNull MockSlingJakartaHttpServletResponse jakartaResponse()

Returns:
  Sling response

### slingScriptHelper

@NotNull public final @NotNull org.apache.sling.api.scripting.SlingScriptHelper slingScriptHelper()

Returns:
  Sling script helper

### load

@NotNull public @NotNull ContentLoader load()

Returns:
  Content loader

### load

@NotNull public @NotNull ContentLoader load(boolean autoCommit)

Parameters:
  `autoCommit` - Automatically commit changes after loading content (default: true)
Returns:
  Content loader

### create

@NotNull public @NotNull ContentBuilder create()
Creates a `ContentBuilder` object for easily creating test content. This API was part of Sling Mocks since version 1.x. You can use alternatively the `build()` method and use the `ResourceBuilder` API.

Returns:
  Content builder for building test content

### build

@NotNull public @NotNull org.apache.sling.resourcebuilder.api.ResourceBuilder build()
Creates a `ResourceBuilder` object for easily creating test content. This is a separate API which can be used inside sling mocks or in a running instance. You can use alternatively the `create()` method to use the `ContentBuilder` API.

Returns:
  Resource builder for building test content.

### currentResource

@Nullable public final @Nullable org.apache.sling.api.resource.Resource currentResource()

Returns:
  Current resource

### currentResource

@Nullable public final @Nullable org.apache.sling.api.resource.Resource currentResource(@Nullable @Nullable String resourcePath)
Set current resource in request.

Parameters:
  `resourcePath` - Resource path
Returns:
  Current resource

### currentResource

@Nullable public @Nullable org.apache.sling.api.resource.Resource currentResource(@Nullable @Nullable org.apache.sling.api.resource.Resource resource)
Set current resource in request.

Parameters:
  `resource` - Resource
Returns:
  Current resource

### addModelsForPackage

public final void addModelsForPackage(@NotNull @NotNull String packageName)
Search classpath for given java package names (and sub packages) to scan for and register all classes with @Model annotation.

Parameters:
  `packageName` - Java package name

### addModelsForPackage

public final void addModelsForPackage(@NotNull @NotNull String @NotNull ... packageNames)
Search classpath for given java package names (and sub packages) to scan for and register all classes with @Model annotation.

Parameters:
  `packageNames` - Java package names

### addModelsForClasses

public final void addModelsForClasses(@NotNull @NotNull String @NotNull ... classNames)
Search classpath for given class names to scan for and register all classes with @Model annotation.

Parameters:
  `classNames` - Java class names

### addModelsForClasses

public final void addModelsForClasses(@NotNull @NotNull Class<?> @NotNull ... classes)
Search classpath for given class names to scan for and register all classes with @Model annotation.

Parameters:
  `classes` - Java classes

### runMode

public final void runMode(@NotNull @NotNull String @NotNull ... runModes)
Set current run mode(s).

Parameters:
  `runModes` - Run mode(s).

### uniqueRoot

@NotNull public @NotNull UniqueRoot uniqueRoot()
Create unique root paths for unit tests (and clean them up after the test run automatically).

Returns:
  Unique root path helper

### registerAdapter

public final <T1, T2> void registerAdapter(@NotNull @NotNull Class<T1> adaptableClass, @NotNull @NotNull Class<T2> adapterClass, @NotNull T2 adapter)
Create a Sling AdapterFactory on the fly which can adapt from `adaptableClass` to `adapterClass` and just returns the given value as result.

Type Parameters:
  `T1` - Adaptable type
  `T2` - Adapter type
Parameters:
  `adaptableClass` - Class to adapt from
  `adapterClass` - Class to adapt to
  `adapter` - Object which is always returned for this adaption.

### registerAdapter

public final <T1, T2> void registerAdapter(@NotNull @NotNull Class<T1> adaptableClass, @NotNull @NotNull Class<T2> adapterClass, @NotNull @NotNull Function<T1,T2> adaptHandler)
Create a Sling AdapterFactory on the fly which can adapt from `adaptableClass` to `adapterClass` and delegates the adapter mapping to the given `adaptHandler` function.

Type Parameters:
  `T1` - Adaptable type
  `T2` - Adapter type
Parameters:
  `adaptableClass` - Class to adapt from
  `adapterClass` - Class to adapt to
  `adaptHandler` - Function to handle the adaption