|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 | +import org.testng.annotations.Test; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.lang.module.ModuleDescriptor; |
| 29 | +import java.util.Set; |
| 30 | + |
| 31 | +import static org.testng.Assert.assertEquals; |
| 32 | +import static org.testng.Assert.assertNotSame; |
| 33 | + |
| 34 | +/** |
| 35 | + * @test |
| 36 | + * @bug 8275509 |
| 37 | + * @run testng ModuleDescriptorHashCodeTest |
| 38 | + * @run testng/othervm -Xshare:off ModuleDescriptorHashCodeTest |
| 39 | + * @summary Tests the ModuleDescriptor.hashCode() for boot layer modules |
| 40 | + */ |
| 41 | +public class ModuleDescriptorHashCodeTest { |
| 42 | + |
| 43 | + /** |
| 44 | + * Verifies that the ModuleDescriptor.hashCode() returned by a boot layer module is |
| 45 | + * the same as that returned by a ModuleDescriptor constructed from the ModuleDescriptor.Builder |
| 46 | + * for the same module. |
| 47 | + */ |
| 48 | + @Test |
| 49 | + public void testBootModuleDescriptor() throws Exception { |
| 50 | + Set<Module> bootModules = ModuleLayer.boot().modules(); |
| 51 | + for (Module bootModule : bootModules) { |
| 52 | + System.out.println("Testing module descriptor of boot module " + bootModule); |
| 53 | + ModuleDescriptor bootMD = bootModule.getDescriptor(); |
| 54 | + ModuleDescriptor mdFromBuilder = fromModuleInfoClass(bootModule); |
| 55 | + // verify that this object is indeed a different object instance than the boot module descriptor |
| 56 | + // to prevent any artificial passing of the test |
| 57 | + assertNotSame(mdFromBuilder, bootMD, "ModuleDescriptor loaded from boot layer and " + |
| 58 | + "one created from module-info.class unexpectedly returned the same instance"); |
| 59 | + assertEquals(mdFromBuilder.hashCode(), bootMD.hashCode(), |
| 60 | + "Unexpected ModuleDescriptor.hashCode() for " + mdFromBuilder); |
| 61 | + assertEquals(mdFromBuilder.compareTo(bootMD), 0, |
| 62 | + "Unexpected ModuleDescriptor.compareTo() for " + mdFromBuilder); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Returns a ModuleDescriptor parsed out of the module-info.class of the passed Module |
| 67 | + private static ModuleDescriptor fromModuleInfoClass(Module module) throws IOException { |
| 68 | + try (InputStream moduleInfo = module.getResourceAsStream("module-info.class")) { |
| 69 | + if (moduleInfo == null) { |
| 70 | + throw new RuntimeException("Could not locate module-info.class in " + module); |
| 71 | + } |
| 72 | + // internally calls ModuleDescriptor.Builder |
| 73 | + return ModuleDescriptor.read(moduleInfo); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments