author | sundar |
Thu, 04 Aug 2016 19:39:42 +0530 | |
changeset 40121 | 4204d69de3a9 |
parent 39834 | 53a6fb443c20 |
child 40124 | 01f8745abd3c |
permissions | -rw-r--r-- |
36511 | 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 |
|
38320 | 29 |
* jdk.jlink/jdk.tools.jlink.builder |
36511 | 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; |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
40 |
import jdk.tools.jlink.internal.ResourcePoolManager; |
36511 | 41 |
import jdk.tools.jlink.builder.DefaultImageBuilder; |
42 |
||
43 |
import jdk.tools.jlink.internal.plugins.FileCopierPlugin; |
|
40121
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
44 |
import jdk.tools.jlink.plugin.PluginException; |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
45 |
import jdk.tools.jlink.plugin.ResourcePoolEntry; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
46 |
import jdk.tools.jlink.plugin.ResourcePool; |
36511 | 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); |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
89 |
ResourcePoolManager poolMgr = new ResourcePoolManager(); |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
90 |
ResourcePool pool = plug.transform( |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
91 |
new ResourcePoolManager().resourcePool(), |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
92 |
poolMgr.resourcePoolBuilder()); |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
93 |
if (pool.entryCount() != expected) { |
36511 | 94 |
throw new AssertionError("Wrong number of added files"); |
95 |
} |
|
38320 | 96 |
pool.entries().forEach(f -> { |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
97 |
if (!f.type().equals(ResourcePoolEntry.Type.OTHER)) { |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
98 |
throw new AssertionError("Invalid type " + f.type() |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
99 |
+ " for file " + f.path()); |
36511 | 100 |
} |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
101 |
if (f.content() == null) { |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
38320
diff
changeset
|
102 |
throw new AssertionError("Null stream for file " + f.path()); |
36511 | 103 |
} |
38320 | 104 |
}); |
36511 | 105 |
Path root = new File(".").toPath(); |
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
106 |
DefaultImageBuilder imgbuilder = new DefaultImageBuilder(root); |
40121
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
107 |
try { |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
108 |
imgbuilder.storeFiles(pool); |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
109 |
} catch (PluginException e) { |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
110 |
// We didn't add any .class resource of the java.base module! |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
111 |
// This cannot happen in non-testing scenario as java.base module |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
112 |
// is minimum mandatory module in a .jimage. jlink depends on java.base |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
113 |
// to generate 'release' file. If the current exception came from that |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
114 |
// part of the code, then it is okay. |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
115 |
if (!e.getMessage().contains("No module-info for java.base module")) { |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
116 |
throw e; |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
117 |
} |
4204d69de3a9
8146721: FileCopierPlugin should not create fake module
sundar
parents:
39834
diff
changeset
|
118 |
} |
36511 | 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 |
} |