1 /*
   2  * Copyright (c) 2008, 2010, 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 6537506
  27  * @ignore 6962535
  28  * @summary Basic unit test for tracing framework
  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 BasicFunctionality {
  48 
  49     public static ProviderFactory factory;
  50     public static BasicProvider bp;
  51 
  52     public static void main(String[] args) throws Exception {
  53 
  54         factory = ProviderFactory.getDefaultFactory();
  55         if (factory != null) {
  56             bp = factory.createProvider(BasicProvider.class);
  57         }
  58 
  59         testProviderFactory();
  60         testProbe();
  61         testProvider();
  62     }
  63 
  64     static void fail(String s) throws Exception {
  65         throw new Exception(s);
  66     }
  67 
  68     static void testProviderFactory() throws Exception {
  69         if (factory == null) {
  70             fail("ProviderFactory.getDefaultFactory: Did not create factory");
  71         }
  72         if (bp == null) {
  73             fail("ProviderFactory.createProvider: Did not create provider");
  74         }
  75         try {
  76             factory.createProvider(null);
  77             fail("ProviderFactory.createProvider: Did not throw NPE for null");
  78         } catch (NullPointerException e) {}
  79 
  80        try {
  81            factory.createProvider(InvalidProvider.class);
  82            fail("Factory.createProvider: Should error with non-void probes");
  83        } catch (IllegalArgumentException e) {}
  84     }
  85 
  86     public static void testProvider() throws Exception {
  87 
  88        // These just shouldn't throw any exeptions:
  89        bp.plainProbe();
  90        bp.probeWithArgs(42, (float)3.14, "spam", new Long(2L));
  91        bp.probeWithArgs(42, (float)3.14, null, null);
  92        bp.probeWithName();
  93        bp.overloadedProbe();
  94        bp.overloadedProbe(42);
  95 
  96        Method m = BasicProvider.class.getMethod("plainProbe");
  97        Probe p = bp.getProbe(m);
  98        if (p == null) {
  99            fail("Provider.getProbe: Did not return probe");
 100        }
 101 
 102        Method m2 = BasicFunctionality.class.getMethod("testProvider");
 103        p = bp.getProbe(m2);
 104        if (p != null) {
 105            fail("Provider.getProbe: Got probe with invalid spec");
 106        }
 107 
 108        bp.dispose();
 109        // These just shouldn't throw any exeptions:
 110        bp.plainProbe();
 111        bp.probeWithArgs(42, (float)3.14, "spam", new Long(2L));
 112        bp.probeWithArgs(42, (float)3.14, null, null);
 113        bp.probeWithName();
 114        bp.overloadedProbe();
 115        bp.overloadedProbe(42);
 116 
 117        if (bp.getProbe(m) != null) {
 118            fail("Provider.getProbe: Should return null after dispose()");
 119        }
 120 
 121        bp.dispose(); // just to make sure nothing bad happens
 122     }
 123 
 124     static void testProbe() throws Exception {
 125        Method m = BasicProvider.class.getMethod("plainProbe");
 126        Probe p = bp.getProbe(m);
 127        p.isEnabled(); // just make sure it doesn't do anything bad
 128        p.trigger();
 129 
 130        try {
 131          p.trigger(0);
 132          fail("Probe.trigger: too many arguments not caught");
 133        } catch (IllegalArgumentException e) {}
 134 
 135        p = bp.getProbe(BasicProvider.class.getMethod(
 136            "probeWithArgs", int.class, float.class, String.class, Long.class));
 137        try {
 138          p.trigger();
 139          fail("Probe.trigger: too few arguments not caught");
 140        } catch (IllegalArgumentException e) {}
 141 
 142        try {
 143          p.trigger((float)3.14, (float)3.14, "", new Long(0L));
 144          fail("Probe.trigger: wrong type primitive arguments not caught");
 145        } catch (IllegalArgumentException e) {}
 146     }
 147 }