Author: rombert
Date: Tue Jan 26 09:56:41 2016
New Revision: 1726752

URL: http://svn.apache.org/viewvc?rev=1726752&view=rev
Log:
SLING-5206 - Create tests for all new features/handling 

Extract a fixture out of the CombinedResourceProviderTest

Added:
    
sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/Fixture.java
Modified:
    
sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/stateful/CombinedResourceProviderTest.java

Added: 
sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/Fixture.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/Fixture.java?rev=1726752&view=auto
==============================================================================
--- 
sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/Fixture.java
 (added)
+++ 
sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/Fixture.java
 Tue Jan 26 09:56:41 2016
@@ -0,0 +1,74 @@
+/*
+ * 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 java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.sling.api.resource.runtime.dto.AuthType;
+import org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo;
+import org.apache.sling.spi.resource.provider.ResourceProvider;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * The <tt>Fixture</tt> holds reusable, shared, test setup code
+ */
+public class Fixture { 
+
+    private final BundleContext bc;
+    private final Map<ResourceProviderInfo, ServiceRegistration> 
providerRegistrations = new HashMap<ResourceProviderInfo, 
ServiceRegistration>();
+    
+    public Fixture(BundleContext bc) {
+        this.bc = bc;
+    }
+
+    public ResourceProviderInfo registerResourceProvider(ResourceProvider<?> 
rp, String root, AuthType authType) throws InvalidSyntaxException {
+        
+        Dictionary<String, String> props = new Hashtable<String, String>();
+        props.put(ResourceProvider.PROPERTY_ROOT, root);
+        props.put(ResourceProvider.PROPERTY_AUTHENTICATE, authType.name());
+        props.put(ResourceProvider.PROPERTY_MODIFIABLE, 
Boolean.TRUE.toString());
+        
+        ServiceRegistration registration = 
bc.registerService(ResourceProvider.class.getName(), rp, props);
+        
+        ServiceReference sr = 
bc.getServiceReferences(ResourceProvider.class.getName(),
+                "(" + ResourceProvider.PROPERTY_ROOT + "=" + root + ")")[0];
+        
+        ResourceProviderInfo providerInfo = new ResourceProviderInfo(sr);
+
+        providerRegistrations.put(providerInfo, registration);
+        
+        return providerInfo;
+    }
+    
+    public void unregisterResourceProvider(ResourceProviderInfo info) {
+        
+        ServiceRegistration registration = providerRegistrations.remove(info);
+        if ( registration == null ) {
+            throw new IllegalArgumentException("No " + 
ServiceRegistration.class.getSimpleName() + " found for " + info);
+        }
+        
+        registration.unregister();
+    }
+}
\ No newline at end of file

Modified: 
sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/stateful/CombinedResourceProviderTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/stateful/CombinedResourceProviderTest.java?rev=1726752&r1=1726751&r2=1726752&view=diff
==============================================================================
--- 
sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/stateful/CombinedResourceProviderTest.java
 (original)
+++ 
sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/stateful/CombinedResourceProviderTest.java
 Tue Jan 26 09:56:41 2016
@@ -32,9 +32,7 @@ import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.Dictionary;
 import java.util.HashMap;
-import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -47,6 +45,7 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.resource.runtime.dto.AuthType;
+import org.apache.sling.resourceresolver.impl.Fixture;
 import org.apache.sling.resourceresolver.impl.ResourceAccessSecurityTracker;
 import org.apache.sling.resourceresolver.impl.SimpleValueMapImpl;
 import 
org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler;
@@ -64,8 +63,6 @@ import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
 
 @SuppressWarnings("unchecked")
 public class CombinedResourceProviderTest {
@@ -92,9 +89,11 @@ public class CombinedResourceProviderTes
 
         BundleContext bc = MockOsgi.newBundleContext();
         
+        Fixture fixture = new Fixture(bc);
+        
         // sub-provider
         subProvider = Mockito.mock(ResourceProvider.class);
-        ResourceProviderInfo info = registerResourceProvider(bc, subProvider, 
"/some/path", AuthType.required);
+        ResourceProviderInfo info = 
fixture.registerResourceProvider(subProvider, "/some/path", AuthType.required);
         ResourceProviderHandler handler = new ResourceProviderHandler(bc, 
info);
         when(subProvider.getQueryLanguageProvider()).thenReturn(new 
SimpleQueryLanguageProvider(QL_MOCK, QL_ANOTHER_MOCK) {
             @Override
@@ -122,7 +121,7 @@ public class CombinedResourceProviderTes
         handler.activate();
 
         rootProvider = mock(ResourceProvider.class);
-        ResourceProviderInfo rootInfo = registerResourceProvider(bc, 
rootProvider, "/", AuthType.required);
+        ResourceProviderInfo rootInfo = 
fixture.registerResourceProvider(rootProvider, "/", AuthType.required);
         ResourceProviderHandler rootHandler = new ResourceProviderHandler(bc, 
rootInfo);
         when(rootProvider.getQueryLanguageProvider()).thenReturn(new 
SimpleQueryLanguageProvider(QL_NOOP));
         rootHandler.activate();
@@ -145,22 +144,7 @@ public class CombinedResourceProviderTes
 
         crp = new CombinedResourceProvider(storage, rr, authenticator);
     }
-
-    private ResourceProviderInfo registerResourceProvider(BundleContext bc, 
ResourceProvider<?> rp, String root, AuthType authType) throws 
InvalidSyntaxException {
-        
-        Dictionary<String, String> props = new Hashtable<String, String>();
-        props.put(ResourceProvider.PROPERTY_ROOT, root);
-        props.put(ResourceProvider.PROPERTY_AUTHENTICATE, authType.name());
-        props.put(ResourceProvider.PROPERTY_MODIFIABLE, 
Boolean.TRUE.toString());
-        
-        bc.registerService(ResourceProvider.class.getName(), rp, props);
-        
-        ServiceReference sr = 
bc.getServiceReferences(ResourceProvider.class.getName(),
-                "(" + ResourceProvider.PROPERTY_ROOT + "=" + root + ")")[0];
-        
-        return new ResourceProviderInfo(sr);
-    }
-
+    
     /**
      * Configures the provider to return a mock resource for the specified path
      * @return 


Reply via email to