Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error when no bundle context is available #504

Merged
merged 2 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.osgi.test.common.context;

import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toMap;
import static org.osgi.test.common.exceptions.ConsumerWithException.asConsumer;
import static org.osgi.test.common.exceptions.ConsumerWithException.asConsumerIgnoreException;
Expand Down Expand Up @@ -138,7 +139,7 @@ public static BundleContext proxy(BundleContext bundleContext) {
}

public CloseableBundleContext(BundleContext bundleContext) {
this.bundleContext = bundleContext;
this.bundleContext = requireNonNull(bundleContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) Contributors to the Eclipse Foundation
*
* Licensed 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.
*
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/

package org.osgi.test.common.context;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.test.common.bitmaps.BundleState;

public final class ContextHelper {
private ContextHelper() {}

public static BundleContext getBundleContext(Class<?> testClass) {
Bundle bundle = FrameworkUtil.getBundle(testClass);
if (bundle == null) {
throw new IllegalStateException(
String.format("No BundleContext available - The class (%s) must be loaded from a bundle", testClass));
}
BundleContext context = bundle.getBundleContext();
if (context == null) {
throw new IllegalStateException(String.format(
"No BundleContext available - The bundle of the class (%s) must be started to have a BundleContext; bundle current state: %s",
testClass, BundleState.toString(bundle.getState())));
}
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
*******************************************************************************/

@org.osgi.annotation.bundle.Export
@org.osgi.annotation.versioning.Version("1.0.0")
@org.osgi.annotation.versioning.Version("1.1.0")
package org.osgi.test.common.context;
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.osgi.test.common.install;

import static java.util.Objects.requireNonNull;
import static org.osgi.test.common.exceptions.Exceptions.duck;

import java.io.FileNotFoundException;
Expand Down Expand Up @@ -243,7 +244,7 @@ public String toString() {
private final BundleContext bundleContext;

public BundleInstaller(BundleContext bundleContext) {
this.bundleContext = bundleContext;
this.bundleContext = requireNonNull(bundleContext);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public ServiceConfiguration(Class<S> serviceType, String format, String[] args,
}

public ServiceConfiguration<S> init(BundleContext bundleContext) {
requireNonNull(bundleContext);
CountDownLatch countDownLatch = new CountDownLatch(getCardinality());

ServiceTracker<S, S> tracker = new ServiceTracker<>(bundleContext, getFilter(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.test.common.annotation.InjectBundleContext;
import org.osgi.test.common.annotation.InjectBundleInstaller;
import org.osgi.test.common.context.CloseableBundleContext;
import org.osgi.test.common.context.ContextHelper;
import org.osgi.test.common.install.BundleInstaller;

/**
Expand Down Expand Up @@ -71,8 +71,8 @@ public BundleContextRule init(Object testInstance) {
return this;
}

BundleContext bundleContext = CloseableBundleContext.proxy(FrameworkUtil.getBundle(testInstance.getClass())
.getBundleContext());
BundleContext bundleContext = CloseableBundleContext
.proxy(ContextHelper.getBundleContext(testInstance.getClass()));

bundleInstaller = new BundleInstaller(bundleContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.common.context.ContextHelper;
import org.osgi.test.common.list.ListSupplierDelegate;
import org.osgi.test.common.service.ServiceAware;
import org.osgi.test.common.service.ServiceConfiguration;
Expand Down Expand Up @@ -66,9 +66,7 @@ public class ServiceRule implements AutoCloseable, MethodRule {
private final Map<ServiceConfigurationKey<?>, ServiceConfiguration<?>> configurations = new ConcurrentHashMap<>();

public ServiceRule init(Object testInstance) {
BundleContext bundleContext = FrameworkUtil.getBundle(testInstance
.getClass())
.getBundleContext();
BundleContext bundleContext = ContextHelper.getBundleContext(testInstance.getClass());
List<Field> fields = findAnnotatedNonStaticFields(testInstance.getClass(), InjectService.class);

fields.forEach(field -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.test.common.annotation.InjectBundleContext;
import org.osgi.test.common.context.CloseableBundleContext;
import org.osgi.test.common.context.ContextHelper;
import org.osgi.test.common.inject.TargetType;
import org.osgi.test.common.install.BundleInstaller;
import org.osgi.test.junit5.inject.InjectingExtension;
Expand Down Expand Up @@ -94,8 +94,7 @@ private static BundleContext getParentBundleContext(ExtensionContext extensionCo
.filter(context -> context.getTestClass()
.isPresent())
.map(BundleContextExtension::getBundleContext)
.orElseGet(() -> FrameworkUtil.getBundle(extensionContext.getRequiredTestClass())
.getBundleContext());
.orElseGet(() -> ContextHelper.getBundleContext(extensionContext.getRequiredTestClass()));
return parentContext;
}

Expand Down Expand Up @@ -130,11 +129,10 @@ static Store getStore(ExtensionContext extensionContext) {
@Override
protected Object resolveValue(TargetType targetType, InjectBundleContext injection,
ExtensionContext extensionContext) throws ParameterResolutionException {
BundleContext retval = getBundleContext(extensionContext);
if (retval == null) {
throw new ParameterResolutionException(
"Bundle context was null. Check that you are running your test in an OSGi framework.");
try {
return getBundleContext(extensionContext);
} catch (IllegalStateException e) {
throw new ParameterResolutionException("No BundleContext available", e);
}
return retval;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.test.common.annotation.InjectInstalledBundle;
import org.osgi.test.common.inject.TargetType;
import org.osgi.test.common.install.BundleInstaller;
Expand All @@ -50,21 +49,30 @@ public InstalledBundleExtension() {

public static Bundle installedBundleOf(InjectInstalledBundle injectBundle, ExtensionContext extensionContext) {
try {
BundleContext bc = BundleContextExtension.getBundleContext(extensionContext);
BundleInstaller ib = BundleInstallerExtension.getBundleInstaller(extensionContext);

String spec = injectBundle.value();
if (spec.startsWith("http:") || spec.startsWith("https:") || spec.startsWith("file:")) {
if (prefixMatch(spec, "http:", "https:", "file:")) {
return ib.installBundle(new URL(spec), injectBundle.start());
} else {
return ib.installBundle(BundleInstaller.EmbeddedLocation.of(bc, spec), injectBundle.start());
return ib.installBundle(BundleInstaller.EmbeddedLocation.of(ib.getBundleContext(), spec),
injectBundle.start());
}
} catch (MalformedURLException e) {
throw new ExtensionConfigurationException(
String.format("Could not parse URL from given String %s.", injectBundle.value()), e);
}
}

private static boolean prefixMatch(String target, String... prefixes) {
for (String prefix : prefixes) {
if (target.regionMatches(true, 0, prefix, 0, prefix.length())) {
return true;
}
}
return false;
}

@Override
protected Object resolveValue(TargetType targetType, InjectInstalledBundle injectBundle,
ExtensionContext extensionContext) throws ParameterResolutionException {
Expand Down