SLING-3196 and SLING-3022 - back to the code of revision 1487419, with improved test coverage

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1534587 13f79535-47bb-0310-9956-ffa450edef68