You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2013/10/31 16:38:36 UTC
svn commit: r1537534 [1/2] - in /sling/trunk/launchpad:
integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/
integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/util/
integration-tests/src/mai...
Author: bdelacretaz
Date: Thu Oct 31 15:38:36 2013
New Revision: 1537534
URL: http://svn.apache.org/r1537534
Log:
SLING-3219 - refactor ResourceResolverTest as a real OSGi test, and use events instead of sleep for sync
Added:
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/EventsCounterTest.java
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/ResourceResolverProxyTest.java
sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/
sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/EventsCounter.java
sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/EventsCounterImpl.java
sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/serversidetests/ResourceResolverTest.java
Removed:
sling/trunk/launchpad/integration-tests/src/main/resources/scripts/sling-it/resourceresolver-integration.jsp
sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/exported/ResourceResolverTest.java
Modified:
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/util/ServerSideTestClient.java
Added: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/EventsCounterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/EventsCounterTest.java?rev=1537534&view=auto
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/EventsCounterTest.java (added)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/EventsCounterTest.java Thu Oct 31 15:38:36 2013
@@ -0,0 +1,66 @@
+/*
+ * 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.launchpad.webapp.integrationtest;
+
+import static org.junit.Assert.assertTrue;
+import org.apache.sling.commons.json.JSONObject;
+import org.apache.sling.commons.testing.integration.HttpTest;
+import org.apache.sling.commons.testing.junit.Retry;
+import org.apache.sling.commons.testing.junit.RetryRule;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+
+/** Test the EventsCounter servlet and underlying events subsystems */
+public class EventsCounterTest {
+
+ private static int initialResourceEventsCount;
+ private static String toDelete;
+ public static final String TOPIC = "org/apache/sling/api/resource/Resource/ADDED";
+
+ @Rule
+ public RetryRule retryRule = new RetryRule();
+
+ /** HTTP tests helper */
+ private static final HttpTest H = new HttpTest();
+
+ private static int getResourceEventsCount() throws Exception {
+ final JSONObject json = new JSONObject(H.getContent(HttpTest.HTTP_BASE_URL + "/testing/EventsCounter.json", HttpTest.CONTENT_TYPE_JSON));
+ return json.has(TOPIC) ? json.getInt(TOPIC) : 0;
+ }
+
+ @BeforeClass
+ public static void setupClass() throws Exception {
+ H.setUp();
+ initialResourceEventsCount = getResourceEventsCount();
+ final String testResourcePath = HttpTest.HTTP_BASE_URL + "/testing/" + EventsCounterTest.class.getName() + "." + System.currentTimeMillis();
+ toDelete = H.getTestClient().createNode(testResourcePath, null);
+ }
+
+ @AfterClass
+ public static void cleanup() throws Exception {
+ H.getTestClient().delete(toDelete);
+ H.tearDown();
+ }
+
+ @Retry
+ @Test
+ public void testResourceEvents() throws Exception {
+ assertTrue("Expecting events counter to have changed", getResourceEventsCount() > initialResourceEventsCount);
+ }
+}
Added: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/ResourceResolverProxyTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/ResourceResolverProxyTest.java?rev=1537534&view=auto
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/ResourceResolverProxyTest.java (added)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/ResourceResolverProxyTest.java Thu Oct 31 15:38:36 2013
@@ -0,0 +1,28 @@
+/*
+ * 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.launchpad.webapp.integrationtest;
+
+import org.apache.sling.launchpad.webapp.integrationtest.util.ServerSideTestClient;
+import org.junit.Test;
+
+/** Run the server-side ResourceResolverTest */
+public class ResourceResolverProxyTest {
+ @Test
+ public void runWriteableResourcesTest() throws Exception {
+ new ServerSideTestClient().assertTestsPass("org.apache.sling.launchpad.testservices.serversidetests.ResourceResolverTest", -50);
+ }
+}
Modified: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/util/ServerSideTestClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/util/ServerSideTestClient.java?rev=1537534&r1=1537533&r2=1537534&view=diff
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/util/ServerSideTestClient.java (original)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/util/ServerSideTestClient.java Thu Oct 31 15:38:36 2013
@@ -18,6 +18,7 @@
*/
package org.apache.sling.launchpad.webapp.integrationtest.util;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@@ -90,9 +91,20 @@ public class ServerSideTestClient extend
return r;
}
+ /** Run server-side test(s)
+ * @param testPackageOrClassName selects which tests to run
+ * @param expectedTestsCount Use a negative -N value to mean "at least N tests"
+ * @throws Exception
+ */
public void assertTestsPass(String testPackageOrClassName, int expectedTestsCount) throws Exception {
TestResults results = runTests(testPackageOrClassName);
- assertEquals("Expecting " + expectedTestsCount + " test(s) for " + testPackageOrClassName, expectedTestsCount, results.getTestCount());
+ if(expectedTestsCount < 0) {
+ assertTrue("Expecting at least " + -expectedTestsCount + " test(s) for " + testPackageOrClassName,
+ results.getTestCount() >= -expectedTestsCount);
+ } else {
+ assertEquals("Expecting " + expectedTestsCount + " test(s) for " + testPackageOrClassName,
+ expectedTestsCount, results.getTestCount());
+ }
if(!results.getFailures().isEmpty()) {
fail(results.getFailures().size() + " tests failed:" + results.getFailures());
}
Added: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/EventsCounter.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/EventsCounter.java?rev=1537534&view=auto
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/EventsCounter.java (added)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/EventsCounter.java Thu Oct 31 15:38:36 2013
@@ -0,0 +1,22 @@
+/*
+ * 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.launchpad.testservices.events;
+
+/** Provide access to events counts */
+public interface EventsCounter {
+ int getEventsCount(String topic);
+}
\ No newline at end of file
Added: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/EventsCounterImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/EventsCounterImpl.java?rev=1537534&view=auto
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/EventsCounterImpl.java (added)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/events/EventsCounterImpl.java Thu Oct 31 15:38:36 2013
@@ -0,0 +1,96 @@
+/*
+ * 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.launchpad.testservices.events;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.io.JSONWriter;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Count the number of OSGi events that we receive on specific topics, and
+ * report them to clients.
+ */
+@SuppressWarnings("serial")
+@Component(immediate=true, metatype=false)
+@Service
+@Properties({
+ @Property(name="service.description", value="Paths Test Servlet"),
+ @Property(name="service.vendor", value="The Apache Software Foundation"),
+ @Property(name="sling.servlet.paths", value="/testing/EventsCounter"),
+ @Property(name="sling.servlet.extensions", value="json"),
+ @Property(
+ name=org.osgi.service.event.EventConstants.EVENT_TOPIC,
+ value= {
+ org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED,
+ "org/apache/sling/api/resource/ResourceResolverMapping/CHANGED"
+ })
+})
+public class EventsCounterImpl extends SlingSafeMethodsServlet implements EventHandler,EventsCounter {
+
+ private final Map<String, AtomicInteger> counters = new HashMap<String, AtomicInteger>();
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ public synchronized void handleEvent(Event event) {
+ final String topic = event.getTopic();
+ AtomicInteger counter = counters.get(topic);
+ if(counter == null) {
+ counter = new AtomicInteger();
+ counters.put(topic, counter);
+ }
+ counter.incrementAndGet();
+ log.debug("{} counter is now {}", topic, counter.get());
+ }
+
+ public int getEventsCount(String topic) {
+ final AtomicInteger counter = counters.get(topic);
+ return counter == null ? 0 : counter.get();
+ }
+
+ @Override
+ protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response)
+ throws ServletException, IOException {
+ response.setContentType("application/json");
+ response.setCharacterEncoding("UTF-8");
+ try {
+ final JSONWriter w = new JSONWriter(response.getWriter());
+ w.setTidy(true);
+ w.object();
+ for(Map.Entry<String, AtomicInteger> entry : counters.entrySet()) {
+ w.key(entry.getKey()).value(entry.getValue());
+ }
+ w.endObject();
+ } catch(JSONException je) {
+ throw (IOException)new IOException("JSONException in doGet").initCause(je);
+ }
+ }
+}
\ No newline at end of file