blob: ced961cbdc566bbddd43b08c8033bf7697d90f47 [file] [log] [blame]
/*
* 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()");
}
}