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 files copy plugin
  27  * @author Jean-Francois Denise
  28  * @modules jdk.jlink/jdk.tools.jlink.internal
  29  *          jdk.jlink/jdk.tools.jlink.builder
  30  *          jdk.jlink/jdk.tools.jlink.internal.plugins
  31  * @run main FileCopierPluginTest
  32  */
  33 
  34 import java.io.File;
  35 import java.net.URI;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 import java.util.Collections;
  40 import java.util.HashMap;
  41 import java.util.Map;
  42 import jdk.tools.jlink.internal.ResourcePoolManager;
  43 import jdk.tools.jlink.builder.DefaultImageBuilder;
  44 
  45 import jdk.tools.jlink.internal.plugins.FileCopierPlugin;
  46 import jdk.tools.jlink.plugin.PluginException;
  47 import jdk.tools.jlink.plugin.ResourcePoolEntry;
  48 import jdk.tools.jlink.plugin.ResourcePool;
  49 
  50 public class FileCopierPluginTest {
  51 
  52     public static void main(String[] args) throws Exception {
  53         new FileCopierPluginTest().test();
  54     }
  55 
  56     /**
  57      * 3 cases - Absolute, no target ==> copy in image root dir - Absolute and
  58      * target ==> copy in image root dir/target - Relative ==> copy from JDK
  59      * home dir.
  60      *
  61      * @throws Exception
  62      */
  63     public void test() throws Exception {
  64         FileCopierPlugin plug = new FileCopierPlugin();
  65         String content = "You \n should \n be \bthere.\n";
  66         String name = "sample.txt";
  67         File src = new File("src");
  68         src.mkdir();
  69         // Need a fake bin
  70         File bin = new File("bin");
  71         bin.mkdir();
  72 
  73         File txt = new File(src, name);
  74         txt.createNewFile();
  75 
  76         String target = "target" + File.separator + name;
  77         Files.write(txt.toPath(), content.getBytes());
  78         File lic = new File(System.getProperty("java.home"), "LICENSE.txt");
  79         StringBuilder builder = new StringBuilder();
  80         int expected = lic.exists() ? 4 : 3;
  81         if (lic.exists()) {
  82             builder.append("LICENSE.txt,");
  83         }
  84         builder.append(txt.getAbsolutePath()+",");
  85         builder.append(txt.getAbsolutePath() + "=" + target+",");
  86         builder.append(src.getAbsolutePath() + "=src2");
  87 
  88         Map<String, String> conf = new HashMap<>();
  89         conf.put(FileCopierPlugin.NAME, builder.toString());
  90         plug.configure(conf);
  91         ResourcePoolManager poolMgr = new ResourcePoolManager();
  92         // java.base/module-info.class is used to add "release" file
  93         // We read it from jrt-fs and add a ResourcePoolEntry
  94         poolMgr.add(
  95             ResourcePoolEntry.create("/java.base/module-info.class",
  96                 ResourcePoolEntry.Type.CLASS_OR_RESOURCE, getJavaBaseModuleInfo()));
  97         expected++;
  98         ResourcePool pool = plug.transform(
  99                 new ResourcePoolManager().resourcePool(),
 100                 poolMgr.resourcePoolBuilder());
 101         if (pool.entryCount() != expected) {
 102             throw new AssertionError("Wrong number of added files");
 103         }
 104         pool.entries().forEach(f -> {
 105             if (!f.type().equals(ResourcePoolEntry.Type.OTHER) &&
 106                 !f.type().equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)) {
 107                 throw new AssertionError("Invalid type " + f.type()
 108                         + " for file " + f.path());
 109             }
 110             if (f.content() == null) {
 111                 throw new AssertionError("Null stream for file " + f.path());
 112             }
 113         });
 114         Path root = new File(".").toPath();
 115         DefaultImageBuilder imgbuilder = new DefaultImageBuilder(root);
 116         imgbuilder.storeFiles(pool);
 117 
 118         if (lic.exists()) {
 119             File license = new File(root.toFile(), "LICENSE.txt");
 120             if (!license.exists() || license.length() == 0) {
 121                 throw new AssertionError("Invalid license file "
 122                         + license.getAbsoluteFile());
 123             }
 124         }
 125 
 126         File sample1 = new File(root.toFile(), txt.getName());
 127         if (!sample1.exists() || sample1.length() == 0) {
 128             throw new AssertionError("Invalide sample1 file "
 129                     + sample1.getAbsoluteFile());
 130         }
 131         if (!new String(Files.readAllBytes(sample1.toPath())).equals(content)) {
 132             throw new AssertionError("Invalid Content in sample1");
 133         }
 134 
 135         File sample2 = new File(root.toFile(), target);
 136         if (!sample2.exists() || sample2.length() == 0) {
 137             throw new AssertionError("Invalide sample2 file "
 138                     + sample2.getAbsoluteFile());
 139         }
 140         if (!new String(Files.readAllBytes(sample2.toPath())).equals(content)) {
 141             throw new AssertionError("Invalid Content in sample2");
 142         }
 143 
 144         File src2 = new File(root.toFile(), "src2");
 145         if (!src2.exists() || src2.list().length != 1) {
 146             throw new AssertionError("Invalide src2 dir "
 147                     + src2.getAbsoluteFile());
 148         }
 149         File f = src2.listFiles()[0];
 150         if (!f.getName().equals(txt.getName())) {
 151             throw new AssertionError("Invalide file name in src2 dir "
 152                     + f.getAbsoluteFile());
 153         }
 154         if (!new String(Files.readAllBytes(f.toPath())).equals(content)) {
 155             throw new AssertionError("Invalid Content in src2 dir");
 156         }
 157     }
 158 
 159     // read java.base/module-info.class from jrt-fs
 160     private static Path getJavaBaseModuleInfo() {
 161         return Paths.get(URI.create("jrt:/modules/java.base/module-info.class"));
 162     }
 163 }