1 /*
   2  * Copyright (c) 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.api.metadata.valuedescriptor;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 import jdk.jfr.Description;
  32 import jdk.jfr.Event;
  33 import jdk.jfr.EventType;
  34 import jdk.jfr.BooleanFlag;
  35 import jdk.jfr.Label;
  36 import jdk.jfr.Name;
  37 import jdk.jfr.Percentage;
  38 import jdk.jfr.ValueDescriptor;
  39 import jdk.test.lib.Asserts;
  40 
  41 /**
  42  * @test
  43  * @summary Test all basic types in ValueDescriptor.
  44  * @key jfr
  45  * 
  46  * @library /lib /
  47  * @run main/othervm jdk.jfr.api.metadata.valuedescriptor.TestSimpleTypes
  48  */
  49 public class TestSimpleTypes {
  50 
  51     public static void main(String[] args) throws Throwable {
  52         EventType type = EventType.getEventType(MyEvent.class);
  53 
  54         List<String> expectedStrings = new ArrayList<>();
  55         expectedStrings.add("desc=myByteDesc; label=null; name=myByte; typename=byte; contenttype=null");
  56         expectedStrings.add("desc=null; label=myShortLabel; name=myShort; typename=short; contenttype=null");
  57         expectedStrings.add("desc=myIntDesc; label=myIntLabel; name=myInt; typename=int; contenttype=null");
  58         expectedStrings.add("desc=null; label=null; name=myLongName; typename=long; contenttype=null");
  59         expectedStrings.add("desc=myCharDesc; label=myCharLabel; name=myCharName; typename=char; contenttype=null");
  60         expectedStrings.add("desc=null; label=null; name=myFloat; typename=float; contenttype=jdk.jfr.Percentage");
  61         expectedStrings.add("desc=null; label=null; name=myDouble; typename=double; contenttype=null");
  62         expectedStrings.add("desc=null; label=null; name=myBoolean; typename=boolean; contenttype=jdk.jfr.BooleanFlag");
  63         expectedStrings.add("desc=null; label=null; name=myString; typename=java.lang.String; contenttype=null");
  64         expectedStrings.add("desc=null; label=null; name=myClass; typename=java.lang.Class; contenttype=null");
  65         expectedStrings.add("desc=null; label=null; name=myThread; typename=java.lang.Thread; contenttype=null");
  66 
  67         List<Long> typeIds = new ArrayList<>();
  68         for (ValueDescriptor d : type.getFields()) {
  69             if (d.getName().startsWith("my")) {
  70                 String s = getCompareString(d);
  71                 System.out.println("got: " + s);
  72                 Asserts.assertTrue(expectedStrings.contains(s), "Wrong type string found");
  73                 expectedStrings.remove(s);
  74 
  75                 long typeId = d.getTypeId();
  76                 Asserts.assertFalse(typeIds.contains(typeId), "TypeIds not unique");
  77                 typeIds.add(typeId);
  78 
  79                 Asserts.assertFalse(d.isArray(), "ValueDescriptor should not be array");
  80             }
  81         }
  82 
  83         if (!expectedStrings.isEmpty()) {
  84             System.out.println("Missing expectedStrings:");
  85             for (String s : expectedStrings) {
  86                 System.out.println(s);
  87             }
  88             Asserts.fail("Not all strings found");
  89         }
  90     }
  91 
  92     private static String getCompareString(ValueDescriptor d) {
  93         return String.format("desc=%s; label=%s; name=%s; typename=%s; contenttype=%s",
  94                               d.getDescription(),
  95                               d.getLabel(),
  96                               d.getName(),
  97                               d.getTypeName(),
  98                               d.getContentType());
  99     }
 100 
 101     @SuppressWarnings("unused")
 102     private static class MyEvent extends Event {
 103 
 104         @Description("myByteDesc")
 105         public byte myByte;
 106 
 107         @Label("myShortLabel")
 108         public short myShort;
 109 
 110         @Label("myIntLabel")
 111         @Description("myIntDesc")
 112         public int myInt;
 113 
 114         @Name("myLongName")
 115         public long myLong;
 116 
 117         @Label("myCharLabel")
 118         @Description("myCharDesc")
 119         @Name("myCharName")
 120         protected char myChar;
 121 
 122         @Percentage
 123         protected float myFloat;
 124         protected double myDouble;
 125         @BooleanFlag
 126         private boolean myBoolean;
 127         private String myString;
 128         @SuppressWarnings("rawtypes")
 129         private Class myClass;
 130         private Thread myThread;
 131     }
 132 }