1
1
/*
2
- * Copyright (c) 2015, 2018 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2015, 2024 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
21
21
* questions.
22
22
*/
23
23
24
- /*
25
- * @test
26
- * @summary jlink test of --add-module ALL-MODULE-PATH
27
- * @library /test/lib
28
- * @modules jdk.compiler
29
- * @build jdk.test.lib.process.ProcessTools
30
- * jdk.test.lib.process.OutputAnalyzer
31
- * jdk.test.lib.compiler.CompilerUtils
32
- * @run testng AllModulePath
33
- */
24
+ import static org .testng .Assert .assertEquals ;
25
+ import static org .testng .Assert .assertTrue ;
34
26
35
- import java .io .File ;
27
+ import java .io .ByteArrayOutputStream ;
36
28
import java .io .IOException ;
37
29
import java .io .PrintWriter ;
38
30
import java .nio .file .Files ;
39
31
import java .nio .file .Path ;
40
32
import java .nio .file .Paths ;
41
- import java .nio .file .attribute .BasicFileAttributes ;
42
33
import java .util .ArrayList ;
43
- import java .util .Arrays ;
44
34
import java .util .HashSet ;
45
35
import java .util .List ;
46
36
import java .util .Set ;
47
- import java .util .stream .Collectors ;
48
- import java .util .stream .Stream ;
49
37
import java .util .spi .ToolProvider ;
50
-
51
- import jdk .test .lib .compiler .CompilerUtils ;
52
- import jdk .test .lib .process .ProcessTools ;
38
+ import java .util .stream .Collectors ;
53
39
54
40
import org .testng .annotations .BeforeClass ;
55
41
import org .testng .annotations .Test ;
56
- import static org .testng .Assert .*;
57
42
43
+ import jdk .test .lib .compiler .CompilerUtils ;
44
+ import jdk .test .lib .process .ProcessTools ;
45
+ import jdk .tools .jlink .internal .LinkableRuntimeImage ;
46
+ import tests .Helper ;
47
+ import tests .Result ;
48
+
49
+ /*
50
+ * @test
51
+ * @bug 8345259
52
+ * @summary jlink test of --add-module ALL-MODULE-PATH
53
+ * @library ../../lib /test/lib
54
+ * @modules jdk.compiler
55
+ * java.base/jdk.internal.jimage
56
+ * jdk.jlink/jdk.tools.jlink.internal
57
+ * jdk.jlink/jdk.tools.jimage
58
+ * @build jdk.test.lib.process.ProcessTools
59
+ * jdk.test.lib.process.OutputAnalyzer
60
+ * jdk.test.lib.compiler.CompilerUtils
61
+ * @run testng/othervm -Duser.language=en -Duser.country=US AllModulePath
62
+ */
58
63
public class AllModulePath {
59
64
60
- private final Path JMODS = Paths .get (System .getProperty ("test.jdk" )).resolve ("jmods" );
61
- private final Path SRC = Paths .get (System .getProperty ("test.src" )).resolve ("src" );
62
- private final Path MODS = Paths .get ("mods" );
65
+ private static final Path JMODS = Paths .get (System .getProperty ("test.jdk" )).resolve ("jmods" );
66
+ private static final Path SRC = Paths .get (System .getProperty ("test.src" )).resolve ("src" );
67
+ private static final Path MODS = Paths .get ("mods" );
68
+ private static final boolean LINKABLE_RUNTIME = LinkableRuntimeImage .isLinkableRuntime ();
69
+ private static final boolean JMODS_EXIST = Files .exists (JMODS );
63
70
64
71
private final static Set <String > MODULES = Set .of ("test" , "m1" );
65
72
66
73
static final ToolProvider JLINK_TOOL = ToolProvider .findFirst ("jlink" )
67
74
.orElseThrow (() ->
68
75
new RuntimeException ("jlink tool not found" )
69
76
);
77
+ private static Helper HELPER ;
78
+
79
+ private static boolean isExplodedJDKImage () {
80
+ if (!JMODS_EXIST && !LINKABLE_RUNTIME ) {
81
+ System .err .println ("Test skipped. Not a linkable runtime and no JMODs" );
82
+ return true ;
83
+ }
84
+ return false ;
85
+ }
70
86
71
87
@ BeforeClass
72
88
public void setup () throws Throwable {
73
- if (Files . notExists ( JMODS )) {
89
+ if (isExplodedJDKImage ( )) {
74
90
return ;
75
91
}
92
+ HELPER = Helper .newHelper (LINKABLE_RUNTIME );
76
93
77
94
Files .createDirectories (MODS );
78
95
@@ -84,60 +101,114 @@ public void setup() throws Throwable {
84
101
}
85
102
}
86
103
104
+ /*
105
+ * --add-modules ALL-MODULE-PATH with an existing module-path.
106
+ */
87
107
@ Test
88
108
public void testAllModulePath () throws Throwable {
89
- if (Files . notExists ( JMODS )) {
109
+ if (isExplodedJDKImage ( )) {
90
110
return ;
91
111
}
92
112
93
- // create custom image
94
- Path image = Paths .get ("image" );
95
- createImage (image , "--add-modules" , "ALL-MODULE-PATH" );
113
+ Path image = HELPER .createNewImageDir ("image" );
114
+ List <String > opts = List .of ("--module-path" , MODS .toString (),
115
+ "--output" , image .toString (),
116
+ "--add-modules" , "ALL-MODULE-PATH" );
117
+ createImage (image , opts , true /* success */ );
96
118
97
119
Set <String > modules = new HashSet <>();
98
- Files .find (JMODS , 1 , (Path p , BasicFileAttributes attr ) ->
99
- p .toString ().endsWith (".jmod" ))
100
- .map (p -> JMODS .relativize (p ).toString ())
101
- .map (n -> n .substring (0 , n .length ()-5 ))
102
- .forEach (modules ::add );
120
+ // java.base is a dependency of any external module
121
+ modules .add ("java.base" );
103
122
modules .add ("m1" );
104
123
modules .add ("test" );
105
124
checkModules (image , modules );
106
125
}
107
126
127
+ /*
128
+ * --add-modules ALL-MODULE-PATH with --limit-modules is an error
129
+ */
108
130
@ Test
109
131
public void testLimitModules () throws Throwable {
110
- if (Files . notExists ( JMODS )) {
132
+ if (isExplodedJDKImage ( )) {
111
133
return ;
112
134
}
113
-
114
- // create custom image
115
- Path image = Paths .get ("image1" );
116
- createImage (image ,
117
- "--add-modules" , "ALL-MODULE-PATH" ,
118
- "--limit-modules" , "m1" );
119
-
120
- checkModules (image , Set .of ("m1" , "java.base" ));
135
+ Path targetPath = HELPER .createNewImageDir ("all-mods-limit-mods" );
136
+ String moduleName = "com.baz.runtime" ;
137
+ Result result = HELPER .generateDefaultJModule (moduleName , "jdk.jfr" );
138
+ Path customModulePath = result .getFile ().getParent ();
139
+ List <String > allArgs = List .of ("--add-modules" , "ALL-MODULE-PATH" ,
140
+ "--limit-modules" , "jdk.jfr" ,
141
+ "--module-path" , customModulePath .toString (),
142
+ "--output" , targetPath .toString ());
143
+ JlinkOutput allOut = createImage (targetPath , allArgs , false /* success */ );
144
+ String actual = allOut .stdout .trim ();
145
+ String expected = "Error: --limit-modules not allowed with --add-modules ALL-MODULE-PATH" ;
146
+ assertEquals (actual , expected );
121
147
}
122
148
149
+
150
+ /*
151
+ * --add-modules *includes* ALL-MODULE-PATH with an existing module path
152
+ */
123
153
@ Test
124
154
public void testAddModules () throws Throwable {
125
- if (Files . notExists ( JMODS )) {
155
+ if (isExplodedJDKImage ( )) {
126
156
return ;
127
157
}
128
158
129
159
// create custom image
130
- Path image = Paths .get ("image2" );
131
- createImage (image ,
132
- "--add-modules" , "m1,test" ,
133
- "--add-modules" , "ALL-MODULE-PATH" ,
134
- "--limit-modules" , "java.base" );
160
+ Path image = HELPER .createNewImageDir ("image2" );
161
+ List <String > opts = List .of ("--module-path" , MODS .toString (),
162
+ "--output" , image .toString (),
163
+ "--add-modules" , "m1" ,
164
+ "--add-modules" , "ALL-MODULE-PATH" );
165
+ createImage (image , opts , true /* success */ );
135
166
136
167
checkModules (image , Set .of ("m1" , "test" , "java.base" ));
137
168
}
138
169
139
170
/*
140
- * check the modules linked in the image
171
+ * No --module-path with --add-modules ALL-MODULE-PATH is an error.
172
+ */
173
+ @ Test
174
+ public void noModulePath () throws IOException {
175
+ if (isExplodedJDKImage ()) {
176
+ return ;
177
+ }
178
+ Path targetPath = HELPER .createNewImageDir ("all-mod-path-no-mod-path" );
179
+ List <String > allArgs = List .of ("--add-modules" , "ALL-MODULE-PATH" ,
180
+ "--output" , targetPath .toString ());
181
+ JlinkOutput allOut = createImage (targetPath , allArgs , false /* expect failure */ );
182
+ String expected = "Error: --module-path option must be specified with --add-modules ALL-MODULE-PATH" ;
183
+ assertEquals (allOut .stdout .trim (), expected );
184
+ }
185
+
186
+ /*
187
+ * --module-path not-exist and --add-modules ALL-MODULE-PATH is an error.
188
+ */
189
+ @ Test
190
+ public void modulePathEmpty () throws IOException {
191
+ if (isExplodedJDKImage ()) {
192
+ return ;
193
+ }
194
+ Path targetPath = HELPER .createNewImageDir ("all-mod-path-not-existing" );
195
+ String strNotExists = "not-exist" ;
196
+ Path notExists = Path .of (strNotExists );
197
+ if (Files .exists (notExists )) {
198
+ throw new AssertionError ("Test setup error, path must not exist!" );
199
+ }
200
+ List <String > allArgs = List .of ("--add-modules" , "ALL-MODULE-PATH" ,
201
+ "--module-path" , notExists .toString (),
202
+ "--output" , targetPath .toString ());
203
+
204
+ JlinkOutput allOut = createImage (targetPath , allArgs , false /* expect failure */ );
205
+ String actual = allOut .stdout .trim ();
206
+ assertTrue (actual .startsWith ("Error: No module found in module path" ));
207
+ assertTrue (actual .contains (strNotExists ));
208
+ }
209
+
210
+ /*
211
+ * check the modules linked in the image using m1/p.ListModules
141
212
*/
142
213
private void checkModules (Path image , Set <String > modules ) throws Throwable {
143
214
Path cmd = findTool (image , "java" );
@@ -164,16 +235,19 @@ private Path findTool(Path image, String tool) {
164
235
return cmd ;
165
236
}
166
237
167
- private void createImage (Path image , String ... options ) throws IOException {
168
- String modulepath = JMODS .toString () + File .pathSeparator + MODS .toString ();
169
- List <String > opts = List .of ("--module-path" , modulepath ,
170
- "--output" , image .toString ());
171
- String [] args = Stream .concat (opts .stream (), Arrays .stream (options ))
172
- .toArray (String []::new );
173
-
174
- System .out .println ("jlink " + Arrays .stream (args ).collect (Collectors .joining (" " )));
175
- PrintWriter pw = new PrintWriter (System .out );
176
- int rc = JLINK_TOOL .run (pw , pw , args );
177
- assertTrue (rc == 0 );
238
+ private JlinkOutput createImage (Path image , List <String > args , boolean success ) throws IOException {
239
+ System .out .println ("jlink " + args .stream ().collect (Collectors .joining (" " )));
240
+
241
+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
242
+ PrintWriter out = new PrintWriter (baos );
243
+ ByteArrayOutputStream berrOs = new ByteArrayOutputStream ();
244
+ PrintWriter err = new PrintWriter (berrOs );
245
+ int rc = JLINK_TOOL .run (out , err , args .toArray (String []::new ));
246
+ String stdOut = new String (baos .toByteArray ());
247
+ String stdErr = new String (berrOs .toByteArray ());
248
+ assertEquals (rc == 0 , success , String .format ("Output was: %nstdout: %s%nstderr: %s%n" , stdOut , stdErr ));
249
+ return new JlinkOutput (stdErr , stdOut );
178
250
}
251
+
252
+ private static record JlinkOutput (String stderr , String stdout ) {};
179
253
}
0 commit comments