1 /*
   2  * Copyright (c) 2015, 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  */
  23 
  24 import java.io.IOException;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.util.HashMap;
  29 import java.util.Map;
  30 
  31 public class CreateMultiReleaseTestJars {
  32     final private String main =
  33             "package version;\n\n"
  34             + "public class Main {\n"
  35             + "    public static void main(String[] args) {\n"
  36             + "        Version v = new Version();\n"
  37             + "        System.out.println(\"I am running on version \" + v.getVersion());\n"
  38             + "    }\n"
  39             + "}\n";
  40     final private String java8 =
  41             "package version;\n\n"
  42             + "public class Version {\n"
  43             + "    public int getVersion() {\n"
  44             + "        return 8;\n"
  45             + "    }\n"
  46             + "}\n";
  47     final private String java9 =
  48             "package version;\n\n"
  49             + "public class Version {\n"
  50             + "    public int getVersion() {\n"
  51             + "        int version = (new PackagePrivate()).getVersion();\n"
  52             + "        if (version == 9) return 9;\n"  // strange I know, but easy to test
  53             + "        return version;\n"
  54             + "    }\n"
  55             + "}\n";
  56     final private String ppjava9 =
  57             "package version;\n\n"
  58             + "class PackagePrivate {\n"
  59             + "    int getVersion() {\n"
  60             + "        return 9;\n"
  61             + "    }\n"
  62             + "}\n";
  63     final private String java10 = java8.replace("8", "10");
  64     final String readme8 = "This is the root readme file";
  65     final String readme9 = "This is the version nine readme file";
  66     final String readme10 = "This is the version ten readme file";
  67     private Map<String,byte[]> rootClasses;
  68     private Map<String,byte[]> version9Classes;
  69     private Map<String,byte[]> version10Classes;
  70 
  71     public void buildUnversionedJar() throws IOException {
  72         JarBuilder jb = new JarBuilder("unversioned.jar");
  73         jb.addEntry("README", readme8.getBytes());
  74         jb.addEntry("version/Main.java", main.getBytes());
  75         jb.addEntry("version/Main.class", rootClasses.get("version.Main"));
  76         jb.addEntry("version/Version.java", java8.getBytes());
  77         jb.addEntry("version/Version.class", rootClasses.get("version.Version"));
  78         jb.build();
  79     }
  80 
  81     public void buildMultiReleaseJar() throws IOException {
  82         JarBuilder jb = new JarBuilder("multi-release.jar");
  83         jb.addAttribute("Multi-Release", "true");
  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.addEntry("META-INF/versions/9/README", readme9.getBytes());
  90         jb.addEntry("META-INF/versions/9/version/Version.java", java9.getBytes());
  91         jb.addEntry("META-INF/versions/9/version/PackagePrivate.java", ppjava9.getBytes());
  92         jb.addEntry("META-INF/versions/9/version/Version.class", version9Classes.get("version.Version"));
  93         jb.addEntry("META-INF/versions/9/version/PackagePrivate.class", version9Classes.get("version.PackagePrivate"));
  94         jb.addEntry("META-INF/versions/10/README", readme10.getBytes());
  95         jb.addEntry("META-INF/versions/10/version/Version.java", java10.getBytes());
  96         jb.addEntry("META-INF/versions/10/version/Version.class", version10Classes.get("version.Version"));
  97         jb.build();
  98     }
  99 
 100     public void buildSignedMultiReleaseJar() throws Exception {
 101         String testsrc = System.getProperty("test.src",".");
 102         String testdir = findTestDir(testsrc);
 103         String keystore = testdir + "/sun/security/tools/jarsigner/JarSigning.keystore";
 104         String[] jsArgs = {
 105                 "-keystore", keystore,
 106                 "-storepass", "bbbbbb",
 107                 "-signedJar", "signed-multi-release.jar",
 108                 "multi-release.jar", "b"
 109         };
 110         sun.security.tools.jarsigner.Main.main(jsArgs);
 111 
 112     }
 113 
 114     String findTestDir(String dir) throws IOException {
 115         Path path = Paths.get(dir).toAbsolutePath();
 116         while (path != null && !path.endsWith("test")) {
 117             path = path.getParent();
 118         }
 119         if (path == null) {
 120             throw new IllegalArgumentException(dir + " is not in a test directory");
 121         }
 122         if (!Files.isDirectory(path)) {
 123             throw new IOException(path.toString() + " is not a directory");
 124         }
 125         return path.toString();
 126     }
 127 
 128     void compileEntries() {
 129         Map<String,String> input = new HashMap<>();
 130         input.put("version.Main", main);
 131         input.put("version.Version", java8);
 132         rootClasses = (new Compiler(input)).setRelease(8).compile();
 133         input.clear();
 134         input.put("version.Version", java9);
 135         input.put("version.PackagePrivate", ppjava9);
 136         version9Classes = (new Compiler(input)).setRelease(9).compile();
 137         input.clear();
 138         input.put("version.Version", java10);
 139         version10Classes = (new Compiler(input)).setRelease(9).compile();  // fixme in JDK 10
 140     }
 141 }