Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2b06d17b58deb6ee931ab0f817160f90efcbe0a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
 * Copyright (c) 2013, 2017 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.tests.container;

import java.io.IOException;
import java.net.URL;
import java.util.*;
import org.eclipse.osgi.container.*;
import org.eclipse.osgi.container.builders.OSGiManifestBuilderFactory;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.osgi.tests.container.dummys.*;
import org.eclipse.osgi.util.ManifestElement;
import org.junit.*;
import org.osgi.framework.*;
import org.osgi.framework.hooks.resolver.ResolverHook;

public abstract class AbstractTest {
	protected Set<ServiceRegistration<?>> serviceRegistrations;

	@Before
	public void setUp() {
		serviceRegistrations = new HashSet<ServiceRegistration<?>>();
	}

	@After
	public void tearDown() {
		for (ServiceRegistration<?> serviceRegistration : serviceRegistrations) {
			try {
				serviceRegistration.unregister();
			} catch (IllegalStateException e) {
				// Service was already unregistered.
			}
		}
	}

	protected DummyContainerAdaptor createDummyAdaptor() {
		return new DummyContainerAdaptor(new DummyCollisionHook(false), Collections.<String, String> emptyMap());
	}

	protected DummyContainerAdaptor createDummyAdaptor(ResolverHook hook) {
		return new DummyContainerAdaptor(new DummyCollisionHook(false), Collections.<String, String> emptyMap(), new DummyResolverHookFactory(hook));
	}

	protected DummyContainerAdaptor createDummyAdaptor(DebugOptions debugOptions) {
		return new DummyContainerAdaptor(new DummyCollisionHook(false), Collections.<String, String> emptyMap(), new DummyResolverHookFactory(), debugOptions);
	}

	protected Bundle getBundle() {
		return ((BundleReference) getClass().getClassLoader()).getBundle();
	}

	protected BundleContext getBundleContext() {
		return getBundle().getBundleContext();
	}

	protected Map<String, String> getManifest(String manifestFile) throws IOException, BundleException {
		URL manifest = getBundle().getEntry("/test_files/containerTests/" + manifestFile);
		Assert.assertNotNull("Could not find manifest: " + manifestFile, manifest);
		return ManifestElement.parseBundleManifest(manifest.openStream(), null);
	}

	protected Bundle getSystemBundle() {
		return getBundleContext().getBundle(0);
	}

	protected BundleContext getSystemBundleContext() {
		return getSystemBundle().getBundleContext();
	}

	protected Module installDummyModule(String manifestFile, String location, ModuleContainer container) throws BundleException, IOException {
		return installDummyModule(manifestFile, location, null, null, null, container);
	}

	protected Module installDummyModule(String manifestFile, String location, String alias, String extraExports, String extraCapabilities, ModuleContainer container) throws BundleException, IOException {
		Map<String, String> manifest = getManifest(manifestFile);
		ModuleRevisionBuilder builder = OSGiManifestBuilderFactory.createBuilder(manifest, alias, extraExports, extraCapabilities);
		Module system = container.getModule(0);
		return container.install(system, location, builder, null);
	}

	protected Module installDummyModule(Map<String, String> manifest, String location, ModuleContainer container) throws BundleException {
		return installDummyModule(manifest, -1, location, container);
	}

	protected Module installDummyModule(Map<String, String> manifest, long id, String location, ModuleContainer container) throws BundleException {
		ModuleRevisionBuilder builder = OSGiManifestBuilderFactory.createBuilder(manifest);
		if (id > 0) {
			builder.setId(id);
		}
		Module system = container.getModule(0);
		return container.install(system, location, builder, null);
	}

	protected void registerService(Class<?> clazz, Object service) {
		serviceRegistrations.add(getSystemBundleContext().registerService(clazz.getName(), service, null));
	}

	protected void unregisterService(Object service) {
		for (ServiceRegistration<?> serviceRegistration : serviceRegistrations)
			if (getSystemBundleContext().getService(serviceRegistration.getReference()).equals(service))
				serviceRegistration.unregister();
	}
}

Back to the top