1/*
2 * Copyright (c) 2016, 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
24package jdk.test;
25
26import java.lang.module.ModuleDescriptor;
27import java.net.URI;
28import java.nio.file.FileSystem;
29import java.nio.file.FileSystems;
30import java.nio.file.Files;
31import java.nio.file.Path;
32import java.util.Arrays;
33import java.util.Collections;
34import java.util.HashMap;
35import java.util.List;
36import java.util.Map;
37import java.util.Optional;
38
39
40/*
41 * Main class to verify if ModuleDescriptor carries the correct version
42 */
43public class Main {
44    static final Map<String, String> nameToVersion = new HashMap<>();
45
46    // jdk.test.Main $count $module-name... $version...
47    public static void main(String... args) throws Exception {
48        int count = args.length > 0 ? Integer.valueOf(args[0]) : 0;
49        if (count < 1 || args.length != count*2+1) {
50            throw new IllegalArgumentException(Arrays.toString(args));
51        }
52
53        List<String> modules = List.of(Arrays.copyOfRange(args, 1, 1+count));
54        List<String> versions = List.of(Arrays.copyOfRange(args, 1+count, args.length));
55        for (int i=0; i < modules.size(); i++) {
56            System.out.format("module %s expects %s version%n",
57                              modules.get(i), versions.get(i));
58            nameToVersion.put(modules.get(i), versions.get(i));
59        }
60
61        FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
62                                                  Collections.emptyMap());
63        // check the module descriptor of a system module
64        for (int i=0; i < modules.size(); i++) {
65            String mn = modules.get(i);
66            Module module = ModuleLayer.boot().findModule(mn).orElseThrow(
67                () -> new RuntimeException(mn + " not found")
68            );
69
70            // check ModuleDescriptor from the run-time
71            validate(module.getDescriptor());
72
73            // check module-info.class in the image
74            Path path = fs.getPath("/", "modules", modules.get(i), "module-info.class");
75            validate(ModuleDescriptor.read(Files.newInputStream(path)));
76        }
77    }
78
79    static void validate(ModuleDescriptor descriptor) {
80        checkVersion(descriptor.name(), descriptor.version());
81        descriptor.requires()
82            .stream()
83            .filter(r -> !r.name().equals("java.base"))
84            .forEach(r -> checkVersion(r.name(), r.compiledVersion()));
85    }
86
87    static void checkVersion(String mn, Optional<ModuleDescriptor.Version> version) {
88        boolean matched;
89        String v = nameToVersion.get(mn);
90        if (version.isPresent()) {
91            matched = version.get().toString().equals(v);
92        } else {
93            // 0 indicate no version
94            matched = v.equals("0");
95        }
96
97        if (!matched) {
98            throw new RuntimeException(mn + " mismatched version " + version
99                + " expected: " + v);
100        }
101    }
102}
103