SLING-3010 integrate patch from Anjan. Added integration tests to guard against future regressions.

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