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-1.4.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit cd84d7c8cef2decb17c9fe1164d40e52b9b47147 Author: Robert Munteanu <[email protected]> AuthorDate: Wed May 27 13:25:27 2015 +0000 SLING-4756 - ServiceListener notifications are not filtered Support creating filters when only objectClass is set. git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1682013 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/testing/mock/osgi/ClassNameFilter.java | 64 ++++++++++++++++++++++ .../sling/testing/mock/osgi/MockBundleContext.java | 27 ++++++++- .../testing/mock/osgi/MockBundleContextTest.java | 33 +++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/ClassNameFilter.java b/src/main/java/org/apache/sling/testing/mock/osgi/ClassNameFilter.java new file mode 100644 index 0000000..db6bf8d --- /dev/null +++ b/src/main/java/org/apache/sling/testing/mock/osgi/ClassNameFilter.java @@ -0,0 +1,64 @@ +/* + * 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.testing.mock.osgi; + +import java.util.Dictionary; +import java.util.Map; + +import org.osgi.framework.Constants; +import org.osgi.framework.Filter; +import org.osgi.framework.ServiceReference; + +/** + * Mock {@link Filter} implementation. + */ +class ClassNameFilter implements Filter { + + private final String className; + + public ClassNameFilter(String className) { + this.className = className; + } + + @Override + public boolean match(final ServiceReference reference) { + String[] classes = (String[]) reference.getProperty(Constants.OBJECTCLASS); + for ( int i = 0 ; i < classes.length ; i++ ) { + if ( className.equalsIgnoreCase(classes[i])) + return true; + } + return false; + } + + @Override + public boolean match(final Dictionary dictionary) { + return false; + } + + @Override + public boolean matchCase(final Dictionary dictionary) { + return false; + } + + // this is part of org.osgi.core 6.0.0 + public boolean matches(Map<String, ?> map) { + return false; + } + +} 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 8be9673..66ec503 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 @@ -27,6 +27,8 @@ import java.util.List; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.Reference; @@ -36,6 +38,7 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleListener; +import org.osgi.framework.Constants; import org.osgi.framework.Filter; import org.osgi.framework.FrameworkListener; import org.osgi.framework.ServiceEvent; @@ -51,11 +54,13 @@ import com.google.common.collect.ImmutableList; */ class MockBundleContext implements BundleContext { + private static final Pattern SIMPLE_OBJECT_CLASS_FILTER = Pattern.compile("^\\(" + Constants.OBJECTCLASS +"="+"([a-zA-Z\\.\\$]+)" +"\\)$"); + private final MockBundle bundle; private final SortedSet<MockServiceRegistration> registeredServices = new TreeSet<MockServiceRegistration>(); private final List<ServiceListener> serviceListeners = new ArrayList<ServiceListener>(); private final List<BundleListener> bundleListeners = new ArrayList<BundleListener>(); - + public MockBundleContext() { this.bundle = new MockBundle(this); } @@ -67,8 +72,26 @@ class MockBundleContext implements BundleContext { @Override public Filter createFilter(final String s) { - // return filter that denies all + String filter = simplifyFilter(s); + + Matcher matcher = SIMPLE_OBJECT_CLASS_FILTER.matcher(filter); + + // try to extract a single objectClass, should cover most cases + if ( matcher.matches() ) { + return new ClassNameFilter(matcher.group(1)); + } + + // fallback to a filter that denies all return new MockFilter(); + + } + + private String simplifyFilter(String s) { + // a single hardcoded simplification for now + if ( s.startsWith("((") && s.endsWith("))") ) { + return s.substring(1, s.length() - 1); + } + return s; } @Override 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 4284b97..00d1384 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 @@ -20,9 +20,11 @@ package org.apache.sling.testing.mock.osgi; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -38,6 +40,7 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleListener; import org.osgi.framework.Constants; +import org.osgi.framework.Filter; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceEvent; import org.osgi.framework.ServiceListener; @@ -177,5 +180,35 @@ public class MockBundleContextTest { public void testGetProperty() { assertNull(bundleContext.getProperty("anyProperty")); } + + @Test + public void testObjectClassFilterMatches() throws InvalidSyntaxException { + + Filter filter = bundleContext.createFilter("(" + Constants.OBJECTCLASS + "=" + Integer.class.getName() + ")"); + + ServiceRegistration serviceRegistration = bundleContext.registerService(Integer.class.getName(), Integer.valueOf(1), null); + + assertTrue(filter.match(serviceRegistration.getReference())); + } + @Test + public void testNestedObjectClassFilterMatches() throws InvalidSyntaxException { + + // this matches what the ServiceTracker creates + Filter filter = bundleContext.createFilter("((" + Constants.OBJECTCLASS + "=" + Integer.class.getName() + "))"); + + ServiceRegistration serviceRegistration = bundleContext.registerService(Integer.class.getName(), Integer.valueOf(1), null); + + assertTrue(filter.match(serviceRegistration.getReference())); + } + + @Test + public void testObjectClassFilterDoesNotMatch() throws InvalidSyntaxException { + + Filter filter = bundleContext.createFilter("(" + Constants.OBJECTCLASS + "=" + Integer.class.getName() + ")"); + + ServiceRegistration serviceRegistration = bundleContext.registerService(Long.class.getName(), Long.valueOf(1), null); + + assertFalse(filter.match(serviceRegistration.getReference())); + } } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
