jdk

Форк
0
/
ListModuleDeps.java 
321 строка · 12.1 Кб
1
/*
2
 * Copyright (c) 2016, 2023, 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
 * @bug 8167057
27
 * @summary Tests --list-deps, --list-reduced-deps, --print-module-deps options
28
 * @modules java.logging
29
 *          java.xml
30
 *          jdk.compiler
31
 *          jdk.jdeps
32
 *          jdk.unsupported
33
 * @library ../lib
34
 * @build CompilerUtils JdepsRunner
35
 * @run testng ListModuleDeps
36
 */
37

38
import java.io.File;
39
import java.nio.file.Path;
40
import java.nio.file.Paths;
41
import java.util.Arrays;
42
import java.util.List;
43
import java.util.stream.Collectors;
44

45
import org.testng.annotations.BeforeTest;
46
import org.testng.annotations.DataProvider;
47
import org.testng.annotations.Test;
48

49
import static org.testng.Assert.assertEquals;
50
import static org.testng.Assert.assertTrue;
51

52
public class ListModuleDeps {
53
    private static final String TEST_SRC = System.getProperty("test.src");
54

55
    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
56
    private static final Path CLASSES_DIR = Paths.get("classes");
57
    private static final Path LIB_DIR = Paths.get("lib");
58
    private static final Path LIB2_DIR = Paths.get("lib2");
59

60
    private static final Path HI_CLASS =
61
        CLASSES_DIR.resolve("hi").resolve("Hi.class");
62
    private static final Path FOO_CLASS =
63
        CLASSES_DIR.resolve("z").resolve("Foo.class");
64
    private static final Path BAR_CLASS =
65
        CLASSES_DIR.resolve("z").resolve("Bar.class");
66
    private static final Path UNSAFE_CLASS =
67
        CLASSES_DIR.resolve("z").resolve("UseUnsafe.class");
68

69
    /**
70
     * Compiles classes used by the test
71
     */
72
    @BeforeTest
73
    public void compileAll() throws Exception {
74
        // compile library
75
        assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "lib2"), LIB2_DIR));
76
        assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "lib"), LIB_DIR, "-cp", LIB2_DIR.toString()));
77

78
        // simple program depends only on java.base
79
        assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "hi"), CLASSES_DIR));
80

81
        // compile classes in unnamed module
82
        assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "z"),
83
            CLASSES_DIR,
84
            "-cp", LIB_DIR.toString(),
85
            "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
86
            "--add-exports=java.base/sun.security.util=ALL-UNNAMED",
87
            "--add-exports=java.xml/jdk.xml.internal=ALL-UNNAMED"
88
        ));
89
    }
90

91
    @DataProvider(name = "jdkModules")
92
    public Object[][] jdkModules() {
93
        return new Object[][]{
94
            {"jdk.compiler", new String[]{
95
                                "java.base/jdk.internal.javac",
96
                                "java.base/jdk.internal.jmod",
97
                                "java.base/jdk.internal.misc",
98
                                "java.base/jdk.internal.module",
99
                                "java.base/sun.reflect.annotation",
100
                                "java.compiler",
101
                                "jdk.internal.opt/jdk.internal.opt",
102
                             }
103
            },
104
        };
105
    }
106

107
    @Test(dataProvider = "jdkModules")
108
    public void testJDKModule(String moduleName, String[] expected) {
109
        JdepsRunner jdeps = JdepsRunner.run(
110
            "--list-deps", "-m", moduleName
111
        );
112
        String[] output = Arrays.stream(jdeps.output())
113
                                .map(s -> s.trim())
114
                                .toArray(String[]::new);
115
        assertEquals(output, expected);
116
    }
117

118
    @Test(dataProvider = "listdeps")
119
    public void testListDeps(Path classes, String[] expected) {
120
        JdepsRunner jdeps = JdepsRunner.run(
121
            "--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),
122
            "--list-deps", classes.toString()
123
        );
124
        String[] output = Arrays.stream(jdeps.output())
125
                                .map(s -> s.trim())
126
                                .toArray(String[]::new);
127
        assertEquals(output, expected);
128
    }
129

130
    @Test(dataProvider = "reduceddeps")
131
    public void testListReducedDeps(Path classes, String[]  expected) {
132
        JdepsRunner jdeps = JdepsRunner.run(
133
            "--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),
134
            "--list-reduced-deps", classes.toString()
135
        );
136
        String[] output = Arrays.stream(jdeps.output())
137
                                .map(s -> s.trim())
138
                                .toArray(String[]::new);
139
        assertEquals(output, expected);
140
    }
141

142

143
    @DataProvider(name = "listdeps")
144
    public Object[][] listdeps() {
145
        return new Object[][] {
146
            { CLASSES_DIR,  new String[] {
147
                                "java.base/jdk.internal.misc",
148
                                "java.base/sun.security.util",
149
                                "java.logging",
150
                                "java.management",
151
                                "java.sql",
152
                                "java.xml/jdk.xml.internal",
153
                                "jdk.unsupported"
154
                            }
155
            },
156

157
            { HI_CLASS,     new String[] {
158
                                "java.base"
159
                            }
160
            },
161

162
            { FOO_CLASS,    new String[] {
163
                                "java.base",
164
                                "java.logging",
165
                                "java.management",
166
                                "java.sql",
167
                                "java.xml"
168
                            }
169
            },
170

171
            { BAR_CLASS,    new String[] {
172
                                "java.base/sun.security.util",
173
                                "java.xml/jdk.xml.internal",
174
                            }
175
            },
176

177
            { UNSAFE_CLASS, new String[] {
178
                                "java.base/jdk.internal.misc",
179
                                "jdk.unsupported"
180
                            }
181
            },
182
        };
183
    }
184

185
    @DataProvider(name = "reduceddeps")
186
    public Object[][] reduceddeps() {
187
        Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");
188

189
        return new Object[][] {
190
            { CLASSES_DIR,  new String[] {
191
                                "java.base/jdk.internal.misc",
192
                                "java.base/sun.security.util",
193
                                "java.management",
194
                                "java.sql",
195
                                "java.xml/jdk.xml.internal",
196
                                "jdk.unsupported"
197
                            }
198
            },
199

200
            { HI_CLASS,     new String[] {
201
                                "java.base"
202
                            }
203
            },
204

205
            { FOO_CLASS,    new String[] {
206
                                "java.base",
207
                                "java.management",
208
                                "java.sql"
209
                            }
210
            },
211

212
            { BAR_CLASS,    new String[] {
213
                                "java.base/sun.security.util",
214
                                "java.xml/jdk.xml.internal",
215
                            }
216
            },
217

218
            { UNSAFE_CLASS, new String[] {
219
                                "java.base/jdk.internal.misc",
220
                                "jdk.unsupported"
221
                            }
222
            },
223
        };
224
    }
225

226
    @Test(dataProvider = "moduledeps")
227
    public void testPrintModuleDeps(Path classes, String expected) {
228
        JdepsRunner jdeps = JdepsRunner.run(
229
            "--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),
230
            "--print-module-deps", classes.toString()
231
        );
232
        String output = Arrays.stream(jdeps.output())
233
            .map(s -> s.trim())
234
            .collect(Collectors.joining(","));
235
        assertEquals(output, expected);
236
    }
237

238

239
    @DataProvider(name = "moduledeps")
240
    public Object[][] moduledeps() {
241
        Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");
242

243
        return new Object[][] {
244
            // java.xml is an implied reads edge from java.sql
245
            { CLASSES_DIR,  "java.base,java.management,java.sql,jdk.unsupported"},
246
            { HI_CLASS,     "java.base"},
247
            { FOO_CLASS,    "java.base,java.management,java.sql"},
248
            { BAR_CLASS,    "java.base,java.xml"},
249
            { UNSAFE_CLASS, "java.base,jdk.unsupported"},
250
        };
251
    }
252

253
    @Test(dataProvider = "noRecursiveModuledeps")
254
    public void testNoRecursiveModuleDeps(Path classes, String expected) {
255
        JdepsRunner jdeps = JdepsRunner.run(
256
            "--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),
257
            "--print-module-deps", "--no-recursive", classes.toString()
258
        );
259
        String output = Arrays.stream(jdeps.output())
260
            .map(s -> s.trim())
261
            .collect(Collectors.joining(","));
262
        assertEquals(output, expected);
263
    }
264

265
    @DataProvider(name = "noRecursiveModuledeps")
266
    public Object[][] noRecursiveModuledeps() {
267
        Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");
268

269
        return new Object[][] {
270
            // java.xml is an implied reads edge from java.sql
271
            { CLASSES_DIR,  "java.base,java.sql,jdk.unsupported"},
272
            { HI_CLASS,     "java.base"},
273
            { FOO_CLASS,    "java.base,java.sql"},
274
            { BAR_CLASS,    "java.base,java.xml"},
275
            { UNSAFE_CLASS, "java.base,jdk.unsupported"},
276
        };
277
    }
278

279
    @DataProvider(name = "recursiveDeps")
280
    public Object[][] recursiveDeps() {
281
        return new Object[][] {
282
            {   // lib2 is classpath but not analyzed because lib.Lib is not present
283
                // but it is the only class depending on lib2.Lib2
284
                List.of("--list-deps", "--class-path", LIB2_DIR.toString(),
285
                        "--ignore-missing-deps", CLASSES_DIR.toString()),
286
                new String[] {
287
                    "java.base/jdk.internal.misc",
288
                    "java.base/sun.security.util",
289
                    "java.logging",
290
                    "java.sql",
291
                    "java.xml/jdk.xml.internal",
292
                    "jdk.unsupported"
293
                }
294
            },
295
            {   // lib2 is classpath but not analyzed because lib.Lib is not present
296
                // but it is the only class depending on lib2.Lib2
297
                List.of("--print-module-deps", "--class-path", LIB2_DIR.toString(),
298
                        "--ignore-missing-deps", CLASSES_DIR.toString()),
299
                new String[] {
300
                    "java.base,java.sql,jdk.unsupported"
301
                }
302
            },
303
            {   // Foo depends on lib.Lib which depends on lib2.Libs
304
                List.of("--print-module-deps",
305
                        "--ignore-missing-deps", FOO_CLASS.toString()),
306
                new String[] {
307
                    "java.base,java.sql"
308
                }
309
            },
310
        };
311
    }
312

313
    @Test(dataProvider = "recursiveDeps")
314
    public void testRecursiveDeps(List<String> options, String[] expected) {
315
        JdepsRunner jdeps = JdepsRunner.run(options.toArray(new String[0]));
316
        String[] output = Arrays.stream(jdeps.output())
317
                                .map(s -> s.trim())
318
                                .toArray(String[]::new);
319
        assertEquals(output, expected);
320
    }
321
}
322

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.