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.internal.plugins
  30  * @run main FileCopierPluginTest
  31  */
  32 
  33 import java.io.File;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.util.Collections;
  37 import java.util.HashMap;
  38 import java.util.Map;
  39 import jdk.tools.jlink.internal.PoolImpl;
  40 import jdk.tools.jlink.builder.DefaultImageBuilder;
  41 
  42 import jdk.tools.jlink.internal.plugins.FileCopierPlugin;
  43 import jdk.tools.jlink.plugin.Pool;
  44 import jdk.tools.jlink.plugin.Pool.ModuleData;
  45 import jdk.tools.jlink.plugin.Pool.ModuleDataType;
  46 
  47 public class FileCopierPluginTest {
  48 
  49     public static void main(String[] args) throws Exception {
  50         new FileCopierPluginTest().test();
  51     }
  52 
  53     /**
  54      * 3 cases - Absolute, no target ==> copy in image root dir - Absolute and
  55      * target ==> copy in image root dir/target - Relative ==> copy from JDK
  56      * home dir.
  57      *
  58      * @throws Exception
  59      */
  60     public void test() throws Exception {
  61         FileCopierPlugin plug = new FileCopierPlugin();
  62         String content = "You \n should \n be \bthere.\n";
  63         String name = "sample.txt";
  64         File src = new File("src");
  65         src.mkdir();
  66         // Need a fake bin
  67         File bin = new File("bin");
  68         bin.mkdir();
  69 
  70         File txt = new File(src, name);
  71         txt.createNewFile();
  72 
  73         String target = "target" + File.separator + name;
  74         Files.write(txt.toPath(), content.getBytes());
  75         File lic = new File(System.getProperty("java.home"), "LICENSE");
  76         StringBuilder builder = new StringBuilder();
  77         int expected = lic.exists() ? 4 : 3;
  78         if (lic.exists()) {
  79             builder.append("LICENSE,");
  80         }
  81         builder.append(txt.getAbsolutePath()+",");
  82         builder.append(txt.getAbsolutePath() + "=" + target+",");
  83         builder.append(src.getAbsolutePath() + "=src2");
  84 
  85         Map<String, String> conf = new HashMap<>();
  86         conf.put(FileCopierPlugin.NAME, builder.toString());
  87         plug.configure(conf);
  88         Pool pool = new PoolImpl();
  89         plug.visit(new PoolImpl(), pool);
  90         if (pool.getContent().size() != expected) {
  91             throw new AssertionError("Wrong number of added files");
  92         }
  93         for (ModuleData f : pool.getContent()) {
  94             if (!f.getType().equals(ModuleDataType.OTHER)) {
  95                 throw new AssertionError("Invalid type " + f.getType()
  96                         + " for file " + f.getPath());
  97             }
  98             if (f.stream() == null) {
  99                 throw new AssertionError("Null stream for file " + f.getPath());
 100             }
 101 
 102         }
 103         Path root = new File(".").toPath();
 104         DefaultImageBuilder imgbuilder = new DefaultImageBuilder(root);
 105         imgbuilder.storeFiles(pool);
 106 
 107         if (lic.exists()) {
 108             File license = new File(root.toFile(), "LICENSE");
 109             if (!license.exists() || license.length() == 0) {
 110                 throw new AssertionError("Invalide license file "
 111                         + license.getAbsoluteFile());
 112             }
 113         }
 114 
 115         File sample1 = new File(root.toFile(), txt.getName());
 116         if (!sample1.exists() || sample1.length() == 0) {
 117             throw new AssertionError("Invalide sample1 file "
 118                     + sample1.getAbsoluteFile());
 119         }
 120         if (!new String(Files.readAllBytes(sample1.toPath())).equals(content)) {
 121             throw new AssertionError("Invalid Content in sample1");
 122         }
 123 
 124         File sample2 = new File(root.toFile(), target);
 125         if (!sample2.exists() || sample2.length() == 0) {
 126             throw new AssertionError("Invalide sample2 file "
 127                     + sample2.getAbsoluteFile());
 128         }
 129         if (!new String(Files.readAllBytes(sample2.toPath())).equals(content)) {
 130             throw new AssertionError("Invalid Content in sample2");
 131         }
 132 
 133         File src2 = new File(root.toFile(), "src2");
 134         if (!src2.exists() || src2.list().length != 1) {
 135             throw new AssertionError("Invalide src2 dir "
 136                     + src2.getAbsoluteFile());
 137         }
 138         File f = src2.listFiles()[0];
 139         if (!f.getName().equals(txt.getName())) {
 140             throw new AssertionError("Invalide file name in src2 dir "
 141                     + f.getAbsoluteFile());
 142         }
 143         if (!new String(Files.readAllBytes(f.toPath())).equals(content)) {
 144             throw new AssertionError("Invalid Content in src2 dir");
 145         }
 146     }
 147 }