You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2016/03/02 15:16:17 UTC

svn commit: r1733296 - in /sling/trunk/bundles/resourceresolver/src: main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java

Author: cziegeler
Date: Wed Mar  2 14:16:17 2016
New Revision: 1733296

URL: http://svn.apache.org/viewvc?rev=1733296&view=rev
Log:
SLING-5580 : Resource Resolver Factory is wrongly activated when zero or more than one required providers

Added:
    sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java   (with props)
Modified:
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java?rev=1733296&r1=1733295&r2=1733296&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java Wed Mar  2 14:16:17 2016
@@ -39,7 +39,7 @@ public class FactoryPreconditions {
         public Filter filter;
     };
 
-    private ResourceProviderTracker tracker;
+    private volatile ResourceProviderTracker tracker;
 
     private volatile List<RequiredProvider> requiredProviders;
 
@@ -74,16 +74,19 @@ public class FactoryPreconditions {
 
     public void deactivate() {
         this.requiredProviders = null;
+        this.tracker = null;
     }
 
     public boolean checkPreconditions() {
         synchronized ( this ) {
-            boolean canRegister = false;
-            if (this.requiredProviders != null) {
-                canRegister = false;
-                for (ResourceProviderHandler h : this.tracker.getResourceProviderStorage().getAllHandlers()) {
-                    for (final RequiredProvider rp : this.requiredProviders) {
-                        ServiceReference ref = h.getInfo().getServiceReference();
+            final List<RequiredProvider> localRequiredProviders = this.requiredProviders;
+            final ResourceProviderTracker localTracker = this.tracker;
+            boolean canRegister = localTracker != null;
+            if (localRequiredProviders != null && localTracker != null ) {
+                for (final RequiredProvider rp : localRequiredProviders) {
+                    canRegister = false;
+                    for (final ResourceProviderHandler h : localTracker.getResourceProviderStorage().getAllHandlers()) {
+                        final ServiceReference ref = h.getInfo().getServiceReference();
                         if (rp.filter != null && rp.filter.match(ref)) {
                             canRegister = true;
                             break;
@@ -95,6 +98,9 @@ public class FactoryPreconditions {
                             break;
                         }
                     }
+                    if ( !canRegister ) {
+                        break;
+                    }
                 }
             }
             return canRegister;

Added: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java?rev=1733296&view=auto
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java (added)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java Wed Mar  2 14:16:17 2016
@@ -0,0 +1,106 @@
+/*
+ * 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.resourceresolver.impl;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler;
+import org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo;
+import org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage;
+import org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+
+public class FactoryPreconditionsTest {
+
+    @Test public void testNoRequiredProviders() {
+        final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
+        final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
+        Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
+
+        FactoryPreconditions conditions = new FactoryPreconditions();
+        conditions.activate(null, null, tracker);
+
+        assertTrue(conditions.checkPreconditions());
+
+        conditions = new FactoryPreconditions();
+        conditions.activate(null, new String[0], tracker);
+
+        assertTrue(conditions.checkPreconditions());
+    }
+
+    @Test public void testDeactivated() {
+        final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
+        final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
+        Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
+
+        FactoryPreconditions conditions = new FactoryPreconditions();
+        conditions.activate(null, null, tracker);
+
+        assertTrue(conditions.checkPreconditions());
+
+        conditions.deactivate();
+
+        assertFalse(conditions.checkPreconditions());
+    }
+
+    private List<ResourceProviderHandler> getResourceProviderHandlers(String[] pids) {
+        final List<ResourceProviderHandler> result = new ArrayList<ResourceProviderHandler>();
+
+        for(final String p : pids) {
+            final ResourceProviderHandler handler = Mockito.mock(ResourceProviderHandler.class);
+            final ResourceProviderInfo info = Mockito.mock(ResourceProviderInfo.class);
+            final ServiceReference ref = Mockito.mock(ServiceReference.class);
+
+            Mockito.when(handler.getInfo()).thenReturn(info);
+            Mockito.when(info.getServiceReference()).thenReturn(ref);
+            Mockito.when(ref.getProperty(Constants.SERVICE_PID)).thenReturn(p);
+
+            result.add(handler);
+        }
+        return result;
+    }
+
+    @Test public void testPIDs() {
+        final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
+        final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
+        Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
+
+        FactoryPreconditions conditions = new FactoryPreconditions();
+        conditions.activate(null, new String[] {"pid1", "pid3"}, tracker);
+
+        final List<ResourceProviderHandler> handlers1 = getResourceProviderHandlers(new String[] {"pid2"});
+        Mockito.when(storage.getAllHandlers()).thenReturn(handlers1);
+        assertFalse(conditions.checkPreconditions());
+
+        final List<ResourceProviderHandler> handlers2 = getResourceProviderHandlers(new String[] {"pid1", "pid2", "pid3"});
+        Mockito.when(storage.getAllHandlers()).thenReturn(handlers2);
+        assertTrue(conditions.checkPreconditions());
+
+        final List<ResourceProviderHandler> handlers3 = getResourceProviderHandlers(new String[] {"pid1"});
+        Mockito.when(storage.getAllHandlers()).thenReturn(handlers3);
+        assertFalse(conditions.checkPreconditions());
+    }
+}

Propchange: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url