forked from bndtools/bnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuilderTest.java
3579 lines (3219 loc) · 108 KB
/
BuilderTest.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
package test;
import static aQute.bnd.osgi.Constants.RESOLUTION_DIRECTIVE;
import static aQute.bnd.test.BndTestCase.assertOk;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.zip.ZipOutputStream;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import aQute.bnd.header.Attrs;
import aQute.bnd.header.OSGiHeader;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Builder;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Domain;
import aQute.bnd.osgi.EmbeddedResource;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Packages;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Resource;
import aQute.bnd.osgi.Verifier;
import aQute.bnd.test.jupiter.InjectTemporaryDirectory;
import aQute.bnd.version.Version;
import aQute.lib.collections.SortedList;
import aQute.lib.hex.Hex;
import aQute.lib.io.FileTree;
import aQute.lib.io.IO;
import aQute.lib.strings.Strings;
import aQute.lib.zip.ZipUtil;
import aQute.service.reporter.Report.Location;
@SuppressWarnings("resource")
public class BuilderTest {
/**
* -includepackage: *;from:=classes will generate an error if there are no
* classes.
*/
@Test
public void testIncludePackageZeroMatchesForWildcard() throws Exception {
try (Builder b = new Builder()) {
b.setProperty("-includepackage", "*;foo:=notexistent");
Jar build = b.build();
assertTrue(b.check("The JAR is empty: "));
}
}
/**
* There shouldn't be "Duplicate name..." warnings with pedantic flag set to
* true #2803 https://github.com/bndtools/bnd/issues/2803
*
* @throws Exception
*/
@Test
public void testNoDuplicateWarningForHeadersThatAllowDuplicates() throws Exception {
try (Builder b = new Builder()) {
b.setPedantic(true);
b.addClasspath(IO.getFile("bin_test"));
b.setProperty("Export-Package", "a;version=1,a;version=2");
b.setProperty("Require-Capability", "ns;filter:='(foo=1)',ns;filter:='(foo=2)',ns;filter:='(foo=1)'");
b.setProperty("Provide-Capability", "ns;foo=1,ns;foo=2");
Jar build = b.build();
b.check();
}
}
/**
* Duplicate Export-Package clause exports second and later package with
* bundle version #2864 https://github.com/bndtools/bnd/issues/2864
*/
@Test
public void testDuplicateExportPackageClauseExportsSecondAndLaterPackageWithBundleVersion() throws Exception {
try (Builder b = new Builder()) {
b.addClasspath(IO.getFile("bin_test"));
b.setBundleVersion("20");
b.setProperty("Export-Package", "a;version=1,a");
Jar build = b.build();
assertTrue(b.check("Export-Package duplicate package name \\(a\\) that uses the default version"));
}
}
/**
* Test the detection of usage of old components: Invalid Service-Component
* header
*/
@Test
public void testDetectWrongServiceComponentHeaderWithoutAnnotatedComponents() throws Exception {
try (Builder b = new Builder()) {
b.addClasspath(IO.getFile("bin_test"));
b.setProperty("Service-Component", "*");
b.setProperty("-includeresource", "foo;literal=x");
Jar build = b.build();
assertTrue(b.check("Service-Component is normally generated by bnd."));
}
}
/**
* Test the detection of usage of old components
*
* @throws Exception
*/
@Test
public void testDetectUseOfBndAnnotations() throws Exception {
try (Builder b = new Builder()) {
b.addClasspath(IO.getFile("bin_test"));
b.setProperty("Private-Package", "test.deprecatedbndann.component");
b.setProperty("Service-Component", "*");
Jar build = b.build();
assertTrue(b.check("Service-Component refers to test.deprecatedbndann.component.AnnotatedComponent1 ",
"Service-Component refers to test.deprecatedbndann.component.AnnotatedComponent2 ",
"Service-Component is normally generated by bnd."));
}
}
/**
* Test if the compression flag is set properly
*/
@Test
public void testCompressionSet(@InjectTemporaryDirectory
File tmp) throws Exception {
try (Builder b = new Builder()) {
b.setIncludeResource("foo;literal='x'");
Jar build = b.build();
assertThat(build.hasCompression()).isEqualTo(Jar.Compression.DEFLATE);
File out = new File(tmp, "default.jar");
build.write(out);
try (JarFile jarFile = new JarFile(out)) {
JarEntry entry = jarFile.getJarEntry("foo");
assertThat(entry.getMethod()).isEqualTo(ZipOutputStream.DEFLATED);
assertThat(entry.getCrc()).isEqualTo(2363233923L);
}
}
try (Builder b = new Builder()) {
b.setIncludeResource("foo;literal='x'");
b.setProperty(Constants.COMPRESSION, "STORE");
Jar build = b.build();
assertThat(build.hasCompression()).isEqualTo(Jar.Compression.STORE);
File out = new File(tmp, "store.jar");
build.write(out);
try (JarFile jarFile = new JarFile(out)) {
JarEntry entry = jarFile.getJarEntry("foo");
assertThat(entry.getMethod()).isEqualTo(ZipOutputStream.STORED);
assertThat(entry.getCrc()).isEqualTo(2363233923L);
assertThat(entry.getCompressedSize()).isEqualTo(entry.getSize());
}
}
try (Builder b = new Builder()) {
b.setIncludeResource("foo;literal='x'");
b.setProperty(Constants.COMPRESSION, "DEFLATE");
Jar build = b.build();
assertThat(build.hasCompression()).isEqualTo(Jar.Compression.DEFLATE);
File out = new File(tmp, "deflate.jar");
build.write(out);
try (JarFile jarFile = new JarFile(out)) {
JarEntry entry = jarFile.getJarEntry("foo");
assertThat(entry.getMethod()).isEqualTo(ZipOutputStream.DEFLATED);
assertThat(entry.getCrc()).isEqualTo(2363233923L);
}
}
}
/**
* In the bnd file for bndlib, we include DS annotations 1.3 and osgi.cmpn 5
* (which includes DS annotations 1.2) on the -buildpath. We use a
* -split-package directive to select the first package (DS annotations 1.3)
* for the bundle. However during -sources: true processing, bnd ignores the
* -split-package directive and includes the sources from all the -buildpath
* entries. The means that the DS annotations 1.2 source, coming later in
* the -buildpath, overlays the DS annotations 1.3 source. The result is a
* mish-mash of DS annotations 1.2 and 1.3 source in OSGI-OPT/src.
*/
@Test
public void testSplitSourcesFirst() throws Exception {
Builder bmaker = new Builder();
try {
bmaker.addClasspath(new File("bin_test"));
bmaker.addClasspath(new File("jar/osgi.jar"));
bmaker.addClasspath(new File("jar/osgi-3.0.0.jar"));
bmaker.setSourcepath(new File[] {
new File("test")
});
bmaker.setProperty("-sources", "true");
bmaker.setProperty("Export-Package", "org.osgi.framework;-split-package:=first, a");
Jar jar = bmaker.build();
assertTrue(bmaker.check());
assertNotNull(jar.getResource("OSGI-OPT/src/a/A.java"));
assertNotNull(jar.getResource("OSGI-OPT/src/a/B.java"));
InputStream in = jar.getResource("OSGI-OPT/src/org/osgi/framework/Bundle.java")
.openInputStream();
assertNotNull(in);
byte[] fw = IO.read(in);
assertEquals(39173, fw.length);
} finally {
bmaker.close();
}
}
@Test
public void testSplitSourcesMergeLast() throws Exception {
Builder bmaker = new Builder();
try {
bmaker.addClasspath(new File("bin_test"));
bmaker.addClasspath(new File("jar/osgi.jar"));
bmaker.addClasspath(new File("jar/osgi-3.0.0.jar"));
bmaker.setSourcepath(new File[] {
new File("test")
});
bmaker.setProperty("-sources", "true");
bmaker.setProperty("Export-Package", "org.osgi.framework;-split-package:=merge-last, a");
Jar jar = bmaker.build();
assertNotNull(jar.getResource("OSGI-OPT/src/a/A.java"));
assertNotNull(jar.getResource("OSGI-OPT/src/a/B.java"));
assertTrue(bmaker.check("Version for package org.osgi.framework is set to different values"));
InputStream in = jar.getResource("OSGI-OPT/src/org/osgi/framework/Bundle.java")
.openInputStream();
assertNotNull(in);
byte[] fw = IO.read(in);
assertEquals(25783, fw.length);
} finally {
bmaker.close();
}
}
/**
* #1017 Wrong import version range being generated On one project I depend
* on bundle A and bundle B. Both only export packages in version 1.0. No
* package is exported by both of them. But B also embeds one package from A
* in version 2.0 (via
* http://njbartlett.name/2014/05/26/static-linking.html) but that one
* package is not exported from B. Now if the classpath first contains B
* then A the import-package statement being generated for that package
* (being embedded in B in version 2.0 and exported from A in 1.0) has a
* version range starting with 2.0 (which is wrong). Only if the classpath
* contains A first and then B, it takes the right import version range for
* that package (namely starting with 1.0) I use the maven-bundle-plugin
* 2.5.3 with bndlib 2.4.0.
*/
@Test
public void test1017UsingPrivatePackagesVersion() throws Exception {
Builder A = new Builder();
A.addClasspath(new File("jar/osgi.jar"));
A.setExportPackage("org.osgi.service.event");
A.build();
assertTrue(A.check());
Builder B = new Builder();
B.addClasspath(new File("jar/osgi.jar"));
B.setExportPackage("org.osgi.service.wireadmin");
B.setPrivatePackage("org.osgi.service.event");
B.setIncludeResource("org/osgi/service/event/packageinfo;literal='version 2.0.0'");
B.build();
assertTrue(B.check());
Builder B_A = new Builder();
B_A.addClasspath(B.getJar());
B_A.addClasspath(A.getJar());
B_A.addClasspath(new File("bin_test"));
B_A.setPrivatePackage("test.reference_to_eventadmin");
B_A.build();
assertTrue(B_A.check());
assertEquals("[1.0,2)", B_A.getImports()
.getByFQN("org.osgi.service.event")
.getVersion());
Builder A_B = new Builder();
A_B.addClasspath(A.getJar());
A_B.addClasspath(B.getJar());
A_B.addClasspath(new File("bin_test"));
A_B.setPrivatePackage("test.reference_to_eventadmin");
A_B.build();
assertTrue(A_B.check());
assertEquals("[1.0,2)", A_B.getImports()
.getByFQN("org.osgi.service.event")
.getVersion());
}
/**
* #971 bnd does not import exported package used by an imported/exported
* package When building org.osgi.impl.service.async bundle in OSGi build,
* the bundle exports the org.osgi.util.promise and org.osgi.util.function
* packages. org.osgi.util.promise uses org.osgi.util.function. bnd
* correctly exports both packages but only imports org.osgi.util.promise.
*
* <pre>
* Export-Package:
* org.osgi.service.async;version="1.0";uses:="org.osgi.framework,org.osgi.
* util.promise",org.osgi.service.async.delegate;version="1.0";uses:="org.
* osgi.util.promise",org.osgi.util.promise;version="1.0";uses:="org.osgi.
* util.function",org.osgi.util.function;version="1.0" Import-Package:
* org.osgi.framework;version="[1.6,2)",org.osgi.framework.wiring;version="[
* 1.0,2)",org.osgi.service.async;version="[1.0,1.1)",org.osgi.service.async
* .delegate;version="[1.0,2)",org.osgi.service.log;version="[1.3,2)",org.
* osgi.util.promise;version="[1.0,1.1)",org.osgi.util.tracker;version="[1.5
* ,2)" Tool: Bnd-3.0.0.201506011706
* </pre>
*
* So effectively the offer to import org.osgi.util.promise is broken. If
* the framework wanted to resolve the bundle by importing
* org.osgi.util.promise, that package has a uses constraint on
* org.osgi.util.function and since the bundle only exports
* org.osgi.util.function, the framework can only resolve the bundle to
* another exporter of org.osgi.util.promise if that exporter imports
* org.osgi.util.function from this bundle. Obviously that wont work for
* additional bundle attempting the same thing. bnd fails to also import
* org.osgi.util.function. This issue exists in bnd 2.4.1 and master. We
* have 4 packages
*
* <pre>
* p1 -> p2 exported (makes p2 importable) p2 -> none exported (force to
* import by p1) p3 -> p1 private (makes p1 importable) p4 -> p3 exported
* (p4 cannot be imported due to private ref)
*
* <pre>
*/
@Test
public void testNoImportForUsedExport_971() throws Exception {
Builder b = new Builder();
b.addClasspath(new File("bin_test"));
b.setExportPackage("test.missingimports_971.p1,test.missingimports_971.p2,test.missingimports_971.p4");
b.setPrivatePackage("test.missingimports_971.p3");
b.build();
assertTrue(b.check());
assertTrue(b.getExports()
.containsFQN("test.missingimports_971.p1"));
assertTrue(b.getExports()
.containsFQN("test.missingimports_971.p2"));
assertTrue(b.getExports()
.containsFQN("test.missingimports_971.p4"));
assertTrue(b.getImports()
.containsFQN("test.missingimports_971.p1"));
assertTrue(b.getImports()
.containsFQN("test.missingimports_971.p2"));
b.getJar()
.getManifest()
.write(System.out);
}
/*
* Private package header doesn't allow the use of negation (!) #840
*/
@Test
public void testNegationInPrivatePackage_840() throws Exception {
Builder b = new Builder();
b.setProperty(Constants.STRICT, "true");
b.addClasspath(IO.getFile("jar/osgi.jar"));
b.setPrivatePackage("!org.osgi.service.event,org.osgi.service.*");
b.build();
assertTrue(b.check(
"Import Package clauses without version range: \\[javax.servlet, javax.microedition.io, javax.servlet.http\\]"));
}
/*
* Warn about missing packages in export
*/
@Test
public void testWarnAboutMissingExports() throws Exception {
Builder b = new Builder();
b.setProperty(Constants.STRICT, "true");
b.addClasspath(IO.getFile("jar/osgi.jar"));
b.setIncludeResource("foo;literal='bla'"); // get rid of warningt
b.setExportPackage("bar");
b.build();
assertTrue(b.check("\\QExport-Package or -exportcontents refers to missing package 'bar'\\E"));
}
/*
* Warn about imports to private imports
*/
@Test
public void testWarnAboutPrivateImportsFromExport() throws Exception {
Builder p3 = setupP3();
p3.setExportPackage("test.activator;version=2");
p3.setPrivatePackage("test.activator.inherits");
p3.build();
assertTrue(p3.check("Import package org\\.osgi\\..* not found in any bundle "));
p3.getJar()
.getManifest()
.write(System.out);
}
/*
* Warn about imports to private imports
*/
@Test
public void testWarnAboutPrivateImports() throws Exception {
Builder p3 = setupP3();
p3.addClasspath(new File("jar/osgi.jar"));
p3.setExportPackage("test.activator.inherits;version=0.0.0");
p3.build();
assertTrue(p3.check("Import package test.activator not found in any bundle on the -buildpath."));
p3.getJar()
.getManifest()
.write(System.out);
}
private Builder setupP3() throws Exception {
Builder p1 = new Builder();
p1.setProperty("Bundle-SymbolicName", "p1");
p1.setProperty("Bundle-Version", "1.2.3");
p1.setPrivatePackage("test.activator");
p1.addClasspath(new File("bin_test"));
p1.addClasspath(new File("jar/osgi.jar"));
p1.build();
assertTrue(p1.check());
Builder p2 = new Builder();
p2.setProperty("Bundle-SymbolicName", "p2");
p2.setExportPackage("test.activator.inherits");
p2.addClasspath(new File("bin_test"));
p2.build();
assertTrue(p2.check());
Builder p3 = new Builder();
p3.setProperty("Bundle-SymbolicName", "p3");
p3.setProperty("-check", "ALL");
p3.addClasspath(p1.getJar());
p3.addClasspath(p2.getJar());
return p3;
}
/**
* #708 if a bundle has a.b.c but imports a.b then bnd cannot find the
* version of a.b because the scanning of a.b.c already has set the
* information for a.b to "nothing". The learnPackage() method must be
* adapted so that "empty" package do not occupy a position This was
* diagnosed by Balázs Zsoldos balazs.zsoldos@everit.biz
*
* @throws Exception
*/
@Test
public void testOverlappingPackageMissesImportVersions() throws Exception {
Builder exporter = new Builder();
exporter.setExportPackage("test._708.a.b");
exporter.addClasspath(new File("bin_test"));
exporter.build();
assertTrue(exporter.check());
//
// We need to build a temp entry because if we include 'bin'
// on the final build then we see the a.b package there, and
// there it has information
//
Builder temp = new Builder();
temp.setPrivatePackage("test._708.a.b.c");
temp.addClasspath(new File("bin_test"));
temp.build();
assertTrue(temp.check());
Builder importer = new Builder();
importer.setPrivatePackage("test._708.a.b.c");
importer.addClasspath(temp.getJar());
importer.addClasspath(exporter.getJar());
importer.build();
assertTrue(importer.check());
assertEquals("test._708.a.b;version=\"1.2.3\"", exporter.getJar()
.getManifest()
.getMainAttributes()
.getValue("Export-Package"));
assertEquals("test._708.a.b;version=\"[1.2,2)\"", importer.getJar()
.getManifest()
.getMainAttributes()
.getValue("Import-Package"));
}
/**
* Test if the Manifest gets the last modified date
*/
@Test
public void testLastModifiedForManifest(@InjectTemporaryDirectory
File tmp) throws Exception {
long time = System.currentTimeMillis();
try (Builder b = new Builder()) {
b.addClasspath(IO.getFile("jar/osgi.jar"));
b.setExportPackage("org.osgi.framework");
Jar build = b.build();
assertTrue(b.check());
File file = new File(tmp, "tmp.jar");
build.write(file);
try (Jar ajr = new Jar(file)) {
Resource r = ajr.getResource("META-INF/MANIFEST.MF");
assertNotNull(r);
long t = r.lastModified();
Date date = new Date(t);
System.out.println(date + " " + t);
// TODO we need to adapt the timestamp handling
assertThat(t).as("%s %s", date, t)
.isEqualTo(1142555622000L);
}
}
}
/**
* A Require-Bundle should remove imports that are exported by its target(s)
*
* @throws Exception
*/
@Test
public void testRemovedImportWithRequireBundle() throws Exception {
try (Builder b = new Builder()) {
b.addClasspath(new File("bin_test"));
b.addClasspath(new File("jar/osgi.core.jar"));
b.setPedantic(true);
// causes an import to org.osgi.framework and javax.swing
b.setExportPackage("test.classreference;version=1,test.classreferencetoosgijar;version=1");
// the require bundle will then remove the osgi ref
b.setProperty("Require-Bundle", "osgi.core");
b.build();
assertTrue(b.check("Imports that lack version ranges: \\[javax.swing\\]"));
assertThat(b.getImports()
.keySet()).containsExactlyInAnyOrder(b.getPackageRef("javax.swing"));
try (Verifier v = new Verifier(b.getJar())) {
v.verify();
assertTrue(v.check("Host .* for this fragment"));
}
}
}
/**
* #479 I have now tested this locally. Apparently the fix doesn't change
* the reported behavior. In my test bundle, I have removed all imports. Bnd
* builds this with no errors reported. I add: DynamicImport-Package: dummy
*/
@Test
public void testMissingImportsWithDynamicImport() throws Exception {
try (Builder b = new Builder()) {
b.addClasspath(new File("bin_test"));
b.setPedantic(true);
b.setExportPackage("test.classreference;version=1");
b.setImportPackage("!*");
b.setProperty(Constants.DYNAMICIMPORT_PACKAGE, "dummy");
b.build();
assertTrue(b.check());
try (Verifier v = new Verifier(b.getJar())) {
v.verify();
assertTrue(v.check("Unresolved references to \\[javax.swing\\] by class\\(es\\)"));
}
}
}
/**
* #479 I have now tested this locally. Apparently the fix doesn't change
* the reported behavior. In my test bundle, I have removed all imports. Bnd
* builds this with no errors reported. I add: DynamicImport-Package: dummy
*/
@Test
public void testMissingImportsWithoutDynamicImport() throws Exception {
try (Builder b = new Builder()) {
b.addClasspath(new File("bin_test"));
b.setPedantic(true);
b.setExportPackage("test.classreference;version=1");
b.setImportPackage("!*");
b.build();
assertTrue(b.check());
try (Verifier v = new Verifier(b.getJar())) {
v.verify();
assertTrue(v.check("Unresolved references to \\[javax.swing\\] by class\\(es\\)"));
}
}
}
/**
* <pre>
* [2013-12-11 15:55:14] BJ Hargrave: init: [echo] Enter project
* org.osgi.test.cases.prefs (${top}) [bndprepare] 2 WARNINGS [bndprepare]
* No translation found for macro:
* classes;extending;junit.framework.TestCase;concrete [bndprepare] No
* translation found for macro: classes,concrete [2013-12-11 15:55:31] BJ
* Hargrave: I am getting this on the latest bnd.master in the OSGi test
* projects
* </pre>
*
* @throws Exception
*/
@Test
public void testClassQuery() throws Exception {
try (Builder a = new Builder()) {
a.addClasspath(new File("bin_test"));
a.setExportPackage("test.component");
a.setProperty("testcases",
"${sort;${uniq;${classes;EXTENDS;junit.framework.TestCase;CONCRETE};${classes;HIERARCHY_ANNOTATED;org.junit.Test;CONCRETE};${classes;HIERARCHY_INDIRECTLY_ANNOTATED;org.junit.platform.commons.annotation.Testable;CONCRETE}}}");
a.setProperty("Test-Cases", "${testcases}");
a.setProperty("-dsannotations", "!*");
a.setProperty("-metatypeannotations", "!*");
Jar jar = a.build();
assertTrue(a.check());
Manifest m = jar.getManifest();
Parameters p = new Parameters(m.getMainAttributes()
.getValue("Test-Cases"));
assertThat(p).hasSizeGreaterThanOrEqualTo(4);
}
}
/**
* Bundle ActivationPolicy
*
* @throws Exception
*/
@Test
public void testBundleActivationPolicy() throws Exception {
Builder b = new Builder();
try {
b.addClasspath(IO.getFile("bin_test"));
b.setProperty("Bundle-ActivationPolicy", "lazy");
b.setProperty("Export-Package", "test.activator");
b.build();
assertTrue(b.check());
} finally {
b.close();
}
}
/**
* #388 Manifest header to get GIT head
*
* @throws IOException
*/
@Test
public void testGitHead() throws IOException {
Builder b = new Builder();
try {
String s = b.getReplacer()
.process("${githead}");
assertTrue(Hex.isHex(s));
} finally {
b.close();
}
}
/**
* If a package-info.java + packageinfo are present then normally
* package-info takes precedence if it sets a Version. This test sees that
* if no version is sets, packageinfo is used.
*/
@Test
public void testPackageInfo_no_version() throws Exception {
Builder b = new Builder();
try {
b.addClasspath(new File("bin_test"));
b.setExportPackage("test.packageinfo.both_no_version");
Jar build = b.build();
assertTrue(b.check());
Attrs imports = b.getExports()
.getByFQN("test.packageinfo.both_no_version");
assertEquals("1.2.3", imports.getVersion());
} finally {
b.close();
}
}
/**
* An old osgi 3.0.0 jar had an old packageinfo in it. This included some
* never well developed syntax which now clashes with the proprty syntax.
*
* @throws Exception
*/
@Test
public void testVeryOldPackageInfo() throws Exception {
Builder b = new Builder();
try {
b.addClasspath(IO.getFile("jar/osgi-3.0.0.jar"));
b.setExportPackage("org.osgi.util.measurement;version=100, org.osgi.util.tracker;version=100, *");
Jar build = b.build();
assertTrue(
b.check("Version for package org.osgi.util.measurement is set to different values in the source ",
"Version for package org.osgi.util.tracker is set to different values in the source"));
} finally {
b.close();
}
}
/**
* Using a package info without the version keyword gives strange results in
* the manifest, should generate an error.
*/
@Test
public void testBadPackageInfo() throws Exception {
Builder b = new Builder();
try {
b.addClasspath(new File("bin_test"));
b.setExportPackage("test.package_info_versioniskey");
b.build();
String message = b.getErrors()
.get(0);
assertThat(message).as("The lacking version error first")
.contains("package info for test.package_info_versioniskey attribute [1.0.0=''],");
Location location = b.getLocation(message);
assertNotNull(location, "Supposed to have a location");
assertNotNull(location.file, "And that must have a file");
assertEquals("packageinfo", new File(location.file).getName(), "Which should be the packaginfo file");
assertEquals(4, location.line);
assertEquals(5, location.length);
assertTrue(b.check("package info for test.package_info_versioniskey attribute \\[1.0.0=''\\],"));
} finally {
b.close();
}
}
/**
* https://github.com/bndtools/bnd/issues/359 Starts with a bundle that has
* one package 'a' containing classes A and B. First removes B from 'a', and
* checks that the last modified date of the resulting bundle changed. Then
* removes A from 'a', and checks again that the last modified data changed.
*/
@Test
public void testRemoveClassFromPackage(@InjectTemporaryDirectory
File tmp) throws Exception {
try (Builder b = new Builder()) {
IO.mkdirs(IO.getFile(tmp, "a"));
IO.copy(IO.getFile("bin_test/a/A.class"), IO.getFile(tmp, "a/A.class"));
IO.copy(IO.getFile("bin_test/a/B.class"), IO.getFile(tmp, "a/B.class"));
Jar classpath = new Jar(tmp);
b.addClasspath(classpath);
b.setPrivatePackage("a");
Jar result = b.build();
Resource ra = result.getResource("a/A.class");
assertThat(ra).isNotNull();
Resource rb = result.getResource("a/B.class");
assertThat(rb).isNotNull();
long lm1 = result.lastModified();
assertThat(lm1).as("Last modified date of bundle > 0")
.isGreaterThan(0L);
// windows has a very low resolution sometimes
Thread.sleep(IO.isWindows() ? 1000 : 100);
IO.delete(IO.getFile(tmp, "a/B.class"));
classpath.remove("a/B.class");
classpath.updateModified(System.currentTimeMillis(), "Removed file B");
result = b.build();
long lm2 = result.lastModified();
assertThat(lm2).as("Last modified date of bundle has increased after deleting class from package")
.isGreaterThan(lm1);
// windows has a very low resolution sometimes
Thread.sleep(IO.isWindows() ? 1000 : 100);
IO.delete(IO.getFile(tmp, "a/A.class"));
classpath.remove("a/A.class");
classpath.updateModified(System.currentTimeMillis(), "Removed file A");
// windows has a very low resolution sometimes
Thread.sleep(IO.isWindows() ? 1000 : 100);
result = b.build();
long lm3 = result.lastModified();
assertThat(lm3).as("Last modified date of bundle has increased after deleting last class from package")
.isGreaterThan(lm2);
}
}
/**
* https://github.com/bndtools/bnd/issues/315 Turns out bnd doesn't seem to
* support a class in a capitalized package name. I accidentally called a
* package with a capital letter and I get the strange error message and a
* refusal to build it. (See title for error message) My package could be
* named "Coffee" and the package named "CoffeeClient", The bnd.bnd file
* could have: Private-Package: Coffee I'm running 2.0.0REL with Eclipse
* Juno.
*/
@Test
public void testUpperCasePackage() throws Exception {
Builder b = new Builder();
try {
b.addClasspath(IO.getFile("bin_test"));
b.setExportPackage("UPPERCASEPACKAGE");
b.build();
assertTrue(b.check());
b.getExports()
.containsFQN("UPPERCASEPACKAGE");
} finally {
b.close();
}
}
public @interface TestAnnotation {}
@TestAnnotation
public static class Target {}
/**
* Dave Smith <dave.smith@candata.com> I have pulled the latest from git and
* am testing out 2.0 with our current application. I am getting the
* following error message on the bnd.bnd file null, for cmd : classes,
* arguments [classes;CONCRETE;ANNOTATION;javax.persistence.Entity] My bnd
* file does have the following line ... Hibernate-Db =
* ${classes;CONCRETE;ANNOTATION;javax.persistence.Entity}
*
* @throws Exception
*/
@Test
public void testClasses() throws Exception {
Builder b = new Builder();
try {
b.setProperty("x", "${classes;CONCRETE;ANNOTATION;test.BuilderTest$TestAnnotation}");
b.setProperty("y", "${classes;CONCRETE;ANNOTATED;test.BuilderTest$TestAnnotation}");
b.setProperty("z", "${classes;CONCRETE;ANNOTATEDX;x.y.Z}");
b.setPrivatePackage("test");
b.addClasspath(IO.getFile("bin_test"));
b.build();
String s = b.getProperty("x");
assertEquals(s, b.getProperty("y"));
assertTrue(s.contains("test.BuilderTest$Target"));
assertEquals("${classes;CONCRETE;ANNOTATEDX;x.y.Z}", b.getProperty("z"));
assertTrue(b.check("ANNOTATEDX"));
} finally {
b.close();
}
}
/**
* Check if we can create digests
*
* @throws Exception
*/
@Test
public void testDigests(@InjectTemporaryDirectory
File tmp) throws Exception {
Builder b = new Builder();
try {
b.addClasspath(IO.getFile("jar/osgi.jar"));
b.setExportPackage("org.osgi.framework");
b.setProperty(Constants.DIGESTS, "MD5, SHA1");
Jar jar = b.build();
assertTrue(b.check());
File f = File.createTempFile("test", ".jar", tmp);
jar.write(f);
Jar other = new Jar(f);
Manifest manifest = other.getManifest();
assertNotNull(manifest);
Attributes attrs = manifest.getAttributes("org/osgi/framework/BundleActivator.class");
assertNotNull(attrs);
assertEquals("RTRhr3kadnulINegRhpmog==", attrs.getValue("MD5-Digest"));
assertEquals("BfVfpnE3Srx/0UWwtzNecrAGf8A=", attrs.getValue("SHA1-Digest"));
other.close();
} finally {
b.close();
}
}
/**
* FELIX-3407 Utterly confusing example that states that generic references
* are not picked up. The classes under test are in
* {@link test.genericinterf.a.A}, {@link test.genericinterf.b.B}, and
* {@link test.genericinterf.c.C}.
*/
@Test
public void testGenericPickup() throws Exception {
Builder b = new Builder();
try {
b.setPrivatePackage("test.genericinterf.a");
b.addClasspath(new File("bin_test"));
b.build();
assertTrue(b.check());
System.out.println(b.getImports());
assertTrue(b.getImports()
.containsFQN("test.genericinterf.b"));
assertTrue(b.getImports()
.containsFQN("test.genericinterf.c"));
} finally {
b.close();
}
}
/**
* Github #130 Consider the following descriptor file: Bundle-Activator:
* org.example.Activator Private-Package: org.example Now suppose that at
* build time, bnd cannot find the package org.example, or it is empty. Bnd
* sees the Bundle-Activator instruction as creating a dependency, so it
* generates a manifest containing an import for that package:
* Import-Package: org.example This is unexpected. If a Private-Package
* instruction is given with a specific package name (i.e. not a wildcard),
* and that package does not exist or is empty, then bnd should fail or
* print an error.
*
* @throws Exception
*/
@Test
public void testPrivatePackageNonExistent() throws Exception {
Builder b = new Builder();
try {
b.addClasspath(IO.getFile("jar/osgi.jar"));
b.setBundleActivator("com.example.Activator");
b.setPrivatePackage("com.example");
b.setIncludeResource("p;literal='x'");
b.build();
assertTrue(b.check("on the class path: \\[com.example\\]",
"Bundle-Activator com.example.Activator is being imported"));
} finally {
b.close();
}
}
/**
* #41 Test the EE macro
*/
@Test
public void testEEMacro() throws Exception {
Builder b = new Builder();
try {
b.addClasspath(IO.getFile("jar/ifc112.jar"));
b.setPrivatePackage("netscape.util.*");