Search in sources :

Example 66 with BundleRevision

use of org.osgi.framework.wiring.BundleRevision in project ddf by codice.

the class ApplicationServiceImplTest method createMockFeaturesService.

/**
     * Creates a mock {@code FeaturesService} object consisting of all of the
     * features contained in a {@code Set} of {@code Repository} objects. Each
     * {@code Feature} will be in the <i>installed</i> state unless it is
     * contained in the received set of features that are not to be installed.
     * Each {@code Bundle} will be in the {@code Bundle#ACTIVE} state and the
     * {@code BundleState#Active} extended bundle state (as reported by a
     * dependency injection framework) unless it is contained in the received
     * set of {@code Bundle}s that are not to be active, in which case the
     * {@code Bundle} will be in the {@code Bundle#INSTALLED} state and the
     * {@code BundleState#Installed} extended bundle state.
     * <p>
     * Note that not all of the state and {@code Bundle} information is
     * contained in the {@code FeaturesService}. As such, this method stores
     * some of the required information in the class's {@code #bundleContext}
     * and {@code bundleStateServices}. As such, these objects must be
     * re-instantiated for each test (i.e., they must be instantiated in the
     * {@link #setUp()} method).
     *
     * @param repos                A {@code Set} of {@link Repository} objects from which to
     *                             obtain the {@link Feature}s that are to be included in the
     *                             mock {@code FeaturesService}
     * @param notInstalledFeatures A {@code Set} of {@code Feature}s that the
     *                             {@code FeaturesService} should report as not installed
     * @param inactiveBundles      A {@code Set} of {@link BundleInfo}s containing the locations
     *                             of {@code Bundle}s that should be set to inactive and for
     *                             which the {@link BundleStateService} contained in index 0 of
     *                             {@link #bundleStateServices} should report a
     *                             {@link BundleState#Installed} state.
     * @return A mock {@link FeaturesService} with {@link Feature}s and
     * {@link Bundle}s in the requested states.
     * @throws Exception
     */
private FeaturesService createMockFeaturesService(Set<Repository> repos, Set<Feature> notInstalledFeatures, Set<BundleInfo> inactiveBundles) throws Exception {
    if (LOGGER.isTraceEnabled()) {
        for (Repository repo : repos) {
            for (Feature feature : repo.getFeatures()) {
                LOGGER.trace("Repo Feature: {}", feature);
                LOGGER.trace("Repo Feature name/version: {}/{}", feature.getName(), feature.getVersion());
                LOGGER.trace("Dependencies: ");
                for (Dependency depFeature : feature.getDependencies()) {
                    LOGGER.trace("Dependency Feature: {}", depFeature);
                    LOGGER.trace("Dependency Feature name/version: {}/{}", depFeature.getName(), depFeature.getVersion());
                }
            }
        }
    }
    if (null == notInstalledFeatures) {
        notInstalledFeatures = new HashSet<Feature>();
    }
    if (null == inactiveBundles) {
        inactiveBundles = new HashSet<BundleInfo>();
    }
    Set<String> installedBundleLocations = new HashSet<String>();
    for (BundleInfo bundleInfo : inactiveBundles) {
        installedBundleLocations.add(bundleInfo.getLocation());
    }
    FeaturesService featuresService = mock(FeaturesService.class);
    Set<Feature> featuresSet = new HashSet<Feature>();
    BundleRevision mockBundleRevision = mock(BundleRevision.class);
    when(mockBundleRevision.getTypes()).thenReturn(0);
    for (Repository curRepo : repos) {
        for (Feature curFeature : curRepo.getFeatures()) {
            featuresSet.add(curFeature);
            when(featuresService.getFeature(curFeature.getName())).thenReturn(curFeature);
            when(featuresService.getFeature(curFeature.getName(), curFeature.getVersion())).thenReturn(curFeature);
            // TODO: File Karaf bug that necessitates this, then reference
            // it here.
            when(featuresService.getFeature(curFeature.getName(), "0.0.0")).thenReturn(curFeature);
            when(featuresService.isInstalled(curFeature)).thenReturn(!notInstalledFeatures.contains(curFeature));
            // of that bundle, this logic will need to be modified.
            for (BundleInfo bundleInfo : curFeature.getBundles()) {
                if (installedBundleLocations.contains(bundleInfo.getLocation())) {
                    Bundle mockInstalledBundle = mock(Bundle.class);
                    when(mockInstalledBundle.getState()).thenReturn(Bundle.INSTALLED);
                    when(mockInstalledBundle.adapt(BundleRevision.class)).thenReturn(mockBundleRevision);
                    when(bundleContext.getBundle(bundleInfo.getLocation())).thenReturn(mockInstalledBundle);
                    when(bundleStateServices.get(0).getState(mockInstalledBundle)).thenReturn(BundleState.Installed);
                } else {
                    Bundle mockActiveBundle = mock(Bundle.class);
                    when(mockActiveBundle.getState()).thenReturn(Bundle.ACTIVE);
                    when(mockActiveBundle.adapt(BundleRevision.class)).thenReturn(mockBundleRevision);
                    when(bundleContext.getBundle(bundleInfo.getLocation())).thenReturn(mockActiveBundle);
                    when(bundleStateServices.get(0).getState(mockActiveBundle)).thenReturn(BundleState.Active);
                }
            }
        }
    }
    when(featuresService.listRepositories()).thenReturn(repos.toArray(new Repository[repos.size()]));
    when(featuresService.listFeatures()).thenReturn(featuresSet.toArray(new Feature[] {}));
    return featuresService;
}
Also used : Bundle(org.osgi.framework.Bundle) Dependency(org.apache.karaf.features.Dependency) Mockito.anyString(org.mockito.Mockito.anyString) Feature(org.apache.karaf.features.Feature) Repository(org.apache.karaf.features.Repository) BundleInfo(org.apache.karaf.features.BundleInfo) BundleRevision(org.osgi.framework.wiring.BundleRevision) FeaturesService(org.apache.karaf.features.FeaturesService) HashSet(java.util.HashSet)

Aggregations

BundleRevision (org.osgi.framework.wiring.BundleRevision)66 Bundle (org.osgi.framework.Bundle)38 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)14 Map (java.util.Map)11 Test (org.junit.Test)11 BundleCapability (org.osgi.framework.wiring.BundleCapability)11 BundleWiring (org.osgi.framework.wiring.BundleWiring)11 Resource (org.osgi.resource.Resource)11 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)9 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)9 HashSet (java.util.HashSet)8 BundleWire (org.osgi.framework.wiring.BundleWire)8 BundleConstituent (org.apache.aries.subsystem.core.internal.BundleResourceInstaller.BundleConstituent)7 Set (java.util.Set)6 TreeMap (java.util.TreeMap)6 TabularData (javax.management.openmbean.TabularData)6 IOException (java.io.IOException)5 Collection (java.util.Collection)5 List (java.util.List)5