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