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