1 /*
   2  * Copyright (c) 2016, 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 package jdk.jfr.api.metadata.settingdescriptor;
  26 
  27 import java.nio.file.Path;
  28 import java.nio.file.Paths;
  29 import java.util.Comparator;
  30 
  31 import jdk.jfr.Description;
  32 import jdk.jfr.EventType;
  33 import jdk.jfr.Label;
  34 import jdk.jfr.Name;
  35 import jdk.jfr.Recording;
  36 import jdk.jfr.SettingDefinition;
  37 import jdk.jfr.SettingDescriptor;
  38 import jdk.jfr.Timespan;
  39 import jdk.jfr.consumer.RecordingFile;
  40 import jdk.test.lib.jfr.Events;
  41 
  42 final class CustomEvent extends BaseEvent {
  43 
  44     public static final String DESCRIPTION_OF_AN_ANNOTATED_METHOD = "Description of an annotated method";
  45     public static final String ANNOTATED_METHOD = "Annotated Method";
  46 
  47     // should be shadowed by built-in setting threshold
  48     @SettingDefinition
  49     boolean threshold(PlainSetting p) {
  50         return false;
  51     }
  52 
  53     @SettingDefinition
  54     private boolean plain(PlainSetting s) {
  55         return true;
  56     }
  57 
  58     @SettingDefinition
  59     protected boolean annotatedType(AnnotatedSetting s) {
  60         return true;
  61     }
  62 
  63     @SettingDefinition
  64     @Name("newName")
  65     @Label(ANNOTATED_METHOD)
  66     @Description(DESCRIPTION_OF_AN_ANNOTATED_METHOD)
  67     @Timespan(Timespan.NANOSECONDS)
  68     public boolean whatever(AnnotatedSetting s) {
  69         return true;
  70     }
  71 
  72     @SettingDefinition
  73     boolean overridden(PlainSetting s) {
  74         return true;
  75     }
  76 
  77     public static void assertOnDisk(Comparator<SettingDescriptor> c) throws Exception {
  78         EventType in = EventType.getEventType(CustomEvent.class);
  79         Path p = Paths.get("custom.jfr");
  80         try (Recording r = new Recording()) {
  81             r.start();
  82             r.stop();
  83             r.dump(p);
  84         }
  85         try (RecordingFile f = new RecordingFile(p)) {
  86             for (EventType out : f.readEventTypes()) {
  87                 if (out.getName().equals(CustomEvent.class.getName())) {
  88                     if (out.getSettingDescriptors().size() != in.getSettingDescriptors().size()) {
  89                         throw new Exception("Number of settings doesn't match");
  90                     }
  91                     for (SettingDescriptor os : out.getSettingDescriptors()) {
  92                         SettingDescriptor is = Events.getSetting(in, os.getName());
  93                         if (c.compare(os, is) != 0) {
  94                             throw new Exception("Setting with name " + is.getName() + " doesn't match");
  95                         }
  96                     }
  97                     return;
  98                 }
  99             }
 100         }
 101         throw new Exception("Could not event type with name " + CustomEvent.class.getName());
 102     }
 103 }