Class 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 Detail

      • resourceResolverFactory

        protected org.apache.sling.api.resource.ResourceResolverFactory resourceResolverFactory
      • resourceResolver

        protected org.apache.sling.api.resource.ResourceResolver resourceResolver
      • slingScriptHelper

        protected org.apache.sling.api.scripting.SlingScriptHelper slingScriptHelper
      • contentLoaderAutoCommit

        protected ContentLoader contentLoaderAutoCommit
      • resourceBuilder

        protected org.apache.sling.resourcebuilder.api.ResourceBuilder resourceBuilder
    • Constructor Detail

      • SlingContextImpl

        public SlingContextImpl()
    • Method Detail

      • setResourceResolverType

        protected void setResourceResolverType​(@Nullable
                                               @Nullable ResourceResolverType resourceResolverType)
        Parameters:
        resourceResolverType - Resource resolver type
      • setResourceResolverFactoryActivatorProps

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

        protected void setRegisterSlingModelsFromClassPath​(boolean registerSlingModelsFromClassPath)
      • 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
      • resolveSlingBindingProperty

        @Nullable
        protected @Nullable Object resolveSlingBindingProperty​(@NotNull
                                                               @NotNull String property,
                                                               @NotNull
                                                               @NotNull org.apache.sling.api.SlingHttpServletRequest 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
      • requestPathInfo

        @NotNull
        public final @NotNull MockRequestPathInfo requestPathInfo()
        Returns:
        Request path info
      • 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