File: ModulesTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (488 lines) | stat: -rw-r--r-- 18,499 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @modules java.scripting
 * @library modules /lib/testlibrary
 * @build bananascript/*
 * @build JarUtils
 * @compile classpath/pearscript/org/pear/PearScriptEngineFactory.java
 *          classpath/pearscript/org/pear/PearScript.java
 * @run testng/othervm ModulesTest
 * @summary Basic test for ServiceLoader with a provider deployed as a module.
 */

import java.io.File;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.ServiceLoader.Provider;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.script.ScriptEngineFactory;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import static org.testng.Assert.*;

/**
 * Basic test for ServiceLoader. The test make use of two service providers:
 * 1. BananaScriptEngine - a ScriptEngineFactory deployed as a module on the
 *    module path. It implementations a singleton via the public static
 *    provider method.
 * 2. PearScriptEngine - a ScriptEngineFactory deployed on the class path
 *    with a service configuration file.
 */

public class ModulesTest {

    // Copy the services configuration file for "pearscript" into place.
    @BeforeTest
    public void setup() throws Exception {
        Path src = Paths.get(System.getProperty("test.src"));
        Path classes = Paths.get(System.getProperty("test.classes"));
        String st = ScriptEngineFactory.class.getName();
        Path config = Paths.get("META-INF", "services", st);
        Path source = src.resolve("classpath").resolve("pearscript").resolve(config);
        Path target = classes.resolve(config);
        Files.createDirectories(target.getParent());
        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
    }

    /**
     * Basic test of iterator() to ensure that providers located as modules
     * and on the class path are found.
     */
    @Test
    public void testIterator() {
        ServiceLoader<ScriptEngineFactory> loader
            = ServiceLoader.load(ScriptEngineFactory.class);
        Set<String> names = collectAll(loader)
                .stream()
                .map(ScriptEngineFactory::getEngineName)
                .collect(Collectors.toSet());
        assertTrue(names.contains("BananaScriptEngine"));
        assertTrue(names.contains("PearScriptEngine"));
    }

    /**
     * Basic test of iterator() to test iteration order. Providers deployed
     * as named modules should be found before providers deployed on the class
     * path.
     */
    @Test
    public void testIteratorOrder() {
        ServiceLoader<ScriptEngineFactory> loader
            = ServiceLoader.load(ScriptEngineFactory.class);
        boolean foundUnnamed = false;
        for (ScriptEngineFactory factory : collectAll(loader)) {
            if (factory.getClass().getModule().isNamed()) {
                if (foundUnnamed) {
                    assertTrue(false, "Named module element after unnamed");
                }
            } else {
                foundUnnamed = true;
            }
        }
    }

    /**
     * Basic test of Provider::type
     */
    @Test
    public void testProviderType() {
        Set<String> types = ServiceLoader.load(ScriptEngineFactory.class)
                .stream()
                .map(Provider::type)
                .map(Class::getName)
                .collect(Collectors.toSet());
        assertTrue(types.contains("org.banana.BananaScriptEngineFactory"));
        assertTrue(types.contains("org.pear.PearScriptEngineFactory"));
    }

    /**
     * Basic test of Provider::get
     */
    @Test
    public void testProviderGet() {
        Set<String> names = ServiceLoader.load(ScriptEngineFactory.class)
                .stream()
                .map(Provider::get)
                .map(ScriptEngineFactory::getEngineName)
                .collect(Collectors.toSet());
        assertTrue(names.contains("BananaScriptEngine"));
        assertTrue(names.contains("PearScriptEngine"));
    }

    /**
     * Basic test of the public static provider method. BananaScriptEngine
     * defines a provider method that returns the same instance.
     */
    @Test
    public void testSingleton() {
        Optional<Provider<ScriptEngineFactory>> oprovider
            = ServiceLoader.load(ScriptEngineFactory.class)
                .stream()
                .filter(p -> p.type().getName().equals("org.banana.BananaScriptEngineFactory"))
                .findFirst();
        assertTrue(oprovider.isPresent());
        Provider<ScriptEngineFactory> provider = oprovider.get();

        // invoke Provider::get twice
        ScriptEngineFactory factory1 = provider.get();
        ScriptEngineFactory factory2 = provider.get();
        assertTrue(factory1 == factory2);
    }

    /**
     * Basic test of stream() to ensure that elements for providers in named
     * modules come before elements for providers in unnamed modules.
     */
    @Test
    public void testStreamOrder() {
        List<Class<?>> types = ServiceLoader.load(ScriptEngineFactory.class)
                .stream()
                .map(Provider::type)
                .collect(Collectors.toList());

        boolean foundUnnamed = false;
        for (Class<?> factoryClass : types) {
            if (factoryClass.getModule().isNamed()) {
                if (foundUnnamed) {
                    assertTrue(false, "Named module element after unnamed");
                }
            } else {
                foundUnnamed = true;
            }
        }
    }

    /**
     * Basic test of ServiceLoader.findFirst()
     */
    @Test
    public void testFindFirst() {
        Optional<ScriptEngineFactory> ofactory
            = ServiceLoader.load(ScriptEngineFactory.class).findFirst();
        assertTrue(ofactory.isPresent());
        ScriptEngineFactory factory = ofactory.get();
        assertTrue(factory.getClass().getModule().isNamed());

        class S { }
        assertFalse(ServiceLoader.load(S.class).findFirst().isPresent());
    }

    /**
     * Basic test ServiceLoader.load specifying the platform class loader.
     * The providers on the module path and class path should not be located.
     */
    @Test
    public void testWithPlatformClassLoader() {
        ClassLoader pcl = ClassLoader.getPlatformClassLoader();

        // iterator
        ServiceLoader<ScriptEngineFactory> loader
            = ServiceLoader.load(ScriptEngineFactory.class, pcl);
        Set<String> names = collectAll(loader)
                .stream()
                .map(ScriptEngineFactory::getEngineName)
                .collect(Collectors.toSet());
        assertFalse(names.contains("BananaScriptEngine"));
        assertFalse(names.contains("PearScriptEngine"));

        // stream
        names = ServiceLoader.load(ScriptEngineFactory.class, pcl)
                .stream()
                .map(Provider::get)
                .map(ScriptEngineFactory::getEngineName)
                .collect(Collectors.toSet());
        assertFalse(names.contains("BananaScriptEngine"));
        assertFalse(names.contains("PearScriptEngine"));
    }

    /**
     * Basic test of ServiceLoader.load where the service provider module is an
     * automatic module.
     */
    @Test
    public void testWithAutomaticModule() throws Exception {
        Path here = Paths.get("");
        Path jar = Files.createTempDirectory(here, "lib").resolve("pearscript.jar");
        Path classes = Paths.get(System.getProperty("test.classes"));

        JarUtils.createJarFile(jar, classes, "META-INF", "org");

        ModuleFinder finder = ModuleFinder.of(jar);
        ModuleLayer bootLayer = ModuleLayer.boot();
        Configuration parent = bootLayer.configuration();
        Configuration cf = parent.resolveAndBind(finder, ModuleFinder.of(), Set.of());
        assertTrue(cf.modules().size() == 1);

        ClassLoader scl = ClassLoader.getSystemClassLoader();
        ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
        assertTrue(layer.modules().size() == 1);

        ClassLoader loader = layer.findLoader("pearscript");
        ScriptEngineFactory factory;

        // load using the class loader as context
        factory = ServiceLoader.load(ScriptEngineFactory.class, loader)
                .findFirst()
                .orElse(null);
        assertNotNull(factory);
        assertTrue(factory.getClass().getClassLoader() == loader);

        // load using the layer as context
        factory = ServiceLoader.load(layer, ScriptEngineFactory.class)
                .findFirst()
                .orElse(null);
        assertNotNull(factory);
        assertTrue(factory.getClass().getClassLoader() == loader);
    }

    /**
     * Basic test of ServiceLoader.load, using the class loader for
     * a module in a custom layer as the context.
     */
    @Test
    public void testWithCustomLayer1() {
        ModuleLayer layer = createCustomLayer("bananascript");

        ClassLoader loader = layer.findLoader("bananascript");
        List<ScriptEngineFactory> providers
            = collectAll(ServiceLoader.load(ScriptEngineFactory.class, loader));

        // should have at least 2 x bananascript + pearscript
        assertTrue(providers.size() >= 3);

        // first element should be the provider in the custom layer
        ScriptEngineFactory factory = providers.get(0);
        assertTrue(factory.getClass().getClassLoader() == loader);
        assertTrue(factory.getClass().getModule().getLayer() == layer);
        assertTrue(factory.getEngineName().equals("BananaScriptEngine"));

        // remainder should be the boot layer
        providers.remove(0);
        Set<String> names = providers.stream()
                .map(ScriptEngineFactory::getEngineName)
                .collect(Collectors.toSet());
        assertTrue(names.contains("BananaScriptEngine"));
        assertTrue(names.contains("PearScriptEngine"));
    }

    /**
     * Basic test of ServiceLoader.load using a custom Layer as the context.
     */
    @Test
    public void testWithCustomLayer2() {
        ModuleLayer layer = createCustomLayer("bananascript");

        List<ScriptEngineFactory> factories
            = collectAll(ServiceLoader.load(layer, ScriptEngineFactory.class));

        // should have at least 2 x bananascript
        assertTrue(factories.size() >= 2);

        // first element should be the provider in the custom layer
        ScriptEngineFactory factory = factories.get(0);
        assertTrue(factory.getClass().getModule().getLayer() == layer);
        assertTrue(factory.getEngineName().equals("BananaScriptEngine"));

        // remainder should be the boot layer
        factories.remove(0);
        Set<String> names = factories.stream()
                .map(ScriptEngineFactory::getEngineName)
                .collect(Collectors.toSet());
        assertTrue(names.contains("BananaScriptEngine"));
        assertFalse(names.contains("PearScriptEngine"));
    }

    /**
     * Basic test of ServiceLoader.load with a tree of layers.
     *
     * Test scenario:
     * - boot layer contains "bananascript", maybe other script engines
     * - layer1, with boot layer as parent, contains "bananascript"
     * - layer2, with boot layer as parent, contains "bananascript"
     * - layer3, with layer1 ad layer as parents, contains "bananascript"
     *
     * ServiceLoader should locate all 4 script engine factories in DFS order.
     */
    @Test
    public void testWithCustomLayer3() {
        ModuleLayer bootLayer = ModuleLayer.boot();
        Configuration cf0 = bootLayer.configuration();

        // boot layer should contain "bananascript"
        List<ScriptEngineFactory> factories
            = collectAll(ServiceLoader.load(bootLayer, ScriptEngineFactory.class));
        int countInBootLayer = factories.size();
        assertTrue(countInBootLayer >= 1);
        assertTrue(factories.stream()
                .map(p -> p.getEngineName())
                .filter("BananaScriptEngine"::equals)
                .findAny()
                .isPresent());

        ClassLoader scl = ClassLoader.getSystemClassLoader();
        ModuleFinder finder = ModuleFinder.of(testModulePath());

        // layer1
        Configuration cf1 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
        ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
        assertTrue(layer1.modules().size() == 1);

        // layer2
        Configuration cf2 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
        ModuleLayer layer2 = bootLayer.defineModulesWithOneLoader(cf2, scl);
        assertTrue(layer2.modules().size() == 1);

        // layer3 with layer1 and layer2 as parents
        Configuration cf3 = Configuration.resolveAndBind(finder,
                List.of(cf1, cf2),
                ModuleFinder.of(),
                Set.of());
        ModuleLayer layer3
            = ModuleLayer.defineModulesWithOneLoader(cf3, List.of(layer1, layer2), scl).layer();
        assertTrue(layer3.modules().size() == 1);


        // class loaders
        ClassLoader loader1 = layer1.findLoader("bananascript");
        ClassLoader loader2 = layer2.findLoader("bananascript");
        ClassLoader loader3 = layer3.findLoader("bananascript");
        assertTrue(loader1 != loader2);
        assertTrue(loader1 != loader3);
        assertTrue(loader2 != loader3);

        // load all factories with layer3 as the context
        factories = collectAll(ServiceLoader.load(layer3, ScriptEngineFactory.class));
        int count = factories.size();
        assertTrue(count == countInBootLayer + 3);

        // the ordering should be layer3, layer1, boot layer, layer2

        ScriptEngineFactory factory = factories.get(0);
        assertTrue(factory.getClass().getModule().getLayer() == layer3);
        assertTrue(factory.getClass().getClassLoader() == loader3);
        assertTrue(factory.getEngineName().equals("BananaScriptEngine"));

        factory = factories.get(1);
        assertTrue(factory.getClass().getModule().getLayer() == layer1);
        assertTrue(factory.getClass().getClassLoader() == loader1);
        assertTrue(factory.getEngineName().equals("BananaScriptEngine"));

        // boot layer "bananascript" and maybe other factories
        int last = count -1;
        boolean found = false;
        for (int i=2; i<last; i++) {
            factory = factories.get(i);
            assertTrue(factory.getClass().getModule().getLayer() == bootLayer);
            if (factory.getEngineName().equals("BananaScriptEngine")) {
                assertFalse(found);
                found = true;
            }
        }
        assertTrue(found);

        factory = factories.get(last);
        assertTrue(factory.getClass().getModule().getLayer() == layer2);
        assertTrue(factory.getClass().getClassLoader() == loader2);
        assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
    }


    // -- nulls --

    @Test(expectedExceptions = { NullPointerException.class })
    public void testLoadNull1() {
        ServiceLoader.load(null);
    }

    @Test(expectedExceptions = { NullPointerException.class })
    public void testLoadNull2() {
        ServiceLoader.load((Class<?>) null, ClassLoader.getSystemClassLoader());
    }

    @Test(expectedExceptions = { NullPointerException.class })
    public void testLoadNull3() {
        class S { }
        ServiceLoader.load((ModuleLayer) null, S.class);
    }

    @Test(expectedExceptions = { NullPointerException.class })
    public void testLoadNull4() {
        ServiceLoader.load(ModuleLayer.empty(), null);
    }

    @Test(expectedExceptions = { NullPointerException.class })
    public void testLoadNull5() {
        ServiceLoader.loadInstalled(null);
    }

    /**
     * Create a custom layer by resolving the given module names. The modules
     * are located on the test module path ({@code ${test.module.path}}).
     */
    private ModuleLayer createCustomLayer(String... modules) {
        ModuleFinder finder = ModuleFinder.of(testModulePath());
        Set<String> roots = new HashSet<>();
        Collections.addAll(roots, modules);
        ModuleLayer bootLayer = ModuleLayer.boot();
        Configuration parent = bootLayer.configuration();
        Configuration cf = parent.resolve(finder, ModuleFinder.of(), roots);
        ClassLoader scl = ClassLoader.getSystemClassLoader();
        ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
        assertTrue(layer.modules().size() == 1);
        return layer;
    }

    private Path[] testModulePath() {
        String mp = System.getProperty("test.module.path");
        return Stream.of(mp.split(File.pathSeparator))
                .map(Paths::get)
                .toArray(Path[]::new);
    }

    private <E> List<E> collectAll(ServiceLoader<E> loader) {
        List<E> list = new ArrayList<>();
        Iterator<E> iterator = loader.iterator();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }
}