SLING-8784 : Move api regions analysers to api regions extension
diff --git a/src/main/java/org/apache/sling/feature/analyser/task/impl/ApiRegions.java b/src/main/java/org/apache/sling/feature/analyser/task/impl/ApiRegions.java
deleted file mode 100644
index 78507ef..0000000
--- a/src/main/java/org/apache/sling/feature/analyser/task/impl/ApiRegions.java
+++ /dev/null
@@ -1,135 +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.feature.analyser.task.impl;
-
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
-
-import javax.json.Json;
-import javax.json.stream.JsonParser;
-import javax.json.stream.JsonParser.Event;
-
-/**
- * A "Sieve" data structure to check exported packages
- */
-final class ApiRegions {
-
- private static final String NAME_KEY = "name";
-
- private static final String EXPORTS_KEY = "exports";
-
- static ApiRegions fromJson(String jsonRepresentation) {
- ApiRegions apiRegions = new ApiRegions();
-
- // pointers
- Event event;
- String region = null;
- Collection<String> apis = null;
-
- JsonParser parser = Json.createParser(new StringReader(jsonRepresentation));
- while (parser.hasNext()) {
- event = parser.next();
- if (Event.KEY_NAME == event) {
- switch (parser.getString()) {
- case NAME_KEY:
- parser.next();
- region = parser.getString();
- break;
-
- case EXPORTS_KEY:
- apis = new LinkedList<>();
-
- // start array
- parser.next();
-
- while (parser.hasNext() && Event.VALUE_STRING == parser.next()) {
- String api = parser.getString();
- // skip comments
- if ('#' != api.charAt(0)) {
- apis.add(api);
- }
- }
-
- break;
-
- default:
- break;
- }
- } else if (Event.END_OBJECT == event) {
- if (region != null && apis != null) {
- apiRegions.add(region, apis);
- }
-
- region = null;
- apis = null;
- }
- }
-
- return apiRegions;
- }
-
- // Use Linked Hash Map to keep the order of the regions as specified in the JSON
- private final Map<String, Set<String>> apis = new LinkedHashMap<>();
-
- ApiRegions() {
- // it should not be directly instantiated outside this package
- }
-
- void add(String region, Collection<String> exportedApis) {
- apis.computeIfAbsent(region, k -> new TreeSet<>()).addAll(exportedApis);
- }
-
- /**
- * Return the regions in the order they were listed.
- * @return The regions.
- */
- List<String> getRegions() {
- // As the regions are stored in a LinkedHashMap the order is preserved.
- // So we can return the keys.
- return new ArrayList<>(apis.keySet());
- }
-
- Set<String> getApis(String region) {
- return apis.computeIfAbsent(region, k -> Collections.emptySet());
- }
-
- void remove(String packageName) {
- apis.values().forEach(apis -> apis.remove(packageName));
- }
-
- boolean isEmpty() {
- for (Set<String> packages : apis.values()) {
- if (!packages.isEmpty()) {
- return false;
- }
- }
- return true;
- }
-
- @Override
- public String toString() {
- return apis.toString().replace(',', '\n');
- }
-}
diff --git a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckBundleExportsImports.java b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckBundleExportsImports.java
index 4bb72bd..baa2100 100644
--- a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckBundleExportsImports.java
+++ b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckBundleExportsImports.java
@@ -18,27 +18,14 @@
*/
package org.apache.sling.feature.analyser.task.impl;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.IOException;
-import java.io.InputStream;
-import java.util.AbstractMap;
import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.List;
import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
-import java.util.stream.Collectors;
-import org.apache.sling.feature.Extension;
-import org.apache.sling.feature.Feature;
import org.apache.sling.feature.analyser.task.AnalyserTask;
import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
import org.apache.sling.feature.scanner.BundleDescriptor;
@@ -46,12 +33,6 @@
import org.osgi.framework.Version;
public class CheckBundleExportsImports implements AnalyserTask {
- private static final String FILE_STORAGE_CONFIG_KEY = "fileStorage";
- private static final String IGNORE_API_REGIONS_CONFIG_KEY = "ignoreAPIRegions";
- private static final String API_REGIONS = "api-regions";
- private static final String GLOBAL_REGION = "global";
- private static final String NO_REGION = " __NO_REGION__ ";
- private static final String OWN_FEATURE = " __OWN_FEATURE__ ";
@Override
public String getName() {
@@ -76,8 +57,6 @@
public List<PackageInfo> missingExportsWithVersion = new ArrayList<>();
public List<PackageInfo> missingExportsForOptional = new ArrayList<>();
-
- public Map<PackageInfo, Map.Entry<Set<String>, Set<String>>> regionInfo = new HashMap<>();
}
private Report getReport(final Map<BundleDescriptor, Report> reports, final BundleDescriptor info) {
@@ -117,11 +96,9 @@
}
}
+
@Override
public void execute(final AnalyserTaskContext ctx) throws IOException {
- boolean ignoreAPIRegions = ctx.getConfiguration().getOrDefault(
- IGNORE_API_REGIONS_CONFIG_KEY, "false").equalsIgnoreCase("true");
-
// basic checks
final Map<BundleDescriptor, Report> reports = new HashMap<>();
checkForVersionOnExportedPackages(ctx, reports);
@@ -143,19 +120,6 @@
exportingBundles.add(ctx.getFrameworkDescriptor());
}
- ApiRegions apiRegions;
- Map<String, Set<String>> bundleToOriginalFeatures;
- Map<String, Set<String>> featureToOriginalRegions;
- if (ignoreAPIRegions) {
- apiRegions = new ApiRegions(); // Empty API Regions
- bundleToOriginalFeatures = Collections.emptyMap();
- featureToOriginalRegions = Collections.emptyMap();
- } else {
- apiRegions = readAPIRegionsFromFeature(ctx);
- bundleToOriginalFeatures = readBundleOrigins(ctx);
- featureToOriginalRegions = readRegionOrigins(ctx);
- }
-
for(final Map.Entry<Integer, List<BundleDescriptor>> entry : bundlesMap.entrySet()) {
// first add all exporting bundles
for(final BundleDescriptor info : entry.getValue()) {
@@ -167,9 +131,7 @@
for(final BundleDescriptor info : entry.getValue()) {
if ( info.getImportedPackages() != null ) {
for(final PackageInfo pck : info.getImportedPackages() ) {
- final Map<BundleDescriptor, Set<String>> candidates =
- getCandidates(exportingBundles, pck, info,
- bundleToOriginalFeatures, featureToOriginalRegions, apiRegions);
+ final List<BundleDescriptor> candidates = getCandidates(exportingBundles, pck);
if ( candidates.isEmpty() ) {
if ( pck.isOptional() ) {
getReport(reports, info).missingExportsForOptional.add(pck);
@@ -178,53 +140,16 @@
}
} else {
final List<BundleDescriptor> matchingCandidates = new ArrayList<>();
-
- Set<String> exportingRegions = new HashSet<>();
- Set<String> importingRegions = new HashSet<>();
- for(final Map.Entry<BundleDescriptor, Set<String>> candidate : candidates.entrySet()) {
- BundleDescriptor bd = candidate.getKey();
- if (bd.isExportingPackage(pck)) {
- Set<String> exRegions = candidate.getValue();
- if (exRegions.contains(NO_REGION)) {
- // If an export is defined outside of a region, it always matches
- matchingCandidates.add(bd);
- continue;
- }
- if (exRegions.contains(GLOBAL_REGION)) {
- // Everyone can import from the global regin
- matchingCandidates.add(bd);
- continue;
- }
- if (exRegions.contains(OWN_FEATURE)) {
- // A feature can always import packages from bundles in itself
- matchingCandidates.add(bd);
- continue;
- }
-
- // Find out what regions the importing bundle is in
- Set<String> imRegions =
- getBundleRegions(info, bundleToOriginalFeatures, featureToOriginalRegions);
-
- // Record the exporting and importing regions for diagnostics
- exportingRegions.addAll(exRegions);
- importingRegions.addAll(imRegions);
-
- // Only keep the regions that also export the package
- imRegions.retainAll(exRegions);
-
- if (!imRegions.isEmpty()) {
- // there is an overlapping region
- matchingCandidates.add(bd);
- }
+ for (final BundleDescriptor i : candidates) {
+ if (i.isExportingPackage(pck)) {
+ matchingCandidates.add(i);
}
}
-
if ( matchingCandidates.isEmpty() ) {
if ( pck.isOptional() ) {
getReport(reports, info).missingExportsForOptional.add(pck);
} else {
getReport(reports, info).missingExportsWithVersion.add(pck);
- getReport(reports, info).regionInfo.put(pck, new AbstractMap.SimpleEntry<>(exportingRegions, importingRegions));
}
} else if ( matchingCandidates.size() > 1 ) {
getReport(reports, info).exportMatchingSeveral.add(pck);
@@ -253,18 +178,10 @@
errorReported = true;
}
if ( !entry.getValue().missingExportsWithVersion.isEmpty() ) {
- StringBuilder message = new StringBuilder(key + " is importing package(s) " + getPackageInfo(entry.getValue().missingExportsWithVersion, true) + " in start level " +
- String.valueOf(entry.getKey().getArtifact().getStartOrder())
- + " but no visible bundle is exporting these for that start level in the required version range.");
-
- for (Map.Entry<PackageInfo, Map.Entry<Set<String>, Set<String>>> regionInfoEntry : entry.getValue().regionInfo.entrySet()) {
- PackageInfo pkg = regionInfoEntry.getKey();
- Map.Entry<Set<String>, Set<String>> regions = regionInfoEntry.getValue();
- if (regions.getKey().size() > 0) {
- message.append("\n" + pkg.getName() + " is exported in regions " + regions.getKey() + " but it is imported in regions " + regions.getValue());
- }
- }
- ctx.reportError(message.toString());
+ ctx.reportError(key + " is importing package(s) "
+ + getPackageInfo(entry.getValue().missingExportsWithVersion, true) + " in start level "
+ + String.valueOf(entry.getKey().getArtifact().getStartOrder())
+ + " but no bundle is exporting these for that start level in the required version range.");
errorReported = true;
}
}
@@ -273,40 +190,6 @@
}
}
- private Set<String> getBundleRegions(BundleDescriptor info, Map<String, Set<String>> bundleToOriginalFeatures,
- Map<String, Set<String>> featureToOriginalRegions) {
- Set<String> result = new HashSet<>();
-
- Set<String> originFeatures = bundleToOriginalFeatures.get(info.getArtifact().getId().toMvnId());
- if (originFeatures != null) {
- for (String feature : originFeatures) {
- Set<String> originRegions = featureToOriginalRegions.get(feature);
- if (originRegions != null) {
- result.addAll(originRegions);
- }
- }
- }
-
- if (result.size() == 0) {
- result.add(NO_REGION);
- }
- return result;
- }
-
- private ApiRegions readAPIRegionsFromFeature(final AnalyserTaskContext ctx) {
- Feature feature = ctx.getFeature();
- Extension apiRegionsExtension = feature.getExtensions().getByName(API_REGIONS);
- if (apiRegionsExtension == null)
- return null;
-
- String apiRegionsJSON = apiRegionsExtension.getJSON();
- if (apiRegionsJSON != null && !apiRegionsJSON.isEmpty()) {
- return ApiRegions.fromJson(apiRegionsJSON);
- } else {
- return null;
- }
- }
-
private String getPackageInfo(final List<PackageInfo> pcks, final boolean includeVersion) {
if ( pcks.size() == 1 ) {
if (includeVersion) {
@@ -334,96 +217,13 @@
return sb.toString();
}
- private Map<BundleDescriptor, Set<String>> getCandidates(
- final List<BundleDescriptor> exportingBundles,
- final PackageInfo pck,
- final BundleDescriptor requestingBundle,
- final Map<String, Set<String>> bundleToOriginalFeatures,
- final Map<String, Set<String>> featureToOriginalRegions,
- final ApiRegions apiRegions) throws IOException {
- Set<String> rf = bundleToOriginalFeatures.get(
- requestingBundle.getArtifact().getId().toMvnId());
- if (rf == null)
- rf = Collections.emptySet();
- final Set<String> requestingFeatures = rf;
-
- final Map<BundleDescriptor, Set<String>> candidates = new HashMap<>();
+ private List<BundleDescriptor> getCandidates(final List<BundleDescriptor> exportingBundles, final PackageInfo pck) {
+ final List<BundleDescriptor> candidates = new ArrayList<>();
for(final BundleDescriptor info : exportingBundles) {
if ( info.isExportingPackage(pck.getName()) ) {
- Set<String> providingFeatures = bundleToOriginalFeatures.get(
- info.getArtifact().getId().toMvnId());
- if (providingFeatures == null)
- providingFeatures = Collections.emptySet();
-
- // Compute the intersection without modifying the sets
- Set<String> intersection = providingFeatures.stream().filter(
- s -> requestingFeatures.contains(s)).collect(Collectors.toSet());
- if (!intersection.isEmpty()) {
- // A requesting bundle can see all exported packages inside its own feature
- candidates.put(info, Collections.singleton(OWN_FEATURE));
- continue;
- }
-
- for (String region : getBundleRegions(info, bundleToOriginalFeatures, featureToOriginalRegions)) {
- if (!NO_REGION.equals(region) &&
- !apiRegions.getApis(region).contains(pck.getName()))
- continue;
-
- Set<String> regions = candidates.get(info);
- if (regions == null) {
- regions = new HashSet<>();
- candidates.put(info, regions);
- }
- regions.add(region);
- }
+ candidates.add(info);
}
}
return candidates;
}
-
- private Map<String, Set<String>> readBundleOrigins(AnalyserTaskContext ctx) throws IOException {
- return readOrigins(ctx, "bundleOrigins.properties");
- }
-
- private Map<String, Set<String>> readRegionOrigins(AnalyserTaskContext ctx) throws IOException {
- return readOrigins(ctx, "regionOrigins.properties");
- }
-
- private Map<String, Set<String>> readOrigins(AnalyserTaskContext ctx, String fileName) throws IOException, FileNotFoundException {
- Feature feature = ctx.getFeature();
- Extension APIRegionExtension = feature.getExtensions().getByName(API_REGIONS);
-
- String fileStorage = ctx.getConfiguration().get(FILE_STORAGE_CONFIG_KEY);
- if (fileStorage == null) {
- if (APIRegionExtension != null) {
- throw new IllegalStateException("Feature " + feature.getId() +
- " has API regions defined, but no storage is configured for origin information files. "
- + "Please configure the " + FILE_STORAGE_CONFIG_KEY + " configuration item.");
- }
- return Collections.emptyMap();
- }
-
- String featureName = feature.getId().toMvnId().replaceAll("[^a-zA-Z0-9\\.\\-]", "_");
- File file = new File(fileStorage, featureName + File.separator + fileName);
- if (!file.exists()) {
- if (APIRegionExtension != null)
- throw new IllegalStateException("Feature " + feature.getId() +
- " has API regions defined but no file with origin information can be found " + file +
- " Configure the org.apache.sling.feature.extension.apiregions appropriately to write this information");
- return Collections.emptyMap();
- }
-
- Properties p = new Properties();
- try (InputStream is = new FileInputStream(file)) {
- p.load(is);
- }
-
- Map<String, Set<String>> m = new HashMap<>();
- for (String key : p.stringPropertyNames()) {
- String val = p.getProperty(key);
- String[] features = val.split(",");
- m.put(key, new HashSet<>(Arrays.asList(features)));
- }
- return m;
- }
-}
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckBundleExportsImportsTest.java b/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckBundleExportsImportsTest.java
index 3cb451a..e56e429 100644
--- a/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckBundleExportsImportsTest.java
+++ b/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckBundleExportsImportsTest.java
@@ -23,8 +23,6 @@
import java.io.File;
import java.io.IOException;
import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
import org.apache.sling.feature.Artifact;
import org.apache.sling.feature.ArtifactId;
@@ -62,7 +60,7 @@
* Bundle b3 imports org.foo.e, but no bundle exports it. The feature is marked
* as complete which it isn't
*/
- public void testImportExportNoRegionsMarkedAsComplete() throws Exception {
+ public void testImportExportMarkedAsComplete() throws Exception {
CheckBundleExportsImports t = new CheckBundleExportsImports();
Feature f = new Feature(ArtifactId.fromMvnId("f:f:1"));
@@ -84,7 +82,7 @@
}
@Test
- public void testImportExportNoRegionsAllOk() throws IOException {
+ public void testImportExportAllOk() throws IOException {
CheckBundleExportsImports t = new CheckBundleExportsImports();
Feature f = new Feature(ArtifactId.fromMvnId("f:f:1"));
@@ -106,7 +104,7 @@
/*
* Bundle b3 imports org.foo.e, but no bundle exports it
*/
- public void testImportExportNoRegionsMissing() throws IOException {
+ public void testImportExportMissing() throws IOException {
CheckBundleExportsImports t = new CheckBundleExportsImports();
Feature f = new Feature(ArtifactId.fromMvnId("f:f:1"));
@@ -127,111 +125,6 @@
@Test
/*
- * Bundle 2 imports org.foo.b from bundle 1, but bundle 1 exports it in a different
- * region, bundle 2 is in no region.
- */
- public void testImportExportWithRegionsMissing() throws Exception {
- String exJson = "[{\"name\": \"something\", \"exports\": [\"org.foo.b\"]}]";
-
- CheckBundleExportsImports t = new CheckBundleExportsImports();
-
- Feature f = new Feature(ArtifactId.fromMvnId("f:f:1"));
- Extension ex = new Extension(ExtensionType.JSON, "api-regions", ExtensionState.OPTIONAL);
- ex.setJSON(exJson);
- f.getExtensions().add(ex);
-
- FeatureDescriptor fd = new FeatureDescriptorImpl(f);
-
- fdAddBundle(fd, "g:b1:1", "test-bundle1.jar");
- fdAddBundle(fd, "g:b2:1", "test-bundle2.jar");
-
- AnalyserTaskContext ctx = Mockito.mock(AnalyserTaskContext.class);
- Mockito.when(ctx.getFeature()).thenReturn(f);
- Mockito.when(ctx.getFeatureDescriptor()).thenReturn(fd);
- Mockito.when(ctx.getConfiguration()).thenReturn(
- Collections.singletonMap("fileStorage",
- resourceRoot + "/origins/testImportExportWithRegionsMissing"));
- t.execute(ctx);
-
- Mockito.verify(ctx).reportError(Mockito.contains("org.foo.b"));
- Mockito.verify(ctx, Mockito.times(1)).reportError(Mockito.anyString());
- Mockito.verify(ctx, Mockito.never()).reportWarning(Mockito.anyString());
- }
-
- @Test
- /*
- * Bundle 2 imports org.foo.b from bundle 1, but bundle 1 exports it in a different
- * region, bundle 1 is in something region, and bundle 2 is in somethingelse region.
- */
- public void testImportExportWithRegionMismatch() throws Exception {
- String exJson = "[{\"name\": \"something\", \"exports\": [\"org.foo.b\"]}]";
-
- CheckBundleExportsImports t = new CheckBundleExportsImports();
-
- Feature f = new Feature(ArtifactId.fromMvnId("f:f:1"));
- Extension ex = new Extension(ExtensionType.JSON, "api-regions", ExtensionState.OPTIONAL);
- ex.setJSON(exJson);
- f.getExtensions().add(ex);
-
- FeatureDescriptor fd = new FeatureDescriptorImpl(f);
-
- fdAddBundle(fd, "g:b1:1", "test-bundle1.jar");
- fdAddBundle(fd, "g:b2:1", "test-bundle2.jar");
-
- Map<String, String> cfgMap = new HashMap<String, String>();
- cfgMap.put("fileStorage", resourceRoot + "/origins/testImportExportWithRegionMismatch");
- cfgMap.put("ignoreAPIRegions", "false");
-
- AnalyserTaskContext ctx = Mockito.mock(AnalyserTaskContext.class);
- Mockito.when(ctx.getFeature()).thenReturn(f);
- Mockito.when(ctx.getFeatureDescriptor()).thenReturn(fd);
- Mockito.when(ctx.getConfiguration()).thenReturn(cfgMap);
- t.execute(ctx);
-
- Mockito.verify(ctx).reportError(Mockito.contains("org.foo.b"));
- Mockito.verify(ctx).reportError(Mockito.contains("something"));
- Mockito.verify(ctx).reportError(Mockito.contains("somethingelse"));
- Mockito.verify(ctx, Mockito.times(1)).reportError(Mockito.anyString());
- Mockito.verify(ctx, Mockito.never()).reportWarning(Mockito.anyString());
- }
-
- @Test
- /*
- * Bundle 2 imports org.foo.b from bundle 1, but bundle 1 exports it in a different
- * region, bundle 1 is in something region, and bundle 2 is in somethingelse region.
- * However this should still pass as the analyzer is configured to ignore regions.
- */
- public void testImportExportWithRegionMismatchIgnoreRegions() throws Exception {
- String exJson = "[{\"name\": \"something\", \"exports\": [\"org.foo.b\"]}]";
-
- CheckBundleExportsImports t = new CheckBundleExportsImports();
-
- Feature f = new Feature(ArtifactId.fromMvnId("f:f:1"));
- Extension ex = new Extension(ExtensionType.JSON, "api-regions", ExtensionState.OPTIONAL);
- ex.setJSON(exJson);
- f.getExtensions().add(ex);
-
- FeatureDescriptor fd = new FeatureDescriptorImpl(f);
-
- fdAddBundle(fd, "g:b1:1", "test-bundle1.jar");
- fdAddBundle(fd, "g:b2:1", "test-bundle2.jar");
-
- Map<String, String> cfgMap = new HashMap<String, String>();
- cfgMap.put("fileStorage", resourceRoot + "/origins/testImportExportWithRegionMismatch");
- cfgMap.put("ignoreAPIRegions", "true");
-
- AnalyserTaskContext ctx = Mockito.mock(AnalyserTaskContext.class);
- Mockito.when(ctx.getFeature()).thenReturn(f);
- Mockito.when(ctx.getFeatureDescriptor()).thenReturn(fd);
- Mockito.when(ctx.getConfiguration()).thenReturn(cfgMap);
- t.execute(ctx);
-
- Mockito.verify(ctx, Mockito.never()).reportError(Mockito.anyString());
- Mockito.verify(ctx, Mockito.never()).reportWarning(Mockito.anyString());
- }
-
- @Test
- /*
* Bundle 3 imports org.foo.a from Bundle 1 and org.foo.e from Bundle 4.
* The Feature is in a region called 'blah' which exports nothing, but because
* all these bundles are in the same feature they can all see each other.
@@ -264,70 +157,6 @@
Mockito.verify(ctx, Mockito.never()).reportWarning(Mockito.anyString());
}
- @Test
- /*
- * Bundle 2 imports org.foo.b from bundle 1. Bundle 1 exports it in the something region
- * and bundle 2 imports it in the something region, so this succeeds.
- */
- public void testImportExportWithMatchingRegion() throws Exception {
- String exJson = "[{\"name\": \"something\", \"exports\": [\"org.foo.b\"]}]";
-
- CheckBundleExportsImports t = new CheckBundleExportsImports();
-
- Feature f = new Feature(ArtifactId.fromMvnId("f:f:1"));
- Extension ex = new Extension(ExtensionType.JSON, "api-regions", ExtensionState.OPTIONAL);
- ex.setJSON(exJson);
- f.getExtensions().add(ex);
-
- FeatureDescriptor fd = new FeatureDescriptorImpl(f);
-
- fdAddBundle(fd, "g:b1:1", "test-bundle1.jar");
- fdAddBundle(fd, "g:b2:1", "test-bundle2.jar");
-
- AnalyserTaskContext ctx = Mockito.mock(AnalyserTaskContext.class);
- Mockito.when(ctx.getFeature()).thenReturn(f);
- Mockito.when(ctx.getFeatureDescriptor()).thenReturn(fd);
- Mockito.when(ctx.getConfiguration()).thenReturn(
- Collections.singletonMap("fileStorage",
- resourceRoot + "/origins/testImportExportWithMatchingRegion"));
- t.execute(ctx);
-
- Mockito.verify(ctx, Mockito.never()).reportError(Mockito.anyString());
- Mockito.verify(ctx, Mockito.never()).reportWarning(Mockito.anyString());
- }
-
- @Test
- /*
- * Bundle 2 imports org.foo.b from bundle 1. Bundle 1 exports it in the global region.
- * Bundle 2 is not explicitly part of the global region, but can still see it
- */
- public void testImportFromGlobalAlwaysSucceeds() throws Exception {
- String exJson = "[{\"name\": \"global\", \"exports\": [\"org.foo.b\"]}]";
-
- CheckBundleExportsImports t = new CheckBundleExportsImports();
-
- Feature f = new Feature(ArtifactId.fromMvnId("f:f:1"));
- Extension ex = new Extension(ExtensionType.JSON, "api-regions", ExtensionState.OPTIONAL);
- ex.setJSON(exJson);
- f.getExtensions().add(ex);
-
- FeatureDescriptor fd = new FeatureDescriptorImpl(f);
-
- fdAddBundle(fd, "g:b1:1", "test-bundle1.jar");
- fdAddBundle(fd, "g:b2:1", "test-bundle2.jar");
-
- AnalyserTaskContext ctx = Mockito.mock(AnalyserTaskContext.class);
- Mockito.when(ctx.getFeature()).thenReturn(f);
- Mockito.when(ctx.getFeatureDescriptor()).thenReturn(fd);
- Mockito.when(ctx.getConfiguration()).thenReturn(
- Collections.singletonMap("fileStorage",
- resourceRoot + "/origins/testImportFromGlobalAlwaysSucceeds"));
- t.execute(ctx);
-
- Mockito.verify(ctx, Mockito.never()).reportError(Mockito.anyString());
- Mockito.verify(ctx, Mockito.never()).reportWarning(Mockito.anyString());
- }
-
private void fdAddBundle(FeatureDescriptor fd, String id, String file) throws IOException {
BundleDescriptor bd1 = new BundleDescriptorImpl(
new Artifact(ArtifactId.fromMvnId(id)), new File(resourceRoot, file).toURI().toURL(), 0);