|
| 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