1 /*
   2  * Copyright (c) 2015, 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.annotations;
  27 
  28 import jdk.jfr.DataAmount;
  29 import jdk.jfr.Event;
  30 import jdk.jfr.Frequency;
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.StackTrace;
  33 import jdk.jfr.consumer.RecordedEvent;
  34 import jdk.test.lib.Asserts;
  35 import jdk.test.lib.jfr.Events;
  36 
  37 /**
  38  * @test
  39  * @key jfr
  40  * @summary Check that event values are properly formatted and sanity check
  41  *              that extreme values don't throws exceptions
  42  * @library /lib /
  43  * @run main/othervm jdk.jfr.api.metadata.annotations.TestFormatMissingValue
  44  */
  45 public class TestFormatMissingValue {
  46 
  47     @StackTrace(false)
  48     static class MultiContentTypeEvent extends Event {
  49         @DataAmount(DataAmount.BYTES)
  50         @Frequency
  51         long a = Long.MIN_VALUE;
  52 
  53         @DataAmount(DataAmount.BYTES)
  54         @Frequency
  55         long b = Long.MAX_VALUE;
  56 
  57         @DataAmount(DataAmount.BYTES)
  58         @Frequency
  59         int c = Integer.MIN_VALUE;
  60 
  61         @DataAmount(DataAmount.BYTES)
  62         @Frequency
  63         int d = Integer.MAX_VALUE;
  64 
  65         @DataAmount(DataAmount.BYTES)
  66         @Frequency
  67         double e = Double.NEGATIVE_INFINITY;
  68 
  69         @DataAmount(DataAmount.BYTES)
  70         @Frequency
  71         double f = Double.POSITIVE_INFINITY;
  72 
  73         @DataAmount(DataAmount.BYTES)
  74         @Frequency
  75         double g = Double.NaN;
  76 
  77         @DataAmount(DataAmount.BYTES)
  78         @Frequency
  79         float h = Float.NEGATIVE_INFINITY;
  80 
  81         @DataAmount(DataAmount.BYTES)
  82         @Frequency
  83         float i = Float.POSITIVE_INFINITY;
  84 
  85         @DataAmount(DataAmount.BYTES)
  86         @Frequency
  87         float j = Float.NaN;
  88     }
  89 
  90     public static void main(String[] args) throws Exception {
  91         try (Recording r = new Recording()) {
  92             r.start();
  93             MultiContentTypeEvent m = new MultiContentTypeEvent();
  94             m.commit();
  95             r.stop();
  96             for (RecordedEvent e : Events.fromRecording(r)) {
  97                 String t = e.toString();
  98                 assertContains(t, "a = N/A");
  99                 assertContains(t, "c = N/A");
 100                 assertContains(t, "e = N/A");
 101                 assertContains(t, "g = N/A");
 102                 assertContains(t, "h = N/A");
 103                 assertContains(t, "j = N/A");
 104 
 105                 assertNotContains(t, "b = N/A");
 106                 assertNotContains(t, "d = N/A");
 107                 assertNotContains(t, "f = N/A");
 108                 assertNotContains(t, "i = N/A");
 109             }
 110         }
 111     }
 112 
 113     private static void assertContains(String t, String text) {
 114         if (!t.contains(text)) {
 115             Asserts.fail("Expected '" + t + "' to contain text '" + text + "'");
 116         }
 117     }
 118 
 119     private static void assertNotContains(String t, String text) {
 120         if (t.contains(text)) {
 121             Asserts.fail("Found unexpected value '" + text + "'  in text '" + t + "'");
 122         }
 123     }
 124 }