summaryrefslogtreecommitdiff
path: root/qpid/java/junit-toolkit/src/main/org/apache/qpid/junit/extensions/TKTestRunner.java
blob: 671d33feedb6c69f4ba3c22c2ac650fb253228d3 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
 *
 * 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.
 *
 */
package org.apache.qpid.junit.extensions;

import junit.framework.Test;
import junit.framework.TestResult;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import org.apache.qpid.junit.extensions.listeners.CSVTestListener;
import org.apache.qpid.junit.extensions.listeners.ConsoleTestListener;
import org.apache.qpid.junit.extensions.listeners.XMLTestListener;
import org.apache.qpid.junit.extensions.util.CommandLineParser;
import org.apache.qpid.junit.extensions.util.MathUtils;
import org.apache.qpid.junit.extensions.util.ParsedProperties;
import org.apache.qpid.junit.extensions.util.TestContextProperties;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * TKTestRunner extends {@link junit.textui.TestRunner} with the ability to run tests multiple times, to execute a test
 * simultaneously using many threads, to put a delay between test runs and adds support for tests that take integer
 * parameters that can be 'stepped' through on multiple test runs. These features can be accessed by using this class
 * as an entry point and passing command line arguments to specify which features to use:
 *
 * <pre>
 * -w ms       The number of milliseconds between invocations of test cases.
 * -c pattern  The number of tests to run concurrently.
 * -r num      The number of times to repeat each test.
 * -d duration The length of time to run the tests for.
 * -t name     The name of the test case to execute.
 * -s pattern  The size parameter to run tests with.
 * -o dir      The name of the directory to output test timings to.
 * --csv       Output test results in CSV format.
 * --xml       Output test results in XML format.
 * </pre>
 *
 * <p/>This command line may also have trailing 'name=value' parameters added to it. All of these values are added
 * to the test context properties and passed to the test, which can access them by name.
 *
 * <p/>The pattern arguments are of the form [lowest(: ...)(: highest)](:sample=s)(:exp), where round brackets
 * enclose optional values. Using this pattern form it is possible to specify a single value, a range of values divided
 * into s samples, a range of values divided into s samples but distributed exponentially, or a fixed set of samples.
 *
 * <p/>The duration arguments are of the form (dD)(hH)(mM)(sS), where round brackets enclose optional values. At least
 * one of the optional values must be present.
 *
 * <p/>When specifying optional test parameters on the command line, in 'name=value' format, it is also possible to use
 * the format 'name=[value1:value2:value3:...]', to specify multiple values for a parameter. All permutations of all
 * parameters with multiple values will be created and tested. If the values are numerical, it is also possible to use
 * the sequence generation patterns instead of fully specifying all of the values.
 *
 * <p/>Here are some examples:
 *
 * <p/><table>
 * <tr><td><pre> -c [10:20:30:40:50] </pre><td> Runs the test with 10,20,...,50 threads.
 * <tr><td><pre> -s [1:100]:samples=10 </pre>
 *     <td> Runs the test with ten different size parameters evenly spaced between 1 and 100.
 * <tr><td><pre> -s [1:1000000]:samples=10:exp </pre>
 *     <td> Runs the test with ten different size parameters exponentially spaced between 1 and 1000000.
 * <tr><td><pre> -r 10 </pre><td> Runs each test ten times.
 * <tr><td><pre> -d 10H </pre><td> Runs the test repeatedly for 10 hours.
 * <tr><td><pre> -d 1M, -r 10 </pre>
 *     <td> Runs the test repeatedly for 1 minute but only takes a timing sample every 10 test runs.
 * <tr><td><pre> -r 10, -c [1:5:10:50], -s [100:1000:10000] </pre>
 *     <td> Runs 12 test cycles (4 concurrency samples * 3 size sample), with 10 repeats each. In total the test
 *          will be run 199 times (3 + 15 + 30 + 150)
 * <tr><td><pre> cache=true </pre><td> Passes the 'cache' parameter with value 'true' to the test.
 * <tr><td><pre> cache=[true:false] </pre><td> Runs the test with the 'cache' parameter set to 'true' and 'false'.
 * <tr><td><pre> cacheSize=[1000:1000000],samples=4,exp </pre>
 *     <td> Runs the test with the 'cache' parameter set to a series of exponentially increasing sizes.
 * </table>
 *
 * <p><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Create the test configuration specified by the command line parameters.
 * </table>
 *
 * @todo Verify that the output directory exists or can be created.
 *
 * @todo Verify that the specific named test case to execute exists.
 *
 * @todo Drop the delay parameter as it is being replaced by throttling.
 *
 * @todo Completely replace the test ui test runner, instead of having TKTestRunner inherit from it, its just not
 *       good code to extend.
 *
 * @author Rupert Smith
 */
public class TKTestRunner extends TestRunnerImprovedErrorHandling
{
    /** Used for debugging. */
    private static final Logger log = Logger.getLogger(TKTestRunner.class);

    /** Used for displaying information on the console. */
    // private static final Logger console = Logger.getLogger("CONSOLE." + TKTestRunner.class.getName());

    /** Used for generating the timestamp when naming output files. */
    protected static final DateFormat TIME_STAMP_FORMAT = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");

    /** Number of times to rerun the test. */
    protected Integer repetitions = 1;

    /** The length of time to run the tests for. */
    protected Long duration;

    /** Number of threads running the tests. */
    protected int[] threads;

    /** Delay in ms to wait between two test cases. */
    protected int delay = 0;

    /** The parameter values to pass to parameterized tests. */
    protected int[] params;

    /** Name of the single test case to execute. */
    protected String testCaseName = null;

    /** Name of the test class. */
    protected String testClassName = null;

    /** Name of the test run. */
    protected String testRunName = null;

    /** Directory to output XML reports into, if specified. */
    protected String reportDir = null;

    /** Flag that indicates the CSV results listener should be used to output results. */
    protected boolean csvResults;

    /** Flag that indiciates the XML results listener should be used to output results. */
    protected boolean xmlResults;

    /**
     * Holds the name of the class of the test currently being run. Ideally passed into the {@link #createTestResult}
     * method, but as the signature is already fixed for this, the current value gets pushed here as a member variable.
     */
    protected String currentTestClassName;

    /** Holds the test results object, which is reponsible for instrumenting tests/threads to record results. */
    protected TKTestResult result;

    /** Holds a list of factories for instantiating optional user specified test decorators. */
    protected List<TestDecoratorFactory> decoratorFactories;

    /**
     * Constructs a TKTestRunner using System.out for all the output.
     *
     * @param repetitions        The number of times to repeat the test, or test batch size.
     * @param duration           The length of time to run the tests for. -1 means no duration has been set.
     * @param threads            The concurrency levels to ramp up to.
     * @param delay              A delay in milliseconds between test runs.
     * @param params             The sets of 'size' parameters to pass to test.
     * @param testCaseName       The name of the test case to run.
     * @param reportDir          The directory to output the test results to.
     * @param runName            The name of the test run; used to name the output file.
     * @param csvResults         <tt>true</tt> if the CSV results listener should be attached.
     * @param xmlResults         <tt>true</tt> if the XML results listener should be attached.
     * @param decoratorFactories List of factories for user specified decorators.
     */
    public TKTestRunner(Integer repetitions, Long duration, int[] threads, int delay, int[] params, String testCaseName,
        String reportDir, String runName, boolean csvResults, boolean xmlResults,
        List<TestDecoratorFactory> decoratorFactories)
    {
        super(new NullResultPrinter(System.out));

        log.debug("public TKTestRunner(): called");

        // Keep all the test parameters.
        this.repetitions = repetitions;
        this.duration = duration;
        this.threads = threads;
        this.delay = delay;
        this.params = params;
        this.testCaseName = testCaseName;
        this.reportDir = reportDir;
        this.testRunName = runName;
        this.csvResults = csvResults;
        this.xmlResults = xmlResults;
        this.decoratorFactories = decoratorFactories;
    }

    /**
     * The entry point for the toolkit test runner.
     *
     * @param args The command line arguments.
     */
    public static void main(String[] args)
    {
        // Use the command line parser to evaluate the command line.
        CommandLineParser commandLine =
            new CommandLineParser(
                new String[][]
                {
                    { "w", "The number of milliseconds between invocations of test cases.", "ms", "false" },
                    { "c", "The number of tests to run concurrently.", "num", "false", MathUtils.SEQUENCE_REGEXP },
                    { "r", "The number of times to repeat each test.", "num", "false" },
                    { "d", "The length of time to run the tests for.", "duration", "false", MathUtils.DURATION_REGEXP },
                    { "f", "The maximum rate to call the tests at.", "frequency", "false", "^([1-9][0-9]*)/([1-9][0-9]*)$" },
                    { "s", "The size parameter to run tests with.", "size", "false", MathUtils.SEQUENCE_REGEXP },
                    { "t", "The name of the test case to execute.", "name", "false" },
                    { "o", "The name of the directory to output test timings to.", "dir", "false" },
                    { "n", "A name for this test run, used to name the output file.", "name", "true" },
                    {
                        "X:decorators", "A list of additional test decorators to wrap the tests in.",
                        "\"class.name[:class.name]*\"", "false"
                    },
                    { "1", "Test class.", "class", "true" },
                    { "-csv", "Output test results in CSV format.", null, "false" },
                    { "-xml", "Output test results in XML format.", null, "false" }
                });

        // Capture the command line arguments or display errors and correct usage and then exit.
        ParsedProperties options = null;

        try
        {
            options = new ParsedProperties(commandLine.parseCommandLine(args));
        }
        catch (IllegalArgumentException e)
        {
            System.out.println(commandLine.getErrors());
            System.out.println(commandLine.getUsage());
            System.exit(FAILURE_EXIT);
        }

        // Extract the command line options.
        Integer delay = options.getPropertyAsInteger("w");
        String threadsString = options.getProperty("c");
        Integer repetitions = options.getPropertyAsInteger("r");
        String durationString = options.getProperty("d");
        String paramsString = options.getProperty("s");
        String testCaseName = options.getProperty("t");
        String reportDir = options.getProperty("o");
        String testRunName = options.getProperty("n");
        String decorators = options.getProperty("X:decorators");
        String testClassName = options.getProperty("1");
        boolean csvResults = options.getPropertyAsBoolean("-csv");
        boolean xmlResults = options.getPropertyAsBoolean("-xml");

        int[] threads = (threadsString == null) ? null : MathUtils.parseSequence(threadsString);
        int[] params = (paramsString == null) ? null : MathUtils.parseSequence(paramsString);
        Long duration = (durationString == null) ? null : MathUtils.parseDuration(durationString);

        // The test run name defaults to the test class name unless a value was specified for it.
        testRunName = (testRunName == null) ? testClassName : testRunName;

        // Add all the command line options and trailing settings to test context properties. Tests may pick up
        // overridden values from there, and these values will be logged in the test results, for analysis and
        // to make tests repeatable.
        commandLine.addTrailingPairsToProperties(TestContextProperties.getInstance());
        commandLine.addOptionsToProperties(TestContextProperties.getInstance());

        // Create and start the test runner.
        try
        {
            // Create a list of test decorator factories for use specified decorators to be applied.
            List<TestDecoratorFactory> decoratorFactories = parseDecorators(decorators);

            TKTestRunner testRunner =
                new TKTestRunner(repetitions, duration, threads, (delay == null) ? 0 : delay, params, testCaseName,
                    reportDir, testRunName, csvResults, xmlResults, decoratorFactories);

            TestResult testResult = testRunner.start(testClassName);

            if (!testResult.wasSuccessful())
            {
                System.exit(FAILURE_EXIT);
            }
        }
        catch (Exception e)
        {
            System.err.println(e.getMessage());
            e.printStackTrace(new PrintStream(System.err));
            System.exit(EXCEPTION_EXIT);
        }
    }

    /**
     * Parses a list of test decorators, in the form "class.name[:class.name]*", and creates factories for those
     * TestDecorator classes , and returns a list of the factories. This list of factories will be in the same
     * order as specified in the string. The factories can be used to succesively wrap tests in layers of
     * decorators, as decorators themselves implement the 'Test' interface.
     *
     * <p/>If the string fails to parse, or if any of the decorators specified in it are cannot be loaded, or are not
     * TestDecorators, a runtime exception with a suitable error message will be thrown. The factories themselves
     * throw runtimes if the constructor method calls on the decorators fail.
     *
     * @param decorators The decorators list to be parsed.
     *
     * @return A list of instantiated decorators.
     */
    protected static List<TestDecoratorFactory> parseDecorators(String decorators)
    {
        List<TestDecoratorFactory> result = new LinkedList<TestDecoratorFactory>();
        String toParse = decorators;

        // Check that the decorators string is not null or empty, returning an empty list of decorator factories it
        // it is.
        if ((decorators == null) || "".equals(decorators))
        {
            return result;
        }

        // Strip any leading and trailing quotes from the string.
        if (toParse.charAt(0) == '\"')
        {
            toParse = toParse.substring(1, toParse.length() - 1);
        }

        if (toParse.charAt(toParse.length() - 1) == '\"')
        {
            toParse = toParse.substring(0, toParse.length() - 2);
        }

        // Instantiate all decorators.
        for (String decoratorClassName : toParse.split(":"))
        {
            try
            {
                Class decoratorClass = Class.forName(decoratorClassName);
                final Constructor decoratorConstructor = decoratorClass.getConstructor(WrappedSuiteTestDecorator.class);

                // Check that the decorator is an instance of WrappedSuiteTestDecorator.
                if (!WrappedSuiteTestDecorator.class.isAssignableFrom(decoratorClass))
                {
                    throw new RuntimeException("The decorator class " + decoratorClassName
                        + " is not a sub-class of WrappedSuiteTestDecorator, which it needs to be.");
                }

                result.add(new TestDecoratorFactory()
                    {
                        public WrappedSuiteTestDecorator decorateTest(Test test)
                        {
                            try
                            {
                                return (WrappedSuiteTestDecorator) decoratorConstructor.newInstance(test);
                            }
                            catch (InstantiationException e)
                            {
                                throw new RuntimeException(
                                    "The decorator class " + decoratorConstructor.getDeclaringClass().getName()
                                    + " cannot be instantiated.", e);
                            }
                            catch (IllegalAccessException e)
                            {
                                throw new RuntimeException(
                                    "The decorator class " + decoratorConstructor.getDeclaringClass().getName()
                                    + " does not have a publicly accessable constructor.", e);
                            }
                            catch (InvocationTargetException e)
                            {
                                throw new RuntimeException(
                                    "The decorator class " + decoratorConstructor.getDeclaringClass().getName()
                                    + " cannot be invoked.", e);
                            }
                        }
                    });
            }
            catch (ClassNotFoundException e)
            {
                throw new RuntimeException("The decorator class " + decoratorClassName + " could not be found.", e);
            }
            catch (NoSuchMethodException e)
            {
                throw new RuntimeException("The decorator class " + decoratorClassName
                    + " does not have a constructor that accepts a single 'WrappedSuiteTestDecorator' argument.", e);
            }
        }

        return result;
    }

    /**
     * TestDecoratorFactory is a factory for creating test decorators from tests.
     */
    protected interface TestDecoratorFactory
    {
        /**
         * Decorates the specified test with a new decorator.
         *
         * @param test The test to decorate.
         *
         * @return The decorated test.
         */
        public WrappedSuiteTestDecorator decorateTest(Test test);
    }

    /**
     * Runs a test or suite of tests, using the super class implemenation. This method wraps the test to be run
     * in any test decorators needed to add in the configured toolkits enhanced junit functionality.
     *
     * @param test The test to run.
     * @param wait Undocumented. Nothing in the JUnit javadocs to say what this is for.
     *
     * @return The results of the test run.
     */
    public TestResult doRun(Test test, boolean wait)
    {
        log.debug("public TestResult doRun(Test \"" + test + "\", boolean " + wait + "): called");

        // Wrap the tests in decorators for duration, scaling, repetition, parameterization etc.
        WrappedSuiteTestDecorator targetTest = decorateTests(test);

        // Delegate to the super method to run the decorated tests.
        log.debug("About to call super.doRun");

        TestResult result = super.doRun(targetTest, wait);
        log.debug("super.doRun returned.");

        /*if (result instanceof TKTestResult)
        {
            TKTestResult tkResult = (TKTestResult) result;

            tkResult.notifyEndBatch();
        }*/

        return result;
    }

    /**
     * Applies test decorators to the tests for parameterization, duration, scaling and repetition.
     *
     * @param test The test to decorat.
     *
     * @return The decorated test.
     */
    protected WrappedSuiteTestDecorator decorateTests(Test test)
    {
        log.debug("params = " + ((params == null) ? null : MathUtils.printArray(params)));
        log.debug("repetitions = " + repetitions);
        log.debug("threads = " + ((threads == null) ? null : MathUtils.printArray(threads)));
        log.debug("duration = " + duration);

        // Wrap all tests in the test suite with WrappedSuiteTestDecorators. This is quite ugly and a bit baffling,
        // but the reason it is done is because the JUnit implementation of TestDecorator has some bugs in it.
        WrappedSuiteTestDecorator targetTest = null;

        if (test instanceof TestSuite)
        {
            log.debug("targetTest is a TestSuite");

            TestSuite suite = (TestSuite) test;

            int numTests = suite.countTestCases();
            log.debug("There are " + numTests + " in the suite.");

            for (int i = 0; i < numTests; i++)
            {
                Test nextTest = suite.testAt(i);
                log.debug("suite.testAt(" + i + ") = " + nextTest);

                if (nextTest instanceof TimingControllerAware)
                {
                    log.debug("nextTest is TimingControllerAware");
                }

                if (nextTest instanceof TestThreadAware)
                {
                    log.debug("nextTest is TestThreadAware");
                }
            }

            targetTest = new WrappedSuiteTestDecorator(suite);
            log.debug("Wrapped with a WrappedSuiteTestDecorator.");
        }
        // If the test has already been wrapped, no need to do it again.
        else if (test instanceof WrappedSuiteTestDecorator)
        {
            targetTest = (WrappedSuiteTestDecorator) test;
        }

        // If size parameter values have been set, then wrap the test in an asymptotic test decorator.
        if (params != null)
        {
            targetTest = new AsymptoticTestDecorator(targetTest, params, (repetitions == null) ? 1 : repetitions);
            log.debug("Wrapped with asymptotic test decorator.");
            log.debug("targetTest = " + targetTest);
        }

        // If no size parameters are set but the repitions parameter is, then wrap the test in an asymptotic test decorator.
        else if ((repetitions != null) && (repetitions > 1))
        {
            targetTest = new AsymptoticTestDecorator(targetTest, new int[] { 1 }, repetitions);
            log.debug("Wrapped with asymptotic test decorator.");
            log.debug("targetTest = " + targetTest);
        }

        // Apply any optional user specified decorators.
        targetTest = applyOptionalUserDecorators(targetTest);

        // If a test run duration has been set then wrap the test in a duration test decorator. This will wrap on
        // top of size, repeat or concurrency wrappings already applied.
        if (duration != null)
        {
            DurationTestDecorator durationTest = new DurationTestDecorator(targetTest, duration);
            targetTest = durationTest;

            log.debug("Wrapped with duration test decorator.");
            log.debug("targetTest = " + targetTest);

            registerShutdownHook(durationTest);
        }

        // ParameterVariationTestDecorator...

        // If a test thread concurrency level is set then wrap the test in a scaled test decorator. This will wrap on
        // top of size scaling or repetition wrappings.
        ScaledTestDecorator scaledDecorator;

        if ((threads != null) && ((threads.length > 1) || (MathUtils.maxInArray(threads) > 1)))
        {
            scaledDecorator = new ScaledTestDecorator(targetTest, threads);
            targetTest = scaledDecorator;
            log.debug("Wrapped with scaled test decorator.");
            log.debug("targetTest = " + targetTest);
        }
        else
        {
            scaledDecorator = new ScaledTestDecorator(targetTest, new int[] { 1 });
            targetTest = scaledDecorator;
            log.debug("Wrapped with scaled test decorator with default of 1 thread.");
            log.debug("targetTest = " + targetTest);
        }

        // Register the scaled test decorators shutdown hook.
        registerShutdownHook(scaledDecorator);

        return targetTest;
    }

    /**
     * If there were any user specified test decorators on the command line, this method instantiates them and wraps
     * the test in them, from inner-most to outer-most in the order in which the decorators were supplied on the
     * command line.
     *
     * @param targetTest The test to wrap.
     *
     * @return A wrapped test.
     */
    protected WrappedSuiteTestDecorator applyOptionalUserDecorators(WrappedSuiteTestDecorator targetTest)
    {
        // If there are user defined test decorators apply them in order now.
        for (TestDecoratorFactory factory : decoratorFactories)
        {
            targetTest = factory.decorateTest(targetTest);
        }

        return targetTest;
    }

    /**
     * Creates the TestResult object to be used for test runs. See {@link TKTestResult} for more information and the
     * enhanced test result class that this uses.
     *
     * @return An instance of the enhanced test result object, {@link TKTestResult}.
     */
    protected TestResult createTestResult()
    {
        log.debug("protected TestResult createTestResult(): called");

        TKTestResult result = new TKTestResult(delay, testCaseName);

        // Check if a directory to output reports to has been specified and attach test listeners if so.
        if (reportDir != null)
        {
            // Create the report directory if it does not already exist.
            File reportDirFile = new File(reportDir);

            if (!reportDirFile.exists())
            {
                reportDirFile.mkdir();
            }

            // Create the results file (make the name of this configurable as a command line parameter).
            Writer timingsWriter;

            // Always set up a console feedback listener.
            ConsoleTestListener feedbackListener = new ConsoleTestListener();
            result.addListener(feedbackListener);
            result.addTKTestListener(feedbackListener);

            // Set up an XML results listener to output the timings to the results file, if requested on the command line.
            if (xmlResults)
            {
                try
                {
                    File timingsFile = new File(reportDirFile, "TEST-" + currentTestClassName + ".xml");
                    timingsWriter = new BufferedWriter(new FileWriter(timingsFile), 20000);
                }
                catch (IOException e)
                {
                    throw new RuntimeException("Unable to create the log file to write test results to: " + e, e);
                }

                XMLTestListener listener = new XMLTestListener(timingsWriter, currentTestClassName);
                result.addListener(listener);
                result.addTKTestListener(listener);

                registerShutdownHook(listener);
            }

            // Set up an CSV results listener to output the timings to the results file, if requested on the command line.
            if (csvResults)
            {
                try
                {
                    File timingsFile =
                        new File(reportDirFile, testRunName + "-" + TIME_STAMP_FORMAT.format(new Date()) + "-timings.csv");
                    timingsWriter = new BufferedWriter(new FileWriter(timingsFile), 20000);
                }
                catch (IOException e)
                {
                    throw new RuntimeException("Unable to create the log file to write test results to: " + e, e);
                }

                CSVTestListener listener = new CSVTestListener(timingsWriter);
                result.addListener(listener);
                result.addTKTestListener(listener);

                // Register the results listeners shutdown hook to flush its data if the test framework is shutdown
                // prematurely.
                registerShutdownHook(listener);
            }

            // Register the results listeners shutdown hook to flush its data if the test framework is shutdown
            // prematurely.
            // registerShutdownHook(listener);

            // Record the start time of the batch.
            // result.notifyStartBatch();

            // At this point in time the test class has been instantiated, giving it an opportunity to read its parameters.
            // Inform any test listers of the test properties.
            result.notifyTestProperties(TestContextProperties.getAccessedProps());
        }

        return result;
    }

    /**
     * Registers the shutdown hook of a {@link ShutdownHookable}.
     *
     * @param hookable The hookable to register.
     */
    protected void registerShutdownHook(ShutdownHookable hookable)
    {
        Runtime.getRuntime().addShutdownHook(hookable.getShutdownHook());
    }

    /**
     * Initializes the test runner with the provided command line arguments and and starts the test run.
     *
     * @param testClassName The fully qualified name of the test class to run.
     *
     * @return The test results.
     *
     * @throws Exception Any exceptions from running the tests are allowed to fall through.
     */
    protected TestResult start(String testClassName) throws Exception
    {
        // Record the current test class, so that the test results can be output to a file incorporating this name.
        this.currentTestClassName = testClassName;

        // Delegate to the super method to run the tests.
        return super.start(new String[] { testClassName });
    }
}