SLING-8936: sync with master, almost: except sling.api SNAPSHOT which breaks my new ITs
diff --git a/pom.xml b/pom.xml
index 7450c5a..675d330 100644
--- a/pom.xml
+++ b/pom.xml
@@ -44,6 +44,8 @@
 
     <properties>
         <site.jira.version.id>12314292</site.jira.version.id>
+        <!-- To debug the pax process, override this with -D -->
+        <pax.vm.options>-Xmx256M -XX:MaxPermSize=256m</pax.vm.options>
     </properties>
 
     <build>
@@ -80,6 +82,40 @@
                     </execution>
                 </executions>
             </plugin>
+            <plugin>
+                <groupId>org.apache.servicemix.tooling</groupId>
+                <artifactId>depends-maven-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-failsafe-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>integration-test</id>
+                        <phase>integration-test</phase>
+                        <goals>
+                            <goal>integration-test</goal>
+                        </goals>
+                    </execution>
+                    <execution>
+                        <id>verify</id>
+                        <phase>integration-test</phase>
+                        <goals>
+                            <goal>verify</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <argLine>${pax.vm.options}</argLine>
+                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
+                    <systemProperties>
+                        <property>
+                        <name>bundle.filename</name>
+                        <value>${basedir}/target/${project.build.finalName}.jar</value>
+                        </property>
+                    </systemProperties>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
     <dependencies>
@@ -108,7 +144,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.22.0-SNAPSHOT</version>
+            <version>2.20.0</version>
             <scope>provided</scope>
         </dependency>
         <!-- for ServiceUserMapped (SLING-4312) -->
@@ -195,5 +231,59 @@
             <version>16.0.2</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam</artifactId>
+            <version>4.13.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-cm</artifactId>
+            <version>4.13.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-container-forked</artifactId>
+            <version>4.13.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-junit4</artifactId>
+            <version>4.13.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-link-mvn</artifactId>
+            <version>4.13.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.url</groupId>
+            <artifactId>pax-url-wrap</artifactId>
+            <version>2.6.2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.paxexam</artifactId>
+            <version>3.0.0</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.framework</artifactId>
+            <version>6.0.3</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.servlet-helpers</artifactId>
+            <version>1.3.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/src/test/java/org/apache/sling/servlets/resolver/it/ServletResolverTestSupport.java b/src/test/java/org/apache/sling/servlets/resolver/it/ServletResolverTestSupport.java
new file mode 100644
index 0000000..39a4fea
--- /dev/null
+++ b/src/test/java/org/apache/sling/servlets/resolver/it/ServletResolverTestSupport.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.servlets.resolver.it;
+
+import org.apache.sling.testing.paxexam.TestSupport;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
+import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.newConfiguration;
+import static org.apache.sling.testing.paxexam.SlingOptions.slingQuickstartOakTar;
+
+public class ServletResolverTestSupport extends TestSupport {
+    protected final int httpPort = findFreePort();
+
+    @Configuration
+    public Option[] configuration() {
+        return new Option[]{
+            baseConfiguration(),
+            slingQuickstartOakTar(workingDirectory(), httpPort),
+            testBundle("bundle.filename"),
+            wrappedBundle(mavenBundle().groupId("org.apache.sling").artifactId("org.apache.sling.servlet-helpers").versionAsInProject()),
+            junitBundles(),
+            newConfiguration("org.apache.sling.jcr.base.internal.LoginAdminWhitelist")
+                .put("whitelist.bundles.regexp", "^PAXEXAM.*$")
+                .asOption(),
+        };
+    }
+}
diff --git a/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java b/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java
new file mode 100644
index 0000000..20be2d0
--- /dev/null
+++ b/src/test/java/org/apache/sling/servlets/resolver/it/ServletSelectionIT.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.servlets.resolver.it;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.servlethelpers.MockSlingHttpServletRequest;
+import org.apache.sling.servlethelpers.MockSlingHttpServletResponse;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class ServletSelectionIT extends ServletResolverTestSupport {
+
+    private final static int STARTUP_WAIT_SECONDS = 30;
+
+    @Inject
+    private BundleContext bundleContext;
+
+    @Inject
+    private ResourceResolverFactory resourceResolverFactory;
+
+    private MockSlingHttpServletResponse executeRequest(String path, int expectedStatus) throws Exception {
+        final ResourceResolver resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
+        assertNotNull("Expecting ResourceResolver", resourceResolver);
+        final MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(resourceResolver);
+        request.setPathInfo(path);
+        final MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
+
+        // Get SlingRequestProcessor.processRequest method and execute request
+        // This module depends on an older version of the sling.engine module and I don't want
+        // to change it just for these tests, so using reflection to get the processor, as we're
+        // running with a more recent version of sling.engine in the pax exam environment
+        final String slingRequestProcessorClassName = "org.apache.sling.engine.SlingRequestProcessor";
+        final ServiceReference<?> ref = bundleContext.getServiceReference(slingRequestProcessorClassName);
+        assertNotNull("Expecting service:" + slingRequestProcessorClassName, ref);
+
+        final Object processor = bundleContext.getService(ref);
+        try {
+            // void processRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse resource, ResourceResolver resourceResolver)
+            final Method processMethod = processor.getClass().getMethod(
+                "processRequest", 
+                HttpServletRequest.class, HttpServletResponse.class, ResourceResolver.class);
+            assertNotNull("Expecting processRequest method", processMethod);
+            processMethod.invoke(processor, request, response, resourceResolver);
+        } finally {
+            bundleContext.ungetService(ref);
+        }
+
+        if(expectedStatus > 0) {
+            assertEquals("Expected status " + expectedStatus + " at " + path, expectedStatus, response.getStatus());
+        }
+
+        return response;
+    }
+
+    @Before
+    public void waitForStableSling() throws Exception {
+        final int expectedStatus = 200;
+        final List<Integer> statuses = new ArrayList<>();
+        final String path = "/.json";
+        final long endTime = System.currentTimeMillis() + STARTUP_WAIT_SECONDS * 1000;
+        while(System.currentTimeMillis() < endTime) {
+            final int status = executeRequest(path, -1).getStatus();
+            statuses.add(status);
+            if(status == expectedStatus) {
+                return;
+            }
+            Thread.sleep(250);
+        }
+        fail("Did not get a 200 status at " + path + " got " + statuses);
+    }
+
+    @Test
+    public void testDefaultJsonServlet() throws Exception {
+        final MockSlingHttpServletResponse response = executeRequest("/.json", 200);
+        final String content = response.getOutputAsString();
+        final String [] expected = {
+            "jcr:primaryType\":\"rep:root",
+            "jcr:mixinTypes\":[\"rep:AccessControllable\"]"
+        };
+        for(String s : expected) {
+            assertTrue("Expecting in output: " + s + ", got " + content, content.contains(s));
+        }
+
+    }
+
+}
\ No newline at end of file