You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2021/11/24 08:09:58 UTC
[sling-org-apache-sling-testing-osgi-mock] 01/01: SLING-10935 improve exception message for failed constructor injection
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch feature/SLING-10935-improve-error-message-for-failing-constructor-injection
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit 481dab080657265f3bee10c2a53a92cbe03db9d3
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Wed Nov 24 09:09:46 2021 +0100
SLING-10935 improve exception message for failed constructor injection
---
.../sling/testing/mock/osgi/OsgiServiceUtil.java | 8 +++--
.../OsgiServiceUtilActivateDeactivateTest.java | 13 ++++++++
.../ServiceReferenceInConstructor.java | 35 ++++++++++++++++++++++
3 files changed, 53 insertions(+), 3 deletions(-)
diff --git a/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java b/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
index dc6d83d..b4d6826 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
@@ -511,6 +511,7 @@ final class OsgiServiceUtil {
throws InstantiationException, IllegalAccessException {
Iterator<Reference> referenceIterator = constructorInjectionReferences.iterator();
List<Object> values = new ArrayList<>();
+ int parameterInxex = 0;
for (Parameter parameter : constructor.getParameters()) {
// check for well-known parameter types first
if (parameter.getType() == ComponentContext.class) {
@@ -530,7 +531,7 @@ final class OsgiServiceUtil {
// check for reference injection
else if (referenceIterator.hasNext()) {
Reference reference = referenceIterator.next();
- Optional<?> referenceValue = buildConstructorInjectionValue(targetClass, parameter.getType(), reference, componentContext);
+ Optional<?> referenceValue = buildConstructorInjectionValue(targetClass, parameter.getType(), reference, componentContext, parameterInxex++);
if (referenceValue != null) {
values.add(referenceValue.isPresent() ? referenceValue.get() : null);
}
@@ -554,18 +555,19 @@ final class OsgiServiceUtil {
* @param parameterType Parameter type
* @param reference Reference
* @param componentContext Component context
+ * @param parameterIndex 0-based index of the constructyor parameter for which the injection value is calculated
* @return null if parameter could not be injected, empty Optional if null value should be injected, or value wrapped in Optional otherwise
* @throws InstantiationException
* @throws IllegalAccessException
*/
private static <T> @Nullable Optional<?> buildConstructorInjectionValue(Class<?> targetClass, Class<T> parameterType, Reference reference,
- MockComponentContext componentContext) throws InstantiationException, IllegalAccessException {
+ MockComponentContext componentContext, int parameterIndex) throws InstantiationException, IllegalAccessException {
// get matching service references
List<ServiceInfo<?>> matchingServices = getMatchingServices(reference.getInterfaceTypeAsClass(),
componentContext.getBundleContext(), reference.getTarget());
if (matchingServices.isEmpty() && !reference.isCardinalityOptional()) {
- throw new ReferenceViolationException("Unable to inject mandatory reference '" + reference.getName() + "' for class " + targetClass.getName() + " : no matching services were found.");
+ throw new ReferenceViolationException("Unable to inject mandatory reference '" + reference.getName() + "' (" + targetClass + ") into constructor parameter " + parameterIndex + " for class " + targetClass.getName() + " : no matching services were found.");
}
// check for field with list/collection reference
diff --git a/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilActivateDeactivateTest.java b/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilActivateDeactivateTest.java
index 65cba39..c0dad32 100644
--- a/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilActivateDeactivateTest.java
+++ b/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilActivateDeactivateTest.java
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Map;
+import java.util.regex.Pattern;
import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.Service1;
import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.Service1Constructor;
@@ -40,6 +41,8 @@ import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeacti
import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.Service6Constructor;
import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.Service7;
import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.Service7Constructor;
+import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.ServiceReferenceInConstructor;
+import org.junit.Assert;
import org.junit.Test;
import org.osgi.framework.BundleContext;
@@ -229,4 +232,14 @@ public class OsgiServiceUtilActivateDeactivateTest {
assertFalse(service.isActivated());
}
+ @Test
+ public void testReferenceInConstructor() {
+ try {
+ MockOsgi.activateInjectServices(ServiceReferenceInConstructor.class, bundleContext);
+ Assert.fail("Unresolvable mandatory reference in constructor should lead to ReferenceViolationException");
+ } catch (ReferenceViolationException e) {
+ String regex = "Unable to inject mandatory reference '.*' " + Pattern.quote("(class org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.ServiceReferenceInConstructor) into constructor parameter 0 for class org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.ServiceReferenceInConstructor : no matching services were found.");
+ assertTrue("Expected exception message matching regex:\n" + regex + "\nbut got:\n" + e.getMessage(), e.getMessage().matches(regex));
+ }
+ }
}
diff --git a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceReferenceInConstructor.java b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceReferenceInConstructor.java
new file mode 100644
index 0000000..0e16556
--- /dev/null
+++ b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceReferenceInConstructor.java
@@ -0,0 +1,35 @@
+/*
+ * 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.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate;
+
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+@Component
+public class ServiceReferenceInConstructor {
+
+ private final Service1 service1;
+
+ @Activate
+ public ServiceReferenceInConstructor(@Reference Service1 service1) {
+ this.service1 = service1;
+ }
+
+}