Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8d56e7d6be70e4f7eeaf8c2a31a3a8b74a557af7 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.tests.bundles;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.service.packageadmin.ExportedPackage;
import org.osgi.service.packageadmin.PackageAdmin;

public class SubstituteExportsBundleTests extends AbstractBundleTests {
	public static Test suite() {
		return new TestSuite(SubstituteExportsBundleTests.class);
	}

	public void testSubstituteExports01x() throws BundleException {
		Bundle a = installer.installBundle("substitutes.a"); //$NON-NLS-1$
		Bundle b = installer.installBundle("substitutes.b"); //$NON-NLS-1$
		Bundle c = installer.installBundle("substitutes.c"); //$NON-NLS-1$
		Bundle d = installer.installBundle("substitutes.d"); //$NON-NLS-1$

		String className = "substitutes.x.Ax"; //$NON-NLS-1$
		Class ax = null;
		Class bx = null;
		Class cx = null;
		Class dx = null;
		try {
			ax = a.loadClass(className);
			bx = b.loadClass(className);
			cx = c.loadClass(className);
			dx = d.loadClass(className);
		} catch (ClassNotFoundException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
		assertEquals("class from b is wrong", ax, bx); //$NON-NLS-1$
		assertEquals("class from c is wrong", ax, cx); //$NON-NLS-1$
		assertEquals("class from d is wrong", ax, dx); //$NON-NLS-1$
	}

	public void testSubstituteExports01y() throws BundleException {
		Bundle a = installer.installBundle("substitutes.a"); //$NON-NLS-1$
		Bundle b = installer.installBundle("substitutes.b"); //$NON-NLS-1$
		Bundle c = installer.installBundle("substitutes.c"); //$NON-NLS-1$
		Bundle d = installer.installBundle("substitutes.d"); //$NON-NLS-1$

		String className = "substitutes.y.Ay"; //$NON-NLS-1$
		Class ax = null;
		Class bx = null;
		Class cx = null;
		Class dx = null;
		try {
			ax = a.loadClass(className);
			bx = b.loadClass(className);
			cx = c.loadClass(className);
			dx = d.loadClass(className);
		} catch (ClassNotFoundException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
		assertEquals("class from b is wrong", ax, bx); //$NON-NLS-1$
		assertEquals("class from c is wrong", ax, cx); //$NON-NLS-1$
		assertEquals("class from d is wrong", ax, dx); //$NON-NLS-1$
	}

	public void testSubstituteExports02() throws BundleException {
		Bundle a = installer.installBundle("substitutes.a"); //$NON-NLS-1$
		Bundle b = installer.installBundle("substitutes.b"); //$NON-NLS-1$
		Bundle e = installer.installBundle("substitutes.e"); //$NON-NLS-1$
		Bundle f = installer.installBundle("substitutes.f"); //$NON-NLS-1$

		String className = "substitutes.x.Ax"; //$NON-NLS-1$
		Class ax = null;
		Class bx = null;
		Class ex = null;
		Class fx = null;
		try {
			ax = a.loadClass(className);
			bx = b.loadClass(className);
			ex = e.loadClass(className);
			fx = f.loadClass(className);
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}
		assertEquals("class from b is wrong", ax, bx); //$NON-NLS-1$
		assertEquals("class from c is wrong", ax, ex); //$NON-NLS-1$
		assertEquals("class from d is wrong", ax, fx); //$NON-NLS-1$
	}

	public void testSubstituteExports03() throws BundleException {
		Bundle a = installer.installBundle("substitutes.a"); //$NON-NLS-1$
		Bundle b = installer.installBundle("substitutes.b"); //$NON-NLS-1$
		Bundle e = installer.installBundle("substitutes.e"); //$NON-NLS-1$
		Bundle f = installer.installBundle("substitutes.f"); //$NON-NLS-1$
		Bundle g = installer.installBundle("substitutes.g"); //$NON-NLS-1$
		Bundle h = installer.installBundle("substitutes.h"); //$NON-NLS-1$

		String className = "substitutes.x.Ax"; //$NON-NLS-1$
		Class ax = null;
		Class bx = null;
		Class ex = null;
		Class fx = null;
		Class gx = null;
		Class hx = null;
		try {
			ax = a.loadClass(className);
			bx = b.loadClass(className);
			ex = e.loadClass(className);
			fx = f.loadClass(className);
			gx = g.loadClass(className);
			hx = h.loadClass(className);
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}
		assertEquals("class from b is wrong", ax, bx); //$NON-NLS-1$
		assertEquals("class from c is wrong", ax, ex); //$NON-NLS-1$
		assertEquals("class from d is wrong", ax, fx); //$NON-NLS-1$
		assertEquals("class from d is wrong", ax, gx); //$NON-NLS-1$
		assertEquals("class from d is wrong", ax, hx); //$NON-NLS-1$
	}

	public void testSubstituteExports04() throws BundleException {
		Bundle a = installer.installBundle("substitutes.a"); //$NON-NLS-1$
		installer.installBundle("substitutes.a.frag"); //$NON-NLS-1$
		Bundle b = installer.installBundle("substitutes.b"); //$NON-NLS-1$
		installer.installBundle("substitutes.b.frag"); //$NON-NLS-1$
		Bundle c = installer.installBundle("substitutes.c"); //$NON-NLS-1$
		Bundle d = installer.installBundle("substitutes.d"); //$NON-NLS-1$

		String className = "substitutes.x.Ax"; //$NON-NLS-1$
		Class ax = null;
		Class bx = null;
		Class cx = null;
		Class dx = null;
		try {
			ax = a.loadClass(className);
			bx = b.loadClass(className);
			cx = c.loadClass(className);
			dx = d.loadClass(className);
		} catch (ClassNotFoundException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
		assertEquals("class from b is wrong", ax, bx); //$NON-NLS-1$
		assertEquals("class from c is wrong", ax, cx); //$NON-NLS-1$
		assertEquals("class from d is wrong", ax, dx); //$NON-NLS-1$

		String className2 = "substitutes.q.AFq"; //$NON-NLS-1$
		Class aq = null;
		Class bq = null;
		Class cq = null;
		Class dq = null;
		try {
			aq = a.loadClass(className2);
			bq = b.loadClass(className2);
			cq = c.loadClass(className2);
			dq = d.loadClass(className2);
		} catch (ClassNotFoundException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
		assertEquals("class from b is wrong", aq, bq); //$NON-NLS-1$
		assertEquals("class from c is wrong", aq, cq); //$NON-NLS-1$
		assertEquals("class from d is wrong", aq, dq); //$NON-NLS-1$
	}

	public void testSubstituteExports05() throws BundleException {
		Bundle a = installer.installBundle("substitutes.a"); //$NON-NLS-1$
		installer.installBundle("substitutes.a.frag"); //$NON-NLS-1$
		Bundle b = installer.installBundle("substitutes.b"); //$NON-NLS-1$
		installer.installBundle("substitutes.b.frag"); //$NON-NLS-1$
		Bundle e = installer.installBundle("substitutes.e"); //$NON-NLS-1$
		Bundle f = installer.installBundle("substitutes.f"); //$NON-NLS-1$
		Bundle g = installer.installBundle("substitutes.g"); //$NON-NLS-1$
		Bundle h = installer.installBundle("substitutes.h"); //$NON-NLS-1$

		String className = "substitutes.x.Ax"; //$NON-NLS-1$
		Class ax = null;
		Class bx = null;
		Class ex = null;
		Class fx = null;
		Class gx = null;
		Class hx = null;
		try {
			ax = a.loadClass(className);
			bx = b.loadClass(className);
			ex = e.loadClass(className);
			fx = f.loadClass(className);
			gx = g.loadClass(className);
			hx = h.loadClass(className);
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}
		assertEquals("class from b is wrong", ax, bx); //$NON-NLS-1$
		assertEquals("class from c is wrong", ax, ex); //$NON-NLS-1$
		assertEquals("class from d is wrong", ax, fx); //$NON-NLS-1$
		assertEquals("class from d is wrong", ax, gx); //$NON-NLS-1$
		assertEquals("class from d is wrong", ax, hx); //$NON-NLS-1$

		String className2 = "substitutes.q.AFq"; //$NON-NLS-1$
		Class aq = null;
		Class bq = null;
		Class eq = null;
		Class fq = null;
		Class gq = null;
		Class hq = null;
		try {
			aq = a.loadClass(className2);
			bq = b.loadClass(className2);
			eq = e.loadClass(className2);
			fq = f.loadClass(className2);
			gq = g.loadClass(className2);
			hq = h.loadClass(className2);
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}
		assertEquals("class from b is wrong", aq, bq); //$NON-NLS-1$
		assertEquals("class from c is wrong", aq, eq); //$NON-NLS-1$
		assertEquals("class from d is wrong", aq, fq); //$NON-NLS-1$
		assertEquals("class from d is wrong", aq, gq); //$NON-NLS-1$
		assertEquals("class from d is wrong", aq, hq); //$NON-NLS-1$
	}

	public void testSubstituteExports06() throws BundleException {
		Bundle iBundle = installer.installBundle("substitutes.i"); //$NON-NLS-1$
		Bundle jBundle = installer.installBundle("substitutes.j"); //$NON-NLS-1$
		Bundle kBundle = installer.installBundle("substitutes.k"); //$NON-NLS-1$
		Bundle lBundle = installer.installBundle("substitutes.l"); //$NON-NLS-1$
		Bundle mBundle = installer.installBundle("substitutes.m"); //$NON-NLS-1$
		Bundle nBundle = installer.installBundle("substitutes.n"); //$NON-NLS-1$
		Bundle pBundle = installer.installBundle("substitutes.p"); //$NON-NLS-1$
		Bundle qBundle = installer.installBundle("substitutes.q"); //$NON-NLS-1$
		Bundle[] allBundles = new Bundle[] {iBundle, jBundle, kBundle, lBundle, mBundle, nBundle, pBundle, qBundle};

		String classNameIx = "substitutes.x.Ix"; //$NON-NLS-1$
		String classNameKx = "substitutes.x.Kx"; //$NON-NLS-1$
		String classNameMx = "substitutes.x.Mx"; //$NON-NLS-1$
		String classNameIy = "substitutes.y.Iy"; //$NON-NLS-1$
		String classNameKy = "substitutes.y.Ky"; //$NON-NLS-1$
		String classNameMy = "substitutes.y.My"; //$NON-NLS-1$

		try {
			Class iX = iBundle.loadClass(classNameIx);
			assertEquals("jBundle has different copy of iX", iX, jBundle.loadClass(classNameIx)); //$NON-NLS-1$
			assertEquals("mBundle has different copy of iX", iX, mBundle.loadClass(classNameIx)); //$NON-NLS-1$
			assertEquals("nBundle has different copy of iX", iX, nBundle.loadClass(classNameIx)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of iX", iX, pBundle.loadClass(classNameIx)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of iX", iX, qBundle.loadClass(classNameIx)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}
		try {
			Class iY = iBundle.loadClass(classNameIy);
			assertEquals("jBundle has different copy of iY", iY, jBundle.loadClass(classNameIy)); //$NON-NLS-1$
			assertEquals("mBundle has different copy of iY", iY, mBundle.loadClass(classNameIy)); //$NON-NLS-1$
			assertEquals("nBundle has different copy of iY", iY, nBundle.loadClass(classNameIy)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of iY", iY, pBundle.loadClass(classNameIy)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of iY", iY, qBundle.loadClass(classNameIy)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}

		try {
			Class kX = kBundle.loadClass(classNameKx);
			assertEquals("lBundle has different copy of Kx", kX, lBundle.loadClass(classNameKx)); //$NON-NLS-1$
			assertEquals("mBundle has different copy of Kx", kX, mBundle.loadClass(classNameKx)); //$NON-NLS-1$
			assertEquals("nBundle has different copy of Kx", kX, nBundle.loadClass(classNameKx)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of Kx", kX, pBundle.loadClass(classNameKx)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of Kx", kX, qBundle.loadClass(classNameKx)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}

		try {
			Class kY = kBundle.loadClass(classNameKy);
			assertEquals("lBundle has different copy of Ky", kY, lBundle.loadClass(classNameKy)); //$NON-NLS-1$
			assertEquals("mBundle has different copy of Ky", kY, mBundle.loadClass(classNameKy)); //$NON-NLS-1$
			assertEquals("nBundle has different copy of Ky", kY, nBundle.loadClass(classNameKy)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of Ky", kY, pBundle.loadClass(classNameKy)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of Ky", kY, qBundle.loadClass(classNameKy)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}

		try {
			Class mX = mBundle.loadClass(classNameMx);
			assertEquals("nBundle has different copy of mX", mX, nBundle.loadClass(classNameMx)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of mX", mX, pBundle.loadClass(classNameMx)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of mX", mX, qBundle.loadClass(classNameMx)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}

		try {
			Class mY = mBundle.loadClass(classNameMy);
			assertEquals("nBundle has different copy of mY", mY, nBundle.loadClass(classNameMy)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of mY", mY, pBundle.loadClass(classNameMy)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of mY", mY, qBundle.loadClass(classNameMy)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}

		String[] unexpectedClasseNames = new String[] {"substitutes.x.Jx", "substitutes.x.Lx", "substitutes.x.Nx", "substitutes.y.Jy", "substitutes.y.Ly", "substitutes.y.Ny"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
		for (int i = 0; i < unexpectedClasseNames.length; i++) {
			for (int j = 0; j < allBundles.length; j++) {
				try {
					Class found = allBundles[j].loadClass(unexpectedClasseNames[i]);
					fail("Found class " + found + " in bundle " + allBundles[j]); //$NON-NLS-1$//$NON-NLS-2$
				} catch (ClassNotFoundException cnfe) {
					// expected
				}
			}
		}
	}

	public void testSubstituteExports07() throws BundleException {
		// same as previous split test but bundles are installed in opposite order to force the opposite classes to load
		Bundle jBundle = installer.installBundle("substitutes.j"); //$NON-NLS-1$
		Bundle iBundle = installer.installBundle("substitutes.i"); //$NON-NLS-1$
		Bundle lBundle = installer.installBundle("substitutes.l"); //$NON-NLS-1$
		Bundle kBundle = installer.installBundle("substitutes.k"); //$NON-NLS-1$
		Bundle nBundle = installer.installBundle("substitutes.n"); //$NON-NLS-1$
		Bundle mBundle = installer.installBundle("substitutes.m"); //$NON-NLS-1$
		Bundle qBundle = installer.installBundle("substitutes.q"); //$NON-NLS-1$
		Bundle pBundle = installer.installBundle("substitutes.p"); //$NON-NLS-1$
		Bundle[] allBundles = new Bundle[] {iBundle, jBundle, kBundle, lBundle, mBundle, nBundle, pBundle, qBundle};

		String classNameJx = "substitutes.x.Jx"; //$NON-NLS-1$
		String classNameLx = "substitutes.x.Lx"; //$NON-NLS-1$
		String classNameNx = "substitutes.x.Nx"; //$NON-NLS-1$
		String classNameJy = "substitutes.y.Jy"; //$NON-NLS-1$
		String classNameLy = "substitutes.y.Ly"; //$NON-NLS-1$
		String classNameNy = "substitutes.y.Ny"; //$NON-NLS-1$

		try {
			Class jX = jBundle.loadClass(classNameJx);
			assertEquals("iBundle has different copy of Jx", jX, iBundle.loadClass(classNameJx)); //$NON-NLS-1$
			assertEquals("mBundle has different copy of Jx", jX, mBundle.loadClass(classNameJx)); //$NON-NLS-1$
			assertEquals("nBundle has different copy of Jx", jX, nBundle.loadClass(classNameJx)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of Jx", jX, pBundle.loadClass(classNameJx)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of Jx", jX, qBundle.loadClass(classNameJx)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}
		try {
			Class jY = jBundle.loadClass(classNameJy);
			assertEquals("jBundle has different copy of Jy", jY, iBundle.loadClass(classNameJy)); //$NON-NLS-1$
			assertEquals("mBundle has different copy of Jy", jY, mBundle.loadClass(classNameJy)); //$NON-NLS-1$
			assertEquals("nBundle has different copy of Jy", jY, nBundle.loadClass(classNameJy)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of Jy", jY, pBundle.loadClass(classNameJy)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of Jy", jY, qBundle.loadClass(classNameJy)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}

		try {
			Class lX = lBundle.loadClass(classNameLx);
			assertEquals("lBundle has different copy of Lx", lX, kBundle.loadClass(classNameLx)); //$NON-NLS-1$
			assertEquals("mBundle has different copy of Lx", lX, mBundle.loadClass(classNameLx)); //$NON-NLS-1$
			assertEquals("nBundle has different copy of Lx", lX, nBundle.loadClass(classNameLx)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of Lx", lX, pBundle.loadClass(classNameLx)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of Lx", lX, qBundle.loadClass(classNameLx)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}

		try {
			Class lY = lBundle.loadClass(classNameLy);
			assertEquals("lBundle has different copy of Ly", lY, kBundle.loadClass(classNameLy)); //$NON-NLS-1$
			assertEquals("mBundle has different copy of Ly", lY, mBundle.loadClass(classNameLy)); //$NON-NLS-1$
			assertEquals("nBundle has different copy of Ly", lY, nBundle.loadClass(classNameLy)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of Ly", lY, pBundle.loadClass(classNameLy)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of Ly", lY, qBundle.loadClass(classNameLy)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}

		try {
			Class nX = nBundle.loadClass(classNameNx);
			assertEquals("nBundle has different copy of Nx", nX, mBundle.loadClass(classNameNx)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of Nx", nX, pBundle.loadClass(classNameNx)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of Nx", nX, qBundle.loadClass(classNameNx)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}

		try {
			Class nY = nBundle.loadClass(classNameNy);
			assertEquals("nBundle has different copy of Ny", nY, mBundle.loadClass(classNameNy)); //$NON-NLS-1$
			assertEquals("pBundle has different copy of Ny", nY, pBundle.loadClass(classNameNy)); //$NON-NLS-1$
			assertEquals("qBundle has different copy of Ny", nY, qBundle.loadClass(classNameNy)); //$NON-NLS-1$
		} catch (ClassNotFoundException cnfe) {
			fail("Unexpected exception", cnfe); //$NON-NLS-1$
		}

		String[] unexpectedClasseNames = new String[] {"substitutes.x.Ix", "substitutes.x.Kx", "substitutes.x.Mx", "substitutes.y.Iy", "substitutes.y.Ky", "substitutes.y.My"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
		for (int i = 0; i < unexpectedClasseNames.length; i++) {
			for (int j = 0; j < allBundles.length; j++) {
				try {
					Class found = allBundles[j].loadClass(unexpectedClasseNames[i]);
					fail("Found class " + found + " in bundle " + allBundles[j]); //$NON-NLS-1$//$NON-NLS-2$
				} catch (ClassNotFoundException cnfe) {
					// expected
				}
			}
		}
	}

	public void testSubstituteExports08() throws BundleException {
		Bundle a = installer.installBundle("substitutes.a"); //$NON-NLS-1$
		Bundle b = installer.installBundle("substitutes.b"); //$NON-NLS-1$
		Bundle c = installer.installBundle("substitutes.c"); //$NON-NLS-1$
		Bundle d = installer.installBundle("substitutes.d"); //$NON-NLS-1$
		Bundle[] allBundles = new Bundle[] {a, b, c, d};
		doRefreshTest(allBundles, a);
	}

	public void testSubstituteExports09() throws BundleException {
		Bundle a = installer.installBundle("substitutes.a"); //$NON-NLS-1$
		Bundle b = installer.installBundle("substitutes.b"); //$NON-NLS-1$
		Bundle e = installer.installBundle("substitutes.e"); //$NON-NLS-1$
		Bundle f = installer.installBundle("substitutes.f"); //$NON-NLS-1$
		Bundle[] allBundles = new Bundle[] {a, b, e, f};
		doRefreshTest(allBundles, a);
	}

	public void testSubstituteExports10() throws BundleException {
		Bundle a = installer.installBundle("substitutes.a"); //$NON-NLS-1$
		Bundle b = installer.installBundle("substitutes.b"); //$NON-NLS-1$
		Bundle e = installer.installBundle("substitutes.e"); //$NON-NLS-1$
		Bundle f = installer.installBundle("substitutes.f"); //$NON-NLS-1$
		Bundle g = installer.installBundle("substitutes.g"); //$NON-NLS-1$
		Bundle h = installer.installBundle("substitutes.h"); //$NON-NLS-1$
		Bundle[] allBundles = new Bundle[] {a, b, e, f, g, h};
		doRefreshTest(allBundles, a);
	}

	public void testSubstituteExports11() throws BundleException {
		Bundle iBundle = installer.installBundle("substitutes.i"); //$NON-NLS-1$
		Bundle jBundle = installer.installBundle("substitutes.j"); //$NON-NLS-1$
		installer.installBundle("substitutes.k"); //$NON-NLS-1$
		installer.installBundle("substitutes.l"); //$NON-NLS-1$
		Bundle mBundle = installer.installBundle("substitutes.m"); //$NON-NLS-1$
		Bundle nBundle = installer.installBundle("substitutes.n"); //$NON-NLS-1$
		Bundle pBundle = installer.installBundle("substitutes.p"); //$NON-NLS-1$
		Bundle qBundle = installer.installBundle("substitutes.q"); //$NON-NLS-1$
		Bundle[] allBundles = new Bundle[] {iBundle, jBundle, mBundle, nBundle, pBundle, qBundle}; // k and l do not depend on i
		doRefreshTest(allBundles, iBundle);
	}

	public void testSubstituteExports12() throws BundleException {
		Bundle jBundle = installer.installBundle("substitutes.j"); //$NON-NLS-1$
		Bundle iBundle = installer.installBundle("substitutes.i"); //$NON-NLS-1$
		installer.installBundle("substitutes.l"); //$NON-NLS-1$
		installer.installBundle("substitutes.k"); //$NON-NLS-1$
		Bundle nBundle = installer.installBundle("substitutes.n"); //$NON-NLS-1$
		Bundle mBundle = installer.installBundle("substitutes.m"); //$NON-NLS-1$
		Bundle qBundle = installer.installBundle("substitutes.q"); //$NON-NLS-1$
		Bundle pBundle = installer.installBundle("substitutes.p"); //$NON-NLS-1$
		Bundle[] allBundles = new Bundle[] {iBundle, jBundle, mBundle, nBundle, pBundle, qBundle}; // k and l do not depend on j
		doRefreshTest(allBundles, jBundle);
	}

	private void doRefreshTest(Bundle[] allBundles, Bundle toRefresh) {
		installer.resolveBundles(allBundles);
		Bundle[] refreshed = installer.refreshPackages(new Bundle[] {toRefresh});
		for (int i = 0; i < allBundles.length; i++) {
			boolean found = false;
			for (int j = 0; j < refreshed.length && !found; j++)
				found = allBundles[i] == refreshed[j];
			if (!found)
				fail("bundle did not get refreshed: " + allBundles[i]); //$NON-NLS-1$
		}
		assertEquals("Wrong number of bundles refreshed", allBundles.length, refreshed.length); //$NON-NLS-1$
	}

	public void testSubstituteExports13() throws BundleException {
		Bundle a = installer.installBundle("substitutes.a"); //$NON-NLS-1$
		Bundle b = installer.installBundle("substitutes.b"); //$NON-NLS-1$
		Bundle c = installer.installBundle("substitutes.c"); //$NON-NLS-1$
		Bundle d = installer.installBundle("substitutes.d"); //$NON-NLS-1$
		Bundle[] allBundles = new Bundle[] {a, b, c, d};
		assertTrue("Bundles are not resolved", installer.resolveBundles(allBundles)); //$NON-NLS-1$

		PackageAdmin pa = installer.getPackageAdmin();

		ExportedPackage[] xPackages = pa.getExportedPackages("substitutes.x"); //$NON-NLS-1$
		assertNotNull("xPackages is null", xPackages); //$NON-NLS-1$
		assertEquals("xPackages wrong number", 1, xPackages.length); //$NON-NLS-1$
		assertEquals("Wrong exporter", a, xPackages[0].getExportingBundle()); //$NON-NLS-1$
		Bundle[] xImporters = xPackages[0].getImportingBundles();
		assertNotNull("xImporters is null", xImporters); //$NON-NLS-1$
		assertEquals("Wrong number of xImporters", 3, xImporters.length); //$NON-NLS-1$

		ExportedPackage[] yPackages = pa.getExportedPackages("substitutes.y"); //$NON-NLS-1$
		assertNotNull("yPackages is null", yPackages); //$NON-NLS-1$
		assertEquals("yPackages wrong number", 1, yPackages.length); //$NON-NLS-1$
		assertEquals("Wrong exporter", a, yPackages[0].getExportingBundle()); //$NON-NLS-1$
		Bundle[] yImporters = yPackages[0].getImportingBundles();
		assertNotNull("yImporters is null", yImporters); //$NON-NLS-1$
		assertEquals("Wrong number of yImporters", 3, yImporters.length); //$NON-NLS-1$

		Bundle[] expectedImporters = new Bundle[] {b, c, d};
		for (int i = 0; i < expectedImporters.length; i++) {
			contains("xPackages importers does not contain", xImporters, expectedImporters[i]); //$NON-NLS-1$
			contains("yPackages importers does not contain", yImporters, expectedImporters[i]); //$NON-NLS-1$
		}
	}

	public void testSubstituteExports14() throws BundleException {
		Bundle iBundle = installer.installBundle("substitutes.i"); //$NON-NLS-1$
		Bundle jBundle = installer.installBundle("substitutes.j"); //$NON-NLS-1$
		Bundle kBundle = installer.installBundle("substitutes.k"); //$NON-NLS-1$
		Bundle lBundle = installer.installBundle("substitutes.l"); //$NON-NLS-1$
		Bundle mBundle = installer.installBundle("substitutes.m"); //$NON-NLS-1$
		Bundle nBundle = installer.installBundle("substitutes.n"); //$NON-NLS-1$
		Bundle pBundle = installer.installBundle("substitutes.p"); //$NON-NLS-1$
		Bundle qBundle = installer.installBundle("substitutes.q"); //$NON-NLS-1$
		Bundle[] allBundles = new Bundle[] {iBundle, jBundle, kBundle, lBundle, mBundle, nBundle, pBundle, qBundle}; // k and l do not depend on i
		assertTrue("Bundles are not resolved", installer.resolveBundles(allBundles)); //$NON-NLS-1$

		PackageAdmin pa = installer.getPackageAdmin();

		ExportedPackage[] xPackages = pa.getExportedPackages("substitutes.x"); //$NON-NLS-1$
		assertNotNull("xPackages is null", xPackages); //$NON-NLS-1$
		assertEquals("xPackages wrong number", 3, xPackages.length); //$NON-NLS-1$

		ExportedPackage[] yPackages = pa.getExportedPackages("substitutes.y"); //$NON-NLS-1$
		assertNotNull("yPackages is null", yPackages); //$NON-NLS-1$
		assertEquals("yPackages wrong number", 3, yPackages.length); //$NON-NLS-1$

		Bundle[] expectedExporters = new Bundle[] {iBundle, kBundle, mBundle};
		for (int i = 0; i < expectedExporters.length; i++) {
			boolean found = false;
			for (int j = 0; j < xPackages.length && !found; j++) {
				found = expectedExporters[i] == xPackages[j].getExportingBundle();
				if (found) {
					Bundle[] importingBundles = xPackages[j].getImportingBundles();
					Bundle[] expectedImporters = null;
					String message = null;
					if (expectedExporters[i] == iBundle) {
						expectedImporters = new Bundle[] {jBundle, mBundle, nBundle, pBundle, qBundle};
						message = "iBundle "; //$NON-NLS-1$
					} else if (expectedExporters[i] == kBundle) {
						expectedImporters = new Bundle[] {lBundle, mBundle, nBundle, pBundle, qBundle};
						message = "kBundle "; //$NON-NLS-1$
					} else if (expectedExporters[i] == mBundle) {
						expectedImporters = new Bundle[] {nBundle, pBundle, qBundle};
						message = "mBundle "; //$NON-NLS-1$
					}
					assertEquals(message, expectedImporters.length, importingBundles.length);
					for (int k = 0; k < expectedImporters.length; k++)
						contains(message, importingBundles, expectedImporters[k]);
				}
			}
		}

	}

	private void contains(String message, Bundle[] bundles, Bundle b) {
		boolean found = false;
		for (int i = 0; i < bundles.length && !found; i++)
			found = bundles[i] == b;
		if (!found)
			fail(message + b);
	}
}

Back to the top