Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0a2eb018c436f92784f23f692a926c6ad96df822 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*******************************************************************************
 * Copyright (c) 2013, 2016 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.plugins;

import java.util.*;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.osgi.service.pluginconversion.PluginConverter;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.eclipse.osgi.tests.bundles.BundleInstaller;
import org.osgi.framework.*;
import org.osgi.framework.namespace.*;
import org.osgi.framework.wiring.*;
import org.osgi.resource.Namespace;

public class OldStylePluginTests extends CoreTest {
	public static int BUNDLE_LISTENER = 0x01;
	public static int SYNC_BUNDLE_LISTENER = 0x02;
	public static int SIMPLE_RESULTS = 0x04;
	public static final String BUNDLES_ROOT = "bundle_tests";
	private BundleInstaller installer;
	private boolean compatPluginInstalled = false;

	public static Test suite() {
		return new TestSuite(OldStylePluginTests.class);
	}

	protected void setUp() throws Exception {
		compatPluginInstalled = false;
		for (BundleWire hostWire : getContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).adapt(BundleWiring.class).getProvidedWires(HostNamespace.HOST_NAMESPACE)) {
			if ("org.eclipse.osgi.compatibility.plugins".equals(hostWire.getRequirer().getSymbolicName())) {
				compatPluginInstalled = true;
				break;
			}
		}
		if (compatPluginInstalled) {
			assertTrue("Plugin compatibility fragment not configured.", getContext().getServiceReference(PluginConverter.class) != null);
		}
		installer = new BundleInstaller(BUNDLES_ROOT, OSGiTestsActivator.getContext());
		installer.refreshPackages(null);
	}

	protected void tearDown() throws Exception {
		installer.shutdown();
		installer = null;
	}

	public BundleContext getContext() {
		return OSGiTestsActivator.getContext();
	}

	public void testSimplePlugin() {
		if (!compatPluginInstalled) {
			return;
		}
		String pluginName = "test.plugins.a";
		Version pluginVersion = Version.parseVersion("1.0.0");
		Bundle bundle = installPlugin(pluginName, false);
		BundleRevision revision = bundle.adapt(BundleRevision.class);
		assertIdentity(revision, pluginName, pluginVersion, false);
		assertTrue("Did not expect any requirements.", revision.getDeclaredRequirements(null).isEmpty());
	}

	public void testImportPlugin() {
		if (!compatPluginInstalled) {
			return;
		}
		String pluginName = "test.plugins.b";
		Version pluginVersion = Version.parseVersion("1.0.0");
		Bundle bundle = installPlugin(pluginName, false);
		BundleRevision revision = bundle.adapt(BundleRevision.class);
		assertIdentity(revision, pluginName, pluginVersion, false);

		Collection<BundleRequirement> requirements = revision.getDeclaredRequirements(null);
		assertEquals("Wrong number of requirements: " + requirements, 7, requirements.size());
		int index = 0;
		for (BundleRequirement req : requirements) {
			index++;
			Map<String, Object> matchAttributes = new HashMap<String, Object>();
			Map<String, Object> noMatchAttributes = new HashMap<String, Object>();

			matchAttributes.put(BundleNamespace.BUNDLE_NAMESPACE, "x" + index);
			noMatchAttributes.put(BundleNamespace.BUNDLE_NAMESPACE, "x" + index);

			Version match = null;
			Version noMatch = null;
			switch (index) {
				case 1 :
					match = Version.parseVersion("1.0.0");
					noMatch = Version.parseVersion("1.0.0.x");
					break;
				case 2 :
					match = Version.parseVersion("1.0.100");
					noMatch = Version.parseVersion("1.1");
					break;
				case 4 :
					match = Version.parseVersion("90000");
					noMatch = Version.parseVersion("0.1");
					break;
				default :
					match = Version.parseVersion("1.1");
					noMatch = Version.parseVersion("2.0");
					break;
			}

			matchAttributes.put(AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, match);
			noMatchAttributes.put(AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, noMatch);
			Filter f = null;
			try {
				f = getContext().createFilter(req.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE));
			} catch (InvalidSyntaxException e) {
				fail("Failed to create filter.", e);
			}
			assertTrue("Did not match requirement: " + req + ": against: " + matchAttributes, f.matches(matchAttributes));
			assertFalse("Unexpected match requirement: " + req + ": against: " + matchAttributes, f.matches(noMatchAttributes));

			String resolution = req.getDirectives().get(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE);
			if (resolution == null) {
				resolution = Namespace.RESOLUTION_MANDATORY;
			}
			String visibility = req.getDirectives().get(BundleNamespace.REQUIREMENT_VISIBILITY_DIRECTIVE);
			if (visibility == null) {
				visibility = BundleNamespace.VISIBILITY_PRIVATE;
			}

			if (index == 6) {
				assertEquals("Wrong visibility: " + req, BundleNamespace.VISIBILITY_REEXPORT, visibility);
			} else {
				assertEquals("Wrong visibility: " + req, BundleNamespace.VISIBILITY_PRIVATE, visibility);
			}
			if (index == 7) {
				assertEquals("Wrong resolution: " + req, Namespace.RESOLUTION_OPTIONAL, resolution);
			} else {
				assertEquals("Wrong resolution: " + req, Namespace.RESOLUTION_MANDATORY, resolution);
			}
		}
	}

	public void testExportSinglePackage() {
		if (!compatPluginInstalled) {
			return;
		}
		String pluginName = "test.plugins.c";
		Version pluginVersion = Version.parseVersion("1.0.0");
		Bundle bundle = installPlugin(pluginName, false);
		BundleRevision revision = bundle.adapt(BundleRevision.class);
		assertIdentity(revision, pluginName, pluginVersion, false);
		assertTrue("Did not expect any requirements.", revision.getDeclaredRequirements(null).isEmpty());

		Collection<BundleCapability> packages = revision.getDeclaredCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
		assertEquals("Wrong number of exports: " + packages, 1, packages.size());
		assertEquals("Wrong package name.", pluginName + ".exported", packages.iterator().next().getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
	}

	public void testExportAllPackage() {
		if (!compatPluginInstalled) {
			return;
		}
		String pluginName = "test.plugins.d";
		Version pluginVersion = Version.parseVersion("1.0.0");
		Bundle bundle = installPlugin(pluginName, false);
		BundleRevision revision = bundle.adapt(BundleRevision.class);
		assertIdentity(revision, pluginName, pluginVersion, false);
		assertTrue("Did not expect any requirements.", revision.getDeclaredRequirements(null).isEmpty());

		Collection<BundleCapability> packages = revision.getDeclaredCapabilities(PackageNamespace.PACKAGE_NAMESPACE);
		assertEquals("Wrong number of exports: " + packages, 4, packages.size());
		Iterator<BundleCapability> iPackages = packages.iterator();
		// Note that '.' is exported because scaning for * found a resource (plugin.xml) in root of the bundle.
		assertEquals("Wrong package name.", ".", iPackages.next().getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
		assertEquals("Wrong package name.", pluginName, iPackages.next().getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
		assertEquals("Wrong package name.", pluginName + ".exported", iPackages.next().getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
		assertEquals("Wrong package name.", pluginName + ".exported.sub", iPackages.next().getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
	}

	public void testActivator() {
		if (!compatPluginInstalled) {
			return;
		}
		String pluginName = "test.plugins.e";
		Version pluginVersion = Version.parseVersion("1.0.0");
		Bundle bundle = installPlugin(pluginName, false);
		BundleRevision revision = bundle.adapt(BundleRevision.class);
		assertIdentity(revision, pluginName, pluginVersion, false);

		try {
			bundle.start();
			Collection<ServiceReference<Object>> testServices = getContext().getServiceReferences(Object.class, "(" + Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE + "=" + bundle.getSymbolicName() + ")");
			assertFalse("Did not find registered service.", testServices.isEmpty());
		} catch (BundleException e) {
			fail("Failed to start bundle.", e);
		} catch (InvalidSyntaxException e) {
			fail("Failed to create filter.", e);
		}
	}

	public void testExtensionSingleton() {
		if (!compatPluginInstalled) {
			return;
		}
		String pluginName = "test.plugins.f";
		Version pluginVersion = Version.parseVersion("1.0.0");
		Bundle bundle = installPlugin(pluginName, false);
		BundleRevision revision = bundle.adapt(BundleRevision.class);
		assertIdentity(revision, pluginName, pluginVersion, true);
	}

	public void testFragment() {
		if (!compatPluginInstalled) {
			return;
		}
		String pluginName = "test.plugins.g";
		Version pluginVersion = Version.parseVersion("1.0.0");
		Bundle bundle = installPlugin(pluginName, false);
		BundleRevision revision = bundle.adapt(BundleRevision.class);
		assertIdentity(revision, pluginName, pluginVersion, false);
		assertTrue("revision is not a fragment.", (revision.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0);
		Collection<BundleRequirement> reqs = revision.getDeclaredRequirements(HostNamespace.HOST_NAMESPACE);
		assertEquals("Wrong number of host requirements.", 1, reqs.size());
		BundleRequirement req = reqs.iterator().next();

		Map<String, Object> matchAttributes = new HashMap<String, Object>();
		Map<String, Object> noMatchAttributes = new HashMap<String, Object>();

		matchAttributes.put(HostNamespace.HOST_NAMESPACE, "x");
		noMatchAttributes.put(HostNamespace.HOST_NAMESPACE, "x");

		Version match = Version.parseVersion("1.1");
		Version noMatch = Version.parseVersion("2.0");

		matchAttributes.put(AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, match);
		noMatchAttributes.put(AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, noMatch);
		Filter f = null;
		try {
			f = getContext().createFilter(req.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE));
		} catch (InvalidSyntaxException e) {
			fail("Failed to create filter.", e);
		}
		assertTrue("Did not match requirement: " + req + ": against: " + matchAttributes, f.matches(matchAttributes));
		assertFalse("Unexpected match requirement: " + req + ": against: " + matchAttributes, f.matches(noMatchAttributes));

	}

	private void assertIdentity(BundleRevision revision, String pluginName, Version pluginVersion, boolean isSingleton) {
		assertEquals("Wrong symbolic name", pluginName, revision.getSymbolicName());
		assertEquals("Wrong version.", pluginVersion, revision.getVersion());
		Collection<BundleCapability> capabilities = new ArrayList<BundleCapability>();
		capabilities.addAll(revision.getDeclaredCapabilities(IdentityNamespace.IDENTITY_NAMESPACE));
		capabilities.addAll(revision.getDeclaredCapabilities(BundleNamespace.BUNDLE_NAMESPACE));
		capabilities.addAll(revision.getDeclaredCapabilities(HostNamespace.HOST_NAMESPACE));
		int expectedNumCaps = (revision.getTypes() & BundleRevision.TYPE_FRAGMENT) == 0 ? 3 : 1;
		assertEquals("Wrong number of capabilities: " + capabilities, expectedNumCaps, capabilities.size());
		for (BundleCapability cap : capabilities) {
			String namespace = cap.getNamespace();
			String versionKey = IdentityNamespace.IDENTITY_NAMESPACE.equals(namespace) ? IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE : AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE;
			Map<String, Object> attrs = cap.getAttributes();
			assertEquals("Wrong name: " + namespace, pluginName, attrs.get(namespace));
			assertEquals("Wrong version: " + namespace, pluginVersion, attrs.get(versionKey));
			String singletonDirective = cap.getDirectives().get(IdentityNamespace.CAPABILITY_SINGLETON_DIRECTIVE);
			singletonDirective = singletonDirective == null ? Boolean.FALSE.toString() : singletonDirective;
			assertEquals("Wrong singleton directive.", isSingleton, Boolean.TRUE.toString().equals(singletonDirective));
		}
	}

	private Bundle installPlugin(String plugin, boolean expectedFailure) {
		try {
			return installer.installBundle(plugin);
		} catch (BundleException e) {
			if (!expectedFailure) {
				fail("Failed to install plugin.", e);
			}
			return null;
		}
	}
}

Back to the top