This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 139399
Collapse All | Expand All

(-)a/nbjunit/apichanges.xml (+18 lines)
Lines 55-60 made subject to such option by the copyr Link Here
55
    <!-- ACTUAL CHANGES BEGIN HERE: -->
55
    <!-- ACTUAL CHANGES BEGIN HERE: -->
56
56
57
<changes>
57
<changes>
58
    <change id="CollectTimeData">
59
        <api name="nbjunit"/>
60
        <summary>Support for measuring time consumption</summary>
61
        <version major="1" minor="53"/>
62
        <date day="9" month="7" year="2008"/>
63
        <author login="pflaska"/>
64
        <compatibility addition="yes"/>
65
        <description>
66
            <p>
67
                Methods <code>enableTimer</code> and 
68
                <code>assertTime</code> added to <code>Log</code> class to
69
                simplify time measuring during test runs.
70
            </p>
71
        </description>
72
        <class package="org.netbeans.junit" name="Log"/>
73
        <issue number="139399"/>
74
    </change>
75
58
    <change id="NbModuleSuite.Configuration.reuseUserDir">
76
    <change id="NbModuleSuite.Configuration.reuseUserDir">
59
        <api name="nbjunit"/>
77
        <api name="nbjunit"/>
60
        <summary>NbModuleSuite.Configuration.reuseUserDir(boolean reuse)</summary>
78
        <summary>NbModuleSuite.Configuration.reuseUserDir(boolean reuse)</summary>
(-)a/nbjunit/manifest.mf (-1 / +1 lines)
Lines 1-5 Manifest-Version: 1.0 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.nbjunit/1
2
OpenIDE-Module: org.netbeans.modules.nbjunit/1
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/junit/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/junit/Bundle.properties
4
OpenIDE-Module-Specification-Version: 1.52
4
OpenIDE-Module-Specification-Version: 1.53
5
5
(-)a/nbjunit/src/org/netbeans/junit/Log.java (+84 lines)
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
}

Return to bug 139399