< prev index next >

test/jdk/lib/testlibrary/java/util/jar/CreateMultiReleaseTestJars.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   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  */


  52             + "    public int getVersion() {\n"
  53             + "        return 8;\n"
  54             + "    }\n"
  55             + "}\n";
  56     final private String java9 =
  57             "package version;\n\n"
  58             + "public class Version {\n"
  59             + "    public int getVersion() {\n"
  60             + "        int version = (new PackagePrivate()).getVersion();\n"
  61             + "        if (version == 9) return 9;\n"  // strange I know, but easy to test
  62             + "        return version;\n"
  63             + "    }\n"
  64             + "}\n";
  65     final private String ppjava9 =
  66             "package version;\n\n"
  67             + "class PackagePrivate {\n"
  68             + "    int getVersion() {\n"
  69             + "        return 9;\n"
  70             + "    }\n"
  71             + "}\n";
  72     final private String java10 = java8.replace("8", "10");


  73     final String readme8 = "This is the root readme file";
  74     final String readme9 = "This is the version nine readme file";
  75     final String readme10 = "This is the version ten readme file";
  76     private Map<String,byte[]> rootClasses;
  77     private Map<String,byte[]> version9Classes;
  78     private Map<String,byte[]> version10Classes;
  79 
  80     public void buildUnversionedJar() throws IOException {
  81         JarBuilder jb = new JarBuilder("unversioned.jar");
  82         jb.addEntry("README", readme8.getBytes());
  83         jb.addEntry("version/Main.java", main.getBytes());
  84         jb.addEntry("version/Main.class", rootClasses.get("version.Main"));
  85         jb.addEntry("version/Version.java", java8.getBytes());
  86         jb.addEntry("version/Version.class", rootClasses.get("version.Version"));
  87         jb.build();
  88     }
  89 
  90     public void buildMultiReleaseJar() throws IOException {
  91         JarBuilder jb = customMultiReleaseJar("multi-release.jar", "true");
  92         addEntries(jb);
  93         jb.addEntry("META-INF/versions/9/version/Version.class", version9Classes.get("version.Version"));
  94         jb.build();
  95     }
  96 
  97     public void buildShortMultiReleaseJar() throws IOException {
  98         JarBuilder jb = customMultiReleaseJar("short-multi-release.jar", "true");


 117         JarBuilder jb = new JarBuilder(filename);
 118         extraAttributes.entrySet()
 119                 .forEach(entry -> jb.addAttribute(entry.getKey(), entry.getValue()));
 120         if (addEntries) {
 121             addEntries(jb);
 122         }
 123         jb.addAttribute("Multi-Release", multiReleaseValue);
 124         jb.build();
 125     }
 126 
 127     private void addEntries(JarBuilder jb) {
 128         jb.addEntry("README", readme8.getBytes());
 129         jb.addEntry("version/Main.java", main.getBytes());
 130         jb.addEntry("version/Main.class", rootClasses.get("version.Main"));
 131         jb.addEntry("version/Version.java", java8.getBytes());
 132         jb.addEntry("version/Version.class", rootClasses.get("version.Version"));
 133         jb.addEntry("META-INF/versions/9/README", readme9.getBytes());
 134         jb.addEntry("META-INF/versions/9/version/Version.java", java9.getBytes());
 135         jb.addEntry("META-INF/versions/9/version/PackagePrivate.java", ppjava9.getBytes());
 136         jb.addEntry("META-INF/versions/9/version/PackagePrivate.class", version9Classes.get("version.PackagePrivate"));
 137         jb.addEntry("META-INF/versions/10/README", readme10.getBytes());
 138         jb.addEntry("META-INF/versions/10/version/Version.java", java10.getBytes());
 139         jb.addEntry("META-INF/versions/10/version/Version.class", version10Classes.get("version.Version"));
 140     }
 141 
 142     public void buildSignedMultiReleaseJar() throws Exception {
 143         String testsrc = System.getProperty("test.src",".");
 144         String testdir = findTestDir(testsrc);
 145         String keystore = testdir + "/sun/security/tools/jarsigner/JarSigning.keystore";
 146 
 147         // jarsigner -keystore keystore -storepass "bbbbbb"
 148         //           -signedJar signed-multi-release.jar multi-release.jar b
 149 
 150         char[] password = "bbbbbb".toCharArray();
 151         KeyStore ks = KeyStore.getInstance(new File(keystore), password);
 152         PrivateKey pkb = (PrivateKey)ks.getKey("b", password);
 153         CertPath cp = CertificateFactory.getInstance("X.509")
 154                 .generateCertPath(Arrays.asList(ks.getCertificateChain("b")));
 155         JarSigner js = new JarSigner.Builder(pkb, cp).build();
 156         try (ZipFile in = new ZipFile("multi-release.jar");
 157              FileOutputStream os = new FileOutputStream("signed-multi-release.jar"))
 158         {
 159             js.sign(in, os);


 169         }
 170         if (child == null) {
 171             throw new IllegalArgumentException(dir + " is not in a test directory");
 172         }
 173         if (!Files.isDirectory(child)) {
 174             throw new IOException(child.toString() + " is not a directory");
 175         }
 176         return child.toString();
 177     }
 178 
 179     void compileEntries() {
 180         Map<String,String> input = new HashMap<>();
 181         input.put("version.Main", main);
 182         input.put("version.Version", java8);
 183         rootClasses = (new Compiler(input)).setRelease(8).compile();
 184         input.clear();
 185         input.put("version.Version", java9);
 186         input.put("version.PackagePrivate", ppjava9);
 187         version9Classes = (new Compiler(input)).setRelease(9).compile();
 188         input.clear();
 189         input.put("version.Version", java10);
 190         version10Classes = (new Compiler(input)).setRelease(9).compile();  // fixme in JDK 10
 191     }
 192 }
   1 /*
   2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   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  */


  52             + "    public int getVersion() {\n"
  53             + "        return 8;\n"
  54             + "    }\n"
  55             + "}\n";
  56     final private String java9 =
  57             "package version;\n\n"
  58             + "public class Version {\n"
  59             + "    public int getVersion() {\n"
  60             + "        int version = (new PackagePrivate()).getVersion();\n"
  61             + "        if (version == 9) return 9;\n"  // strange I know, but easy to test
  62             + "        return version;\n"
  63             + "    }\n"
  64             + "}\n";
  65     final private String ppjava9 =
  66             "package version;\n\n"
  67             + "class PackagePrivate {\n"
  68             + "    int getVersion() {\n"
  69             + "        return 9;\n"
  70             + "    }\n"
  71             + "}\n";
  72     final int currentVersion = Runtime.version().major();
  73     final String currentVersionStr = Integer.toString(currentVersion);
  74     final private String javaCurrent = java8.replace("8", currentVersionStr);
  75     final String readme8 = "This is the root readme file";
  76     final String readme9 = "This is the version nine readme file";
  77     final String readmeCurrent = "This is the current version readme file";
  78     private Map<String,byte[]> rootClasses;
  79     private Map<String,byte[]> version9Classes;
  80     private Map<String,byte[]> versionCurrentClasses;
  81 
  82     public void buildUnversionedJar() throws IOException {
  83         JarBuilder jb = new JarBuilder("unversioned.jar");
  84         jb.addEntry("README", readme8.getBytes());
  85         jb.addEntry("version/Main.java", main.getBytes());
  86         jb.addEntry("version/Main.class", rootClasses.get("version.Main"));
  87         jb.addEntry("version/Version.java", java8.getBytes());
  88         jb.addEntry("version/Version.class", rootClasses.get("version.Version"));
  89         jb.build();
  90     }
  91 
  92     public void buildMultiReleaseJar() throws IOException {
  93         JarBuilder jb = customMultiReleaseJar("multi-release.jar", "true");
  94         addEntries(jb);
  95         jb.addEntry("META-INF/versions/9/version/Version.class", version9Classes.get("version.Version"));
  96         jb.build();
  97     }
  98 
  99     public void buildShortMultiReleaseJar() throws IOException {
 100         JarBuilder jb = customMultiReleaseJar("short-multi-release.jar", "true");


 119         JarBuilder jb = new JarBuilder(filename);
 120         extraAttributes.entrySet()
 121                 .forEach(entry -> jb.addAttribute(entry.getKey(), entry.getValue()));
 122         if (addEntries) {
 123             addEntries(jb);
 124         }
 125         jb.addAttribute("Multi-Release", multiReleaseValue);
 126         jb.build();
 127     }
 128 
 129     private void addEntries(JarBuilder jb) {
 130         jb.addEntry("README", readme8.getBytes());
 131         jb.addEntry("version/Main.java", main.getBytes());
 132         jb.addEntry("version/Main.class", rootClasses.get("version.Main"));
 133         jb.addEntry("version/Version.java", java8.getBytes());
 134         jb.addEntry("version/Version.class", rootClasses.get("version.Version"));
 135         jb.addEntry("META-INF/versions/9/README", readme9.getBytes());
 136         jb.addEntry("META-INF/versions/9/version/Version.java", java9.getBytes());
 137         jb.addEntry("META-INF/versions/9/version/PackagePrivate.java", ppjava9.getBytes());
 138         jb.addEntry("META-INF/versions/9/version/PackagePrivate.class", version9Classes.get("version.PackagePrivate"));
 139         jb.addEntry("META-INF/versions/" + currentVersionStr + "/README", readmeCurrent.getBytes());
 140         jb.addEntry("META-INF/versions/" + currentVersionStr + "/version/Version.java", javaCurrent.getBytes());
 141         jb.addEntry("META-INF/versions/" + currentVersionStr + "/version/Version.class", versionCurrentClasses.get("version.Version"));
 142     }
 143 
 144     public void buildSignedMultiReleaseJar() throws Exception {
 145         String testsrc = System.getProperty("test.src",".");
 146         String testdir = findTestDir(testsrc);
 147         String keystore = testdir + "/sun/security/tools/jarsigner/JarSigning.keystore";
 148 
 149         // jarsigner -keystore keystore -storepass "bbbbbb"
 150         //           -signedJar signed-multi-release.jar multi-release.jar b
 151 
 152         char[] password = "bbbbbb".toCharArray();
 153         KeyStore ks = KeyStore.getInstance(new File(keystore), password);
 154         PrivateKey pkb = (PrivateKey)ks.getKey("b", password);
 155         CertPath cp = CertificateFactory.getInstance("X.509")
 156                 .generateCertPath(Arrays.asList(ks.getCertificateChain("b")));
 157         JarSigner js = new JarSigner.Builder(pkb, cp).build();
 158         try (ZipFile in = new ZipFile("multi-release.jar");
 159              FileOutputStream os = new FileOutputStream("signed-multi-release.jar"))
 160         {
 161             js.sign(in, os);


 171         }
 172         if (child == null) {
 173             throw new IllegalArgumentException(dir + " is not in a test directory");
 174         }
 175         if (!Files.isDirectory(child)) {
 176             throw new IOException(child.toString() + " is not a directory");
 177         }
 178         return child.toString();
 179     }
 180 
 181     void compileEntries() {
 182         Map<String,String> input = new HashMap<>();
 183         input.put("version.Main", main);
 184         input.put("version.Version", java8);
 185         rootClasses = (new Compiler(input)).setRelease(8).compile();
 186         input.clear();
 187         input.put("version.Version", java9);
 188         input.put("version.PackagePrivate", ppjava9);
 189         version9Classes = (new Compiler(input)).setRelease(9).compile();
 190         input.clear();
 191         input.put("version.Version", javaCurrent);
 192         versionCurrentClasses = (new Compiler(input)).compile();  // Use default release
 193     }
 194 }
< prev index next >