LoggerEnteringWithParams.java revision 15491:6f390eafc676
1/* 2 * Copyright (c) 2014, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.io.ByteArrayOutputStream; 25import java.util.Arrays; 26import java.util.List; 27import java.util.concurrent.CopyOnWriteArrayList; 28import java.util.logging.Handler; 29import java.util.logging.Level; 30import java.util.logging.LogRecord; 31import java.util.logging.Logger; 32import java.util.logging.SimpleFormatter; 33import java.util.logging.StreamHandler; 34 35/** 36 * @test 37 * @bug 8028788 38 * @summary tests that the message format string is correctly constructed 39 * by Logger.entering 40 * @run main/othervm LoggerEnteringWithParams 41 * @author daniel fuchs 42 */ 43public class LoggerEnteringWithParams { 44 45 static final Object[] data = { 46 "one", "two", "three", "four", "five", "six", "seven", "eight" 47 }; 48 49 static final class TestHandler extends Handler { 50 final List<LogRecord> events = new CopyOnWriteArrayList<>(); 51 final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 52 final StreamHandler wrapped = new StreamHandler(bytes, new SimpleFormatter()); 53 54 @Override 55 public void publish(LogRecord record) { 56 events.add(record); 57 wrapped.publish(record); 58 wrapped.flush(); 59 } 60 @Override 61 public void flush() { 62 wrapped.flush(); 63 } 64 @Override 65 public void close() throws SecurityException { 66 wrapped.close(); 67 } 68 69 @Override 70 public synchronized void setLevel(Level newLevel) throws SecurityException { 71 super.setLevel(newLevel); 72 wrapped.setLevel(newLevel); 73 } 74 75 public void reset() { 76 bytes.reset(); 77 events.clear(); 78 } 79 80 } 81 82 public static void main(String[] args) { 83 Logger logger = Logger.getLogger("some.logger"); 84 TestHandler handler = new TestHandler(); 85 logger.setUseParentHandlers(false); 86 handler.setLevel(Level.ALL); 87 logger.setLevel(Level.FINEST); 88 logger.addHandler(handler); 89 90 // Auto-detect the line termination used by SimpleFormatter - (CR vs CRLF) 91 logger.entering("test", "test"); 92 final String test = handler.bytes.toString(); 93 System.out.println(test); 94 final String lineEnd = test.substring(test.indexOf("ENTRY")+"ENTRY".length()); 95 System.out.print("Line termination is " + lineEnd.length() + " char(s) long:"); 96 for (int i=0; i<lineEnd.length(); i++) { 97 System.out.print(" code="+(int)lineEnd.charAt(i)); 98 } 99 System.out.println(); 100 handler.reset(); 101 102 for (int i=0 ; i<=data.length; i++) { 103 final StringBuilder b = new StringBuilder("ENTRY"); 104 final StringBuilder f = new StringBuilder("ENTRY"); 105 final Object[] params = new Object[i]; 106 for (int k=0; k<i; k++) { 107 params[k] = data[k]; 108 b.append(' ').append(String.valueOf(params[k])); 109 f.append(" {").append(String.valueOf(k)).append('}'); 110 } 111 112 final String expected = b.toString(); 113 final String format = f.toString(); 114 final String className = "class"+i; 115 final String methodName = "method"+i; 116 117 logger.entering(className, methodName, params); 118 final String logged = handler.bytes.toString(); 119 System.out.println("got:\n" + logged); 120 121 if (!logged.contains(className)) { 122 throw new RuntimeException("Marker for " + className 123 + " not found." 124 + "\n\tgot: <<\n" + logged + "\t >>"); 125 } 126 if (!logged.contains(methodName)) { 127 throw new RuntimeException("Marker for " + methodName 128 + " not found." 129 + "\n\tgot: <<\n" + logged + "\t >>"); 130 } 131 if (!logged.contains(expected)) { 132 throw new RuntimeException("Marker for parameters[size=" 133 + i + "] not found" 134 + "\n\tgot: <<\n" + logged + "\t >>"); 135 } 136 if (!logged.contains(expected+lineEnd)) { 137 throw new RuntimeException("Marker for parameters[size=" 138 + i + "] has extra characters" 139 + "\n\tgot: <<\n" + logged + "\t >>"); 140 } 141 142 LogRecord record = handler.events.remove(0); 143 if (!handler.events.isEmpty()) { 144 throw new RuntimeException("Handler has more log records: " 145 + handler.events.toString()); 146 } 147 if (!className.equals(record.getSourceClassName())) { 148 throw new RuntimeException("Unexpected class name in LogRecord." 149 + "\n\texpected: " + className 150 + "\n\tgot: " + record.getSourceClassName()); 151 } 152 if (!methodName.equals(record.getSourceMethodName())) { 153 throw new RuntimeException("Unexpected method name in LogRecord." 154 + "\n\texpected: " + methodName 155 + "\n\tgot: " + record.getSourceMethodName()); 156 } 157 if (!format.equals(record.getMessage())) { 158 throw new RuntimeException("Unexpected message format in LogRecord." 159 + "\n\texpected: " + format 160 + "\n\tgot: " + record.getMessage()); 161 } 162 if (!Arrays.deepEquals(params, record.getParameters())) { 163 throw new RuntimeException("Unexpected parameter array in LogRecord." 164 + "\n\texpected: " + Arrays.toString(params) 165 + "\n\tgot: " + Arrays.toString(record.getParameters())); 166 } 167 168 handler.reset(); 169 } 170 } 171 172} 173