You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:19:19 UTC
[sling-org-apache-sling-testing-osgi-mock] 08/17: SLING-4756 add
support for filtering via felix.framework FilterImpl implementation
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 5644c49ebea2bf110965d360830c1f74fb7f298e
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Mon Jun 1 20:53:35 2015 +0000
SLING-4756 add support for filtering via felix.framework FilterImpl implementation
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1683002 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 7 +++
.../sling/testing/mock/osgi/ClassNameFilter.java | 64 ----------------------
.../sling/testing/mock/osgi/MockBundleContext.java | 56 +++++++------------
.../apache/sling/testing/mock/osgi/MockFilter.java | 52 ------------------
.../testing/mock/osgi/MockServiceRegistration.java | 7 ++-
.../testing/mock/osgi/context/OsgiContextImpl.java | 3 +-
.../testing/mock/osgi/MockBundleContextTest.java | 11 ----
.../sling/testing/mock/osgi/MockFilterTest.java | 46 ----------------
8 files changed, 32 insertions(+), 214 deletions(-)
diff --git a/pom.xml b/pom.xml
index 756ebca..7029406 100644
--- a/pom.xml
+++ b/pom.xml
@@ -71,6 +71,13 @@
</dependency>
<dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.framework</artifactId>
+ <version>5.0.0</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
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
deleted file mode 100644
index db6bf8d..0000000
--- a/src/main/java/org/apache/sling/testing/mock/osgi/ClassNameFilter.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * 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 051a62f..540babb 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
@@ -29,10 +29,9 @@ import java.util.Map;
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.felix.framework.FilterImpl;
import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.Reference;
import org.apache.sling.testing.mock.osgi.OsgiServiceUtil.ReferenceInfo;
import org.apache.sling.testing.mock.osgi.OsgiServiceUtil.ServiceInfo;
@@ -40,9 +39,9 @@ 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.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceListener;
@@ -56,8 +55,6 @@ 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 Map<ServiceListener, Filter> serviceListeners = new HashMap<ServiceListener, Filter>();
@@ -73,27 +70,8 @@ class MockBundleContext implements BundleContext {
}
@Override
- public Filter createFilter(final String s) {
- 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;
+ public Filter createFilter(final String s) throws InvalidSyntaxException {
+ return new FilterImpl(s);
}
@Override
@@ -183,12 +161,16 @@ class MockBundleContext implements BundleContext {
@Override
public ServiceReference getServiceReference(final String clazz) {
- ServiceReference[] serviceRefs = getServiceReferences(clazz, null);
- if (serviceRefs != null && serviceRefs.length > 0) {
- return serviceRefs[0];
- } else {
- return null;
+ try {
+ ServiceReference[] serviceRefs = getServiceReferences(clazz, null);
+ if (serviceRefs != null && serviceRefs.length > 0) {
+ return serviceRefs[0];
+ }
+ }
+ catch (InvalidSyntaxException ex) {
+ // should not happen
}
+ return null;
}
// this is part of org.osgi.core 6.0.0
@@ -197,7 +179,7 @@ class MockBundleContext implements BundleContext {
}
@Override
- public ServiceReference[] getServiceReferences(final String clazz, final String filter) {
+ public ServiceReference[] getServiceReferences(final String clazz, final String filter) throws InvalidSyntaxException {
Set<ServiceReference> result = new TreeSet<ServiceReference>();
for (MockServiceRegistration serviceRegistration : this.registeredServices) {
if (serviceRegistration.matches(clazz, filter)) {
@@ -212,12 +194,12 @@ class MockBundleContext implements BundleContext {
}
// this is part of org.osgi.core 6.0.0
- public Collection<ServiceReference> getServiceReferences(Class clazz, String filter) {
+ public Collection<ServiceReference> getServiceReferences(Class clazz, String filter) throws InvalidSyntaxException {
return ImmutableList.<ServiceReference>copyOf(getServiceReferences(clazz.getName(), filter));
}
@Override
- public ServiceReference[] getAllServiceReferences(final String clazz, final String filter) {
+ public ServiceReference[] getAllServiceReferences(final String clazz, final String filter) throws InvalidSyntaxException {
// for now just do the same as getServiceReferences
return getServiceReferences(clazz, filter);
}
@@ -235,12 +217,12 @@ class MockBundleContext implements BundleContext {
@Override
public void addServiceListener(final ServiceListener serviceListener) {
- addServiceListener(serviceListener, null);
+ serviceListeners.put(serviceListener, null);
}
@Override
- public void addServiceListener(final ServiceListener serviceListener, final String filter) {
- serviceListeners.put(serviceListener, filter != null ? createFilter(filter) : null );
+ public void addServiceListener(final ServiceListener serviceListener, final String filter) throws InvalidSyntaxException {
+ serviceListeners.put(serviceListener, createFilter(filter));
}
@Override
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockFilter.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockFilter.java
deleted file mode 100644
index d8a3ec1..0000000
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockFilter.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * 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.Filter;
-import org.osgi.framework.ServiceReference;
-
-/**
- * Mock {@link Filter} implementation.
- */
-class MockFilter implements Filter {
-
- @Override
- public boolean match(final ServiceReference reference) {
- 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/MockServiceRegistration.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
index fafc13b..1c05f05 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
@@ -24,9 +24,11 @@ import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
+import org.apache.felix.framework.FilterImpl;
import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.OsgiMetadata;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
@@ -79,9 +81,10 @@ class MockServiceRegistration implements ServiceRegistration, Comparable<MockSer
return this.properties;
}
- boolean matches(final String clazz, final String filter) {
+ boolean matches(final String clazz, final String filter) throws InvalidSyntaxException {
// ignore filter for now
- return this.clazzes.contains(clazz);
+ return this.clazzes.contains(clazz)
+ && (filter == null || new FilterImpl(filter).match(properties));
}
Set<String> getClasses() {
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java b/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
index 27abf9f..c200f8e 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
@@ -171,8 +171,7 @@ public class OsgiContextImpl {
@SuppressWarnings("unchecked")
public final <ServiceType> ServiceType[] getServices(final Class<ServiceType> serviceType, final String filter) {
try {
- ServiceReference[] serviceReferences = bundleContext().getServiceReferences(serviceType.getName(),
- filter);
+ ServiceReference[] serviceReferences = bundleContext().getServiceReferences(serviceType.getName(), filter);
if (serviceReferences != null) {
ServiceType[] services = (ServiceType[])Array.newInstance(serviceType, serviceReferences.length);
for (int i = 0; i < serviceReferences.length; i++) {
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 00d1384..092ab81 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
@@ -192,17 +192,6 @@ public class MockBundleContextTest {
}
@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() + ")");
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockFilterTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockFilterTest.java
deleted file mode 100644
index 9c4a955..0000000
--- a/src/test/java/org/apache/sling/testing/mock/osgi/MockFilterTest.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * 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 static org.junit.Assert.assertFalse;
-
-import java.util.Hashtable;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.osgi.framework.Filter;
-import org.osgi.framework.ServiceReference;
-
-public class MockFilterTest {
-
- private Filter filter;
-
- @Before
- public void setUp() {
- this.filter = new MockFilter();
- }
-
- @Test
- public void testDenyAll() {
- assertFalse(this.filter.match((ServiceReference) null));
- assertFalse(this.filter.match((Hashtable) null));
- assertFalse(this.filter.matchCase(null));
- }
-
-}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.