Can't test a Sling Model which is Adaptable from ContentPolicy due to different MockContentPolicy adaptTo behaviour

Description

Given the following Sling Model annotation:

you can't use the MockContentPolicy to adapt it to your own Sling Model implementation.

The above code won't work, because there is a difference in implementation behaviour between the ContentPolicy from Adobe, which extends SlingAdaptble and the MockContentPolicy which extends a MockLabeledResource, which extends ResourceWrapper.

The problem is that both call super.adaptTo(type), when the Class<AdapterType> is not a Resource or LabeledResource. But the MockLabeledResource calls in the end the ResourceWrapper its adaptTo function, which removes the original type information and uses the Resource object instead of the original ContentPolicy for executing the adaptTo method. See https://github.com/apache/sling-org-apache-sling-api/blob/master/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java#L181

I think we don't need the ResourceWrapper methods and we can just implement everything in the MockContentPolicy and let it extend SlingAdaptable, so the normal adaption behaviour will be the same as the implementation from Adobe.

Activity

Show:

Stefan Seifert August 26, 2021 at 10:19 AM

lgtm, thanks!

Stefan Seifert August 24, 2021 at 1:10 PM

definitely a good idea, will look at it

Robin Brouns August 24, 2021 at 12:11 PM

Created a pull request to let MockContentPolicy extend SlingAdaptable.

What do you think about this Stefan Seifert?

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Created August 20, 2021 at 12:43 PM
Updated August 26, 2021 at 10:19 AM
Resolved August 26, 2021 at 10:19 AM