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  * @library /test/lib
  46  * @run main/othervm jdk.jfr.api.metadata.valuedescriptor.TestSimpleTypes
  47  */
  48 public class TestSimpleTypes {
  49 
  50     public static void main(String[] args) throws Throwable {
  51         EventType type = EventType.getEventType(MyEvent.class);
  52 
  53         List<String> expectedStrings = new ArrayList<>();
  54         expectedStrings.add("desc=myByteDesc; label=null; name=myByte; typename=byte; contenttype=null");
  55         expectedStrings.add("desc=null; label=myShortLabel; name=myShort; typename=short; contenttype=null");
  56         expectedStrings.add("desc=myIntDesc; label=myIntLabel; name=myInt; typename=int; contenttype=null");
  57         expectedStrings.add("desc=null; label=null; name=myLongName; typename=long; contenttype=null");
  58         expectedStrings.add("desc=myCharDesc; label=myCharLabel; name=myCharName; typename=char; contenttype=null");
  59         expectedStrings.add("desc=null; label=null; name=myFloat; typename=float; contenttype=jdk.jfr.Percentage");
  60         expectedStrings.add("desc=null; label=null; name=myDouble; typename=double; contenttype=null");
  61         expectedStrings.add("desc=null; label=null; name=myBoolean; typename=boolean; contenttype=jdk.jfr.BooleanFlag");
  62         expectedStrings.add("desc=null; label=null; name=myString; typename=java.lang.String; contenttype=null");
  63         expectedStrings.add("desc=null; label=null; name=myClass; typename=java.lang.Class; contenttype=null");
  64         expectedStrings.add("desc=null; label=null; name=myThread; typename=java.lang.Thread; contenttype=null");
  65 
  66         List<Long> typeIds = new ArrayList<>();
  67         for (ValueDescriptor d : type.getFields()) {
  68             if (d.getName().startsWith("my")) {
  69                 String s = getCompareString(d);
  70                 System.out.println("got: " + s);
  71                 Asserts.assertTrue(expectedStrings.contains(s), "Wrong type string found");
  72                 expectedStrings.remove(s);
  73 
  74                 long typeId = d.getTypeId();
  75                 Asserts.assertFalse(typeIds.contains(typeId), "TypeIds not unique");
  76                 typeIds.add(typeId);
  77 
  78                 Asserts.assertFalse(d.isArray(), "ValueDescriptor should not be array");
  79             }
  80         }
  81 
  82         if (!expectedStrings.isEmpty()) {
  83             System.out.println("Missing expectedStrings:");
  84             for (String s : expectedStrings) {
  85                 System.out.println(s);
  86             }
  87             Asserts.fail("Not all strings found");
  88         }
  89     }
  90 
  91     private static String getCompareString(ValueDescriptor d) {
  92         return String.format("desc=%s; label=%s; name=%s; typename=%s; contenttype=%s",
  93                               d.getDescription(),
  94                               d.getLabel(),
  95                               d.getName(),
  96                               d.getTypeName(),
  97                               d.getContentType());
  98     }
  99 
 100     @SuppressWarnings("unused")
 101     private static class MyEvent extends Event {
 102 
 103         @Description("myByteDesc")
 104         public byte myByte;
 105 
 106         @Label("myShortLabel")
 107         public short myShort;
 108 
 109         @Label("myIntLabel")
 110         @Description("myIntDesc")
 111         public int myInt;
 112 
 113         @Name("myLongName")
 114         public long myLong;
 115 
 116         @Label("myCharLabel")
 117         @Description("myCharDesc")
 118         @Name("myCharName")
 119         protected char myChar;
 120 
 121         @Percentage
 122         protected float myFloat;
 123         protected double myDouble;
 124         @BooleanFlag
 125         private boolean myBoolean;
 126         private String myString;
 127         @SuppressWarnings("rawtypes")
 128         private Class myClass;
 129         private Thread myThread;
 130     }
 131 }