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