Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
Testing Sling Mock 2.2.18
-
Patch
Description
If we have an AdapterFactory and a Service registered by the following code:
// code placeholder SlingContext context = new SlingContext(); context.registerInjectActivateService(new TestAdapterFactory()); context.registerInjectActivateService(new TestService());
it turns out that adapting a Resource to another Class (defined by the TestAdapterFactory) returns null when done in the deactivate method of the TestService, i.e.
resource.adaptTo(TestClass.class) // returns null in the deactivate method. resource.adaptTo(TestClass.class) // works correctly in any other method.
Reason:
It looks like the registered adapters are cleaned up before registered services are deactivated in the tearDown method of SlingContextImpl.
// SlingContextImpl void tearDown() { ... MockSling.clearAdapterManagerBundleContext(); ... super.tearDown(); // calls the deactivate of the registered services. }
Cleaning up the registered adapters after deactivating the services seems to solve the issue.
// SlingContextImpl void tearDown() { ... super.tearDown(); MockSling.clearAdapterManagerBundleContext(); }
but I'm not sure about any other potential impacts of this change (attached patch).