/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License.
*/
/** Collects log messages. * * @author Jaroslav Tulach
*/ publicfinalclass Log extends Handler { /** the test that is currently running */ privatestatic NbTestCase current; /** last 40Kb of collected error messages */ privatestaticfinal StringBuffer messages = new StringBuffer (); /** initial length of messages */ privatestaticint initialMessages; /** stream to log to */ private Reference<PrintStream> log; /** logger we are assigned to */ private Logger logger;
/** Creates a new instance of Log */ public Log() {
}
/** Enables logging for given logger name and given severity. * Everything logged to the object is going to go to the returned * CharSequence object which can be used to check the content or * converted <code>toString</code>. * <p> * The logging stops when the returned object is garbage collected. * * @param loggerName the name to capture logging for * @param level the level of details one wants to get * @return character sequence which can be check or converted to string * @since 1.27
*/ publicstatic CharSequence enable(String loggerName, Level level) {
IL il = new IL(false); class MyPs extends PrintStream implements CharSequence { private ByteArrayOutputStream os;
public MyPs() { this(new ByteArrayOutputStream());
}
private MyPs(ByteArrayOutputStream arr) { super(arr);
os = arr;
}
public CharSequence subSequence(int start, int end) { return toString().subSequence(start, end);
}
@Override public String toString() { return os.toString();
}
}
Logger l = Logger.getLogger(loggerName); if (l.getLevel() == null || l.getLevel().intValue() > level.intValue()) {
l.setLevel(level);
}
MyPs ps = new MyPs();
Log log = new Log(l, ps);
log.setLevel(level);
l.addHandler(log); return ps;
}
/** * Can emulate the execution flow of multiple threads in a deterministic * way so it is easy to emulate race conditions or deadlocks just with * the use of additional log messages inserted into the code. * <p> * The best example showing usage of this method is real life test. * Read <a href="https://github.com/apache/netbeans/tree/master/harness/nbjunit/test/unit/src/org/netbeans/junit/FlowControlTest.java">FlowControlTest.java</a> to know everything * about the expected usage of this method. * <p> * The method does listen on output send to a logger <code>listenTo</code> * by various threads and either suspends them or wake them up trying * as best as it can to mimic the log output described in <code>order</code>. * Of course, this may not always be possible, so there is the <code>timeout</code> * value which specifies the maximum time a thread can be suspended while * waiting for a single message. The information about the internal behaviour * of the controlFlow method can be send to <code>reportTo</code> logger, * if provided, so in case of failure one can analyse what went wrong. * <p> * The format of the order is a set of lines like: * <pre> * THREAD:name_of_the_thread MSG:message_to_expect * </pre> * which define the order saying that at this time a thread with a given name * is expected to send given message. Both the name of the thread and * the message are regular expressions so one can shorten them by using <code>.*</code> * or any other trick. Btw. the format of the <code>order</code> is similar * to the one logged by the {@link Log#enable} or {@link NbTestCase#logLevel} methods, * so when one gets a test failure with enabled logging, * it is enough to just delete the unnecessary messages, replace too specific * texts like <code>@574904</code> with <code>.*</code> and the order is * ready for use. * * @param listenTo the logger to listen to and guide the execution according to messages sent to it * @param reportTo the logger to report internal state to or <code>null</code> if the logging is not needed * @param order the string describing the expected execution order of threads * @param timeout the maximal wait time of each thread on given message, zero if the waiting shall be infinite * * @author Jaroslav Tulach, invented during year 2005 * @since 1.28
*/ publicstaticvoid controlFlow(Logger listenTo, Logger reportTo, String order, int timeout) {
ControlFlow.registerSwitches(listenTo, reportTo, order, timeout);
}
/** Starts to listen on given log and collect parameters of messages that * were send to it. This is supposed to be called at the beginning of a test, * to get messages from the programs that use * <a href="https://netbeans.apache.org/wiki/FitnessViaTimersCounter">timers/counters</a> * infrastructure. At the end one should call {@link #assertInstances}. * * * @param log logger to listen on, if null, it uses the standard timers/counters one * @param msg name of messages to collect, if null, all messages will be recorded * @param level level of messages to record * @since 1.44
*/ publicstaticvoid enableInstances(Logger log, String msg, Level level) { if (log == null) {
log = Logger.getLogger("TIMER"); // NOI18N
}
/** Assert to verify that all collected instances via {@link #enableInstances} * can disappear. Uses {@link NbTestCase#assertGC} on each of them. * * @param msg message to display in case of potential failure
*/ publicstaticvoid assertInstances(String msg) {
InstancesHandler.assertGC(msg);
}
/** Assert to verify that all properly named instances collected via {@link #enableInstances} * can disappear. Uses {@link NbTestCase#assertGC} on each of them. * * @param msg message to display in case of potential failure * @param names list of names of instances to test for and verify that they disappear * @since 1.53
*/ publicstaticvoid assertInstances(String msg, String... names) {
InstancesHandler.assertGC(msg, names);
}
staticvoid configure(Level lev, String root, NbTestCase current) {
IL il = new IL(false);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.