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.PluginException; 45 import jdk.tools.jlink.plugin.ResourcePoolEntry; 46 import jdk.tools.jlink.plugin.ResourcePool; 47 48 public class FileCopierPluginTest { 49 50 public static void main(String[] args) throws Exception { 51 new FileCopierPluginTest().test(); 52 } 53 54 /** 55 * 3 cases - Absolute, no target ==> copy in image root dir - Absolute and 56 * target ==> copy in image root dir/target - Relative ==> copy from JDK 57 * home dir. 58 * 59 * @throws Exception 60 */ 61 public void test() throws Exception { 62 FileCopierPlugin plug = new FileCopierPlugin(); 63 String content = "You \n should \n be \bthere.\n"; 64 String name = "sample.txt"; 65 File src = new File("src"); 66 src.mkdir(); 67 // Need a fake bin 68 File bin = new File("bin"); 69 bin.mkdir(); 70 71 File txt = new File(src, name); 72 txt.createNewFile(); 73 74 String target = "target" + File.separator + name; 75 Files.write(txt.toPath(), content.getBytes()); 76 File lic = new File(System.getProperty("java.home"), "LICENSE"); 77 StringBuilder builder = new StringBuilder(); 78 int expected = lic.exists() ? 4 : 3; 79 if (lic.exists()) { 80 builder.append("LICENSE,"); 81 } 82 builder.append(txt.getAbsolutePath()+","); 83 builder.append(txt.getAbsolutePath() + "=" + target+","); 84 builder.append(src.getAbsolutePath() + "=src2"); 85 86 Map<String, String> conf = new HashMap<>(); 87 conf.put(FileCopierPlugin.NAME, builder.toString()); 88 plug.configure(conf); 89 ResourcePoolManager poolMgr = new ResourcePoolManager(); 90 ResourcePool pool = plug.transform( 91 new ResourcePoolManager().resourcePool(), 92 poolMgr.resourcePoolBuilder()); 93 if (pool.entryCount() != expected) { 94 throw new AssertionError("Wrong number of added files"); 95 } 96 pool.entries().forEach(f -> { 97 if (!f.type().equals(ResourcePoolEntry.Type.OTHER)) { 98 throw new AssertionError("Invalid type " + f.type() 99 + " for file " + f.path()); 100 } 101 if (f.content() == null) { 102 throw new AssertionError("Null stream for file " + f.path()); 103 } 104 }); 105 Path root = new File(".").toPath(); 106 DefaultImageBuilder imgbuilder = new DefaultImageBuilder(root); 107 try { 108 imgbuilder.storeFiles(pool); 109 } catch (PluginException e) { 110 // We didn't add any .class resource of the java.base module! 111 // This cannot happen in non-testing scenario as java.base module 112 // is minimum mandatory module in a .jimage. jlink depends on java.base 113 // to generate 'release' file. If the current exception came from that 114 // part of the code, then it is okay. 115 if (!e.getMessage().contains("No module-info for java.base module")) { 116 throw e; 117 } 118 } 119 120 if (lic.exists()) { 121 File license = new File(root.toFile(), "LICENSE"); 122 if (!license.exists() || license.length() == 0) { 123 throw new AssertionError("Invalide license file " 124 + license.getAbsoluteFile()); 125 } 126 } 127 128 File sample1 = new File(root.toFile(), txt.getName()); 129 if (!sample1.exists() || sample1.length() == 0) { 130 throw new AssertionError("Invalide sample1 file " 131 + sample1.getAbsoluteFile()); 132 } 133 if (!new String(Files.readAllBytes(sample1.toPath())).equals(content)) { 134 throw new AssertionError("Invalid Content in sample1"); 135 } 136 137 File sample2 = new File(root.toFile(), target); 138 if (!sample2.exists() || sample2.length() == 0) { 139 throw new AssertionError("Invalide sample2 file " 140 + sample2.getAbsoluteFile()); 141 } 142 if (!new String(Files.readAllBytes(sample2.toPath())).equals(content)) { 143 throw new AssertionError("Invalid Content in sample2"); 144 } 145 146 File src2 = new File(root.toFile(), "src2"); 147 if (!src2.exists() || src2.list().length != 1) { 148 throw new AssertionError("Invalide src2 dir " 149 + src2.getAbsoluteFile()); 150 } 151 File f = src2.listFiles()[0]; 152 if (!f.getName().equals(txt.getName())) { 153 throw new AssertionError("Invalide file name in src2 dir " 154 + f.getAbsoluteFile()); 155 } 156 if (!new String(Files.readAllBytes(f.toPath())).equals(content)) { 157 throw new AssertionError("Invalid Content in src2 dir"); 158 } 159 } 160 }