SLING-5040 - new TeleporterRule and teleporter client module
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1703893 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/junit/rules/ServerSideTeleporter.java b/src/main/java/org/apache/sling/junit/rules/ServerSideTeleporter.java
new file mode 100644
index 0000000..2d17f6f
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/rules/ServerSideTeleporter.java
@@ -0,0 +1,56 @@
+/*
+ * 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.junit.rules;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.junit.Activator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+/** Server-side variant of the TeleporterRule, which provides
+ * access to OSGi services for convenience, but does not do
+ * much more.
+ */
+class ServerSideTeleporter extends TeleporterRule {
+ private final List<ServiceReference> toUnget = new ArrayList<ServiceReference>();
+ private final BundleContext bundleContext;
+
+ ServerSideTeleporter() {
+ bundleContext = Activator.getBundleContext();
+ if(bundleContext == null) {
+ throw new IllegalStateException("Null BundleContext, should not happen when this class is used");
+ }
+ }
+
+ @Override
+ protected void after() {
+ super.after();
+ for(ServiceReference r : toUnget) {
+ if(r != null) {
+ bundleContext.ungetService(r);
+ }
+ }
+ }
+
+ public <T> T getService (Class<T> serviceClass, String ldapFilter) {
+ final ServiceGetter sg = new ServiceGetter(bundleContext, serviceClass, ldapFilter);
+ toUnget.add(sg.serviceReference);
+ return serviceClass.cast(sg.service);
+ }
+}
diff --git a/src/main/java/org/apache/sling/junit/rules/Service.java b/src/main/java/org/apache/sling/junit/rules/Service.java
index a3a8cd4..bc2f414 100644
--- a/src/main/java/org/apache/sling/junit/rules/Service.java
+++ b/src/main/java/org/apache/sling/junit/rules/Service.java
@@ -22,7 +22,6 @@
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
/**
* Allows a test class to obtain a reference to an OSGi service. This rule embodies the logic to get a bundle context,
@@ -55,32 +54,15 @@
return;
}
- ServiceReference serviceReference = null;
- if(serviceClass.equals(BundleContext.class)) {
- // Special case to provide the BundleContext to tests
- Service.this.service = bundleContext;
- } else {
- serviceReference = bundleContext.getServiceReference(serviceClass.getName());
-
- if (serviceReference == null) {
- throw new IllegalStateException("unable to get a service reference");
- }
-
- final Object service = bundleContext.getService(serviceReference);
-
- if (service == null) {
- throw new IllegalStateException("unable to get an instance of the service");
- }
-
- Service.this.service = serviceClass.cast(service);
- }
+ final ServiceGetter sg = new ServiceGetter(bundleContext, serviceClass, null);
+ Service.this.service = serviceClass.cast(sg.service);
try {
base.evaluate();
} finally {
Service.this.service = null;
- if(serviceReference != null) {
- bundleContext.ungetService(serviceReference);
+ if(sg.serviceReference != null) {
+ bundleContext.ungetService(sg.serviceReference);
}
}
}
diff --git a/src/main/java/org/apache/sling/junit/rules/ServiceGetter.java b/src/main/java/org/apache/sling/junit/rules/ServiceGetter.java
new file mode 100644
index 0000000..c98436c
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/rules/ServiceGetter.java
@@ -0,0 +1,60 @@
+/*
+ * 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.junit.rules;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+/** Implements the logic used to get a service */
+class ServiceGetter {
+ final ServiceReference serviceReference;
+ final Object service;
+
+ ServiceGetter(BundleContext bundleContext, Class<?> serviceClass, String ldapFilter) {
+ Object s;
+
+ if(ldapFilter != null && !ldapFilter.isEmpty()) {
+ throw new UnsupportedOperationException("LDAP service filter is not supported so far");
+ }
+
+ if (serviceClass.equals(BundleContext.class)) {
+ // Special case to provide the BundleContext to tests
+ s = serviceClass.cast(bundleContext);
+ serviceReference = null;
+ } else {
+ serviceReference = bundleContext.getServiceReference(serviceClass
+ .getName());
+
+ if (serviceReference == null) {
+ throw new IllegalStateException(
+ "unable to get a service reference");
+ }
+
+ final Object service = bundleContext.getService(serviceReference);
+
+ if (service == null) {
+ throw new IllegalStateException(
+ "unable to get an instance of the service");
+ }
+
+ s = serviceClass.cast(service);
+ }
+
+ service = s;
+ }
+}
diff --git a/src/main/java/org/apache/sling/junit/rules/TeleporterRule.java b/src/main/java/org/apache/sling/junit/rules/TeleporterRule.java
new file mode 100644
index 0000000..ced961c
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/rules/TeleporterRule.java
@@ -0,0 +1,85 @@
+/*
+ * 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.junit.rules;
+
+import org.apache.sling.junit.Activator;
+import org.junit.rules.ExternalResource;
+
+/** JUnit Rule used to teleport a server-side test to a Sling instance
+ * to execute it there. See the launchpad/integration-tests module for
+ * usage examples (coming soon).
+ * A concrete TeleporterRule class is selected to match the different required
+ * behaviors of the server-side and client-side variants of this rule.
+ * The junit.core module only contains the server-side code, to minimize
+ * its dependencies, and the client-side part is in the sling testing.teleporter
+ * module.
+ */
+public abstract class TeleporterRule extends ExternalResource {
+ protected Class<?> classUnderTest;
+
+ /** Name of the implementation class to use by default when running on the client side */
+ public static final String DEFAULT_CLIENT_CLASS = "org.apache.sling.testing.teleporter.client.ClientSideTeleporter";
+
+ protected TeleporterRule() {
+ }
+
+ private void setClassUnderTest(Class<?> c) {
+ this.classUnderTest = c;
+ }
+
+ /** Build a TeleporterRule for the given class, using the default
+ * client class name.
+ */
+ public static TeleporterRule forClass(Class <?> classUnderTest) {
+ return forClass(classUnderTest, null);
+ }
+
+ /** Build a TeleporterRule for the given class, optionally using
+ * a specific client class.
+ */
+ public static TeleporterRule forClass(Class <?> classUnderTest, String clientTeleporterClassName) {
+ TeleporterRule result = null;
+
+ if(Activator.getBundleContext() != null) {
+ // We're running on the server side
+ result = new ServerSideTeleporter();
+ } else {
+ // Client-side. Instantiate the class dynamically to
+ // avoid bringing its dependencies into this module when
+ // it's running on the server side
+ final String clientClass = clientTeleporterClassName == null ? DEFAULT_CLIENT_CLASS : clientTeleporterClassName;
+ try {
+ result = (TeleporterRule)(TeleporterRule.class.getClassLoader().loadClass(clientClass).newInstance());
+ } catch(Exception e) {
+ throw new RuntimeException("Unable to instantiate Teleporter client " + clientClass, e);
+ }
+ }
+
+ result.setClassUnderTest(classUnderTest);
+ return result;
+ }
+
+ /** If running on the server side, get an OSGi service */
+ public final <T> T getService (Class<T> serviceClass) {
+ return getService(serviceClass, null);
+ }
+
+ /** If running on the server side, get an OSGi service specified by an LDAP service filter */
+ public <T> T getService (Class<T> serviceClass, String ldapFilter) {
+ throw new UnsupportedOperationException("This TeleporterRule does not implement getService()");
+ }
+}