private Resource createResource()

in core/src/main/java/org/apache/sling/testing/mock/sling/loader/LoaderContentHandler.java [75:114]


    private Resource createResource(@NotNull Resource parentResource, @NotNull String childName, @Nullable Map<String,Object> content) throws PersistenceException {

        // collect all properties first
        boolean hasJcrData = false;
        String referencedNodePath = null;
        Map<String, Object> props = new HashMap<String, Object>();
        if (content != null) {
            for (Map.Entry<String,Object> entry : content.entrySet()) {
                final String name = entry.getKey();
                if (StringUtils.equals(name, JCR_DATA_PLACEHOLDER)) {
                    hasJcrData = true;
                }
                else if (StringUtils.equals(name, JCR_REFERENCE_PLACEHOLDER)) {
                    referencedNodePath = (String) entry.getValue();
                }
                else {
                    props.put(name, entry.getValue());
                }
            }
        }

        // create resource
        Resource resource = resourceResolver.create(parentResource, childName, props);

        ModifiableValueMap valueMap = resource.adaptTo(ModifiableValueMap.class);
        if (valueMap != null) {
            if (hasJcrData) {
                // we cannot import binary data here - but to avoid complaints by JCR we create it with empty binary data
                valueMap.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(new byte[0]));
            } else if (StringUtils.isNotBlank(referencedNodePath)) {
                // target reference has to be specified relative to linking node, as tests may apply a dynamic root directory
                Resource referencedNodeResource = resourceResolver.getResource(resource.getPath() + "/" + referencedNodePath);
                if (referencedNodeResource != null) {
                    valueMap.put(JcrConstants.JCR_CONTENT, referencedNodeResource.adaptTo(Node.class));
                }
            }
        }

        return resource;
    }