AddModuleUsesAndProvidesTest.java revision 12735:afedee84773e
1/* 2 * Copyright (c) 2016, 2017, 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 MyPackage; 25 26/** 27 * @test 28 * @summary Basic test for JVMTI AddModuleUses and AddModuleProvides 29 * @build java.base/java.lang.TestProvider 30 * java.base/jdk.internal.test.TestProviderImpl 31 * @compile AddModuleUsesAndProvidesTest.java 32 * @run main/othervm/native -agentlib:AddModuleUsesAndProvidesTest MyPackage.AddModuleUsesAndProvidesTest 33 */ 34 35import java.io.PrintStream; 36import java.lang.TestProvider; 37 38public class AddModuleUsesAndProvidesTest { 39 40 static { 41 try { 42 System.loadLibrary("AddModuleUsesAndProvidesTest"); 43 } catch (UnsatisfiedLinkError ule) { 44 System.err.println("Could not load AddModuleUsesAndProvidesTest library"); 45 System.err.println("java.library.path: " 46 + System.getProperty("java.library.path")); 47 throw ule; 48 } 49 } 50 51 native static int checkUses(Module baseModule, Class<?> service); 52 native static int checkProvides(Module baseModule, Class<?> service, Class<?> serviceImpl); 53 54 public static void main(String args[]) throws Exception { 55 Module baseModule = Object.class.getModule(); 56 Class<?> service = TestProvider.class; 57 Class<?> serviceImpl = Class.forName("jdk.internal.test.TestProviderImpl"); 58 59 System.out.println("\n*** Checks for JVMTI AddModuleUses ***\n"); 60 61 int status = checkUses(baseModule, service); 62 if (status != 0) { 63 throw new RuntimeException("Non-zero status returned from the agent: " + status); 64 } 65 66 System.out.println("\n*** Checks for JVMTI AddModuleProvides ***\n"); 67 68 System.out.println("Check #PC1:"); 69 if (TestProvider.providers().iterator().hasNext()) { 70 throw new RuntimeException("Check #PC1: Unexpectedly service is provided"); 71 } 72 73 status = checkProvides(baseModule, service, serviceImpl); 74 if (status != 0) { 75 throw new RuntimeException("Non-zero status returned from the agent: " + status); 76 } 77 78 System.out.println("Check #PC3:"); 79 if (!TestProvider.providers().iterator().hasNext()) { 80 throw new RuntimeException("Check #PC3: Unexpectedly service is not provided"); 81 } 82 } 83} 84