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.nio.file.Files; 36 import java.nio.file.Path; 37 import java.util.Collections; 38 import java.util.HashMap; 39 import java.util.Map; 40 import jdk.tools.jlink.internal.ResourcePoolManager; 41 import jdk.tools.jlink.builder.DefaultImageBuilder; 42 43 import jdk.tools.jlink.internal.plugins.FileCopierPlugin; 44 import jdk.tools.jlink.plugin.ResourcePoolEntry; 45 import jdk.tools.jlink.plugin.ResourcePool; 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 ResourcePoolManager poolMgr = new ResourcePoolManager(); 89 ResourcePool pool = plug.transform( 90 new ResourcePoolManager().resourcePool(), 91 poolMgr.resourcePoolBuilder()); 92 if (pool.entryCount() != expected) { 93 throw new AssertionError("Wrong number of added files"); 94 } 95 pool.entries().forEach(f -> { 96 if (!f.type().equals(ResourcePoolEntry.Type.OTHER)) { 97 throw new AssertionError("Invalid type " + f.type() 98 + " for file " + f.path()); 99 } 100 if (f.content() == null) { 101 throw new AssertionError("Null stream for file " + f.path()); 102 } 103 }); 104 Path root = new File(".").toPath(); 105 DefaultImageBuilder imgbuilder = new DefaultImageBuilder(root); 106 imgbuilder.storeFiles(pool); 107 108 if (lic.exists()) { 109 File license = new File(root.toFile(), "LICENSE"); 110 if (!license.exists() || license.length() == 0) { 111 throw new AssertionError("Invalide license file " 112 + license.getAbsoluteFile()); 113 } 114 } 115 116 File sample1 = new File(root.toFile(), txt.getName()); 117 if (!sample1.exists() || sample1.length() == 0) { 118 throw new AssertionError("Invalide sample1 file " 119 + sample1.getAbsoluteFile()); 120 } 121 if (!new String(Files.readAllBytes(sample1.toPath())).equals(content)) { 122 throw new AssertionError("Invalid Content in sample1"); 123 } 124 125 File sample2 = new File(root.toFile(), target); 126 if (!sample2.exists() || sample2.length() == 0) { 127 throw new AssertionError("Invalide sample2 file " 128 + sample2.getAbsoluteFile()); 129 } 130 if (!new String(Files.readAllBytes(sample2.toPath())).equals(content)) { 131 throw new AssertionError("Invalid Content in sample2"); 132 } 133 134 File src2 = new File(root.toFile(), "src2"); 135 if (!src2.exists() || src2.list().length != 1) { 136 throw new AssertionError("Invalide src2 dir " 137 + src2.getAbsoluteFile()); 138 } 139 File f = src2.listFiles()[0]; 140 if (!f.getName().equals(txt.getName())) { 141 throw new AssertionError("Invalide file name in src2 dir " 142 + f.getAbsoluteFile()); 143 } 144 if (!new String(Files.readAllBytes(f.toPath())).equals(content)) { 145 throw new AssertionError("Invalid Content in src2 dir"); 146 } 147 } 148 }