< prev index next >

test/jdk/java/lang/ClassLoader/GetSystemPackage.java

Print this page

  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8060130

 27  * @library /test/lib
 28  * @build package2.Class2 GetSystemPackage
 29  * @summary Test if getSystemPackage() return consistent values for cases
 30  *          where a manifest is provided or not and ensure only jars on
 31  *          bootclasspath gets resolved via Package.getSystemPackage
 32  * @run main GetSystemPackage
 33  */
 34 
 35 import java.io.File;
 36 import java.io.FileInputStream;
 37 import java.io.FileNotFoundException;
 38 import java.io.FileOutputStream;
 39 import java.io.IOException;
 40 import java.util.jar.Attributes;
 41 import java.util.jar.JarEntry;
 42 import java.util.jar.JarOutputStream;
 43 import java.util.jar.Manifest;


 44 import jdk.test.lib.process.ProcessTools;
 45 
 46 public class GetSystemPackage {
 47 
 48     static final String testClassesDir = System.getProperty("test.classes", ".");
 49     static final File tmpFolder = new File(testClassesDir);
 50     static final String manifestTitle = "Special JAR";
 51 
 52     public static void main(String ... args) throws Exception {
 53         if (args.length == 0) {
 54             buildJarsAndInitiateSystemPackageTest();
 55             return;
 56         }
 57         switch (args[0]) {
 58             case "system-manifest":
 59                 verifyPackage(true, true);
 60                 break;
 61             case "system-no-manifest":
 62                 verifyPackage(false, true);
 63                 break;

101                 "non-system-manifest");
102 
103         runSubProcess("Classpath package with no manifest improperly resolved",
104                 "-cp", testClassesDir + "/no-manifest.jar", "GetSystemPackage",
105                 "non-system-no-manifest");
106 
107     }
108 
109     private static void buildJar(String name, Manifest man) throws Exception {
110         JarBuilder jar = new JarBuilder(tmpFolder, name, man);
111         jar.addClassFile("package2/Class2.class",
112                 testClassesDir + "/package2/Class2.class");
113         jar.addClassFile("GetSystemPackage.class",
114                 testClassesDir + "/GetSystemPackage.class");
115         jar.build();
116     }
117 
118     private static void runSubProcess(String messageOnError, String ... args)
119             throws Exception
120     {
121         ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(args);
122         int res = pb.directory(tmpFolder).inheritIO().start().waitFor();

123         if (res != 0) {
124             throw new RuntimeException(messageOnError);
125         }
126     }
127 
128     private static void verifyPackage(boolean hasManifest,
129                                       boolean isSystemPackage)
130             throws Exception
131     {
132         Class<?> c = Class.forName("package2.Class2");
133         Package pkg = c.getPackage();
134         if (pkg == null || pkg != Package.getPackage("package2") ||
135                 !"package2".equals(pkg.getName())) {
136             fail("package2 not found via Package.getPackage()");
137         }
138 
139         String specificationTitle = pkg.getSpecificationTitle();
140         if (!"package2".equals(pkg.getName())) {
141             fail("Invalid package for Class2");
142         }

  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8060130
 27  * @requires vm.flagless
 28  * @library /test/lib
 29  * @build package2.Class2 GetSystemPackage
 30  * @summary Test if getSystemPackage() return consistent values for cases
 31  *          where a manifest is provided or not and ensure only jars on
 32  *          bootclasspath gets resolved via Package.getSystemPackage
 33  * @run main GetSystemPackage
 34  */
 35 
 36 import java.io.File;
 37 import java.io.FileInputStream;
 38 import java.io.FileNotFoundException;
 39 import java.io.FileOutputStream;
 40 import java.io.IOException;
 41 import java.util.jar.Attributes;
 42 import java.util.jar.JarEntry;
 43 import java.util.jar.JarOutputStream;
 44 import java.util.jar.Manifest;
 45 
 46 import jdk.test.lib.process.OutputAnalyzer;
 47 import jdk.test.lib.process.ProcessTools;
 48 
 49 public class GetSystemPackage {
 50 
 51     static final String testClassesDir = System.getProperty("test.classes", ".");
 52     static final File tmpFolder = new File(testClassesDir);
 53     static final String manifestTitle = "Special JAR";
 54 
 55     public static void main(String ... args) throws Exception {
 56         if (args.length == 0) {
 57             buildJarsAndInitiateSystemPackageTest();
 58             return;
 59         }
 60         switch (args[0]) {
 61             case "system-manifest":
 62                 verifyPackage(true, true);
 63                 break;
 64             case "system-no-manifest":
 65                 verifyPackage(false, true);
 66                 break;

104                 "non-system-manifest");
105 
106         runSubProcess("Classpath package with no manifest improperly resolved",
107                 "-cp", testClassesDir + "/no-manifest.jar", "GetSystemPackage",
108                 "non-system-no-manifest");
109 
110     }
111 
112     private static void buildJar(String name, Manifest man) throws Exception {
113         JarBuilder jar = new JarBuilder(tmpFolder, name, man);
114         jar.addClassFile("package2/Class2.class",
115                 testClassesDir + "/package2/Class2.class");
116         jar.addClassFile("GetSystemPackage.class",
117                 testClassesDir + "/GetSystemPackage.class");
118         jar.build();
119     }
120 
121     private static void runSubProcess(String messageOnError, String ... args)
122             throws Exception
123     {
124         ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(args)
125                                         .directory(tmpFolder);
126         int res = ProcessTools.executeProcess(pb).getExitValue();
127         if (res != 0) {
128             throw new RuntimeException(messageOnError);
129         }
130     }
131 
132     private static void verifyPackage(boolean hasManifest,
133                                       boolean isSystemPackage)
134             throws Exception
135     {
136         Class<?> c = Class.forName("package2.Class2");
137         Package pkg = c.getPackage();
138         if (pkg == null || pkg != Package.getPackage("package2") ||
139                 !"package2".equals(pkg.getName())) {
140             fail("package2 not found via Package.getPackage()");
141         }
142 
143         String specificationTitle = pkg.getSpecificationTitle();
144         if (!"package2".equals(pkg.getName())) {
145             fail("Invalid package for Class2");
146         }
< prev index next >