1 /* 2 * Copyright (c) 2015, 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 8049365 27 * @summary Tests the JDI and JDWP update for modules 28 * 29 * @modules jdk.jdi 30 * @run build TestScaffold VMConnection TargetListener TargetAdapter 31 * @run compile -g ModulesTest.java 32 * @run driver ModulesTest 33 */ 34 import com.sun.jdi.*; 35 import com.sun.jdi.event.*; 36 import com.sun.jdi.request.*; 37 38 import java.util.*; 39 40 /********** target program **********/ 41 42 class ModulesTarg { 43 static int[] intArray = new int[10]; 44 45 static { 46 // make sure our class loader "creates" int[] before tested 47 intArray[1] = 99; 48 } 49 50 public static void main(String[] args){ 51 System.out.println("Goodbye from ModulesTarg!"); 52 } 53 } 54 55 /********** test program **********/ 56 57 public class ModulesTest extends TestScaffold { 58 ReferenceType targetClass; 59 60 ModulesTest (String args[]) { 61 super(args); 62 } 63 64 public static void main(String[] args) throws Exception { 65 new ModulesTest(args).startTests(); 66 } 67 68 /********** test core **********/ 69 70 private void checkClassLoader(Module module, ClassLoaderReference loader) { 71 List<ReferenceType> definedClasses = loader.definedClasses(); 72 List<ReferenceType> visibleClasses = loader.visibleClasses(); 73 String moduleName = module.name(); 74 75 println(" loader defined classes count: " + definedClasses.size()); 76 for (ReferenceType rt: definedClasses) { 77 Module other = rt.module(); 78 if (!other.equals(module)) { 79 println("ModulesTest: broken class loader invariant for module: " + moduleName); 80 testFailed = true; 81 } 82 } 83 for (ReferenceType type: visibleClasses) { 84 if (!type.isPrepared()) continue; // skip unprepared classes 85 86 Module other = type.module(); 87 String otherName = other.name(); 88 ClassLoaderReference otherLoader = other.classLoader(); 89 if (other == null) { 90 testFailed = true; 91 println("ModulesTest: failed: module reference is null #2"); 92 } 93 if (otherName != null && !module.canRead(other)) { 94 println(" module: " + moduleName + " can not read the visible class module: " + otherName); 95 testFailed = true; 96 } 97 } 98 99 } 100 101 private void checkModule(Module module, Module other, int checkIdx) { 102 if (module == null) { 103 testFailed = true; 104 println("ModulesTest: failed: module reference is null #1"); 105 } 106 String name = module.name(); 107 println("\n--- Check #" + checkIdx); 108 println(" module name: " + name); 109 110 ClassLoaderReference loader = module.classLoader(); 111 println(" loader: " + loader); 112 113 if (loader != null) { 114 checkClassLoader(module, loader); 115 } 116 117 boolean cond = module.canRead(other); 118 println(" can read: " + cond); 119 } 120 121 protected void runTests() throws Exception { 122 /* 123 * Get to the top of main() to determine targetClass 124 */ 125 BreakpointEvent bpe = startToMain("ModulesTarg"); 126 targetClass = bpe.location().declaringType(); 127 128 int checkIdx = 0; 129 List<Module> modules = vm().allModules(); 130 Module other = targetClass.module(); 131 checkModule(other, other, checkIdx++); 132 133 for (Module module : modules) { 134 checkModule(module, other, checkIdx++); 135 other = module; 136 } 137 138 /* 139 * resume the target until end 140 */ 141 listenUntilVMDisconnect(); 142 143 /* 144 * deal with results of test 145 * if anything has called failure("foo") testFailed will be true 146 */ 147 if (!testFailed) { 148 println("ModulesTest: passed"); 149 } else { 150 throw new Exception("ModulesTest: failed"); 151 } 152 } 153 }