Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b23bb5a7e26676d31059db2a155707d2305b75fb (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*******************************************************************************
 * Copyright (c) 2004, 2015 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.internal.plugins;

import java.io.IOException;
import org.eclipse.core.tests.harness.BundleTestingHelper;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.eclipse.osgi.util.ManifestElement;
import org.osgi.framework.*;
import org.osgi.service.packageadmin.PackageAdmin;

/**
 * Provisory home for tests that install plugins.
 */
public class InstallTests extends CoreTest {

	public InstallTests() {
		super();
	}

	public InstallTests(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
	}

	public void testInstallNoVersionManifest01() throws BundleException, IOException {
		// Note that this test case has changed since the removing of plugin.xml conversion
		// Before this tested that a plugin.xml with no version specified would fail.
		// It is valid for a bundle to omit Bundle-Version header (and default to version 0.0.0
		Bundle installed = null;
		try {
			installed = BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle03"); //$NON-NLS-1$
			// success - should allow manifests with no version.
		} catch (BundleException be) {
			// should not have failed with BundleException
			fail("1.0"); //$NON-NLS-1$
		} finally {
			if (installed != null)
				// clean-up - only runs if we end-up accepting an invalid manifest				
				installed.uninstall();
		}
	}

	/**
	 * Test invalid manifest; missing Bundle-SymbolicName
	 */
	public void testInstallInvalidManifest02() throws IOException, BundleException {
		Bundle installed = null;
		try {
			installed = BundleTestingHelper.installBundle("testInstallInvalidManifest02", OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle11"); //$NON-NLS-1$ //$NON-NLS-2$
			// should have failed with BundleException
			fail("Expected a failure with no Bundle-SymbolicName header"); //$NON-NLS-1$
		} catch (BundleException be) {
			// success - the manifest was invalid
			assertEquals("Expected manifest error", BundleException.MANIFEST_ERROR, be.getType()); //$NON-NLS-1$
		} finally {
			if (installed != null)
				// clean-up - only runs if we end-up accepting an invalid manifest				
				installed.uninstall();
		}
	}

	/**
	 * Test invalid manifest; duplicate directive
	 */
	public void testInstallInvalidManifest03() throws IOException, BundleException {
		Bundle installed = null;
		try {
			installed = BundleTestingHelper.installBundle("testInstallInvalidManifest03", OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle12"); //$NON-NLS-1$ //$NON-NLS-2$
			// should have failed with BundleException
			fail("Expected a failure with duplicate directives"); //$NON-NLS-1$
		} catch (BundleException be) {
			// success - the manifest was invalid
			assertEquals("Expected manifest error", BundleException.MANIFEST_ERROR, be.getType()); //$NON-NLS-1$
		} finally {
			if (installed != null)
				// clean-up - only runs if we end-up accepting an invalid manifest				
				installed.uninstall();
		}
	}

	/**
	 * Test invalid manifest; use attributes bundle-version and bundle-symbolic-name in Export-Package
	 */
	public void testInstallInvalidManifest04() throws IOException, BundleException {
		Bundle installed = null;
		try {
			installed = BundleTestingHelper.installBundle("testInstallInvalidManifest04", OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle13"); //$NON-NLS-1$ //$NON-NLS-2$
			// should have failed with BundleException
			fail("Expected a failure with duplicate attributes"); //$NON-NLS-1$
		} catch (BundleException be) {
			// success - the manifest was invalid
			assertEquals("Expected manifest error", BundleException.MANIFEST_ERROR, be.getType()); //$NON-NLS-1$
		} finally {
			if (installed != null)
				// clean-up - only runs if we end-up accepting an invalid manifest				
				installed.uninstall();
		}
	}

	/**
	 * Test invalid manifest; imports same package twice
	 */
	public void testInstallInvalidManifest05() throws IOException, BundleException {
		Bundle installed = null;
		try {
			installed = BundleTestingHelper.installBundle("testInstallInvalidManifest05", OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle14"); //$NON-NLS-1$ //$NON-NLS-2$
			// should have failed with BundleException
			fail("Expected a failure with duplicate imports"); //$NON-NLS-1$
		} catch (BundleException be) {
			// success - the manifest was invalid
			assertEquals("Expected manifest error", BundleException.MANIFEST_ERROR, be.getType()); //$NON-NLS-1$
		} finally {
			if (installed != null)
				// clean-up - only runs if we end-up accepting an invalid manifest				
				installed.uninstall();
		}
	}

	/**
	 * Test unresolvable
	 */
	public void testStartError01() throws IOException, BundleException {
		Bundle installed = null;
		try {
			try {
				installed = BundleTestingHelper.installBundle("testStartError01", OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle15"); //$NON-NLS-1$ //$NON-NLS-2$
				// should be able to install
			} catch (BundleException be) {
				// failed to install unresolvable bundle
				fail("Unexpected installation error", be); //$NON-NLS-1$
			}
			try {
				installed.start();
				// expected exception starting
				fail("Expected a failure to start unresolved bundle"); //$NON-NLS-1$
			} catch (BundleException be) {
				// success - the bundle can not resolve
				assertEquals("Expected manifest error", BundleException.RESOLVE_ERROR, be.getType()); //$NON-NLS-1$
			}

		} finally {
			if (installed != null)
				// clean-up - only runs if we end-up accepting an invalid manifest				
				installed.uninstall();
		}
	}

	/**
	 * Test start fragment
	 */
	public void testStartError02() throws IOException, BundleException {
		Bundle host = null;
		Bundle fragment = null;
		try {
			try {
				host = BundleTestingHelper.installBundle("testStartError02_host", OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle16"); //$NON-NLS-1$ //$NON-NLS-2$
				fragment = BundleTestingHelper.installBundle("testStartError02_frag", OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle17"); //$NON-NLS-1$ //$NON-NLS-2$
				// should be able to install host
			} catch (BundleException be) {
				// failed to install unresolvable bundle
				fail("Unexpected installation error", be); //$NON-NLS-1$
			}
			try {
				host.start();
			} catch (BundleException be) {
				fail("Unexpected start host error", be); //$NON-NLS-1$
			}
			try {
				fragment.start();
				// expected exception starting
				fail("Expected a failure to start fragment bundle"); //$NON-NLS-1$
			} catch (BundleException be) {
				// success - the bundle can not resolve
				assertEquals("Expected manifest error", BundleException.INVALID_OPERATION, be.getType()); //$NON-NLS-1$
			}

		} finally {
			if (host != null)
				host.uninstall();
			if (fragment != null)
				fragment.uninstall();

		}
	}

	/**
	 * Test unsupported operation with boot classpath extension
	 */
	public void testUnsupportedOperation01() throws IOException, BundleException {
		Bundle installed = null;
		try {
			installed = BundleTestingHelper.installBundle("testUnsupportedOperation01", OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle18"); //$NON-NLS-1$ //$NON-NLS-2$
			// should have failed with BundleException
			fail("Expected an unsupported operation exception"); //$NON-NLS-1$
		} catch (BundleException be) {
			// success - the manifest was invalid
			assertEquals("Expected unsupported error", BundleException.UNSUPPORTED_OPERATION, be.getType()); //$NON-NLS-1$
		} finally {
			if (installed != null)
				// clean-up - only runs if we end-up accepting an invalid manifest				
				installed.uninstall();
		}
	}

	public void testInstallLocationWithSpaces() throws BundleException, IOException {
		Bundle installed = null;
		installed = BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle 01"); //$NON-NLS-1$
		try {
			assertEquals("1.0", "bundle01", installed.getSymbolicName()); //$NON-NLS-1$ //$NON-NLS-2$
			assertEquals("1.1", Bundle.INSTALLED, installed.getState()); //$NON-NLS-1$
		} finally {
			// clean-up
			installed.uninstall();
		}
	}

	public void testInstallLocationWithUnderscores() throws BundleException, IOException {
		Bundle installed = null;
		installed = BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle02_1.0.0"); //$NON-NLS-1$
		try {
			assertEquals("1.0", "bundle02", installed.getSymbolicName()); //$NON-NLS-1$ //$NON-NLS-2$
			assertEquals("1.1", Bundle.INSTALLED, installed.getState()); //$NON-NLS-1$
			assertEquals("1.2", new Version("2.0"), new Version(installed.getHeaders().get(Constants.BUNDLE_VERSION))); //$NON-NLS-1$ //$NON-NLS-2$
		} finally {
			// clean-up
			installed.uninstall();
		}
	}

	/** Ensures we see a bundle with only a extension point as a singleton */
	public void testInstallBundleWithExtensionPointOnly() throws BundleException, IOException {
		Bundle installed = null;
		installed = BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle04"); //$NON-NLS-1$
		try {
			assertEquals("1.0", "bundle04", installed.getSymbolicName()); //$NON-NLS-1$ //$NON-NLS-2$
			assertEquals("1.1", Bundle.INSTALLED, installed.getState()); //$NON-NLS-1$
			assertEquals("1.2", "1.3.7", installed.getHeaders().get(Constants.BUNDLE_VERSION)); //$NON-NLS-1$ //$NON-NLS-2$
			String symbolicNameString = installed.getHeaders().get(Constants.BUNDLE_SYMBOLICNAME);
			assertNotNull("1.3", symbolicNameString); //$NON-NLS-1$
			ManifestElement[] symbolicNameHeader = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, symbolicNameString);
			assertEquals("1.4", 1, symbolicNameHeader.length); //$NON-NLS-1$
			assertEquals("1.5", "true", symbolicNameHeader[0].getDirective(Constants.SINGLETON_DIRECTIVE)); //$NON-NLS-1$ //$NON-NLS-2$

		} finally {
			// clean-up
			installed.uninstall();
		}
	}

	/** Ensures we see a bundle with only a extension as a singleton */
	public void testInstallBundleWithExtensionOnly() throws BundleException, IOException {
		Bundle installed = null;
		installed = BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle05"); //$NON-NLS-1$
		try {
			assertEquals("1.0", "bundle05", installed.getSymbolicName()); //$NON-NLS-1$ //$NON-NLS-2$
			assertEquals("1.1", Bundle.INSTALLED, installed.getState()); //$NON-NLS-1$
			assertEquals("1.2", "1.3.8", installed.getHeaders().get(Constants.BUNDLE_VERSION)); //$NON-NLS-1$ //$NON-NLS-2$
			String symbolicNameString = installed.getHeaders().get(Constants.BUNDLE_SYMBOLICNAME);
			assertNotNull("1.3", symbolicNameString); //$NON-NLS-1$
			ManifestElement[] symbolicNameHeader = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, symbolicNameString);
			assertEquals("1.4", 1, symbolicNameHeader.length); //$NON-NLS-1$
			assertEquals("1.5", "true", symbolicNameHeader[0].getDirective(Constants.SINGLETON_DIRECTIVE)); //$NON-NLS-1$ //$NON-NLS-2$

		} finally {
			// clean-up
			installed.uninstall();
		}
	}

	/** Ensures we see a bundle with only extension and extension point as a singleton */
	public void testInstallBundleWithExtensionAndExtensionPoint() throws BundleException, IOException {
		Bundle installed = null;
		installed = BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle06"); //$NON-NLS-1$
		try {
			assertEquals("1.0", "bundle06", installed.getSymbolicName()); //$NON-NLS-1$ //$NON-NLS-2$
			assertEquals("1.1", Bundle.INSTALLED, installed.getState()); //$NON-NLS-1$
			assertEquals("1.2", "1.3.9", installed.getHeaders().get(Constants.BUNDLE_VERSION)); //$NON-NLS-1$ //$NON-NLS-2$
			String symbolicNameString = installed.getHeaders().get(Constants.BUNDLE_SYMBOLICNAME);
			assertNotNull("1.3", symbolicNameString); //$NON-NLS-1$
			ManifestElement[] symbolicNameHeader = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, symbolicNameString);
			assertEquals("1.4", 1, symbolicNameHeader.length); //$NON-NLS-1$
			assertEquals("1.5", "true", symbolicNameHeader[0].getDirective(Constants.SINGLETON_DIRECTIVE)); //$NON-NLS-1$ //$NON-NLS-2$

		} finally {
			// clean-up
			installed.uninstall();
		}
	}

	/** Ensures two versions of a non-singleton bundle are accepted */
	public void testInstall2NonSingletonBundles() throws BundleException, IOException {
		Bundle installed1 = org.eclipse.core.tests.harness.BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle07"); //$NON-NLS-1$
		ServiceReference packageAdminSR = OSGiTestsActivator.getContext().getServiceReference(PackageAdmin.class.getName());
		PackageAdmin packageAdmin = (PackageAdmin) OSGiTestsActivator.getContext().getService(packageAdminSR);
		packageAdmin.resolveBundles(null);
		Bundle installed2 = BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle07b"); //$NON-NLS-1$
		packageAdmin.resolveBundles(null);
		OSGiTestsActivator.getContext().ungetService(packageAdminSR);
		try {
			assertEquals("1.0", "bundle07", installed2.getSymbolicName()); //$NON-NLS-1$ //$NON-NLS-2$
			assertEquals("1.1", Bundle.RESOLVED, installed2.getState()); //$NON-NLS-1$
			assertEquals("1.2", "1.0.0.b", installed2.getHeaders().get(Constants.BUNDLE_VERSION)); //$NON-NLS-1$ //$NON-NLS-2$

			assertEquals("1.3", "bundle07", installed1.getSymbolicName()); //$NON-NLS-1$ //$NON-NLS-2$
			assertEquals("1.4", Bundle.RESOLVED, installed1.getState()); //$NON-NLS-1$
			assertEquals("1.5", "1.0.0", installed1.getHeaders().get(Constants.BUNDLE_VERSION)); //$NON-NLS-1$ //$NON-NLS-2$
		} finally {
			installed1.uninstall();
			installed2.uninstall();
		}
	}

	/** Ensures two versions of a singleton bundle are accepted */
	public void testInstall2SingletonBundles() throws BundleException, IOException {
		Bundle installed1 = BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle08"); //$NON-NLS-1$
		ServiceReference packageAdminSR = OSGiTestsActivator.getContext().getServiceReference(PackageAdmin.class.getName());
		PackageAdmin packageAdmin = (PackageAdmin) OSGiTestsActivator.getContext().getService(packageAdminSR);
		packageAdmin.resolveBundles(null);
		Bundle installed2 = BundleTestingHelper.installBundle(OSGiTestsActivator.getContext(), OSGiTestsActivator.TEST_FILES_ROOT + "internal/plugins/installTests/bundle08b"); //$NON-NLS-1$
		packageAdmin.resolveBundles(null);
		OSGiTestsActivator.getContext().ungetService(packageAdminSR);
		try {
			assertEquals("1.0", "bundle08", installed1.getSymbolicName()); //$NON-NLS-1$ //$NON-NLS-2$
			assertEquals("1.1", Bundle.RESOLVED, installed1.getState()); //$NON-NLS-1$
			assertEquals("1.2", "1.0.0", installed1.getHeaders().get(Constants.BUNDLE_VERSION)); //$NON-NLS-1$ //$NON-NLS-2$

			assertEquals("1.3", "bundle08", installed2.getSymbolicName()); //$NON-NLS-1$ //$NON-NLS-2$
			assertEquals("1.4", Bundle.INSTALLED, installed2.getState()); //$NON-NLS-1$
			assertEquals("1.5", "1.0.0.b", installed2.getHeaders().get(Constants.BUNDLE_VERSION)); //$NON-NLS-1$ //$NON-NLS-2$
		} finally {
			installed1.uninstall();
			installed2.uninstall();
		}
	}

}

Back to the top