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