Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/tracing/BasicWithSecurityMgr.java
38838 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 689960526* @summary Basic unit test for tracing framework with security manager27* enabled28*/2930import com.sun.tracing.*;31import java.lang.reflect.Method;3233@ProviderName("NamedProvider")34interface BasicProvider extends Provider {35void plainProbe();36void probeWithArgs(int a, float f, String s, Long l);37@ProbeName("namedProbe") void probeWithName();38void overloadedProbe();39void overloadedProbe(int i);40}4142interface InvalidProvider extends Provider {43int nonVoidProbe();44}4546public class BasicWithSecurityMgr {4748public static ProviderFactory factory;49public static BasicProvider bp;5051public static void main(String[] args) throws Exception {52// enable security manager53System.setSecurityManager(new SecurityManager());5455factory = ProviderFactory.getDefaultFactory();56if (factory != null) {57bp = factory.createProvider(BasicProvider.class);58}5960testProviderFactory();61testProbe();62testProvider();63}6465static void fail(String s) throws Exception {66throw new Exception(s);67}6869static void testProviderFactory() throws Exception {70if (factory == null) {71fail("ProviderFactory.getDefaultFactory: Did not create factory");72}73if (bp == null) {74fail("ProviderFactory.createProvider: Did not create provider");75}76try {77factory.createProvider(null);78fail("ProviderFactory.createProvider: Did not throw NPE for null");79} catch (NullPointerException e) {}8081try {82factory.createProvider(InvalidProvider.class);83fail("Factory.createProvider: Should error with non-void probes");84} catch (IllegalArgumentException e) {}85}8687public static void testProvider() throws Exception {8889// These just shouldn't throw any exeptions:90bp.plainProbe();91bp.probeWithArgs(42, (float)3.14, "spam", new Long(2L));92bp.probeWithArgs(42, (float)3.14, null, null);93bp.probeWithName();94bp.overloadedProbe();95bp.overloadedProbe(42);9697Method m = BasicProvider.class.getMethod("plainProbe");98Probe p = bp.getProbe(m);99if (p == null) {100fail("Provider.getProbe: Did not return probe");101}102103Method m2 = BasicWithSecurityMgr.class.getMethod("testProvider");104p = bp.getProbe(m2);105if (p != null) {106fail("Provider.getProbe: Got probe with invalid spec");107}108109bp.dispose();110// These just shouldn't throw any exeptions:111bp.plainProbe();112bp.probeWithArgs(42, (float)3.14, "spam", new Long(2L));113bp.probeWithArgs(42, (float)3.14, null, null);114bp.probeWithName();115bp.overloadedProbe();116bp.overloadedProbe(42);117118if (bp.getProbe(m) != null) {119fail("Provider.getProbe: Should return null after dispose()");120}121122bp.dispose(); // just to make sure nothing bad happens123}124125static void testProbe() throws Exception {126Method m = BasicProvider.class.getMethod("plainProbe");127Probe p = bp.getProbe(m);128p.isEnabled(); // just make sure it doesn't do anything bad129p.trigger();130131try {132p.trigger(0);133fail("Probe.trigger: too many arguments not caught");134} catch (IllegalArgumentException e) {}135136p = bp.getProbe(BasicProvider.class.getMethod(137"probeWithArgs", int.class, float.class, String.class, Long.class));138try {139p.trigger();140fail("Probe.trigger: too few arguments not caught");141} catch (IllegalArgumentException e) {}142143try {144p.trigger((float)3.14, (float)3.14, "", new Long(0L));145fail("Probe.trigger: wrong type primitive arguments not caught");146} catch (IllegalArgumentException e) {}147}148}149150151