Author: bdelacretaz
Date: Thu Sep 17 13:51:29 2015
New Revision: 1703625

URL: http://svn.apache.org/r1703625
Log:
SLING-5038 - also provide access to BundleContext

Modified:
    
sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/Service.java

Modified: 
sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/Service.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/Service.java?rev=1703625&r1=1703624&r2=1703625&view=diff
==============================================================================
--- 
sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/Service.java
 (original)
+++ 
sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/rules/Service.java
 Thu Sep 17 13:51:29 2015
@@ -52,26 +52,34 @@ public class Service implements TestRule
                     base.evaluate();
                     return;
                 }
-
-                final ServiceReference serviceReference = 
bundleContext.getServiceReference(serviceClass.getName());
-
-                if (serviceReference == null) {
-                    throw new IllegalStateException("unable to get a service 
reference");
-                }
-
-                final Object service = 
bundleContext.getService(serviceReference);
-
-                if (service == null) {
-                    throw new IllegalStateException("unable to get an instance 
of the service");
+                
+                ServiceReference serviceReference = null;
+                if(serviceClass.equals(BundleContext.class)) {
+                    // Special case to provide the BundleContext to tests
+                    Service.this.service = bundleContext;
+                } else {
+                    serviceReference = 
bundleContext.getServiceReference(serviceClass.getName());
+    
+                    if (serviceReference == null) {
+                        throw new IllegalStateException("unable to get a 
service reference");
+                    }
+    
+                    final Object service = 
bundleContext.getService(serviceReference);
+    
+                    if (service == null) {
+                        throw new IllegalStateException("unable to get an 
instance of the service");
+                    }
+    
+                    Service.this.service = serviceClass.cast(service);
                 }
 
-                Service.this.service = serviceClass.cast(service);
-
                 try {
                     base.evaluate();
                 } finally {
                     Service.this.service = null;
-                    bundleContext.ungetService(serviceReference);
+                    if(serviceReference != null) {
+                        bundleContext.ungetService(serviceReference);
+                    }
                 }
             }
 


Reply via email to