< prev index next >

jdk/test/tools/jlink/JLinkTest.java

Print this page




  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.io.PrintWriter;
  26 import java.io.StringWriter;
  27 import java.lang.module.ModuleDescriptor;
  28 import java.lang.reflect.Layer;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.ArrayList;
  33 import java.util.Collections;
  34 import java.util.List;

  35 import java.util.stream.Stream;
  36 
  37 import jdk.tools.jlink.plugin.Plugin;
  38 import jdk.tools.jlink.internal.PluginRepository;
  39 import tests.Helper;
  40 import tests.JImageGenerator;
  41 import tests.JImageGenerator.InMemoryFile;
  42 
  43 /*
  44  * @test
  45  * @summary Test image creation
  46  * @author Jean-Francois Denise
  47  * @library ../lib
  48  * @modules java.base/jdk.internal.jimage
  49  *          jdk.jdeps/com.sun.tools.classfile
  50  *          jdk.jlink/jdk.tools.jlink.internal
  51  *          jdk.jlink/jdk.tools.jmod
  52  *          jdk.jlink/jdk.tools.jimage
  53  *          jdk.compiler
  54  * @build tests.*
  55  * @run main/othervm -Xmx1g JLinkTest
  56  */
  57 public class JLinkTest {





  58     // number of built-in plugins from jdk.jlink module
  59     private static int getNumJlinkPlugins() {
  60         ModuleDescriptor desc = Plugin.class.getModule().getDescriptor();
  61         return desc.provides().
  62                     get(Plugin.class.getName()).
  63                     providers().size();
  64     }
  65 
  66     private static boolean isOfJLinkModule(Plugin p) {
  67         return p.getClass().getModule() == Plugin.class.getModule();
  68     }
  69 
  70     public static void main(String[] args) throws Exception {
  71 
  72         Helper helper = Helper.newHelper();
  73         if (helper == null) {
  74             System.err.println("Test not run");
  75             return;
  76         }
  77         helper.generateDefaultModules();


 163             JImageGenerator.getJLinkTask()
 164                     .modulePath(helper.defaultModulePath())
 165                     .output(helper.createNewImageDir(moduleName))
 166                     .limitMods(moduleName)
 167                     .repeatedLimitMods("java.base") // second --limit-modules overrides first
 168                     .addMods(moduleName)
 169                     .call().assertFailure("Error: Module m_8165735dependency not found, required by m_8165735");
 170 
 171             JImageGenerator.getJLinkTask()
 172                     .modulePath(helper.defaultModulePath())
 173                     .output(helper.createNewImageDir(moduleName))
 174                     .limitMods("java.base")
 175                     .repeatedLimitMods(moduleName) // second --limit-modules overrides first
 176                     .addMods(moduleName)
 177                     .call().assertSuccess();
 178         }
 179 
 180         {
 181             // Help
 182             StringWriter writer = new StringWriter();
 183             jdk.tools.jlink.internal.Main.run(new String[]{"--help"}, new PrintWriter(writer));

 184             String output = writer.toString();
 185             if (output.split("\n").length < 10) {
 186                 System.err.println(output);
 187                 throw new AssertionError("Help");
 188             }
 189         }
 190 
 191         {
 192             // License files
 193             String copied = "LICENSE";
 194             String[] arr = copied.split(",");
 195             String[] copyFiles = new String[2];
 196             copyFiles[0] = "--copy-files";
 197             copyFiles[1] = copied;
 198             Path imageDir = helper.generateDefaultImage(copyFiles, "composite2").assertSuccess();
 199             helper.checkImage(imageDir, "composite2", null, null, arr);
 200         }
 201 
 202         {
 203             // List plugins
 204             StringWriter writer = new StringWriter();
 205             jdk.tools.jlink.internal.Main.run(new String[]{"--list-plugins"}, new PrintWriter(writer));


 206             String output = writer.toString();
 207             long number = Stream.of(output.split("\\R"))
 208                     .filter((s) -> s.matches("Plugin Name:.*"))
 209                     .count();
 210             if (number != totalPlugins) {
 211                 System.err.println(output);
 212                 throw new AssertionError("Found: " + number + " expected " + totalPlugins);
 213             }
 214         }
 215 
 216         // filter out files and resources + Skip debug + compress
 217         {
 218             String[] userOptions = {"--compress", "2", "--strip-debug",
 219                 "--exclude-resources", "*.jcov, */META-INF/*", "--exclude-files",
 220                 "*" + Helper.getDebugSymbolsExtension()};
 221             String moduleName = "excludezipskipdebugcomposite2";
 222             helper.generateDefaultJModule(moduleName, "composite2");
 223             String[] res = {".jcov", "/META-INF/"};
 224             String[] files = {Helper.getDebugSymbolsExtension()};
 225             Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();




  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.io.PrintWriter;
  26 import java.io.StringWriter;
  27 import java.lang.module.ModuleDescriptor;
  28 import java.lang.reflect.Layer;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.ArrayList;
  33 import java.util.Collections;
  34 import java.util.List;
  35 import java.util.spi.ToolProvider;
  36 import java.util.stream.Stream;
  37 
  38 import jdk.tools.jlink.plugin.Plugin;
  39 import jdk.tools.jlink.internal.PluginRepository;
  40 import tests.Helper;
  41 import tests.JImageGenerator;

  42 
  43 /*
  44  * @test
  45  * @summary Test image creation
  46  * @author Jean-Francois Denise
  47  * @library ../lib
  48  * @modules java.base/jdk.internal.jimage
  49  *          jdk.jdeps/com.sun.tools.classfile
  50  *          jdk.jlink/jdk.tools.jlink.internal

  51  *          jdk.jlink/jdk.tools.jimage
  52  *          jdk.compiler
  53  * @build tests.*
  54  * @run main/othervm -Xmx1g JLinkTest
  55  */
  56 public class JLinkTest {
  57     static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
  58         .orElseThrow(() ->
  59             new RuntimeException("jlink tool not found")
  60         );
  61 
  62     // number of built-in plugins from jdk.jlink module
  63     private static int getNumJlinkPlugins() {
  64         ModuleDescriptor desc = Plugin.class.getModule().getDescriptor();
  65         return desc.provides().
  66                     get(Plugin.class.getName()).
  67                     providers().size();
  68     }
  69 
  70     private static boolean isOfJLinkModule(Plugin p) {
  71         return p.getClass().getModule() == Plugin.class.getModule();
  72     }
  73 
  74     public static void main(String[] args) throws Exception {
  75 
  76         Helper helper = Helper.newHelper();
  77         if (helper == null) {
  78             System.err.println("Test not run");
  79             return;
  80         }
  81         helper.generateDefaultModules();


 167             JImageGenerator.getJLinkTask()
 168                     .modulePath(helper.defaultModulePath())
 169                     .output(helper.createNewImageDir(moduleName))
 170                     .limitMods(moduleName)
 171                     .repeatedLimitMods("java.base") // second --limit-modules overrides first
 172                     .addMods(moduleName)
 173                     .call().assertFailure("Error: Module m_8165735dependency not found, required by m_8165735");
 174 
 175             JImageGenerator.getJLinkTask()
 176                     .modulePath(helper.defaultModulePath())
 177                     .output(helper.createNewImageDir(moduleName))
 178                     .limitMods("java.base")
 179                     .repeatedLimitMods(moduleName) // second --limit-modules overrides first
 180                     .addMods(moduleName)
 181                     .call().assertSuccess();
 182         }
 183 
 184         {
 185             // Help
 186             StringWriter writer = new StringWriter();
 187             PrintWriter pw = new PrintWriter(writer);
 188             JLINK_TOOL.run(pw, pw, "--help");
 189             String output = writer.toString();
 190             if (output.split("\n").length < 10) {
 191                 System.err.println(output);
 192                 throw new AssertionError("Help");
 193             }
 194         }
 195 
 196         {
 197             // License files
 198             String copied = "LICENSE";
 199             String[] arr = copied.split(",");
 200             String[] copyFiles = new String[2];
 201             copyFiles[0] = "--copy-files";
 202             copyFiles[1] = copied;
 203             Path imageDir = helper.generateDefaultImage(copyFiles, "composite2").assertSuccess();
 204             helper.checkImage(imageDir, "composite2", null, null, arr);
 205         }
 206 
 207         {
 208             // List plugins
 209             StringWriter writer = new StringWriter();
 210             PrintWriter pw = new PrintWriter(writer);
 211 
 212             JLINK_TOOL.run(pw, pw, "--list-plugins");
 213             String output = writer.toString();
 214             long number = Stream.of(output.split("\\R"))
 215                     .filter((s) -> s.matches("Plugin Name:.*"))
 216                     .count();
 217             if (number != totalPlugins) {
 218                 System.err.println(output);
 219                 throw new AssertionError("Found: " + number + " expected " + totalPlugins);
 220             }
 221         }
 222 
 223         // filter out files and resources + Skip debug + compress
 224         {
 225             String[] userOptions = {"--compress", "2", "--strip-debug",
 226                 "--exclude-resources", "*.jcov, */META-INF/*", "--exclude-files",
 227                 "*" + Helper.getDebugSymbolsExtension()};
 228             String moduleName = "excludezipskipdebugcomposite2";
 229             helper.generateDefaultJModule(moduleName, "composite2");
 230             String[] res = {".jcov", "/META-INF/"};
 231             String[] files = {Helper.getDebugSymbolsExtension()};
 232             Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();


< prev index next >