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 /*
  25  * @test
  26  * @summary Test config, cmd and lib directories of jmod.
  27  * @author Andrei Eremeev
  28  * @library ../lib
  29  * @modules java.base/jdk.internal.jimage
  30  *          jdk.jdeps/com.sun.tools.classfile
  31  *          jdk.jlink/jdk.tools.jlink
  32  *          jdk.jlink/jdk.tools.jlink.internal
  33  *          jdk.jlink/jdk.tools.jmod
  34  *          jdk.jlink/jdk.tools.jimage
  35  * @build tests.*
  36  * @run main NativeTest
  37  */
  38 
  39 import java.io.File;
  40 import java.io.IOException;
  41 import java.nio.file.Files;
  42 import java.nio.file.Path;
  43 import java.util.Arrays;
  44 
  45 import tests.Helper;
  46 import tests.JImageGenerator;
  47 
  48 public class NativeTest {
  49 
  50     private final Helper helper;
  51 
  52     public NativeTest(Helper helper) {
  53         this.helper = helper;
  54     }
  55 
  56     public static void main(String[] args) throws IOException {
  57         Helper helper = Helper.newHelper();
  58         if (helper == null) {
  59             System.err.println("Not run");
  60             return;
  61         }
  62         new NativeTest(helper).test();
  63     }
  64 
  65     public void test() throws IOException {
  66         String moduleName = "native_library";
  67         Path classesDir = helper.generateModuleCompiledClasses(
  68                 helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName);
  69         Path libsDir = helper.getJmodDir().resolve("lib").resolve(moduleName);
  70         Path configDir = helper.getJmodDir().resolve("config").resolve(moduleName);
  71         Path cmdDir = helper.getJmodDir().resolve("cmd").resolve(moduleName);
  72 
  73         Path config = writeFile(configDir.resolve("config.txt"), "AAAA\nBBBB");
  74         Path cmd = writeFile(cmdDir.resolve("ls.sh"), "ls");
  75         Path lib = writeFile(libsDir.resolve("native.so"), "native library");
  76 
  77         JImageGenerator.getJModTask()
  78                 .addClassPath(classesDir)
  79                 .addNativeLibraries(libsDir)
  80                 .addCmds(cmdDir)
  81                 .addConfig(configDir)
  82                 .jmod(helper.createNewJmodFile(moduleName))
  83                 .create()
  84                 .assertSuccess();
  85 
  86         String[] expectedFiles = new String[] {
  87                 "bin" + File.separator + cmd.getFileName(),
  88                 "conf" + File.separator + config.getFileName(),
  89                 "lib" + File.separator + lib.getFileName()
  90         };
  91         Path image = JImageGenerator.getJLinkTask()
  92                 .modulePath(helper.defaultModulePath())
  93                 .addMods(moduleName)
  94                 .output(helper.createNewImageDir(moduleName))
  95                 .call().assertSuccess();
  96         helper.checkImage(image, moduleName, null, null, expectedFiles);
  97     }
  98 
  99     private Path writeFile(Path path, String content) throws IOException {
 100         if (path.getParent() != null) {
 101             Files.createDirectories(path.getParent());
 102         }
 103         Files.write(path, Arrays.asList(content.split("\n")));
 104         return path;
 105     }
 106 }