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.jvm;
  27 
  28 import jdk.jfr.internal.JVM;
  29 import jdk.jfr.internal.Logger;
  30 import jdk.jfr.internal.LogTag;
  31 import jdk.jfr.internal.LogLevel;
  32 
  33 /*
  34  * @test TestLogImplementation
  35  * @key jfr
  36  * @library /test/lib
  37  * @modules jdk.jfr/jdk.jfr.internal
  38  * @run main/othervm jdk.jfr.jvm.TestLogImplementation
  39  */
  40 public class TestLogImplementation {
  41     private static LogLevel DEFAULT_TEST_LOG_LEVEL = LogLevel.ERROR;
  42     private static LogTag JFR_LOG_TAG = LogTag.JFR;
  43 
  44     private static void assertAllLevelsForTag(LogTag tag, String message) {
  45         for (LogLevel level : LogLevel.values()) {
  46             Logger.log(tag, level, message);
  47         }
  48     }
  49 
  50     private static void assertAllTagsForLevel(LogLevel level, String message) {
  51         for (LogTag tag : LogTag.values()) {
  52             Logger.log(tag, level, message);
  53         }
  54     }
  55 
  56     public static void main(String... args) {
  57         assertEmptyMessageOK();
  58         assertNullMessageOK();
  59         assertAllCharsOK();
  60         assertLargeMessageOK();
  61         assertFormatSpecifierOK();
  62         assertAllLevelOK();
  63         assertLogLevelUnderflow();
  64         assertLogLevelOverflow();
  65         assertLogTagUnderflow();
  66         assertLogTagOverflow();
  67     }
  68 
  69     private static void assertAllLevelOK() {
  70         assertAllLevelsForTag(JFR_LOG_TAG, "");
  71         assertAllTagsForLevel(DEFAULT_TEST_LOG_LEVEL, "");
  72     }
  73 
  74     private static void assertFormatSpecifierOK() {
  75         assertAllTagsForLevel(DEFAULT_TEST_LOG_LEVEL, "%s");
  76         assertAllTagsForLevel(DEFAULT_TEST_LOG_LEVEL, "%n");
  77         assertAllTagsForLevel(DEFAULT_TEST_LOG_LEVEL, "%h");
  78     }
  79 
  80     private static void assertLargeMessageOK() {
  81         StringBuilder large = new StringBuilder();
  82         for (int i = 0; i < 1000 * 1000; i++) {
  83             large.append('o');
  84         }
  85         Logger.log(JFR_LOG_TAG, DEFAULT_TEST_LOG_LEVEL, large.toString());
  86     }
  87 
  88     private static void assertAllCharsOK() {
  89         char c = Character.MIN_VALUE;
  90         StringBuilder large = new StringBuilder();
  91         int logSize = 0;
  92         for(int i = Character.MIN_VALUE; i< (int)Character.MAX_VALUE; i++, c++) {
  93             large.append(c);
  94             logSize++;
  95             if (logSize == 80) {
  96                 Logger.log(JFR_LOG_TAG, DEFAULT_TEST_LOG_LEVEL, large.toString());
  97                 large = new StringBuilder();
  98             }
  99         }
 100     }
 101 
 102     private static void assertEmptyMessageOK() {
 103         assertAllLevelsForTag(JFR_LOG_TAG, "");
 104     }
 105 
 106     private static void assertNullMessageOK() {
 107         assertAllLevelsForTag(JFR_LOG_TAG, (String)null);
 108     }
 109 
 110     private static void assertLogLevelUnderflow() {
 111         try {
 112             JVM.log(JFR_LOG_TAG.ordinal(), 0, (String)null);
 113         } catch (IllegalArgumentException ex) {
 114             // Expected
 115         }
 116     }
 117 
 118     private static void assertLogLevelOverflow() {
 119         try {
 120             JVM.log(JFR_LOG_TAG.ordinal(), LogLevel.ERROR.ordinal() + 1, (String)null);
 121         } catch (IllegalArgumentException ex) {
 122             // Expected
 123         }
 124     }
 125 
 126     private static void assertLogTagUnderflow() {
 127         try {
 128             JVM.log(JFR_LOG_TAG.ordinal() - 1, DEFAULT_TEST_LOG_LEVEL.ordinal(), (String)null);
 129         } catch (IllegalArgumentException ex) {
 130             // Expected
 131         }
 132     }
 133 
 134     // since the enum will grow with new entries, provoking an overflow is problematic
 135     // is there a built-in "current max" in a Java enum
 136     private static void assertLogTagOverflow() {
 137         try {
 138             JVM.log(10000, DEFAULT_TEST_LOG_LEVEL.ordinal(), (String)null);
 139         } catch (IllegalArgumentException ex) {
 140             // Expected
 141         }
 142     }
 143 }