SLING-4813 - Support CGLIB enhanced classes for Mockito based service mocks
Submitted By: Krystian Panek
Closes #97
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1686426 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
index 971882e..f520617 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
@@ -204,7 +204,8 @@
* @return XPath query fragment to find matching XML node in SCR metadata
*/
private static String getComponentXPathQuery(Class clazz) {
- return "//*[implementation/@class='" + clazz.getName() + "' or @name='" + clazz.getName() + "']";
+ String className = StringUtils.substringBefore(clazz.getName(), "$$Enhancer");
+ return "//*[implementation/@class='" + className + "' or @name='" + className + "']";
}
private static boolean matchesService(Class clazz, Document metadata) {
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
index 1243116..53cdd0d 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
@@ -41,6 +41,7 @@
import org.apache.felix.scr.annotations.Service;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mockito;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
@@ -88,7 +89,7 @@
List<Map<String, Object>> reference3Configs = service3.getReference3Configs();
assertEquals(1, reference3Configs.size());
assertEquals(200, reference3Configs.get(0).get(Constants.SERVICE_RANKING));
-
+
assertTrue(MockOsgi.deactivate(service3));
assertNull(service3.getComponentContext());
}
@@ -96,13 +97,13 @@
@Test
public void testService3_Config() {
BundleContext bundleContext = MockOsgi.newBundleContext();
-
+
Map<String,Object> initialProperites = ImmutableMap.<String, Object>of("prop1", "value1");
Service3 service3 = new Service3();
MockOsgi.activate(service3, bundleContext, initialProperites);
assertEquals(initialProperites.get("prop1"), service3.getConfig().get("prop1"));
-
+
Map<String,Object> newProperties = ImmutableMap.<String, Object>of("prop2", "value2");
MockOsgi.modified(service3, bundleContext, newProperties);
assertEquals(newProperties.get("prop2"), service3.getConfig().get("prop2"));
@@ -112,7 +113,7 @@
MockOsgi.modified(service3, bundleContext, newPropertiesDictonary);
assertEquals(newProperties.get("prop3"), service3.getConfig().get("prop3"));
}
-
+
@Test
public void testService4() {
Service4 service4 = new Service4();
@@ -127,22 +128,22 @@
public void testInjectServicesNoMetadata() {
MockOsgi.injectServices(new Object(), MockOsgi.newBundleContext());
}
-
+
@Test(expected=NoScrMetadataException.class)
public void testActivateNoMetadata() {
MockOsgi.activate(new Object());
}
-
+
@Test(expected=NoScrMetadataException.class)
public void testDeactivateNoMetadata() {
MockOsgi.deactivate(new Object());
}
-
+
@Test(expected=NoScrMetadataException.class)
public void testModifiedNoMetadata() {
MockOsgi.modified(new Object(), MockOsgi.newBundleContext(), ImmutableMap.<String,Object>of());
}
-
+
public interface ServiceInterface1 {
// no methods
}
@@ -206,7 +207,7 @@
private void deactivate(ComponentContext ctx) {
this.componentContext = null;
}
-
+
@Modified
private void modified(Map<String,Object> newConfig) {
this.config = newConfig;
@@ -239,7 +240,7 @@
public ComponentContext getComponentContext() {
return this.componentContext;
}
-
+
public Map<String, Object> getConfig() {
return config;
}
@@ -300,4 +301,34 @@
}
+ @Component
+ @Service({ ServiceInterface5.class })
+ public static class Service5 implements ServiceInterface5 {
+
+ @Override
+ public boolean doRemoteThing() {
+ return false;
+ }
+ }
+
+ public interface ServiceInterface5 {
+
+ boolean doRemoteThing();
+
+ }
+
+ @Test
+ public void testMockedService() {
+ Service5 service5 = Mockito.spy(new Service5());
+ Mockito.doReturn(true).when(service5).doRemoteThing();
+
+ MockOsgi.injectServices(service5, bundleContext);
+ MockOsgi.activate(service5, bundleContext, (Dictionary<String, Object>) null);
+ bundleContext.registerService(ServiceInterface5.class.getName(), service5, null);
+
+ assertSame(service5, bundleContext.getService(
+ bundleContext.getServiceReference(ServiceInterface5.class.getName())));
+ assertEquals(true, service5.doRemoteThing());
+ }
+
}
diff --git a/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml b/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
index ae8d655..d822d05 100644
--- a/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
+++ b/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
@@ -48,4 +48,11 @@
<property name="service.pid" value="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service4"/>
<reference name="customName" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface1" cardinality="1..1" policy="static" bind="customBind" unbind="customUnbind"/>
</scr:component>
+ <scr:component name="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service5">
+ <implementation class="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service5"/>
+ <service servicefactory="false">
+ <provide interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface5"/>
+ </service>
+ <property name="service.pid" value="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service5"/>
+ </scr:component>
</components>