SLING-9727 sling-mock: Make compatible with o.a.s.resourceresolver 1.7.0
diff --git a/core/src/main/java/org/apache/sling/testing/mock/sling/ResourceResolverFactoryInitializer.java b/core/src/main/java/org/apache/sling/testing/mock/sling/ResourceResolverFactoryInitializer.java
index b1d7ea5..48f0d5a 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/sling/ResourceResolverFactoryInitializer.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/sling/ResourceResolverFactoryInitializer.java
@@ -48,7 +48,7 @@
* Initializes Sling Resource Resolver factories with JCR-resource mapping.
*/
class ResourceResolverFactoryInitializer {
-
+
private ResourceResolverFactoryInitializer() {
// static methods only
}
@@ -72,7 +72,7 @@
ensureJcrResourceProviderDependencies(bundleContext);
initializeJcrResourceProvider(bundleContext);
}
-
+
// initialize resource resolver factory activator
ensureResourceResolverFactoryActivatorDependencies(bundleContext);
initializeResourceResolverFactoryActivator(bundleContext);
@@ -83,7 +83,7 @@
}
return (ResourceResolverFactory)bundleContext.getService(factoryRef);
}
-
+
/**
* Ensure dependencies for JcrResourceProvider are present.
* @param bundleContext Bundle context
@@ -125,7 +125,7 @@
MockOsgi.activate(provider, bundleContext, config);
bundleContext.registerService(ResourceProvider.class, provider, config);
}
-
+
/**
* Ensure dependencies for ResourceResolverFactoryActivator are present.
* @param bundleContext Bundle context
@@ -137,6 +137,9 @@
registerServiceIfNotPresent(bundleContext, ResourceAccessSecurityTracker.class, new ResourceAccessSecurityTracker());
registerServiceIfNotPresent(bundleContext, EventAdmin.class, new MockEventAdmin());
+ // dependency required since resourceresolver 1.7.0
+ registerServiceIfNotPresentByName(bundleContext, "org.apache.sling.resourceresolver.impl.mapping.StringInterpolationProvider",
+ "org.apache.sling.resourceresolver.impl.mapping.StringInterpolationProviderImpl");
}
/**
@@ -153,7 +156,19 @@
MockOsgi.activate(activator, bundleContext, config);
bundleContext.registerService(ResourceResolverFactoryActivator.class.getName(), activator, config);
}
-
+
+ @SuppressWarnings({ "unchecked", "null" })
+ private static void registerServiceIfNotPresentByName(@NotNull BundleContext bundleContext,
+ @NotNull String interfaceClassName, @NotNull String implClassName) {
+ try {
+ Class<?> interfaceClass = Class.forName(interfaceClassName);
+ Class<?> implClass = Class.forName(implClassName);
+ registerServiceIfNotPresent(bundleContext, (Class)interfaceClass, implClass.newInstance());
+ } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
+ // ignore - probably not the latest sling models impl version
+ }
+ }
+
/**
* Registers a service if the service class is found in classpath,
* and if no service with this class is already registered.
@@ -165,7 +180,7 @@
@NotNull T instance) {
registerServiceIfNotPresent(bundleContext, serviceClass, instance, new Hashtable<String, Object>());
}
-
+
/**
* Registers a service if the service class is found in classpath,
* and if no service with this class is already registered.
@@ -182,7 +197,7 @@
bundleContext.registerService(serviceClass, instance, config);
}
}
-
+
/**
* Registers all JCR node types found in classpath.
* @param slingRepository Sling repository
@@ -204,5 +219,5 @@
}
}
}
-
+
}