jdk/test/com/sun/tracing/BasicWithSecurityMgr.java
changeset 29037 8a11fed0d1a0
parent 28961 868dc757eab6
parent 29036 63d5aec20b48
child 29038 c2058b635c17
equal deleted inserted replaced
28961:868dc757eab6 29037:8a11fed0d1a0
     1 /*
       
     2  * Copyright (c) 2008, 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 6899605
       
    27  * @summary Basic unit test for tracing framework with security manager
       
    28  *          enabled
       
    29  */
       
    30 
       
    31 import com.sun.tracing.*;
       
    32 import java.lang.reflect.Method;
       
    33 
       
    34 @ProviderName("NamedProvider")
       
    35 interface BasicProvider extends Provider {
       
    36     void plainProbe();
       
    37     void probeWithArgs(int a, float f, String s, Long l);
       
    38     @ProbeName("namedProbe") void probeWithName();
       
    39     void overloadedProbe();
       
    40     void overloadedProbe(int i);
       
    41 }
       
    42 
       
    43 interface InvalidProvider extends Provider {
       
    44     int nonVoidProbe();
       
    45 }
       
    46 
       
    47 public class BasicWithSecurityMgr {
       
    48 
       
    49     public static ProviderFactory factory;
       
    50     public static BasicProvider bp;
       
    51 
       
    52     public static void main(String[] args) throws Exception {
       
    53         // enable security manager
       
    54         System.setSecurityManager(new SecurityManager());
       
    55 
       
    56         factory = ProviderFactory.getDefaultFactory();
       
    57         if (factory != null) {
       
    58             bp = factory.createProvider(BasicProvider.class);
       
    59         }
       
    60 
       
    61         testProviderFactory();
       
    62         testProbe();
       
    63         testProvider();
       
    64     }
       
    65 
       
    66     static void fail(String s) throws Exception {
       
    67         throw new Exception(s);
       
    68     }
       
    69 
       
    70     static void testProviderFactory() throws Exception {
       
    71         if (factory == null) {
       
    72             fail("ProviderFactory.getDefaultFactory: Did not create factory");
       
    73         }
       
    74         if (bp == null) {
       
    75             fail("ProviderFactory.createProvider: Did not create provider");
       
    76         }
       
    77         try {
       
    78             factory.createProvider(null);
       
    79             fail("ProviderFactory.createProvider: Did not throw NPE for null");
       
    80         } catch (NullPointerException e) {}
       
    81 
       
    82        try {
       
    83            factory.createProvider(InvalidProvider.class);
       
    84            fail("Factory.createProvider: Should error with non-void probes");
       
    85        } catch (IllegalArgumentException e) {}
       
    86     }
       
    87 
       
    88     public static void testProvider() throws Exception {
       
    89 
       
    90        // These just shouldn't throw any exeptions:
       
    91        bp.plainProbe();
       
    92        bp.probeWithArgs(42, (float)3.14, "spam", new Long(2L));
       
    93        bp.probeWithArgs(42, (float)3.14, null, null);
       
    94        bp.probeWithName();
       
    95        bp.overloadedProbe();
       
    96        bp.overloadedProbe(42);
       
    97 
       
    98        Method m = BasicProvider.class.getMethod("plainProbe");
       
    99        Probe p = bp.getProbe(m);
       
   100        if (p == null) {
       
   101            fail("Provider.getProbe: Did not return probe");
       
   102        }
       
   103 
       
   104        Method m2 = BasicWithSecurityMgr.class.getMethod("testProvider");
       
   105        p = bp.getProbe(m2);
       
   106        if (p != null) {
       
   107            fail("Provider.getProbe: Got probe with invalid spec");
       
   108        }
       
   109 
       
   110        bp.dispose();
       
   111        // These just shouldn't throw any exeptions:
       
   112        bp.plainProbe();
       
   113        bp.probeWithArgs(42, (float)3.14, "spam", new Long(2L));
       
   114        bp.probeWithArgs(42, (float)3.14, null, null);
       
   115        bp.probeWithName();
       
   116        bp.overloadedProbe();
       
   117        bp.overloadedProbe(42);
       
   118 
       
   119        if (bp.getProbe(m) != null) {
       
   120            fail("Provider.getProbe: Should return null after dispose()");
       
   121        }
       
   122 
       
   123        bp.dispose(); // just to make sure nothing bad happens
       
   124     }
       
   125 
       
   126     static void testProbe() throws Exception {
       
   127        Method m = BasicProvider.class.getMethod("plainProbe");
       
   128        Probe p = bp.getProbe(m);
       
   129        p.isEnabled(); // just make sure it doesn't do anything bad
       
   130        p.trigger();
       
   131 
       
   132        try {
       
   133          p.trigger(0);
       
   134          fail("Probe.trigger: too many arguments not caught");
       
   135        } catch (IllegalArgumentException e) {}
       
   136 
       
   137        p = bp.getProbe(BasicProvider.class.getMethod(
       
   138            "probeWithArgs", int.class, float.class, String.class, Long.class));
       
   139        try {
       
   140          p.trigger();
       
   141          fail("Probe.trigger: too few arguments not caught");
       
   142        } catch (IllegalArgumentException e) {}
       
   143 
       
   144        try {
       
   145          p.trigger((float)3.14, (float)3.14, "", new Long(0L));
       
   146          fail("Probe.trigger: wrong type primitive arguments not caught");
       
   147        } catch (IllegalArgumentException e) {}
       
   148     }
       
   149 }