Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2e5e3d73f9c241c0a260f88060b317990103dbf4 (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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
/*******************************************************************************
 * Copyright (c) 2008, 2011 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.composites;

import java.util.*;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.osgi.tests.OSGiTestsActivator;
import org.osgi.framework.*;
import org.osgi.framework.launch.Framework;
import org.osgi.service.framework.*;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;

public class CompositeShareTests extends AbstractCompositeTests {
	public static Test suite() {
		return new TestSuite(CompositeShareTests.class);
	}

	public void testCompositeShare01() {
		// simple test to create an empty composite bundle
		// sharing one package from the parent
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare01"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "org.osgi.service.application"); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "testCompositeShare01", null, linkManifest, false, false); //$NON-NLS-1$
		startCompositeBundle(compositeBundle, false);
		SurrogateBundle surrogate = compositeBundle.getSurrogateBundle();
		assertNotNull("Surrogate is null", surrogate); //$NON-NLS-1$
		try {
			surrogate.loadClass("org.osgi.service.application.ApplicationDescriptor"); //$NON-NLS-1$
		} catch (ClassNotFoundException e) {
			fail("Unexpected class load exception", e); //$NON-NLS-1$
		}
		// make sure missing classes do not endlessly loop
		try {
			surrogate.loadClass("does.not.exist.Here"); //$NON-NLS-1$
			fail("Expected a loadClass exception"); //$NON-NLS-1$
		} catch (ClassNotFoundException e) {
			// expected
		}
		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare01a() {
		// simple test to create an empty composite bundle
		// sharing one package from the parent
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, getName());
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "org.osgi.service.application"); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, getName(), null, linkManifest, false, false);
		startCompositeBundle(compositeBundle, false);

		stopCompositeBundle(compositeBundle);
		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare02() {
		// simple test to create an empty composite bundle
		// sharing one package from the parent and sharing one non-existing package from child
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare02"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "org.osgi.service.application"); //$NON-NLS-1$
		linkManifest.put(Constants.EXPORT_PACKAGE, "foo"); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "testCompositeShare02", null, linkManifest, false, false); //$NON-NLS-1$

		startCompositeBundle(compositeBundle, true);
		SurrogateBundle surrogate = compositeBundle.getSurrogateBundle();
		assertNotNull("Companion is null", surrogate); //$NON-NLS-1$
		try {
			surrogate.loadClass("org.osgi.service.application.ApplicationDescriptor"); //$NON-NLS-1$
			fail("Expected a loadClass exception"); //$NON-NLS-1$
		} catch (ClassNotFoundException e) {
			// expected
		}
		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare03() {
		// create a composite bundle with one bundle that exports some api to parent
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare03"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"value1\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "testCompositeShare03", null, linkManifest, false, false); //$NON-NLS-1$
		installIntoChild(compositeBundle.getCompositeFramework(), "test.link.a"); //$NON-NLS-1$

		startCompositeBundle(compositeBundle, false);
		try {
			compositeBundle.loadClass("test.link.a.SomeAPI"); //$NON-NLS-1$
		} catch (ClassNotFoundException e) {
			fail("Unexpected a loadClass exception", e); //$NON-NLS-1$
		}
		//make sure missing classes do not endlessly loop
		try {
			compositeBundle.loadClass("does.not.exist.Here"); //$NON-NLS-1$
			fail("Expected a loadClass exception"); //$NON-NLS-1$
		} catch (ClassNotFoundException e) {
			// expected
		}
		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare04a() {
		// create a composite bundle with one bundle that exports some api to parent
		// install one bundle into parent that uses API from child
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare04a"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"bad value\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"bad value\""); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "testCompositeShare04a", null, linkManifest, false, false); //$NON-NLS-1$
		installIntoChild(compositeBundle.getCompositeFramework(), "test.link.a"); //$NON-NLS-1$
		Bundle testClient = installIntoCurrent("test.link.a.client"); //$NON-NLS-1$

		SurrogateBundle surrogate = compositeBundle.getSurrogateBundle();
		assertNotNull("Companion is null", surrogate); //$NON-NLS-1$
		startCompositeBundle(compositeBundle, true);
		try {
			testClient.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}
		// put good value back
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"value1\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}

		startCompositeBundle(compositeBundle, false);
		try {
			testClient.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		// put bad value back
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"bad value\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"bad value\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}
		installer.refreshPackages(new Bundle[] {compositeBundle});
		startCompositeBundle(compositeBundle, true);
		try {
			testClient.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}

		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare04b() {
		// create a composite bundle with one bundle that exports some api to parent
		// install one bundle into parent that uses API from child
		// test lazy uninstall
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare04b"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"bad value\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"bad value\""); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "testCompositeShare04b", null, linkManifest, false, false); //$NON-NLS-1$
		installIntoChild(compositeBundle.getCompositeFramework(), "test.link.a"); //$NON-NLS-1$
		Bundle testClient = installIntoCurrent("test.link.a.client"); //$NON-NLS-1$

		SurrogateBundle surrogate = compositeBundle.getSurrogateBundle();
		assertNotNull("Surrogate is null", surrogate); //$NON-NLS-1$
		startCompositeBundle(compositeBundle, true);
		try {
			testClient.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}
		// put good value back
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"value1\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}

		startCompositeBundle(compositeBundle, false);
		try {
			testClient.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
		// refresh client to clear class laoder
		installer.refreshPackages(new Bundle[] {testClient});

		// put bad value back
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"bad value\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"bad value\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}

		// make sure testClient can still start before refresh
		try {
			testClient.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		installer.refreshPackages(new Bundle[] {compositeBundle});
		startCompositeBundle(compositeBundle, true);
		try {
			testClient.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}

		// put good value back
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"value1\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}

		startCompositeBundle(compositeBundle, false);
		uninstallCompositeBundle(compositeBundle);
		// make sure testClient can still start after uninstall
		try {
			testClient.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		installer.refreshPackages(new Bundle[] {compositeBundle});
		try {
			testClient.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}
	}

	public void testCompositeShare04c() {
		// create a composite bundle with one bundle that exports some api to parent
		// install one bundle into parent that uses API from child
		// Setup the composite to have substitutable exports
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, getName()); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"value1\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.a, test.link.a.params"); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, getName(), null, linkManifest, false, false); //$NON-NLS-1$
		installIntoChild(compositeBundle.getCompositeFramework(), "test.link.a"); //$NON-NLS-1$
		Bundle testClient = installIntoCurrent("test.link.a.client"); //$NON-NLS-1$

		SurrogateBundle surrogate = compositeBundle.getSurrogateBundle();
		assertNotNull("Companion is null", surrogate); //$NON-NLS-1$
		startCompositeBundle(compositeBundle, false);
		try {
			testClient.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		// put bad value for export
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"bad value\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"bad value\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}
		installer.refreshPackages(new Bundle[] {compositeBundle});
		startCompositeBundle(compositeBundle, true);
		try {
			testClient.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}

		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare04d_Bug363561() {
		// create a composite bundle with one bundle that exports some api to parent
		// install one bundle into parent that uses API from child
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, getName()); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.a; attr1=\"value1\"; uses:=\"org.osgi.framework, test.link.a.params\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "testCompositeShare04a", null, linkManifest, false, false); //$NON-NLS-1$
		Bundle constituent = installIntoChild(compositeBundle.getCompositeFramework(), "test.link.a"); //$NON-NLS-1$
		Bundle testClient = installIntoCurrent("test.link.a.client"); //$NON-NLS-1$
		startCompositeBundle(compositeBundle, false);

		try {
			constituent.uninstall();
		} catch (BundleException e) {
			fail("Error on uninstall of constituent.", e);
		}
		stopCompositeBundle(compositeBundle);
		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare05() {
		// create a composite bundle with one bundle that exports some api to child
		// install one bundle into child that uses API from parent
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare05"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.a; attr1=\"bad value\", test.link.a.params;  attr2=\"bad value\""); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "testCompositeShare03", null, linkManifest, false, false); //$NON-NLS-1$
		installIntoCurrent("test.link.a"); //$NON-NLS-1$
		Bundle testClient = installIntoChild(compositeBundle.getCompositeFramework(), "test.link.a.client"); //$NON-NLS-1$

		SurrogateBundle surrogate = compositeBundle.getSurrogateBundle();
		assertNotNull("Surrogate is null", surrogate); //$NON-NLS-1$
		startCompositeBundle(compositeBundle, true);
		assertFalse("Bundle should not be resolved", resolveBundle(compositeBundle.getCompositeFramework(), testClient));
		try {
			testClient.start();
		} catch (BundleException e) {
			fail("Unexpected exception type", e); //$NON-NLS-1$
		}

		// put good value back
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.a; attr1=\"value1\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}

		startCompositeBundle(compositeBundle, false);
		try {
			testClient.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		// put bad value back
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.a; attr1=\"bad value\", test.link.a.params;  attr2=\"bad value\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}
		installer.refreshPackages(new Bundle[] {compositeBundle});
		startCompositeBundle(compositeBundle, true);
		try {
			testClient.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}

		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare06() {
		// simple test to create an empty composite bundle
		// sharing one package from a bundle in the parent and 
		// one package from the system bundle in the parent
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare06"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "org.osgi.service.application, org.osgi.framework"); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "testCompositeShare06", null, linkManifest, false, false); //$NON-NLS-1$
		startCompositeBundle(compositeBundle, false);
		SurrogateBundle surrogate = compositeBundle.getSurrogateBundle();
		assertNotNull("Surrogate is null", surrogate); //$NON-NLS-1$
		try {
			surrogate.loadClass("org.osgi.service.application.ApplicationDescriptor"); //$NON-NLS-1$
			Class bundleClass = surrogate.loadClass("org.osgi.framework.Bundle"); //$NON-NLS-1$
			assertEquals("Bundle classes are not the same", Bundle.class, bundleClass); //$NON-NLS-1$
		} catch (ClassNotFoundException e) {
			fail("Unexpected class load exception", e); //$NON-NLS-1$
		}

		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare07a() {
		// test two way sharing
		// create a composite bundle with one bundle that exports some api to child
		// install one bundle into child that uses API from parent
		// install a child bundle that exports some api to parent
		// install one bundle into parent that uses API from child
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare07a"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.a; attr1=\"value1\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.b; attr1=\"value1\"; uses:=\"org.osgi.framework, test.link.b.params\", test.link.b.params; attr2=\"value2\""); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "testCompositeShare07a", null, linkManifest, false, false); //$NON-NLS-1$
		installIntoCurrent("test.link.a"); //$NON-NLS-1$
		Bundle testClientA = installIntoChild(compositeBundle.getCompositeFramework(), "test.link.a.client"); //$NON-NLS-1$
		installIntoChild(compositeBundle.getCompositeFramework(), "test.link.b"); //$NON-NLS-1$
		Bundle testClientB = installIntoCurrent("test.link.b.client"); //$NON-NLS-1$

		startCompositeBundle(compositeBundle, false);
		try {
			testClientA.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		try {
			testClientB.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		// use bad import package value
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.a; attr1=\"bad value\", test.link.a.params;  attr2=\"bad value\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}
		installer.refreshPackages(new Bundle[] {compositeBundle});
		startCompositeBundle(compositeBundle, true);
		try {
			testClientA.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}
		try {
			testClientB.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}

		// put good value back
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.a; attr1=\"value1\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}

		startCompositeBundle(compositeBundle, false);
		try {
			testClientA.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		try {
			testClientB.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare07b() {
		// test two way sharing AND lazy update
		// create a composite bundle with one bundle that exports some api to child
		// install one bundle into child that uses API from parent
		// install a child bundle that exports some api to parent
		// install one bundle into parent that uses API from child
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare07b"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.a; attr1=\"value1\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.b; attr1=\"value1\"; uses:=\"org.osgi.framework, test.link.b.params\", test.link.b.params; attr2=\"value2\""); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "testCompositeShare07b", null, linkManifest, false, false); //$NON-NLS-1$
		installIntoCurrent("test.link.a"); //$NON-NLS-1$
		Bundle testClientA = installIntoChild(compositeBundle.getCompositeFramework(), "test.link.a.client"); //$NON-NLS-1$
		installIntoChild(compositeBundle.getCompositeFramework(), "test.link.b"); //$NON-NLS-1$
		Bundle testClientB = installIntoCurrent("test.link.b.client"); //$NON-NLS-1$

		startCompositeBundle(compositeBundle, false);
		try {
			testClientA.start();
			testClientA.stop();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		try {
			testClientB.start();
			testClientB.stop();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		// force a refresh packages of the composite to get new class loaders for the clients
		installer.refreshPackages(new Bundle[] {compositeBundle});

		// use bad import package value
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0.a"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.a; attr1=\"bad value\", test.link.a.params;  attr2=\"bad value\""); //$NON-NLS-1$
		try {
			compositeBundle.update(linkManifest);
		} catch (BundleException e) {
			fail("Unexpected composite update exception", e); //$NON-NLS-1$
		}

		// make sure we can still start the clients before a refresh; 
		// they should still be wired to the old content
		try {
			testClientA.start();
			testClientA.stop();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		try {
			testClientB.start();
			testClientB.stop();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		installer.refreshPackages(new Bundle[] {compositeBundle});
		startCompositeBundle(compositeBundle, true);
		try {
			testClientA.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}
		try {
			testClientB.start();
			fail("Expected start failure"); //$NON-NLS-1$
		} catch (BundleException e) {
			assertEquals("Unexpected exception type", BundleException.RESOLVE_ERROR, e.getType()); //$NON-NLS-1$
		}

		uninstallCompositeBundle(compositeBundle);
	}

	public void testCompositeShare08() {
		// Create a child framework to install composites into to test persistence
		CompositeBundle compositeLevel0 = createCompositeBundle(linkBundleFactory, "testCompositeShare08LV0", null, null, true, false); //$NON-NLS-1$
		CompositeBundleFactory factoryLevel1 = getFactory(compositeLevel0.getCompositeFramework());
		// create a level 1 composites
		CompositeBundle compositeLevel1 = createCompositeBundle(factoryLevel1, "testCompositeShare08LV1", null, null, true, false); //$NON-NLS-1$
		CompositeBundleFactory factoryLevel2 = getFactory(compositeLevel1.getCompositeFramework());
		long level1ID = compositeLevel1.getBundleId();

		// test two way sharing
		// create a composite bundle with one bundle that exports some api to child
		// install one bundle into child that uses API from parent
		// install a child bundle that exports some api to parent
		// install one bundle into parent that uses API from child
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare08LV2"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.a; attr1=\"value1\", test.link.a.params; attr2=\"value2\""); //$NON-NLS-1$
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.b; attr1=\"value1\"; uses:=\"org.osgi.framework, test.link.b.params\", test.link.b.params; attr2=\"value2\""); //$NON-NLS-1$
		CompositeBundle compositeLevel2 = createCompositeBundle(factoryLevel2, "testCompositeShare08LV2", null, linkManifest, false, false); //$NON-NLS-1$
		long level2ID = compositeLevel2.getBundleId();

		installIntoChild(compositeLevel1.getCompositeFramework(), "test.link.a"); //$NON-NLS-1$
		Bundle testClientA = installIntoChild(compositeLevel2.getCompositeFramework(), "test.link.a.client"); //$NON-NLS-1$
		long clientAID = testClientA.getBundleId();
		installIntoChild(compositeLevel2.getCompositeFramework(), "test.link.b"); //$NON-NLS-1$
		Bundle testClientB = installIntoChild(compositeLevel1.getCompositeFramework(), "test.link.b.client"); //$NON-NLS-1$
		long clientBID = testClientB.getBundleId();

		startCompositeBundle(compositeLevel2, false);
		try {
			testClientA.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		try {
			testClientB.start();
		} catch (BundleException e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}

		// stop level 0 composite
		stopCompositeBundle(compositeLevel0);

		// check the state of bundles installed into composite frameworks
		assertEquals("Wrong state for client A", Bundle.UNINSTALLED, testClientA.getState()); //$NON-NLS-1$
		assertEquals("Wrong state for client B", Bundle.UNINSTALLED, testClientB.getState()); //$NON-NLS-1$

		// start level 0 composite again
		startCompositeBundle(compositeLevel0, false);

		// get reified level 1 and 2 composites and client A and B bundles 
		compositeLevel1 = (CompositeBundle) compositeLevel0.getCompositeFramework().getBundleContext().getBundle(level1ID);
		compositeLevel2 = (CompositeBundle) compositeLevel1.getCompositeFramework().getBundleContext().getBundle(level2ID);
		testClientA = compositeLevel2.getCompositeFramework().getBundleContext().getBundle(clientAID);
		testClientB = compositeLevel1.getCompositeFramework().getBundleContext().getBundle(clientBID);

		// check the state of bundles installed into composite frameworks
		assertEquals("Wrong state for client A", Bundle.ACTIVE, testClientA.getState()); //$NON-NLS-1$
		assertEquals("Wrong state for client B", Bundle.ACTIVE, testClientB.getState()); //$NON-NLS-1$

		uninstallCompositeBundle(compositeLevel0);
	}

	public void testCompositeShare09a() {
		// exports packages from a bundle installed into the parent
		// and imports the packages from a bundle installed into the child
		// and services registered in the parent are propagated to the child
		Bundle cService = installIntoCurrent("test.link.c"); //$NON-NLS-1$
		try {
			cService.start();
		} catch (BundleException e) {
			fail("Failed to start test.link.c", e); //$NON-NLS-1$
		}
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare09a"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.c.service1,test.link.c.service2,test.link.c.service3"); //$NON-NLS-1$
		linkManifest.put(CompositeBundleFactory.COMPOSITE_SERVICE_FILTER_IMPORT, "(objectClass=test.link.c.service1.Service1),(objectClass=test.link.c.service2.Service2),(objectClass=test.link.c.service3.Service3)"); //$NON-NLS-1$
		CompositeBundle composite = createCompositeBundle(linkBundleFactory, "testCompositeShare09a", null, linkManifest, true, false); //$NON-NLS-1$

		startCompositeBundle(composite, false);

		ServiceReference[] targetServices = composite.getSurrogateBundle().getRegisteredServices();
		assertNotNull("No target services found", targetServices); //$NON-NLS-1$
		assertEquals("Wrong number of target services", 3, targetServices.length); //$NON-NLS-1$

		Bundle cClient = installIntoChild(composite.getCompositeFramework(), "test.link.c.client"); //$NON-NLS-1$

		try {
			cClient.start();
			cClient.stop();
		} catch (BundleException e) {
			fail("Failed to start/stop the client", e); //$NON-NLS-1$
		}

		try {
			cService.stop();
		} catch (BundleException e) {
			fail("Failed to stop cService", e); //$NON-NLS-1$
		}

		try {
			cClient.start();
			fail("Should have failed to start cClient"); //$NON-NLS-1$
		} catch (BundleException e) {
			// expected
		}
		try {
			cService.start();
		} catch (BundleException e) {
			fail("Failed to start test.link.c", e); //$NON-NLS-1$
		}
		try {
			cClient.start();
			cClient.stop();
		} catch (BundleException e) {
			fail("Failed to start/stop the client", e); //$NON-NLS-1$
		}
		uninstallCompositeBundle(composite);
	}

	public void testCompositeShare09b() {
		// two way package and service tests
		// exports packages from a bundle installed into the parent
		// and imports the packages from a bundle installed into the child
		// and services registered in the parent are propagated to the child
		// and services registered in the child are propagated to the parent
		Bundle cService = installIntoCurrent("test.link.c"); //$NON-NLS-1$
		try {
			cService.start();
		} catch (BundleException e) {
			fail("Failed to start test.link.c", e); //$NON-NLS-1$
		}
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare09b"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(Constants.IMPORT_PACKAGE, "test.link.c.service1,test.link.c.service2,test.link.c.service3"); //$NON-NLS-1$
		linkManifest.put(Constants.EXPORT_PACKAGE, "test.link.e.service1; attr1=\"value1\", test.link.e.service2; attr2=\"value2\"; uses:=\"test.link.e.service1\", test.link.e.service3; attr3=\"value3\"; uses:=\"test.link.e.service2\""); //$NON-NLS-1$
		linkManifest.put(CompositeBundleFactory.COMPOSITE_SERVICE_FILTER_IMPORT, "(objectClass=test.link.c.service1.Service1),(objectClass=test.link.c.service2.Service2),(objectClass=test.link.c.service3.Service3)"); //$NON-NLS-1$
		linkManifest.put(CompositeBundleFactory.COMPOSITE_SERVICE_FILTER_EXPORT, "(objectClass=test.link.e.service1.Service1),(objectClass=test.link.e.service2.Service2),(objectClass=test.link.e.service3.Service3)"); //$NON-NLS-1$
		CompositeBundle composite = createCompositeBundle(linkBundleFactory, "testCompositeShare09b", null, linkManifest, false, false); //$NON-NLS-1$

		Bundle eService = installIntoChild(composite.getCompositeFramework(), "test.link.e"); //$NON-NLS-1$
		startCompositeBundle(composite, false);

		try {
			eService.start();
		} catch (BundleException e) {
			fail("Failed to start test.link.e", e); //$NON-NLS-1$
		}

		ServiceReference[] childServices = composite.getSurrogateBundle().getRegisteredServices();
		assertNotNull("No child services found", childServices); //$NON-NLS-1$
		assertEquals("Wrong number of child services", 3, childServices.length); //$NON-NLS-1$

		ServiceReference[] parentServices = composite.getRegisteredServices();
		assertNotNull("No parent services found", parentServices); //$NON-NLS-1$
		assertEquals("Wrong number of parent services", 3, parentServices.length); //$NON-NLS-1$

		Bundle cClient = installIntoChild(composite.getCompositeFramework(), "test.link.c.client"); //$NON-NLS-1$
		Bundle eClient = installIntoCurrent("test.link.e.client"); //$NON-NLS-1$

		try {
			cClient.start();
			cClient.stop();
		} catch (BundleException e) {
			fail("Failed to start/stop the client", e); //$NON-NLS-1$
		}

		try {
			cService.stop();
		} catch (BundleException e) {
			fail("Failed to stop cService", e); //$NON-NLS-1$
		}

		try {
			cClient.start();
			fail("Should have failed to start cClient"); //$NON-NLS-1$
		} catch (BundleException e) {
			// expected
		}
		try {
			cService.start();
		} catch (BundleException e) {
			fail("Failed to start test.link.c", e); //$NON-NLS-1$
		}
		try {
			cClient.start();
			cClient.stop();
		} catch (BundleException e) {
			fail("Failed to start/stop the client", e); //$NON-NLS-1$
		}

		try {
			eClient.start();
			eClient.stop();
		} catch (BundleException e) {
			fail("Failed to start/stop the client", e); //$NON-NLS-1$
		}

		try {
			eService.stop();
		} catch (BundleException e) {
			fail("Failed to stop eService", e); //$NON-NLS-1$
		}

		try {
			eClient.start();
			fail("Should have failed to start eClient"); //$NON-NLS-1$
		} catch (BundleException e) {
			// expected
		}
		try {
			eService.start();
		} catch (BundleException e) {
			fail("Failed to start test.link.e", e); //$NON-NLS-1$
		}
		try {
			eClient.start();
			eClient.stop();
		} catch (BundleException e) {
			fail("Failed to start/stop the client", e); //$NON-NLS-1$
		}
		uninstallCompositeBundle(composite);
	}

	public void testCompositeShare10() {
		// test service listeners with services shared to child
		String filter1 = "(key1=1)"; //$NON-NLS-1$
		String filter2 = "(key2=value1)"; //$NON-NLS-1$
		String filter3 = "(key3=true)"; //$NON-NLS-1$

		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare10"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(CompositeBundleFactory.COMPOSITE_SERVICE_FILTER_IMPORT, filter1 + ',' + filter2);
		CompositeBundle composite = createCompositeBundle(linkBundleFactory, "testCompositeShare10", null, linkManifest, true, false); //$NON-NLS-1$

		startCompositeBundle(composite, false);

		Hashtable props = new Hashtable();
		props.put("key1", new Integer(1)); //$NON-NLS-1$
		props.put("key2", "value1"); //$NON-NLS-1$//$NON-NLS-2$
		props.put("key3", Boolean.TRUE); //$NON-NLS-1$

		// install listener to test with before registering the service so we can get the REGISTERED event
		TestServiceListener resultListener = new TestServiceListener();
		try {
			composite.getCompositeFramework().getBundleContext().addServiceListener(resultListener, "(&" + filter1 + filter2 + filter3 + ")"); //$NON-NLS-1$//$NON-NLS-2$
		} catch (InvalidSyntaxException e) {
			fail("failed to add listener", e); //$NON-NLS-1$
		}

		// register a service that should match the listener filter
		ServiceRegistration registration = OSGiTestsActivator.getContext().registerService(new String[] {Object.class.getName()}, new Object(), props);
		try {
			// expecting to have gotten 1 REGISTERED event
			int[] results = resultListener.getResults();
			assertEquals("Wrong listener results", new int[] {1, 0, 0, 0}, results); //$NON-NLS-1$

			ServiceReference[] targetServices = composite.getSurrogateBundle().getRegisteredServices();
			assertNotNull("No target services found", targetServices); //$NON-NLS-1$
			assertEquals("Wrong number of target services", 1, targetServices.length); //$NON-NLS-1$

			// modify the service properties so they match both the link description
			// and the test listener filter
			props.put("key4", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
			registration.setProperties(props);
			// expecting to get a MODIFIED event
			assertEquals("Wrong listener results", new int[] {1, 1, 0, 0}, results); //$NON-NLS-1$

			// modify the service properties so they match the link description
			// but not the test listener filter
			props.put("key3", Boolean.FALSE); //$NON-NLS-1$
			registration.setProperties(props);
			// expecting to get a MODIFIED_ENDMATCH event
			assertEquals("Wrong listener results", new int[] {1, 1, 1, 0}, results); //$NON-NLS-1$

			// modify the service properties so they still match teh link description
			// but still do not match the test listener filter
			props.put("key4", "value2"); //$NON-NLS-1$ //$NON-NLS-2$
			registration.setProperties(props);
			// expecting no new events
			assertEquals("Wrong listener results", new int[] {1, 1, 1, 0}, results); //$NON-NLS-1$

			// modify the service properties so they match the link description
			// and again match the test listener filter
			props.put("key3", Boolean.TRUE); //$NON-NLS-1$
			registration.setProperties(props);
			// expecting to get a MODIFIED event
			assertEquals("Wrong listener results", new int[] {1, 2, 1, 0}, results); //$NON-NLS-1$

			// modify the service properties so they do not match the link description
			// or the test listener filter
			props.put("key1", new Integer(2)); //$NON-NLS-1$
			props.put("key2", "value2"); //$NON-NLS-1$ //$NON-NLS-2$
			registration.setProperties(props);
			// expecting to get an UNREGISTERING event 
			assertEquals("Wrong listener results", new int[] {1, 2, 1, 1}, results); //$NON-NLS-1$

		} finally {
			composite.getCompositeFramework().getBundleContext().removeServiceListener(resultListener);
			if (registration != null)
				registration.unregister();
		}

		uninstallCompositeBundle(composite);
	}

	public void testCompositeShare11() {
		// Test service trackers with shared services to child
		String filter1 = "(key1=1)"; //$NON-NLS-1$
		String filter2 = "(key2=value1)"; //$NON-NLS-1$
		String filter3 = "(key3=true)"; //$NON-NLS-1$

		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "testCompositeShare11"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		linkManifest.put(CompositeBundleFactory.COMPOSITE_SERVICE_FILTER_IMPORT, filter1 + ',' + filter2);
		CompositeBundle composite = createCompositeBundle(linkBundleFactory, "testCompositeShare10", null, linkManifest, true, false); //$NON-NLS-1$

		startCompositeBundle(composite, false);

		Hashtable props = new Hashtable();
		props.put("key1", new Integer(1)); //$NON-NLS-1$
		props.put("key2", "value1"); //$NON-NLS-1$//$NON-NLS-2$
		props.put("key3", Boolean.TRUE); //$NON-NLS-1$

		// open tracker to test with before registering the service so we can get the REGISTERED event
		ServiceTracker tracker = null;
		TestTrackerCustomizer resultListener = new TestTrackerCustomizer();
		try {
			tracker = new ServiceTracker(composite.getCompositeFramework().getBundleContext(), composite.getCompositeFramework().getBundleContext().createFilter("(&" + filter1 + filter2 + filter3 + ")"), resultListener); //$NON-NLS-1$ //$NON-NLS-2$
			tracker.open();
		} catch (InvalidSyntaxException e) {
			fail("failed to add listener", e); //$NON-NLS-1$
		}

		// register a service that should match the listener filter
		ServiceRegistration registration = OSGiTestsActivator.getContext().registerService(new String[] {Object.class.getName()}, new Object(), props);
		try {
			// expecting to have gotten 1 addingService call
			int[] results = resultListener.getResults();
			assertEquals("Wrong listener results", new int[] {1, 0, 0}, results); //$NON-NLS-1$

			ServiceReference[] targetServices = composite.getSurrogateBundle().getRegisteredServices();
			assertNotNull("No target services found", targetServices); //$NON-NLS-1$
			assertEquals("Wrong number of target services", 1, targetServices.length); //$NON-NLS-1$

			// modify the service properties so they match both the link description
			// and the test tracker filter
			props.put("key4", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
			registration.setProperties(props);
			// expecting to get 1 modifiedService call
			assertEquals("Wrong listener results", new int[] {1, 1, 0}, results); //$NON-NLS-1$

			// modify the service properties so they match the link description
			// but not the test tracker filter
			props.put("key3", Boolean.FALSE); //$NON-NLS-1$
			registration.setProperties(props);
			// expecting to get a removedService call
			assertEquals("Wrong listener results", new int[] {1, 1, 1}, results); //$NON-NLS-1$

			// modify the service properties so they still match the link description
			// but still do not match the test listener filter
			props.put("key4", "value2"); //$NON-NLS-1$ //$NON-NLS-2$
			registration.setProperties(props);
			// expecting no new method calls
			assertEquals("Wrong listener results", new int[] {1, 1, 1}, results); //$NON-NLS-1$

			// modify the service properties so they match the link description
			// and again match the test tracker filter
			props.put("key3", Boolean.TRUE); //$NON-NLS-1$
			registration.setProperties(props);
			// expecting to get an addingService call
			assertEquals("Wrong listener results", new int[] {2, 1, 1}, results); //$NON-NLS-1$

			// modify the service properties so they do not match the link description
			// or the test tracker filter
			props.put("key1", new Integer(2)); //$NON-NLS-1$
			props.put("key2", "value2"); //$NON-NLS-1$ //$NON-NLS-2$
			registration.setProperties(props);
			// expecting to get a removedService call 
			assertEquals("Wrong listener results", new int[] {2, 1, 2}, results); //$NON-NLS-1$

		} finally {
			if (tracker != null)
				tracker.close();
			if (registration != null)
				registration.unregister();
		}

		uninstallCompositeBundle(composite);
	}

	public void testBug258209_1() {
		// create a composite bundle to test thread context class loaders
		Map linkManifest = new HashMap();
		linkManifest.put(Constants.BUNDLE_SYMBOLICNAME, "Bug258209.01"); //$NON-NLS-1$
		linkManifest.put(Constants.BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
		CompositeBundle compositeBundle = createCompositeBundle(linkBundleFactory, "Bug258209.01", null, linkManifest, false, false); //$NON-NLS-1$
		Bundle testTCCL = installIntoChild(compositeBundle.getCompositeFramework(), "test.tccl"); //$NON-NLS-1$
		assertTrue("Bundle should be resolved", resolveBundle(compositeBundle.getCompositeFramework(), testTCCL));
		try {
			testTCCL.start();
		} catch (BundleException e) {
			fail("Unexpected exception starting bundle", e); //$NON-NLS-1$
		}
		assertEquals("Unexpected state", Bundle.RESOLVED, testTCCL.getState()); //$NON-NLS-1$
		// this will start the framework on the current thread; test that the correct tccl is used
		startCompositeBundle(compositeBundle, false);
		assertEquals("Unexpected state", Bundle.ACTIVE, testTCCL.getState()); //$NON-NLS-1$

		// test that the correct tccl is used for framework update
		Framework framework = compositeBundle.getCompositeFramework();
		try {
			framework.update();
			checkActive(testTCCL);
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
		assertEquals("Unexpected state", Bundle.ACTIVE, testTCCL.getState()); //$NON-NLS-1$

		// test that the correct tccl is used for refresh packages
		PackageAdmin pa = getPackageAdmin(compositeBundle.getCompositeFramework());
		pa.refreshPackages(new Bundle[] {testTCCL});
		checkActive(testTCCL);
		assertEquals("Unexpected state", Bundle.ACTIVE, testTCCL.getState()); //$NON-NLS-1$

		// use the tccl service to start the test bundle.
		BundleContext context = compositeBundle.getCompositeFramework().getBundleContext();
		ClassLoader serviceTCCL = null;
		try {
			serviceTCCL = (ClassLoader) context.getService(context.getServiceReferences(ClassLoader.class.getName(), "(equinox.classloader.type=contextClassLoader)")[0]);//$NON-NLS-1$
		} catch (InvalidSyntaxException e) {
			fail("Unexpected", e);//$NON-NLS-1$
		}
		ClassLoader current = Thread.currentThread().getContextClassLoader();
		Thread.currentThread().setContextClassLoader(serviceTCCL);
		try {
			testTCCL.stop();
			testTCCL.start();
		} catch (BundleException e) {
			fail("Unepected", e); //$NON-NLS-1$
		} finally {
			Thread.currentThread().setContextClassLoader(current);
		}
		// test that starting directly works
		try {
			testTCCL.stop();
			testTCCL.start();
		} catch (BundleException e) {
			fail("Unepected", e); //$NON-NLS-1$
		}
		uninstallCompositeBundle(compositeBundle);
	}

	private void checkActive(Bundle b) {
		try {
			// just a hack to make sure we are restarted
			Thread.sleep(500);
			if (b.getState() != Bundle.ACTIVE)
				Thread.sleep(500);
		} catch (Exception e) {
			fail("Unexpected exception", e); //$NON-NLS-1$
		}
	}

	private boolean resolveBundle(Bundle framework, Bundle bundle) {
		PackageAdmin pa = getPackageAdmin(framework);
		return pa.resolveBundles(new Bundle[] {bundle});
	}
}

Back to the top