Skip to content

Commit ca1df7f

Browse files
Jaroslav TulachMarcono1234
Jaroslav Tulach
andauthored
#1981: Optional OSGi bundle's dependency on sun.misc package (#1993)
* #1981: Avoid OSGi bundle's dependency on sun.misc package * Specify optional dependency on sun.misc.Unsafe * Adjusting the test to sun.misc import being optional * Using Collections.list and for loop * Let the fail message include name of package Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com> * Closing the input stream * Dedicated assertSubstring method Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
1 parent c54caf3 commit ca1df7f

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

gson/bnd.bnd

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Bundle-ContactAddress: ${project.parent.url}
66
Bundle-RequiredExecutionEnvironment: JavaSE-1.6, JavaSE-1.7, JavaSE-1.8
77
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.6))"
88

9+
# Optional dependency for JDK's sun.misc.Unsafe
10+
# https://bnd.bndtools.org/chapters/920-faq.html#remove-unwanted-imports-
11+
Import-Package: sun.misc;resolution:=optional, *
12+
913
-removeheaders: Private-Package
1014

1115
-exportcontents:\

gson/src/main/java/module-info.java

+3
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010

1111
// Optional dependency on java.sql
1212
requires static java.sql;
13+
14+
// Optional dependency on jdk.unsupported for JDK's sun.misc.Unsafe
15+
requires static jdk.unsupported;
1316
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2016 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.gson.regression;
17+
18+
import java.io.InputStream;
19+
import java.io.IOException;
20+
import java.net.URL;
21+
import java.util.ArrayList;
22+
import java.util.Collections;
23+
import java.util.List;
24+
import java.util.jar.Manifest;
25+
26+
import junit.framework.TestCase;
27+
28+
public class OSGiTest extends TestCase {
29+
public void testComGoogleGsonAnnotationsPackage() throws Exception {
30+
Manifest mf = findManifest("com.google.gson");
31+
String importPkg = mf.getMainAttributes().getValue("Import-Package");
32+
assertNotNull("Import-Package statement is there", importPkg);
33+
assertSubstring("There should be com.google.gson.annotations dependency", importPkg, "com.google.gson.annotations");
34+
}
35+
36+
public void testSunMiscImportPackage() throws Exception {
37+
Manifest mf = findManifest("com.google.gson");
38+
String importPkg = mf.getMainAttributes().getValue("Import-Package");
39+
assertNotNull("Import-Package statement is there", importPkg);
40+
for (String dep : importPkg.split(",")) {
41+
if (dep.contains("sun.misc")) {
42+
assertSubstring("sun.misc import is optional", dep, "resolution:=optional");
43+
return;
44+
}
45+
}
46+
fail("There should be sun.misc dependency, but was: " + importPkg);
47+
}
48+
49+
private Manifest findManifest(String pkg) throws IOException {
50+
List<URL> urls = new ArrayList<URL>();
51+
for (URL u : Collections.list(getClass().getClassLoader().getResources("META-INF/MANIFEST.MF"))) {
52+
InputStream is = u.openStream();
53+
Manifest mf = new Manifest(is);
54+
is.close();
55+
if (pkg.equals(mf.getMainAttributes().getValue("Bundle-SymbolicName"))) {
56+
return mf;
57+
}
58+
urls.add(u);
59+
}
60+
fail("Cannot find " + pkg + " OSGi bundle manifest among: " + urls);
61+
return null;
62+
}
63+
64+
private static void assertSubstring(String msg, String wholeText, String subString) {
65+
if (wholeText.contains(subString)) {
66+
return;
67+
}
68+
fail(msg + ". Expecting " + subString + " but was: " + wholeText);
69+
}
70+
}

0 commit comments

Comments
 (0)