SLING-10616 Respect Target filter for optional/static/greedy reference updates
diff --git a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
index f465ef2..ed69b3d 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
@@ -176,17 +176,19 @@
         Set<MockServiceRegistration<?>> servicesToRestart = new HashSet<>();
         for (ReferenceInfo<?> referenceInfo : affectedStaticGreedyReferences) {
             Reference reference = referenceInfo.getReference();
-            switch (reference.getCardinality()) {
-            case MANDATORY_UNARY:
-                // nothing to do - reference is already set
-                break;
-            case MANDATORY_MULTIPLE:
-            case OPTIONAL_MULTIPLE:
-            case OPTIONAL_UNARY:
-                servicesToRestart.add(referenceInfo.getServiceRegistration());
-                break;
-            default:
-                throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+            if (reference.matchesTargetFilter(registration.getReference())) {
+                switch (reference.getCardinality()) {
+                case MANDATORY_UNARY:
+                    // nothing to do - reference is already set
+                    break;
+                case MANDATORY_MULTIPLE:
+                case OPTIONAL_MULTIPLE:
+                case OPTIONAL_UNARY:
+                    servicesToRestart.add(referenceInfo.getServiceRegistration());
+                    break;
+                default:
+                    throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+                }
             }
         }
         servicesToRestart.forEach(this::restartService);
@@ -253,15 +255,17 @@
         Set<MockServiceRegistration<?>> servicesToRestart = new HashSet<>();
         for (ReferenceInfo<?> referenceInfo : affectedStaticGreedyReferences) {
             Reference reference = referenceInfo.getReference();
-            switch (reference.getCardinality()) {
-            case MANDATORY_UNARY:
-            case MANDATORY_MULTIPLE:
-            case OPTIONAL_MULTIPLE:
-            case OPTIONAL_UNARY:
-                servicesToRestart.add(referenceInfo.getServiceRegistration());
-                break;
-            default:
-                throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+            if (reference.matchesTargetFilter(registration.getReference())) {
+                switch (reference.getCardinality()) {
+                case MANDATORY_UNARY:
+                case MANDATORY_MULTIPLE:
+                case OPTIONAL_MULTIPLE:
+                case OPTIONAL_UNARY:
+                    servicesToRestart.add(referenceInfo.getServiceRegistration());
+                    break;
+                default:
+                    throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+                }
             }
         }
         servicesToRestart.forEach(this::restartService);
diff --git a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedySelfReferenceTest.java b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedySelfReferenceTest.java
new file mode 100644
index 0000000..e72fad0
--- /dev/null
+++ b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedySelfReferenceTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Collection;
+
+import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface3;
+import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface3Impl;
+import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface3ImplSelfReferencing;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Test cases for SLING-10616
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class MockBundleContextStaticGreedySelfReferenceTest {
+
+    private BundleContext bundleContext;
+    @Before
+    public void setUp() {
+        bundleContext = MockOsgi.newBundleContext();
+    }
+
+    @Test
+    public void testSelfReferenceWithTargetFilter() throws InvalidSyntaxException {
+        ServiceInterface3 defaultImpl = MockOsgi.activateInjectServices(ServiceInterface3Impl.class, bundleContext);
+        bundleContext.registerService(ServiceInterface3.class.getName(), defaultImpl, null);
+
+        ServiceInterface3 selfReferencingImpl = MockOsgi.activateInjectServices(ServiceInterface3ImplSelfReferencing.class, bundleContext);
+        bundleContext.registerService(ServiceInterface3.class.getName(), selfReferencingImpl, null);
+
+        assertNotNull(getDefaultImplFromReference());
+    }
+
+    @Test
+    public void testSelfReferenceWithTargetFilterReverse() throws InvalidSyntaxException {
+        ServiceInterface3 selfReferencingImpl = MockOsgi.activateInjectServices(ServiceInterface3ImplSelfReferencing.class, bundleContext);
+        bundleContext.registerService(ServiceInterface3.class.getName(), selfReferencingImpl, null);
+
+        ServiceInterface3 defaultImpl = MockOsgi.activateInjectServices(ServiceInterface3Impl.class, bundleContext);
+        bundleContext.registerService(ServiceInterface3.class.getName(), defaultImpl, null);
+
+        assertNotNull(getDefaultImplFromReference());
+    }
+
+    /**
+     * Get injected filtered service from ServiceInterface3ImplSelfReferencing.
+     */
+    @SuppressWarnings("null")
+    private ServiceInterface3 getDefaultImplFromReference() throws InvalidSyntaxException {
+        Collection<ServiceReference<ServiceInterface3>> references = bundleContext.getServiceReferences(ServiceInterface3.class, "(!(prop1=abc))");
+        ServiceInterface3 service = bundleContext.getService(references.iterator().next());
+        return ((ServiceInterface3ImplSelfReferencing)service).getDefaultImplementation();
+    }
+
+}
diff --git a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/ServiceInterface3Impl.java b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/ServiceInterface3Impl.java
new file mode 100644
index 0000000..a8b493d
--- /dev/null
+++ b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/ServiceInterface3Impl.java
@@ -0,0 +1,30 @@
+/*
+ * 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;
+
+import org.osgi.service.component.annotations.Component;
+
+@Component(service = ServiceInterface3.class, property = {
+        "prop1=abc"
+})
+public class ServiceInterface3Impl implements ServiceInterface3 {
+
+    // no methods
+
+}
diff --git a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/ServiceInterface3ImplSelfReferencing.java b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/ServiceInterface3ImplSelfReferencing.java
new file mode 100644
index 0000000..ab7ea55
--- /dev/null
+++ b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/ServiceInterface3ImplSelfReferencing.java
@@ -0,0 +1,40 @@
+/*
+ * 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;
+
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
+
+@Component(service = ServiceInterface3.class)
+public class ServiceInterface3ImplSelfReferencing implements ServiceInterface3 {
+
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL,
+            policy = ReferencePolicy.STATIC,
+            policyOption = ReferencePolicyOption.GREEDY,
+            target = "(prop1=abc)")
+    private ServiceInterface3 defaultImplementation;
+
+    public ServiceInterface3 getDefaultImplementation() {
+        return defaultImplementation;
+    }
+
+}