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