-
Notifications
You must be signed in to change notification settings - Fork 36
/
ClassLoadingTests.java
3193 lines (2871 loc) · 93.1 KB
/
ClassLoadingTests.java
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
/*
* Copyright (c) OSGi Alliance (2004, 2020). All Rights Reserved.
*
* Implementation of certain elements of the OSGi Specification may be subject
* to third party intellectual property rights, including without limitation,
* patent rights (such a third party may or may not be a member of the OSGi
* Alliance). The OSGi Alliance is not responsible and shall not be held
* responsible in any manner for identifying or failing to identify any or all
* such third party intellectual property rights.
*
* This document and the information contained herein are provided on an "AS IS"
* basis and THE OSGI ALLIANCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
* HEREIN WILL NOT INFRINGE ANY RIGHTS AND ANY IMPLIED WARRANTIES OF
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL THE
* OSGI ALLIANCE BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF BUSINESS, LOSS OF
* USE OF DATA, INTERRUPTION OF BUSINESS, OR FOR DIRECT, INDIRECT, SPECIAL OR
* EXEMPLARY, INCIDENTIAL, PUNITIVE OR CONSEQUENTIAL DAMAGES OF ANY KIND IN
* CONNECTION WITH THIS DOCUMENT OR THE INFORMATION CONTAINED HEREIN, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH LOSS OR DAMAGE.
*
* All Company, brand and product names may be trademarks that are the sole
* property of their respective owners. All rights reserved.
*/
package org.osgi.test.cases.framework.junit.classloading;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.function.BiFunction;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.test.support.compatibility.DefaultTestBundleControl;
import org.osgi.test.support.sleep.Sleep;
import org.osgi.test.support.wiring.Wiring;
/**
* This class contains tests related with the framework class loading policies.
*
* @author left
* @author $Id$
*/
public class ClassLoadingTests extends DefaultTestBundleControl {
protected void tearDown() {
Wiring.synchronousRefreshBundles(getContext());
}
// Service Registry --------------------------
/**
* BundleContext.getServiceReference returns the highest ranked service that
* BundleContext.getServiceReferences returns.
*
* This test case will check the behavior of method getServiceReference()
* when more than one service is installed.
*
* @spec BundleContext.getServiceReference(String)
* @throws Exception if there is any problem or an assert fails
*/
public void testBundleContextGetReference001() throws Exception {
Bundle tb1;
Bundle tb1a;
Bundle tb1b;
Bundle tb4a;
tb1 = installBundle("classloading.tb1.jar");
tb1.start();
tb1a = installBundle("classloading.tb1a.jar");
tb1a.start();
tb1b = installBundle("classloading.tb1b.jar");
tb1b.start();
tb4a = getContext().installBundle(
getWebServer() + "classloading.tb4a.jar");
try {
trace("Checking the service reference returned by BundleContext.getServiceReference()");
tb4a.start();
tb4a.stop();
}
catch (BundleException ex) {
if (ex.getCause() != null) {
fail(ex.getCause().getMessage());
}
else {
fail(ex.getMessage());
}
}
finally {
tb4a.uninstall();
tb1b.stop();
tb1b.uninstall();
tb1a.stop();
tb1a.uninstall();
tb1.stop();
tb1.uninstall();
}
}
/**
* If getServiceReferences returns no service references, then
* getServiceReference returns null.
*
* This test case will check the behavior of method getServiceReference()
* without any installed services.
*
* @spec BundleContext.getServiceReference(String)
* @throws Exception if there is any problem or an assert fails
*/
public void testBundleContextGetReference002() throws Exception {
Bundle tb4b;
tb4b = getContext().installBundle(
getWebServer() + "classloading.tb4b.jar");
try {
trace("Checking the behavior of BundleContext.getServiceReference() when BundleContext.getServiceReferences() returns no references");
tb4b.start();
tb4b.stop();
}
catch (BundleException ex) {
if (ex.getCause() != null) {
fail(ex.getCause().getMessage());
}
else {
fail(ex.getMessage());
}
}
finally {
tb4b.uninstall();
}
}
/**
* Some bundles may wish to get all service references available in the
* framework regardless of what version of a package they are wired to. A
* new method is added (BundleContext.getAllServiceReferences(String,
* String)) to allow a bundle to get all ServiceReference objects.
* getAllServiceReferences returns all the ServiceReference objects without
* performing any class tests.
*
* The test bundle will importing the version 3.0.0 of the interface
* SomeService and check the behavior of method getServiceReferences() when
* two services using the interface version 1.0.0 are installed. The method
* getAllServiceReferences() must returns all services independent of the
* interface wired to the test bundle.
*
* @spec BundleContext.getAllServiceReferences(String, String)
* @throws Exception if there is any problem or an assert fails
*/
public void testBundleContextGetAllServiceReferences001() throws Exception {
Bundle tb1;
Bundle tb1a;
Bundle tb1b;
Bundle tb3;
Bundle tb4c;
tb1 = installBundle("classloading.tb1.jar");
tb1.start();
tb1a = installBundle("classloading.tb1a.jar");
tb1a.start();
tb1b = installBundle("classloading.tb1b.jar");
tb1b.start();
tb3 = installBundle("classloading.tb3.jar");
tb3.start();
tb4c = getContext().installBundle(
getWebServer() + "classloading.tb4c.jar");
try {
trace("Checking if the method getAllServiceReferences returns the number of services as expected");
tb4c.start();
tb4c.stop();
}
catch (BundleException ex) {
if (ex.getCause() != null) {
fail(ex.getCause().getMessage());
}
else {
fail(ex.getMessage());
}
}
finally {
tb4c.uninstall();
tb3.stop();
tb3.uninstall();
tb1b.stop();
tb1b.uninstall();
tb1a.stop();
tb1a.uninstall();
tb1.stop();
tb1.uninstall();
}
}
/**
* This test case will check the behavior of method isAssignableTo() when
* the wired package of the getter bundle (the bundle requesting the
* service) is the same of the registrant bundle.
*
* @spec ServiceReference.isAssignableTo(Bundle, String)
* @throws Exception if there is any problem or an assert fails
*/
public void testBundleContextIsAssignableTo001() throws Exception {
Bundle tb1;
Bundle tb1a;
Bundle tb4d;
tb1 = installBundle("classloading.tb1.jar");
tb1.start();
tb1a = installBundle("classloading.tb1a.jar");
tb1a.start();
tb4d = getContext().installBundle(
getWebServer() + "classloading.tb4d.jar");
try {
trace("Checking if the method isAssignableTo returns true when the package source of the getter bundle is the same of the registrant bundle");
tb4d.start();
tb4d.stop();
}
catch (BundleException ex) {
if (ex.getCause() != null) {
fail(ex.getCause().getMessage());
}
else {
fail(ex.getMessage());
}
}
finally {
tb4d.uninstall();
tb1a.stop();
tb1a.uninstall();
tb1.stop();
tb1.uninstall();
}
}
/**
* This test case will check the behavior of method isAssignableTo() when
* the wired package of the getter bundle (the bundle requesting the
* service) is not the same of the registrant bundle.
*
* @spec ServiceReference.isAssignableTo(Bundle, String)
* @throws Exception if there is any problem or an assert fails
*/
public void testBundleContextIsAssignableTo002() throws Exception {
Bundle tb1;
Bundle tb1a;
Bundle tb3;
Bundle tb4e;
tb1 = installBundle("classloading.tb1.jar");
tb1.start();
tb1a = installBundle("classloading.tb1a.jar");
tb1a.start();
tb3 = installBundle("classloading.tb3.jar");
tb3.start();
tb4e = getContext().installBundle(
getWebServer() + "classloading.tb4e.jar");
try {
trace("Checking if the method isAssignableTo returns false when the package source of the getter bundle is not the same of the registrant bundle");
tb4e.start();
tb4e.stop();
}
catch (BundleException ex) {
if (ex.getCause() != null) {
fail(ex.getCause().getMessage());
}
else {
fail(ex.getMessage());
}
}
finally {
tb4e.uninstall();
tb3.stop();
tb3.uninstall();
tb1a.stop();
tb1a.uninstall();
tb1.stop();
tb1.uninstall();
}
}
private static final String SOME_SERVICE_CLASS = "org.osgi.test.cases.framework.classloading.exports.service.SomeService";
private static final String SOME_SERVICE_IMPL = "org.osgi.test.cases.framework.classloading.exports.service.SimpleSomeServiceImpl";
private enum IMPL_BUNDLE {
VERSION1, VERSION2, REGISTRANT, USER
}
/**
* User bundle has no package source; should see other's service
*
* @throws Exception
*/
public void testBundleContextIsAssignableTo003() throws Exception {
BiFunction<Bundle,Object,ServiceReference< ? >> register = (b, s) -> {
return b.getBundleContext()
.registerService(SOME_SERVICE_CLASS, s, null)
.getReference();
};
doTestBundleContextIsAssibnableTo( //
"classloading.tb18c.jar", //
"classloading.tb18d.jar", //
IMPL_BUNDLE.VERSION1, //
true, //
register);
}
/**
* Tests two bundles have private packages; should not see eachother's
* service
*
* @throws Exception
*/
public void testBundleContextIsAssignableTo004() throws Exception {
BiFunction<Bundle,Object,ServiceReference< ? >> register = (b, s) -> {
return b.getBundleContext()
.registerService(SOME_SERVICE_CLASS, s, null)
.getReference();
};
doTestBundleContextIsAssibnableTo( //
"classloading.tb18a.jar", //
"classloading.tb18b.jar", //
IMPL_BUNDLE.REGISTRANT, //
false, //
register);
}
/**
* Test user has package source, registrant does not, a factory from another
* bundle is used; should see the service
*
* @throws Exception
*/
public void testBundleContextIsAssignableTo005() throws Exception {
BiFunction<Bundle,Object,ServiceReference< ? >> register = (b, s) -> {
return b.getBundleContext()
.registerService(SOME_SERVICE_CLASS,
new ServiceFactory<Object>() {
@Override
public Object getService(Bundle bundle,
ServiceRegistration<Object> registration) {
return s;
}
@Override
public void ungetService(Bundle bundle,
ServiceRegistration<Object> registration,
Object service) {
}
}, null)
.getReference();
};
doTestBundleContextIsAssibnableTo( //
"classloading.tb18d.jar", //
"classloading.tb18c.jar", //
IMPL_BUNDLE.VERSION1, //
true, //
register);
}
/**
* Test user has package source, registrant does not, register an object
* that has a different package source; should not see service
*
* @throws Exception
*/
public void testBundleContextIsAssignableTo006() throws Exception {
BiFunction<Bundle,Object,ServiceReference< ? >> register = (b, s) -> {
return b.getBundleContext()
.registerService(SOME_SERVICE_CLASS, s, null)
.getReference();
};
doTestBundleContextIsAssibnableTo( //
"classloading.tb18d.jar", //
"classloading.tb18c.jar", //
IMPL_BUNDLE.VERSION2, //
false, //
register);
}
/**
* Test user has package source, registrant does not, register an object
* that has same package source as user; should see service
*
* @throws Exception
*/
public void testBundleContextIsAssignableTo007() throws Exception {
BiFunction<Bundle,Object,ServiceReference< ? >> register = (b, s) -> {
return b.getBundleContext()
.registerService(SOME_SERVICE_CLASS, s, null)
.getReference();
};
doTestBundleContextIsAssibnableTo( //
"classloading.tb18d.jar", //
"classloading.tb18c.jar", //
IMPL_BUNDLE.VERSION1, //
true, //
register);
}
private void doTestBundleContextIsAssibnableTo(String registrantLoc,
String userLoc, IMPL_BUNDLE implBundle, boolean expected,
BiFunction<Bundle,Object,ServiceReference< ? >> register)
throws Exception {
List<Bundle> bundles = new ArrayList<>();
try {
Bundle exporter1;
Bundle exporter2;
Bundle registrant;
Bundle user;
bundles.add(exporter1 = installBundle("classloading.tb1.jar"));
bundles.add(exporter2 = installBundle("classloading.tb2.jar"));
bundles.add(registrant = installBundle(registrantLoc));
bundles.add(user = installBundle(userLoc));
bundles.forEach(b -> {
try {
b.start();
} catch (BundleException e) {
sneakyThrow(e);
}
});
Class< ? > serviceImpl;
switch (implBundle) {
case VERSION1 :
serviceImpl = exporter1.loadClass(SOME_SERVICE_IMPL);
break;
case VERSION2 :
serviceImpl = exporter2.loadClass(SOME_SERVICE_IMPL);
break;
case REGISTRANT :
serviceImpl = registrant.loadClass(SOME_SERVICE_IMPL);
break;
case USER :
serviceImpl = user.loadClass(SOME_SERVICE_IMPL);
default :
throw new RuntimeException("unknown");
}
ServiceReference< ? > ref = register.apply(registrant,
serviceImpl.getConstructor().newInstance());
assertEquals("Wrong result from isAssignableTo.", expected,
ref.isAssignableTo(user,
"org.osgi.test.cases.framework.classloading.exports.service.SomeService"));
} finally {
bundles.forEach(b -> {
try {
b.uninstall();
} catch (BundleException e) {
sneakyThrow(e);
}
});
}
}
@SuppressWarnings("unchecked")
public static <E extends Throwable> void sneakyThrow(Throwable e) throws E {
throw (E) e;
}
/**
* The service registry must deal with multiple versions of a particular
* interface being published. It should record the module which registered
* each interface and use this information to match against a module later
* attempting to find the interface. A find should be satisfied with the
* version of the service interface imported by the module attempting to
* find the interface.
*
* @spec BundleContext.installBundle(String)
* @throws Exception if there is any problem or an assert fails
*/
public void testServiceRegistryWithMultipleServices001() throws Exception {
Bundle tb1;
Bundle tb1a;
Bundle tb2;
Bundle tb2a;
Bundle tb4f;
Bundle tb4g;
tb1 = installBundle("classloading.tb1.jar");
tb1.start();
tb2 = installBundle("classloading.tb2.jar");
tb2.start();
try {
trace("Checking if the correct service is imported");
tb4f = getContext().installBundle(
getWebServer() + "classloading.tb4f.jar");
tb1a = installBundle("classloading.tb1a.jar");
tb1a.start();
tb4f.start();
tb4f.stop();
tb1a.stop();
tb1a.uninstall();
tb4f.uninstall();
trace("Checking if the correct service is imported");
tb4g = getContext().installBundle(
getWebServer() + "classloading.tb4g.jar");
tb2a = installBundle("classloading.tb2a.jar");
tb2a.start();
tb4g.start();
tb4g.stop();
tb2a.stop();
tb2a.uninstall();
tb4g.uninstall();
}
catch (BundleException ex) {
if (ex.getCause() != null) {
fail(ex.getCause().getMessage());
}
else {
fail(ex.getMessage());
}
}
finally {
tb2.stop();
tb2.uninstall();
tb1.stop();
tb1.uninstall();
}
}
// Service Events ----------------------------
/**
* Service events must only be delivered to event listeners which can
* validly cast the event.
*
* Tests the case which the event cannot be delivered.
*
* @spec ServiceListener.serviceChanged(ServiceEvent)
* @throws Exception if there is any problem or an assert fails
*/
public void testServiceListener001() throws Exception {
Bundle tb1;
Bundle tb1a;
Bundle tb2;
Bundle tb5;
Bundle tb5a;
Method method;
Object service;
ServiceReference< ? > serviceReference;
tb1 = installBundle("classloading.tb1.jar");
tb1.start();
tb1a = installBundle("classloading.tb1a.jar");
tb2 = installBundle("classloading.tb2.jar");
tb2.start();
tb5 = installBundle("classloading.tb5.jar");
tb5.start();
tb5a = getContext().installBundle(
getWebServer() + "classloading.tb5a.jar");
try {
tb5a.start();
tb1a.start();
tb1a.stop();
try {
Sleep.sleep(1000);
}
catch (InterruptedException ex) {
// Ignore this exception
}
// Check if the some event is delivered
serviceReference = getContext()
.getServiceReference(
"org.osgi.test.cases.framework.classloading.exports.listener.ServiceListenerTester");
service = getContext().getService(serviceReference);
method = service.getClass().getMethod("getServiceEventDelivered",
(Class[]) null);
assertNull(
"Checking if an event is delivered for a bundle which imports a non-assignable service interface",
method.invoke(service, (Object[]) null));
getContext().ungetService(serviceReference);
tb5a.stop();
}
finally {
tb5a.uninstall();
tb5.start();
tb5.uninstall();
tb1a.uninstall();
tb2.stop();
tb2.uninstall();
tb1.stop();
tb1.uninstall();
}
}
/**
* Service events must only be delivered to event listeners which can
* validly cast the event.
*
* Tests the case which the event must be delivered.
*
* @spec ServiceListener.serviceChanged(ServiceEvent)
* @throws Exception if there is any problem or an assert fails
*/
public void testServiceListener002() throws Exception {
Bundle tb1;
Bundle tb1a;
Bundle tb5;
Bundle tb5b;
Method method;
Object service;
ServiceReference< ? > serviceReference;
tb1 = installBundle("classloading.tb1.jar");
tb1.start();
tb1a = installBundle("classloading.tb1a.jar");
tb5 = installBundle("classloading.tb5.jar");
tb5.start();
tb5b = getContext().installBundle(
getWebServer() + "classloading.tb5b.jar");
try {
tb5b.start();
tb1a.start();
tb1a.stop();
try {
Sleep.sleep(1000);
}
catch (InterruptedException ex) {
// Ignore this exception
}
// Check if the some event is delivered
serviceReference = getContext()
.getServiceReference(
"org.osgi.test.cases.framework.classloading.exports.listener.ServiceListenerTester");
service = getContext().getService(serviceReference);
method = service.getClass().getMethod("getServiceEventDelivered",
(Class[]) null);
assertNotNull(
"Checking if an event is delivered for a bundle which imports an assignable service interface",
method.invoke(service, (Object[]) null));
getContext().ungetService(serviceReference);
tb5b.stop();
}
finally {
tb5b.uninstall();
tb5.start();
tb5.uninstall();
tb1a.uninstall();
tb1.stop();
tb1.uninstall();
}
}
/**
* Some bundles may wish to listen to all service events regardless of what
* version of a package they are wired to. A new type of ServiceListener is
* added (AllServiceListener) to allow a bundle to listen to all service
* events. When an AllServiceLisetener is used the framework does not do
* class checks on the ServiceReference for the ServiceEvent before
* delivering it to the AllServiceListener.
*
* @spec AllServiceListener.serviceChanged(ServiceEvent)
* @throws Exception if there is any problem or an assert fails
*/
public void testAllServiceListener001() throws Exception {
Bundle tb1;
Bundle tb1a;
Bundle tb2;
Bundle tb2a;
Bundle tb5;
Bundle tb5c;
Method method;
Object service;
ServiceReference< ? > serviceReference;
tb1 = installBundle("classloading.tb1.jar");
tb1.start();
tb1a = installBundle("classloading.tb1a.jar");
tb2 = installBundle("classloading.tb2.jar");
tb2.start();
tb2a = installBundle("classloading.tb2a.jar");
tb5 = installBundle("classloading.tb5.jar");
tb5.start();
tb5c = getContext().installBundle(
getWebServer() + "classloading.tb5c.jar");
try {
tb5c.start();
// This bundle install a service which is assignable to the imported
// interface
tb1a.start();
tb1a.stop();
try {
Sleep.sleep(1000);
}
catch (InterruptedException ex) {
// Ignore this exception
}
// Check if the some event is delivered
serviceReference = getContext()
.getServiceReference(
"org.osgi.test.cases.framework.classloading.exports.listener.AllServiceListenerTester");
service = getContext().getService(serviceReference);
method = service.getClass().getMethod("getServiceEventDelivered",
(Class[]) null);
assertNotNull(
"Checking if an event is delivered for a bundle which imports a assignable service interface",
method.invoke(service, (Object[]) null));
getContext().ungetService(serviceReference);
// This bundle install a service which is not assignable to the
// imported interface
tb2a.start();
tb2a.stop();
try {
Sleep.sleep(1000);
}
catch (InterruptedException ex) {
// Ignore this exception
}
// Check if the some event is delivered
serviceReference = getContext()
.getServiceReference(
"org.osgi.test.cases.framework.classloading.exports.listener.AllServiceListenerTester");
service = getContext().getService(serviceReference);
method = service.getClass().getMethod("getServiceEventDelivered",
(Class[]) null);
assertNotNull(
"Checking if an event is delivered for a bundle which imports a non-assignable service interface",
method.invoke(service, (Object[]) null));
getContext().ungetService(serviceReference);
tb5c.stop();
}
finally {
tb5c.uninstall();
tb5.uninstall();
tb2a.uninstall();
tb2.uninstall();
tb1a.uninstall();
tb1.uninstall();
}
}
/**
* Some bundles may wish to listen to all service events regardless of what
* version of a package they are wired to. A new type of ServiceListener is
* added (AllServiceListener) to allow a bundle to listen to all service
* events. When an AllServiceLisetener is used the framework does not do
* class checks on the ServiceReference for the ServiceEvent before
* delivering it to the AllServiceListener.
*
* This test uses an AllServiceListener which is also an
* UnfilteredServiceListener. The listener is added with a filter that can
* never match (!(objectClass=*)).
*
* @spec AllServiceListener.serviceChanged(ServiceEvent)
* @throws Exception if there is any problem or an assert fails
*/
public void testUnfilteredAllServiceListener001() throws Exception {
Bundle tb1;
Bundle tb1a;
Bundle tb2;
Bundle tb2a;
Bundle tb5;
Bundle tb5d;
Method method;
Object service;
ServiceReference< ? > serviceReference;
tb1 = installBundle("classloading.tb1.jar");
tb1.start();
tb1a = installBundle("classloading.tb1a.jar");
tb2 = installBundle("classloading.tb2.jar");
tb2.start();
tb2a = installBundle("classloading.tb2a.jar");
tb5 = installBundle("classloading.tb5.jar");
tb5.start();
tb5d = installBundle("classloading.tb5d.jar");
try {
tb5d.start();
// This bundle install a service which is assignable to the imported
// interface
tb1a.start();
tb1a.stop();
try {
Sleep.sleep(1000);
}
catch (InterruptedException ex) {
// Ignore this exception
}
// Check if the some event is delivered
serviceReference = getContext()
.getServiceReference(
"org.osgi.test.cases.framework.classloading.exports.listener.AllServiceListenerTester");
service = getContext().getService(serviceReference);
method = service.getClass().getMethod("getServiceEventDelivered",
(Class[]) null);
assertNotNull(
"Checking if an event is delivered for a bundle which imports a assignable service interface",
method.invoke(service, (Object[]) null));
getContext().ungetService(serviceReference);
// This bundle install a service which is not assignable to the
// imported interface
tb2a.start();
tb2a.stop();
try {
Sleep.sleep(1000);
}
catch (InterruptedException ex) {
// Ignore this exception
}
// Check if the some event is delivered
serviceReference = getContext()
.getServiceReference(
"org.osgi.test.cases.framework.classloading.exports.listener.AllServiceListenerTester");
service = getContext().getService(serviceReference);
method = service.getClass().getMethod("getServiceEventDelivered",
(Class[]) null);
assertNotNull(
"Checking if an event is delivered for a bundle which imports a non-assignable service interface",
method.invoke(service, (Object[]) null));
getContext().ungetService(serviceReference);
tb5d.stop();
}
finally {
tb5d.uninstall();
tb5.uninstall();
tb2a.uninstall();
tb2.uninstall();
tb1a.uninstall();
tb1.uninstall();
}
}
// Permission Checking -----------------------
/**
* Since Java SE 9 added support for the Java Platform Module System to Java
* SE, The set of java.* provided by the Java platform is no longer constant
* for a specific version of the Java Platform. It depends on the set of
* modules that are enabled for the runtime instance. The OSGi framework
* specification is updated to support requirements on java.* packages.
* Explicitly, this means, a bundle can now declare imports for java.*
* packages. But, exporting java.* packages is not allowed by a bundle and
* doing so will result in an error.
*
* @spec BundleContext.installBundle(String)
* @throws Exception if there is any problem or an assert fails
*/
public void testJavaPackageExplicityExportImport001() {
Bundle tb7a;
Bundle tb7b;
try {
trace("Installing a bundle which declares imports for java.*");
tb7a = getContext().installBundle(
getWebServer() + "classloading.tb7a.jar");
tb7a.uninstall();
}
catch (BundleException ex) {
fail("Should not have thrown the exception: " + ex.toString());
}
try {
trace("Installing a bundle which declares exports for java.*");
tb7b = getContext().installBundle(
getWebServer() + "classloading.tb7b.jar");
tb7b.uninstall();
fail("A bundle which declares exports for java.* packages should fail to install");
}
catch (BundleException ex) {
assertEquals("It should throw a bundle exception of type manifest error", BundleException.MANIFEST_ERROR, ex.getType());
}
}
/**
* All other packages accessible via the system class path must be hidden
* from executing bundles.
*
* @ Bundle.start()
* @throws Exception if there is any problem or an assert fails
*/
public void testHiddenPackages001() throws Exception {
Bundle tb7c;
String bootPackages;
tb7c = getContext().installBundle(
getWebServer() + "classloading.tb7c.jar");
try {
try {
ClassLoader.getSystemClassLoader().loadClass(
"javax.xml.parsers.SAXParser");
}
catch (ClassNotFoundException ex) {
fail("A class required by this test cannot be loaded");
}
bootPackages = getProperty(Constants.FRAMEWORK_BOOTDELEGATION, "");
if (bootPackages != null) {
StringTokenizer packages = new StringTokenizer(bootPackages, ",");
while(packages.hasMoreTokens()) {
String pkg = packages.nextToken().trim();
if (pkg.equals("*") || pkg.indexOf("org.*") != -1 || pkg.indexOf("org.omg.*") != -1 || pkg.indexOf("org.omg.CORBA") != -1) {
fail("This test does not run with the org.omg.CORBA package available in '" + Constants.FRAMEWORK_BOOTDELEGATION + "'");
}
}
}
trace("Installing a bundle which uses a package accessible using the system class loader");
try {
tb7c.start();
tb7c.stop();
fail("All packages (except java.*) loaded by system class loader must be hidden from executing bundles");
} catch (BundleException be) {
// expected
}
}
finally {
tb7c.uninstall();
}
}
// Class Loading Search Order ----------------
/**
* When an R4 bundle's class loader attempts to load a class or find a
* resource, the search must be performed in a specified order.
*
* If the class or resource is in a java.* package, the request is delegated
* to the parent class loader; otherwise the search continues with the next
* step. If the request is delegated to the parent class loader and the
* class or resource is not found, then the search terminates and the
* request fails.
*
* If the class or resource is a package included in the boot delegation
* list, the request is delegated to the parent classloader.
*
* @spec Bundle.loadClass(String)
* @throws Exception if there is any problem or an assert fails
*/
public void testClassLoadingSearchOrder001() throws Exception {
Bundle tb8a;
Bundle tb8b;
Bundle tb8c;
Bundle tb8d;
Class< ? > clazz;
tb8a = installBundle("classloading.tb8a.jar");
tb8b = installBundle("classloading.tb8b.jar");
tb8c = installBundle("classloading.tb8c.jar");
try {
tb8d = installBundle("classloading.tb8d.jar");