You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/07/13 12:30:40 UTC
[sling-whiteboard] branch master updated: Improvements and testing
for the features service.
This is an automated email from the ASF dual-hosted git repository.
davidb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
The following commit(s) were added to refs/heads/master by this push:
new 4a7a397 Improvements and testing for the features service.
4a7a397 is described below
commit 4a7a397094d3e1e5e0d0f4106ec18123c8c1c723
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Fri Jul 13 14:30:13 2018 +0200
Improvements and testing for the features service.
---
featuremodel/feature-service/pom.xml | 17 ++----
.../org/apache/sling/feature/service/Features.java | 16 +++++-
.../sling/feature/service/FeaturesFactory.java | 18 +++++-
.../service/impl/FeaturesServiceFactoryImpl.java | 9 +--
.../feature/service/impl/FeaturesServiceImpl.java | 7 ++-
.../service/impl/FeatureServiceImplTest.java | 24 +++++---
.../impl/FeaturesServiceFactoryImplTest.java | 66 ++++++++++++++++++++++
7 files changed, 129 insertions(+), 28 deletions(-)
diff --git a/featuremodel/feature-service/pom.xml b/featuremodel/feature-service/pom.xml
index 82e798d..52089ef 100644
--- a/featuremodel/feature-service/pom.xml
+++ b/featuremodel/feature-service/pom.xml
@@ -63,12 +63,6 @@
</build>
<dependencies>
<dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.feature</artifactId>
- <version>0.1.3-SNAPSHOT</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.versioning</artifactId>
<scope>provided</scope>
@@ -78,11 +72,6 @@
<artifactId>osgi.core</artifactId>
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>osgi.cmpn</artifactId>
- <scope>provided</scope>
- </dependency>
<!-- Testing -->
<dependency>
@@ -90,6 +79,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>2.8.9</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/Features.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/Features.java
index afc3886..254f4c7 100644
--- a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/Features.java
+++ b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/Features.java
@@ -20,6 +20,20 @@ package org.apache.sling.feature.service;
import org.osgi.framework.Version;
+import java.util.Set;
+
+/**
+ * The Features service provides information over Features at runtime.
+ */
public interface Features {
- String getFeatureForBundle(String bsn, Version ver);
+ /**
+ * Find out what features a given bundle belongs to.
+ * @param bsn The symbolic name of the bundle.
+ * @param ver The version of the bundle.
+ * @return The set of Feature IDs in the Maven ID syntax of the features
+ * that the bundle belongs to. A bundle can belong to more than one
+ * feature. If a bundle is not part of a feature then the returned set
+ * must contain a {@code null} value to represent this.
+ */
+ Set<String> getFeaturesForBundle(String bsn, Version ver);
}
diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/FeaturesFactory.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/FeaturesFactory.java
index f1fc474..2bd7c9d 100644
--- a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/FeaturesFactory.java
+++ b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/FeaturesFactory.java
@@ -19,7 +19,23 @@
package org.apache.sling.feature.service;
import java.util.Map;
+import java.util.Set;
+/**
+ * Service to initialize the {@link Features} service.
+ */
public interface FeaturesFactory {
- void initialize(Map<String, String> bundleFeatureMapping);
+ /**
+ * Initialize the Features service. The implementation of this
+ * factory service will register a {@link Features} service
+ * based on the mapping provided.
+ * @param bundleFeatureMapping A mapping from a bundle to the features
+ * that this bundle belongs to. If a bundle is not part of a feature
+ * this must be represented by a {@code null} value in the set.
+ * Bundles are listed using the {@code symbolic-name:version} syntax.
+ * Features are described using the maven ID syntax:
+ * {@code groupID:artifactID:type:classifier:version} where
+ * type and classifier are optional.
+ */
+ void initialize(Map<String, Set<String>> bundleFeatureMapping);
}
diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceFactoryImpl.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceFactoryImpl.java
index 91de294..7ea17c3 100644
--- a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceFactoryImpl.java
+++ b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceFactoryImpl.java
@@ -27,7 +27,7 @@ import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
-import java.util.Map.Entry;
+import java.util.Set;
class FeaturesServiceFactoryImpl implements FeaturesFactory {
private final BundleContext bundleContext;
@@ -37,9 +37,10 @@ class FeaturesServiceFactoryImpl implements FeaturesFactory {
}
@Override
- public void initialize(Map<String, String> bfm) {
- Map<Entry<String, Version>, String> bundleFeatureMapping = new HashMap<>();
- for (Map.Entry<String, String> entry : bfm.entrySet()) {
+ public void initialize(Map<String, Set<String>> bfm) {
+ Map<Map.Entry<String, Version>, Set<String>> bundleFeatureMapping = new HashMap<>();
+
+ for (Map.Entry<String, Set<String>> entry : bfm.entrySet()) {
String[] bv = entry.getKey().split(":");
if (bv.length == 2) {
try {
diff --git a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceImpl.java b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceImpl.java
index c9e17b3..5ec7146 100644
--- a/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceImpl.java
+++ b/featuremodel/feature-service/src/main/java/org/apache/sling/feature/service/impl/FeaturesServiceImpl.java
@@ -24,16 +24,17 @@ import org.osgi.framework.Version;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.Map;
+import java.util.Set;
class FeaturesServiceImpl implements Features {
- private final Map<Map.Entry<String, Version>, String> bundleFeatureMap;
+ private final Map<Map.Entry<String, Version>, Set<String>> bundleFeatureMap;
- FeaturesServiceImpl(Map<Map.Entry<String, Version>, String> bundleIDFeatures) {
+ FeaturesServiceImpl(Map<Map.Entry<String, Version>, Set<String>> bundleIDFeatures) {
bundleFeatureMap = Collections.unmodifiableMap(bundleIDFeatures);
}
@Override
- public String getFeatureForBundle(String bsn, Version version) {
+ public Set<String> getFeaturesForBundle(String bsn, Version version) {
return bundleFeatureMap.get(new AbstractMap.SimpleEntry<String, Version>(bsn, version));
}
}
diff --git a/featuremodel/feature-service/src/test/java/org/apache/sling/feature/service/impl/FeatureServiceImplTest.java b/featuremodel/feature-service/src/test/java/org/apache/sling/feature/service/impl/FeatureServiceImplTest.java
index 32a1859..ba35fcc 100644
--- a/featuremodel/feature-service/src/test/java/org/apache/sling/feature/service/impl/FeatureServiceImplTest.java
+++ b/featuremodel/feature-service/src/test/java/org/apache/sling/feature/service/impl/FeatureServiceImplTest.java
@@ -23,9 +23,13 @@ import org.junit.Test;
import org.osgi.framework.Version;
import java.util.AbstractMap;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@@ -33,20 +37,24 @@ import static org.junit.Assert.assertNull;
public class FeatureServiceImplTest {
@Test
public void testFeatureService() {
- Map<Entry<String, Version>, String> bif = new HashMap<>();
+ Map<Entry<String, Version>, Set<String>> bif = new HashMap<>();
String f1 = "gid:aid:1.0.0:myfeature:slingfeature";
- bif.put(new AbstractMap.SimpleEntry<String,Version>("mybsn", new Version(1,2,3)), f1);
- bif.put(new AbstractMap.SimpleEntry<String,Version>("mybsn2", new Version(4,5,6)), f1);
+ bif.put(new AbstractMap.SimpleEntry<String,Version>("mybsn", new Version(1,2,3)),
+ new HashSet<>(Arrays.asList(f1)));
+ bif.put(new AbstractMap.SimpleEntry<String,Version>("mybsn2", new Version(4,5,6)),
+ new HashSet<>(Arrays.asList(f1, null)));
String f2 = "gid:aid2:1.0.0";
- bif.put(new AbstractMap.SimpleEntry<String,Version>("mybsn", new Version(7,8,9)), f2);
+ bif.put(new AbstractMap.SimpleEntry<String,Version>("mybsn", new Version(7,8,9)),
+ new HashSet<>(Collections.singleton(f2)));
Features fs = new FeaturesServiceImpl(bif);
- assertEquals(f1, fs.getFeatureForBundle("mybsn", new Version(1,2,3)));
- assertEquals(f1, fs.getFeatureForBundle("mybsn2", new Version(4,5,6)));
- assertEquals(f2, fs.getFeatureForBundle("mybsn", new Version(7,8,9)));
- assertNull(fs.getFeatureForBundle("mybsn2", new Version(1,2,3)));
+ assertEquals(Collections.singleton(f1), fs.getFeaturesForBundle("mybsn", new Version(1,2,3)));
+ assertEquals(new HashSet<>(Arrays.asList(null, f1)),
+ fs.getFeaturesForBundle("mybsn2", new Version(4,5,6)));
+ assertEquals(Collections.singleton(f2), fs.getFeaturesForBundle("mybsn", new Version(7,8,9)));
+ assertNull(fs.getFeaturesForBundle("mybsn2", new Version(1,2,3)));
}
}
diff --git a/featuremodel/feature-service/src/test/java/org/apache/sling/feature/service/impl/FeaturesServiceFactoryImplTest.java b/featuremodel/feature-service/src/test/java/org/apache/sling/feature/service/impl/FeaturesServiceFactoryImplTest.java
new file mode 100644
index 0000000..e7c809b
--- /dev/null
+++ b/featuremodel/feature-service/src/test/java/org/apache/sling/feature/service/impl/FeaturesServiceFactoryImplTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.feature.service.impl;
+
+import org.apache.sling.feature.service.Features;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Version;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+public class FeaturesServiceFactoryImplTest {
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testInitialize() {
+ List<Object> featuresService = new ArrayList<Object>();
+
+ BundleContext bc = Mockito.mock(BundleContext.class);
+ Mockito.when(bc.registerService(Mockito.isA(Class.class), Mockito.isA(Object.class), Mockito.isA(Dictionary.class)))
+ .then(i -> { featuresService.add(i.getArgument(1)); return null; });
+
+ FeaturesServiceFactoryImpl fsf = new FeaturesServiceFactoryImpl(bc);
+
+ Map<String, Set<String>> bfm = new HashMap<>();
+ bfm.put("foo:123", Collections.singleton("g:a:f1"));
+ bfm.put("bar:1.2.3", Collections.singleton(null));
+ bfm.put("incomplete", Collections.singleton("g:a:f1"));
+ bfm.put("tar:0.0.0", new HashSet<>(Arrays.asList("g:a:f1", null, "g:a:f2")));
+ fsf.initialize(bfm);
+
+ Features features = (Features) featuresService.get(0);
+ assertEquals(Collections.singleton("g:a:f1"), features.getFeaturesForBundle("foo", new Version(123,0,0)));
+ assertEquals(Collections.singleton(null), features.getFeaturesForBundle("bar", new Version(1,2,3)));
+ assertEquals(new HashSet<>(Arrays.asList(null, "g:a:f1", "g:a:f2")),
+ features.getFeaturesForBundle("tar", new Version(0,0,0)));
+ assertNull(features.getFeaturesForBundle("incomplete", new Version(0,0,0)));
+ }
+}