1928N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
1928N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1928N/A *
1928N/A * This code is free software; you can redistribute it and/or modify it
1928N/A * under the terms of the GNU General Public License version 2 only, as
1928N/A * published by the Free Software Foundation.
1928N/A *
1928N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1928N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1928N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1928N/A * version 2 for more details (a copy is included in the LICENSE file that
1928N/A * accompanied this code).
1928N/A *
1928N/A * You should have received a copy of the GNU General Public License version
1928N/A * 2 along with this work; if not, write to the Free Software Foundation,
1928N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1928N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1928N/A */
1928N/A
1928N/A/**
1928N/A * @test
1928N/A * @bug 6899605
1928N/A * @summary Basic unit test for tracing framework with security manager
1928N/A * enabled
1928N/A */
1928N/A
1928N/Aimport com.sun.tracing.*;
1928N/Aimport java.lang.reflect.Method;
1928N/A
1928N/A@ProviderName("NamedProvider")
1928N/Ainterface BasicProvider extends Provider {
1928N/A void plainProbe();
1928N/A void probeWithArgs(int a, float f, String s, Long l);
1928N/A @ProbeName("namedProbe") void probeWithName();
1928N/A void overloadedProbe();
1928N/A void overloadedProbe(int i);
1928N/A}
1928N/A
1928N/Ainterface InvalidProvider extends Provider {
1928N/A int nonVoidProbe();
1928N/A}
1928N/A
1928N/Apublic class BasicWithSecurityMgr {
1928N/A
1928N/A public static ProviderFactory factory;
1928N/A public static BasicProvider bp;
1928N/A
1928N/A public static void main(String[] args) throws Exception {
1928N/A // enable security manager
1928N/A System.setSecurityManager(new SecurityManager());
1928N/A
1928N/A factory = ProviderFactory.getDefaultFactory();
1928N/A if (factory != null) {
1928N/A bp = factory.createProvider(BasicProvider.class);
1928N/A }
1928N/A
1928N/A testProviderFactory();
1928N/A testProbe();
1928N/A testProvider();
1928N/A }
1928N/A
1928N/A static void fail(String s) throws Exception {
1928N/A throw new Exception(s);
1928N/A }
1928N/A
1928N/A static void testProviderFactory() throws Exception {
1928N/A if (factory == null) {
1928N/A fail("ProviderFactory.getDefaultFactory: Did not create factory");
1928N/A }
1928N/A if (bp == null) {
1928N/A fail("ProviderFactory.createProvider: Did not create provider");
1928N/A }
1928N/A try {
1928N/A factory.createProvider(null);
1928N/A fail("ProviderFactory.createProvider: Did not throw NPE for null");
1928N/A } catch (NullPointerException e) {}
1928N/A
1928N/A try {
1928N/A factory.createProvider(InvalidProvider.class);
1928N/A fail("Factory.createProvider: Should error with non-void probes");
1928N/A } catch (IllegalArgumentException e) {}
1928N/A }
1928N/A
1928N/A public static void testProvider() throws Exception {
1928N/A
1928N/A // These just shouldn't throw any exeptions:
1928N/A bp.plainProbe();
1928N/A bp.probeWithArgs(42, (float)3.14, "spam", new Long(2L));
1928N/A bp.probeWithArgs(42, (float)3.14, null, null);
1928N/A bp.probeWithName();
1928N/A bp.overloadedProbe();
1928N/A bp.overloadedProbe(42);
1928N/A
1928N/A Method m = BasicProvider.class.getMethod("plainProbe");
1928N/A Probe p = bp.getProbe(m);
1928N/A if (p == null) {
1928N/A fail("Provider.getProbe: Did not return probe");
1928N/A }
1928N/A
1928N/A Method m2 = BasicWithSecurityMgr.class.getMethod("testProvider");
1928N/A p = bp.getProbe(m2);
1928N/A if (p != null) {
1928N/A fail("Provider.getProbe: Got probe with invalid spec");
1928N/A }
1928N/A
1928N/A bp.dispose();
1928N/A // These just shouldn't throw any exeptions:
1928N/A bp.plainProbe();
1928N/A bp.probeWithArgs(42, (float)3.14, "spam", new Long(2L));
1928N/A bp.probeWithArgs(42, (float)3.14, null, null);
1928N/A bp.probeWithName();
1928N/A bp.overloadedProbe();
1928N/A bp.overloadedProbe(42);
1928N/A
1928N/A if (bp.getProbe(m) != null) {
1928N/A fail("Provider.getProbe: Should return null after dispose()");
1928N/A }
1928N/A
1928N/A bp.dispose(); // just to make sure nothing bad happens
1928N/A }
1928N/A
1928N/A static void testProbe() throws Exception {
1928N/A Method m = BasicProvider.class.getMethod("plainProbe");
1928N/A Probe p = bp.getProbe(m);
1928N/A p.isEnabled(); // just make sure it doesn't do anything bad
1928N/A p.trigger();
1928N/A
1928N/A try {
1928N/A p.trigger(0);
1928N/A fail("Probe.trigger: too many arguments not caught");
1928N/A } catch (IllegalArgumentException e) {}
1928N/A
1928N/A p = bp.getProbe(BasicProvider.class.getMethod(
1928N/A "probeWithArgs", int.class, float.class, String.class, Long.class));
1928N/A try {
1928N/A p.trigger();
1928N/A fail("Probe.trigger: too few arguments not caught");
1928N/A } catch (IllegalArgumentException e) {}
1928N/A
1928N/A try {
1928N/A p.trigger((float)3.14, (float)3.14, "", new Long(0L));
1928N/A fail("Probe.trigger: wrong type primitive arguments not caught");
1928N/A } catch (IllegalArgumentException e) {}
1928N/A }
1928N/A}