This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.osgi-mock-2.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit 5d2ecfeae6a3c9b5b6c7e22c2dcfd16da56cf6f3 Author: Stefan Seifert <[email protected]> AuthorDate: Thu Dec 10 21:39:16 2015 +0000 SLING-5372 osgi-mock: MockBundleContext.getServices(Class,String) throws NPE when no services found git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1719219 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/sling/testing/mock/osgi/MockBundleContext.java | 8 +++++++- .../sling/testing/mock/osgi/MockBundleContextTest.java | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java index 7ea264c..3c0ad53 100644 --- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java +++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java @@ -218,7 +218,13 @@ class MockBundleContext implements BundleContext { @SuppressWarnings("unchecked") @Override public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> clazz, String filter) throws InvalidSyntaxException { - return ImmutableList.<ServiceReference<S>>copyOf(getServiceReferences(clazz.getName(), filter)); + ServiceReference<S>[] result = getServiceReferences(clazz.getName(), filter); + if (result == null) { + return ImmutableList.<ServiceReference<S>>of(); + } + else { + return ImmutableList.<ServiceReference<S>>copyOf(result); + } } @SuppressWarnings("unchecked") diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java index fb704f1..2b1cded 100644 --- a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java +++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java @@ -29,6 +29,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import java.util.Collection; import java.util.Dictionary; import java.util.Hashtable; @@ -92,6 +93,10 @@ public class MockBundleContextTest { assertSame(reg1.getReference(), refsString[0]); assertSame(reg2.getReference(), refsString[1]); + Collection<ServiceReference<String>> refColString = bundleContext.getServiceReferences(String.class, null); + assertEquals(2, refColString.size()); + assertSame(reg1.getReference(), refColString.iterator().next()); + ServiceReference<?>[] refsInteger = bundleContext.getServiceReferences(Integer.class.getName(), null); assertEquals(2, refsInteger.length); assertSame(reg3.getReference(), refsInteger[0]); @@ -112,6 +117,16 @@ public class MockBundleContextTest { } @Test + public void testNoServiceReferences() throws InvalidSyntaxException { + ServiceReference<?>[] refs = bundleContext.getServiceReferences(String.class.getName(), null); + assertNull(refs); + + Collection<ServiceReference<String>> refCol = bundleContext.getServiceReferences(String.class, null); + assertNotNull(refCol); + assertTrue(refCol.isEmpty()); + } + + @Test public void testServiceUnregistration() { // prepare test services String clazz1 = String.class.getName(); -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
