Lines 51-56
import java.text.MessageFormat;
Link Here
|
51 |
import java.text.MessageFormat; |
51 |
import java.text.MessageFormat; |
52 |
import java.util.ArrayList; |
52 |
import java.util.ArrayList; |
53 |
import java.util.Collections; |
53 |
import java.util.Collections; |
|
|
54 |
import java.util.HashMap; |
54 |
import java.util.List; |
55 |
import java.util.List; |
55 |
import java.util.Map; |
56 |
import java.util.Map; |
56 |
import java.util.MissingResourceException; |
57 |
import java.util.MissingResourceException; |
Lines 213-218
public final class Log extends Handler {
Link Here
|
213 |
} |
214 |
} |
214 |
} |
215 |
} |
215 |
|
216 |
|
|
|
217 |
/** Starts to listen on given log and collect parameters of messages that |
218 |
* were send to it. This is supposed to be called at the begining of a test, |
219 |
* to get messages from the programs that use |
220 |
* <a href="http://wiki.netbeans.org/wiki/view/FitnessViaTimersCounters">timers/counters</a> |
221 |
* infrastructure. At the end one should call {@link #assertTime}. |
222 |
* |
223 |
* |
224 |
* @param log logger to listen on, if null, it uses the standard timers/counters one |
225 |
* @param msg name of messages to collect, if null, all messages will be recorded |
226 |
* @param level level of messages to record |
227 |
* @since 1.53 |
228 |
*/ |
229 |
public static void enableTimer(Logger log, String msg, Level level) { |
230 |
if (log == null) { |
231 |
log = Logger.getLogger("TIMER"); // NOI18N |
232 |
} |
233 |
|
234 |
log.addHandler(new TimerHandler(null, level)); |
235 |
|
236 |
if (log.getLevel() == null || log.getLevel().intValue() > level.intValue()) { |
237 |
log.setLevel(level); |
238 |
} |
239 |
} |
240 |
|
216 |
/** Assert to verify that all collected instances via {@link #enableInstances} |
241 |
/** Assert to verify that all collected instances via {@link #enableInstances} |
217 |
* can disappear. Uses {@link NbTestCase#assertGC} on each of them. |
242 |
* can disappear. Uses {@link NbTestCase#assertGC} on each of them. |
218 |
* |
243 |
* |
Lines 222-227
public final class Log extends Handler {
Link Here
|
222 |
InstancesHandler.assertGC(msg); |
247 |
InstancesHandler.assertGC(msg); |
223 |
} |
248 |
} |
224 |
|
249 |
|
|
|
250 |
/** |
251 |
* Assert to verify that running time is in limit. |
252 |
* |
253 |
* @param msg message to display in case of potential failure |
254 |
* @param key measurement identifier |
255 |
* @param limit time limit in ms |
256 |
*/ |
257 |
public static void assertTime(String msg, Object key, long limit) { |
258 |
TimerHandler.assertTime(msg, key, limit); |
259 |
} |
225 |
|
260 |
|
226 |
|
261 |
|
227 |
static void configure(Level lev, NbTestCase current) { |
262 |
static void configure(Level lev, NbTestCase current) { |
Lines 417-420
public final class Log extends Handler {
Link Here
|
417 |
} |
452 |
} |
418 |
|
453 |
|
419 |
} // end of InstancesHandler |
454 |
} // end of InstancesHandler |
|
|
455 |
|
456 |
private static class TimerHandler extends Handler { |
457 |
public static Map<String, Object[]> data; |
458 |
|
459 |
private TimerHandler(String msg, Level level) { |
460 |
setLevel(level); |
461 |
data = new HashMap<String, Object[]>(); |
462 |
} |
463 |
|
464 |
@Override |
465 |
public void publish(LogRecord record) { |
466 |
data.put(record.getMessage(), record.getParameters()); |
467 |
} |
468 |
|
469 |
private static void assertTime(String msg, Object key, long millis) { |
470 |
if (data.containsKey(key)) { |
471 |
Object[] result = data.get(key); |
472 |
} else { |
473 |
boolean found = false; |
474 |
for (Object[] rec : data.values()) { |
475 |
for (Object item : rec) { |
476 |
if (!found && item.equals(key)) { |
477 |
found = true; |
478 |
} else { |
479 |
if (found) { |
480 |
Long val = (Long) item; |
481 |
if (val > millis) { |
482 |
Assert.fail("Limit exceed. " + msg + " (" + item + " > " + millis + ")"); |
483 |
data.clear(); |
484 |
return; |
485 |
} |
486 |
found = false; |
487 |
} |
488 |
} |
489 |
} |
490 |
} |
491 |
} |
492 |
data.clear(); |
493 |
} // end of TimerHandler |
494 |
|
495 |
@Override |
496 |
public void close() throws SecurityException { |
497 |
} |
498 |
|
499 |
@Override |
500 |
public void flush() { |
501 |
} |
502 |
|
503 |
} |
420 |
} |
504 |
} |