You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:20:53 UTC
[sling-org-apache-sling-testing-osgi-mock] 05/10: SLING-5327
refactor and unify lifecycle method handling (and fix missing argument
variants for modified methods - should support the same as the activate
method)
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.testing.osgi-mock-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit eb580c37ebd52ad0f540f7e9928d8e279eda7cd0
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Nov 24 11:00:48 2015 +0000
SLING-5327 refactor and unify lifecycle method handling (and fix missing argument variants for modified methods - should support the same as the activate method)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1716103 13f79535-47bb-0310-9956-ffa450edef68
---
.../apache/sling/testing/mock/osgi/MockOsgi.java | 3 +-
.../sling/testing/mock/osgi/OsgiServiceUtil.java | 112 +++++++++++++--------
2 files changed, 70 insertions(+), 45 deletions(-)
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
index cf2452a..4834647 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
@@ -215,7 +215,8 @@ public final class MockOsgi {
*/
public static boolean modified(Object target, BundleContext bundleContext, Map<String, Object> properties) {
Map<String, Object> mergedProperties = propertiesMergeWithOsgiMetadata(target, getConfigAdmin(bundleContext), properties);
- return OsgiServiceUtil.modified(target, bundleContext, mergedProperties);
+ ComponentContext componentContext = newComponentContext(bundleContext, mergedProperties);
+ return OsgiServiceUtil.modified(target, componentContext, mergedProperties);
}
/**
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
index 3e22353..e05a15c 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
@@ -80,7 +80,61 @@ final class OsgiServiceUtil {
}
// try to find matching activate/deactivate method and execute it
+ if (invokeLifecycleMethod(target, targetClass, methodName, !activate,
+ componentContext, MapUtil.toMap(componentContext.getProperties()))) {
+ return true;
+ }
+
+ if (fallbackDefaultName) {
+ return false;
+ }
+ throw new RuntimeException("No matching " + (activate ? "activation" : "deactivation") + " method with name '" + methodName + "' "
+ + " found in class " + targetClass.getName());
+ }
+
+ /**
+ * Simulate modification of configuration of OSGi service instance.
+ * @param target Service instance.
+ * @param properties Updated configuration
+ * @return true if modified method was called. False if it failed.
+ */
+ public static boolean modified(Object target, ComponentContext componentContext, Map<String,Object> properties) {
+ Class<?> targetClass = target.getClass();
+
+ // get method name for activation/deactivation from osgi metadata
+ OsgiMetadata metadata = OsgiMetadataUtil.getMetadata(targetClass);
+ if (metadata == null) {
+ throw new NoScrMetadataException(targetClass);
+ }
+ String methodName = metadata.getModifiedMethodName();
+ if (StringUtils.isEmpty(methodName)) {
+ return false;
+ }
+
+ // try to find matching modified method and execute it
+ if (invokeLifecycleMethod(target, targetClass, methodName, false, componentContext, properties)) {
+ return true;
+ }
+
+ throw new RuntimeException("No matching modified method with name '" + methodName + "' "
+ + " found in class " + targetClass.getName());
+ }
+
+ /**
+ * Invokes a lifecycle method (activation, deactivation or modified) with variable method arguments.
+ * @param target Target object
+ * @param targetClass Target object class
+ * @param methodName Method name
+ * @param allowIntegerArgument Allow int or Integer as arguments (only decactivate)
+ * @param componentContext Component context
+ * @param properties Component properties
+ * @return true if a method was found and invoked
+ */
+ private static boolean invokeLifecycleMethod(Object target, Class<?> targetClass,
+ String methodName, boolean allowIntegerArgument,
+ ComponentContext componentContext, Map<String,Object> properties) {
+
// 1. componentContext
Method method = getMethod(targetClass, methodName, new Class<?>[] { ComponentContext.class });
if (method != null) {
@@ -102,8 +156,11 @@ final class OsgiServiceUtil {
return true;
}
- // 4. int (deactivation only)
- if (!activate) {
+ // 4. Component property type (annotation lass)
+ // TODO: implement
+
+ // 5. int (deactivation only)
+ if (allowIntegerArgument) {
method = getMethod(targetClass, methodName, new Class<?>[] { int.class });
if (method != null) {
invokeMethod(target, method, new Object[] { 0 });
@@ -111,8 +168,8 @@ final class OsgiServiceUtil {
}
}
- // 5. Integer (deactivation only)
- if (!activate) {
+ // 6. Integer (deactivation only)
+ if (allowIntegerArgument) {
method = getMethod(targetClass, methodName, new Class<?>[] { Integer.class });
if (method != null) {
invokeMethod(target, method, new Object[] { 0 });
@@ -120,9 +177,10 @@ final class OsgiServiceUtil {
}
}
- // 6. mixed arguments of componentContext, bundleContext and map
- Class<?>[] mixedArgsAllowed = activate ? new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class }
- : new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class, int.class, Integer.class };
+ // 7. mixed arguments
+ Class<?>[] mixedArgsAllowed = allowIntegerArgument ?
+ new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class, int.class, Integer.class }
+ : new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class };
method = getMethodWithAnyCombinationArgs(targetClass, methodName, mixedArgsAllowed);
if (method != null) {
Object[] args = new Object[method.getParameterTypes().length];
@@ -144,48 +202,14 @@ final class OsgiServiceUtil {
return true;
}
- // 7. noargs
+ // 8. noargs
method = getMethod(targetClass, methodName, new Class<?>[0]);
if (method != null) {
invokeMethod(target, method, new Object[0]);
return true;
- }
+ }
- if (fallbackDefaultName) {
- return false;
- }
- throw new RuntimeException("No matching " + (activate ? "activation" : "deactivation") + " method with name '" + methodName + "' "
- + " found in class " + targetClass.getName());
- }
-
- /**
- * Simulate modification of configuration of OSGi service instance.
- * @param target Service instance.
- * @param properties Updated configuration
- * @return true if modified method was called. False if it failed.
- */
- public static boolean modified(Object target, BundleContext bundleContext, Map<String,Object> properties) {
- Class<?> targetClass = target.getClass();
-
- // get method name for activation/deactivation from osgi metadata
- OsgiMetadata metadata = OsgiMetadataUtil.getMetadata(targetClass);
- if (metadata == null) {
- throw new NoScrMetadataException(targetClass);
- }
- String methodName = metadata.getModifiedMethodName();
- if (StringUtils.isEmpty(methodName)) {
- return false;
- }
-
- // try to find matching modified method and execute it
- Method method = getMethod(targetClass, methodName, new Class<?>[] { Map.class });
- if (method != null) {
- invokeMethod(target, method, new Object[] { properties });
- return true;
- }
-
- throw new RuntimeException("No matching modified method with name '" + methodName + "' "
- + " found in class " + targetClass.getName());
+ return false;
}
private static Method getMethod(Class clazz, String methodName, Class<?>[] types) {
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.