summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/BrokerLoggingTest.java
blob: 7969ffc059daa5509a55d1369e7a51eaaedc7318 (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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
/*
*
* 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.server.logging;

import junit.framework.AssertionFailedError;

import org.apache.qpid.server.BrokerOptions;
import org.apache.qpid.server.Main;
import org.apache.qpid.transport.ConnectionException;
import org.apache.qpid.util.LogMonitor;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.List;

/**
 * Broker Test Suite
 *
 * The Broker test suite validates that the follow log messages as specified in the Functional Specification.
 *
 * BRK-1001 : Startup : Version: <Version> Build: <Build>
 * BRK-1002 : Starting : Listening on <Transport> port <Port>
 * BRK-1003 : Shuting down : <Transport> port <Port>
 * BRK-1004 : Ready
 * BRK-1005 : Stopped
 * BRK-1006 : Using configuration : <path>
 * BRK-1007 : Using logging configuration : <path>
 *
 * These messages should only occur during startup. The tests need to verify the order of messages. In the case of the BRK-1002 and BRK-1003 the respective ports should only be available between the two log messages.
 */
public class BrokerLoggingTest extends AbstractTestLogging
{
    private static final String BRK_LOG_PREFIX = "BRK-";

    public void setUp() throws Exception
    {
        setLogMessagePrefix();

        // We either do this here or have a null check in tearDown.
        // As when this test is run against profiles other than java it will NPE
        _monitor = new LogMonitor(_outputFile);
        //We explicitly do not call super.setUp as starting up the broker is
        //part of the test case.
    }

    /**
     * Description:
     * On startup the broker must report the active configuration file. The
     * logging system must output this so that we can know what configuration
     * is being used for this broker instance.
     *
     * Input:
     * The value of -c specified on the command line.
     * Output:
     * <date> MESSAGE BRK-1006 : Using configuration : <config file>
     * Constraints:
     * This MUST BE the first BRK log message.
     *
     * Validation Steps:
     * 1. This is first BRK log message.
     * 2. The BRK ID is correct
     * 3. The config file is the full path to the file specified on
     * the commandline.
     *
     * @throws Exception caused by broker startup
     */
    public void testBrokerStartupConfiguration() throws Exception
    {
        String TESTID="BRK-1006";

        // This logging startup code only occurs when you run a Java broker,
        // that broker must be started via Main so not an InVM broker.
        if (isJavaBroker() && isExternalBroker())
        {
            startBroker();

            // Now we can create the monitor as _outputFile will now be defined
            _monitor = new LogMonitor(_outputFile);


            String configFilePath = _configFile.toString();

            // Ensure we wait for TESTID to be logged
            waitAndFindMatches(TESTID);

            List<String> results = waitAndFindMatches(BRK_LOG_PREFIX);
            try
            {
                // Validation

                assertTrue("BRKer message not logged", results.size() > 0);

                String log = getLogMessage(results, 0);

                //1
                validateMessageID(TESTID, log);

                //2
                results = findMatches(TESTID);
                assertEquals("More than one configuration message found.",
                             1, results.size());

                //3
                assertTrue("Config file details not correctly logged",
                           log.endsWith(configFilePath));
            }
            catch (AssertionFailedError afe)
            {
                dumpLogs(results, _monitor);

                throw afe;
            }
        }
    }

    /**
     * Description:
     * On startup the broker must report correctly report the log4j file in use. This is important as it can help diagnose why logging messages are not being reported.
     * Input:
     * No custom -l value should be provided on the command line so that the default value is correctly reported.
     * Output:
     *
     * <date> MESSAGE BRK-1007 : Using logging configuration : <$QPID_HOME>/etc/log4j.xml
     *
     * Validation Steps:
     *
     * 1. The BRK ID is correct
     * 2. This occurs before the BRK-1001 startup message.
     * 3. The log4j file is the full path to the file specified on the commandline.
     *
     * @throws Exception caused by broker startup
     */
    public void testBrokerStartupDefaultLog4j() throws Exception
    {
        // This logging startup code only occurs when you run a Java broker,
        // that broker must be started via Main so not an InVM broker.
        if (isJavaBroker() && isExternalBroker() && !isInternalBroker())
        {
            String TESTID = "BRK-1007";

            //Remove test Log4j config from the commandline
            _brokerCommand = _brokerCommand.substring(0, _brokerCommand.indexOf("-l"));

            startBroker();

            // Now we can create the monitor as _outputFile will now be defined
            _monitor = new LogMonitor(_outputFile);

            // Ensure broker has fully started up.
            getConnection();

            // Ensure we wait for TESTID to be logged
            waitAndFindMatches(TESTID);

            List<String> results = waitAndFindMatches(BRK_LOG_PREFIX);
            try
            {
                // Validation

                assertTrue("BRKer message not logged", results.size() > 0);

                boolean validation = false;
                for (String rawLog : results)
                {
                    // We don't care about messages after we have our log config
                    if (validation)
                    {
                        break;
                    }

                    String log = getLog(rawLog);

                    // Ensure we do not have a BRK-1001 message before
                    if (!getMessageID(log).equals(TESTID))
                    {
                        assertFalse(getMessageID(log).equals("BRK-1001"));
                        continue;
                    }

                    //1
                    validateMessageID(TESTID, log);

                    //2
                    //There will be 1 copy of this startup message (via SystemOut)
                    assertEquals("Unexpected log4j configuration message count.",
                                 1, findMatches(TESTID).size());

                    //3
                    String defaultLog4j = System.getProperty(QPID_HOME) + "/" + BrokerOptions.DEFAULT_LOG_CONFIG_FILE;
                    assertTrue("Log4j file(" + defaultLog4j + ") details not correctly logged:" + getMessageString(log),
                               getMessageString(log).endsWith(defaultLog4j));

                    validation = true;
                }

                assertTrue("Validation not performed: " + TESTID + " not logged", validation);
            }
            catch (AssertionFailedError afe)
            {
                dumpLogs(results, _monitor);

                throw afe;
            }
        }
    }

    /**
     * Description:
     * On startup the broker must report correctly report the log4j file in use. This is important as it can help diagnose why logging messages are not being reported. The broker must also be capable of correctly recognising the command line property to specify the custom logging configuration.
     * Input:
     * The value of -l specified on the command line.
     * Output:
     *
     * <date> MESSAGE BRK-1007 : Using logging configuration : <log4j file>
     *
     * Validation Steps:
     *
     * 1. The BRK ID is correct
     * 2. This should occur before the BRK-1001 : Startup message
     * 3. The log4j file is the full path to the file specified on the commandline.
     *
     * @throws Exception caused by broker startup
     */
    public void testBrokerStartupCustomLog4j() throws Exception
    {
        // This logging startup code only occurs when you run a Java broker
        if (isJavaBroker() && isExternalBroker())
        {
            // Get custom -l value used during testing for the broker startup
            String customLog4j = _brokerCommand.substring(_brokerCommand.indexOf("-l") + 2).trim();

            String TESTID = "BRK-1007";

            startBroker();

            // Now we can create the monitor as _outputFile will now be defined
            _monitor = new LogMonitor(_outputFile);


            // Ensure broker has fully started up.
            getConnection();

            // Ensure we wait for TESTID to be logged
            waitAndFindMatches(TESTID);

            List<String> results = waitAndFindMatches(BRK_LOG_PREFIX);
            try
            {
                // Validation

                assertTrue("BRKer message not logged", results.size() > 0);

                boolean validation = false;
                for (String rawLog : results)
                {
                    // We don't care about messages after we have our log config
                    if (validation)
                    {
                        break;
                    }
                    String log = getLog(rawLog);

                    // Ensure we do not have a BRK-1001 message before
                    if (!getMessageID(log).equals(TESTID))
                    {
                        assertFalse(getMessageID(log).equals("BRK-1001"));
                        continue;
                    }

                    //1
                    validateMessageID(TESTID, log);

                    //2
                    //There will be 1 copy of this startup message (via SystemOut)
                    assertEquals("Unexpected log4j configuration message count.",
                                 1, findMatches(TESTID).size());

                    //3
                    assertTrue("Log4j file details not correctly logged:" + getMessageString(log),
                               getMessageString(log).endsWith(customLog4j));

                    validation = true;
                }

                assertTrue("Validation not performed: " + TESTID + " not logged", validation);
            }
            catch (AssertionFailedError afe)
            {
                dumpLogs(results, _monitor);

                throw afe;
            }
        }
    }

    /**
     * Description: On startup the broker reports the broker version number and svn build revision. This information is retrieved from the resource 'qpidversion.properties' which is located via the classloader.
     * Input: The 'qpidversion.properties' file located on the classpath.
     * Output:
     *
     * <date> MESSAGE BRK-1001 : Startup : qpid Version: 0.6 Build: 767150
     *
     * Validation Steps:
     *
     * 1. The BRK ID is correct
     * 2. This occurs before any BRK-1002 listening messages are reported.
     *
     * @throws Exception caused by broker startup
     */
    public void testBrokerStartupStartup() throws Exception
    {
        // This logging startup code only occurs when you run a Java broker,
        // that broker must be started via Main so not an InVM broker.
        if (isJavaBroker() && isExternalBroker())
        {
            String TESTID = "BRK-1001";

            startBroker();

            // Now we can create the monitor as _outputFile will now be defined
            _monitor = new LogMonitor(_outputFile);
            
            // Ensure we wait for TESTID to be logged
            waitAndFindMatches(TESTID);

            // Retrieve all BRK- log messages so we can check for an erroneous
            // BRK-1002 message.
            List<String> results = findMatches(BRK_LOG_PREFIX);

            try
            {
                // Validation

                assertTrue("BRKer message not logged", results.size() > 0);

                boolean validation = false;
                for (String rawLog : results)
                {
                    if (validation)
                    {
                        //Stop checking once we have got to our startup test
                        break;
                    }
                    String log = getLog(rawLog);

                    // Ensure we do not have a BRK-1002 message
                    if (!getMessageID(log).equals(TESTID))
                    {
                        assertFalse(getMessageID(log).equals("BRK-1002"));
                        continue;
                    }

                    //1
                    validateMessageID(TESTID, log);

                    //2
                    //There will be 2 copies of the startup message (one via SystemOut, and one via Log4J)
                    assertEquals("Unexpected startup message count",
                                 2, findMatches(TESTID).size());

                    validation = true;
                }

                assertTrue("Validation not performed: " + TESTID + " not logged", validation);
            }
            catch (AssertionFailedError afe)
            {
                dumpLogs(results, _monitor);
                
                throw afe;
            }
        }
    }

    /**
     * Description:
     * On startup the broker may listen on a number of ports and protocols. Each of these must be reported as they are made available.
     * Input:
     * The default configuration with no SSL
     * Output:
     *
     * <date> MESSAGE BRK-1002 : Starting : Listening on TCP port 5672
     *
     * Constraints:
     * Additional broker configuration will occur between the Startup(BRK-1001) and Starting(BRK-1002) messages depending on what VirtualHosts are configured.
     * Validation Steps:
     *
     * 1. The BRK ID is correct
     * 2. This occurs after the BRK-1001 startup message
     * 3. Using the default configuration a single BRK-1002 will be printed showing values TCP / 5672
     *
     * @throws Exception caused by broker startup
     */
    public void testBrokerStartupListeningTCPDefault() throws Exception
    {
        // This logging startup code only occurs when you run a Java broker,
        // that broker must be started via Main so not an InVM broker.
        if (isJavaBroker() && isExternalBroker())
        {
            String TESTID = "BRK-1002";

            startBroker();

            // Now we can create the monitor as _outputFile will now be defined
            _monitor = new LogMonitor(_outputFile);

            // Ensure broker has fully started up.
            getConnection();

            // Ensure we wait for TESTID to be logged
            waitAndFindMatches(TESTID);

            // Retrieve all BRK- log messages so we can check for an erroneous
            // BRK-1002 message.
            List<String> results = findMatches(BRK_LOG_PREFIX);
            try
            {
                // Validation

                assertTrue("BRKer message not logged", results.size() > 0);

                boolean validation = false;
                boolean foundBRK1001 = false;
                for (String rawLog : results)
                {
                    String log = getLog(rawLog);

                    // Ensure we do not have a BRK-1002 message
                    if (!getMessageID(log).equals(TESTID))
                    {
                        if (getMessageID(log).equals("BRK-1001"))
                        {
                            foundBRK1001 = true;
                        }
                        continue;
                    }

                    assertTrue("BRK-1001 not logged before this message", foundBRK1001);

                    //1
                    validateMessageID(TESTID, log);

                    //2
                    //There will be 2 copies of the startup message (one via SystemOut, and one via Log4J)
                    assertEquals("Unexpected listen message count",
                                 2, findMatches(TESTID).size());

                    //3
                    String message = getMessageString(log);
                    assertTrue("Expected Listen log not correct" + message,
                               message.endsWith("Listening on TCP port " + getPort()));

                    validation = true;
                }

                assertTrue("Validation not performed: " + TESTID + " not logged", validation);
            }
            catch (AssertionFailedError afe)
            {
                dumpLogs(results, _monitor);
                
                throw afe;
            }
        }
    }

    /**
     * Description:
     * On startup the broker may listen on a number of ports and protocols. Each of these must be reported as they are made available.
     * Input:
     * The default configuration with SSL enabled
     * Output:
     *
     * <date> MESSAGE BRK-1002 : Starting : Listening on TCP port 5672
     * <date> MESSAGE BRK-1002 : Starting : Listening on TCP/SSL port 8672
     *
     * Constraints:
     * Additional broker configuration will occur between the Startup(BRK-1001) and Starting(BRK-1002) messages depending on what VirtualHosts are configured.
     * Validation Steps:
     *
     * 1. The BRK ID is correct
     * 2. This occurs after the BRK-1001 startup message
     * 3. With SSL enabled in the configuration two BRK-1002 will be printed (order is not specified)
     * 1. One showing values TCP / 5672
     * 2. One showing values TCP/SSL / 5672
     *
     * @throws Exception caused by broker startup
     */
    public void testBrokerStartupListeningTCPSSL() throws Exception
    {
        // This logging startup code only occurs when you run a Java broker,
        // that broker must be started via Main so not an InVM broker.
        if (isJavaBroker() && isExternalBroker())
        {
            String TESTID = "BRK-1002";

            // Enable SSL on the connection
            setConfigurationProperty("connector.ssl.enabled", "true");
            setConfigurationProperty("connector.ssl.sslOnly", "false");
            setConfigurationProperty("connector.ssl.keyStorePath", getConfigurationStringProperty("management.ssl.keyStorePath"));
            setConfigurationProperty("connector.ssl.keyStorePassword", getConfigurationStringProperty("management.ssl.keyStorePassword"));

            Integer sslPort = Integer.parseInt(getConfigurationStringProperty("connector.ssl.port"));

            startBroker();

            // Now we can create the monitor as _outputFile will now be defined
            _monitor = new LogMonitor(_outputFile);

            // Ensure broker has fully started up.
            getConnection();

            // Ensure we wait for TESTID to be logged
            waitAndFindMatches(TESTID);

            // Retrieve all BRK- log messages so we can check for an erroneous
            // BRK-1002 message.
            List<String> results = findMatches(BRK_LOG_PREFIX);
            try
            {
                // Validation

                assertTrue("BRKer message not logged", results.size() > 0);

                boolean validation = false;
                boolean foundBRK1001 = false;
                for (String rawLog : results)
                {
                    String log = getLog(rawLog);

                    // Ensure we do not have a BRK-1002 message
                    if (!getMessageID(log).equals(TESTID))
                    {
                        if (getMessageID(log).equals("BRK-1001"))
                        {
                            foundBRK1001 = true;
                        }
                        continue;
                    }

                    assertTrue("BRK-1001 not logged before this message", foundBRK1001);

                    //1
                    validateMessageID(TESTID, log);

                    //2
                    //There will be 4 copies of the startup message (two via SystemOut, and two via Log4J)
                    List<String> listenMessages  = findMatches(TESTID);
                    assertEquals("Four listen messages should be found.",
                                 4, listenMessages .size());

                    //3
                    //Check the first
                    String message = getMessageString(getLog(listenMessages .get(0)));
                    assertTrue("Expected Listen log not correct" + message,
                               message.endsWith("Listening on TCP port " + getPort()));

                    // Check the third, ssl listen.
                    message = getMessageString(getLog(listenMessages .get(2)));
                    assertTrue("Expected Listen log not correct" + message,
                               message.endsWith("Listening on TCP/SSL port " + sslPort));

                    //4 Test ports open
                    testSocketOpen(getPort());
                    testSocketOpen(sslPort);

                    validation = true;
                }

                assertTrue("Validation not performed: " + TESTID + " not logged", validation);
            }
            catch (AssertionFailedError afe)
            {
                dumpLogs(results, _monitor);

                throw afe;
            }
        }
    }

    /**
     * Description:
     * The final message the broker will print when it has performed all initialisation and listener startups will be to log the BRK-1004 Ready message
     * Input:
     * No input, all successful broker startups will show BRK-1004 messages.
     * Output:
     *
     * 2009-07-09 15:50:20 +0100 MESSAGE BRK-1004 : Qpid Broker Ready
     *
     * Validation Steps:
     *
     * 1. The BRK ID is correct
     * 2. This occurs after the BRK-1001 startup message
     * 3. This must be the last message the broker prints after startup. Currently, if there is no further interaction with the broker then there should be no more logging.
     *
     * @throws Exception caused by broker startup
     */
    public void testBrokerStartupReady() throws Exception
    {
        // This logging startup code only occurs when you run a Java broker,
        // that broker must be started via Main so not an InVM broker.
        if (isJavaBroker() && isExternalBroker())
        {
            String TESTID = "BRK-1004";

            startBroker();

            //Ensure the broker has fully started up.
            getConnection();
            // Ensure we wait for TESTID to be logged
            waitAndFindMatches(TESTID);

            // Retrieve all BRK- log messages so we can check for an erroneous
            // BRK-1001 message.
            List<String> results = findMatches(BRK_LOG_PREFIX);
            try
            {
                // Validation

                assertTrue("BRKer message not logged", results.size() > 0);

                boolean validationComplete = false;
                boolean foundBRK1001 = false;
                
                for (int i=0; i < results.size(); i++)
                {
                    String rawLog = results.get(i);
                    String log = getLog(rawLog);

                    // Ensure we do not have a BRK-1001 message
                    if (!getMessageID(log).equals(TESTID))
                    {
                        if (getMessageID(log).equals("BRK-1001"))
                        {
                            foundBRK1001 = true;
                        }
                        continue;
                    }

                    assertTrue("BRK-1001 not logged before this message", foundBRK1001);

                    //1
                    validateMessageID(TESTID, log);

                    //2
                    assertEquals("Ready message not present", "Qpid Broker Ready", getMessageString(log));
                    
                    //There will be 2 copies of the startup message (one via SystemOut, and one via Log4J)
                    assertEquals("Unexpected ready message count",
                                 2, findMatches(TESTID).size());
                    assertEquals("The ready messages should have been the last 2 messages", results.size() - 2, i);

                    validationComplete = true;
                    break;
                }

                assertTrue("Validation not performed: " + TESTID + " not logged", validationComplete);
            }
            catch (AssertionFailedError afe)
            {
                dumpLogs(results, _monitor);

                throw afe;
            }
        }
    }

    /**
     * Description:
     * On startup the broker may listen on a number of ports and protocols. Each of these must then report a shutting down message as they stop listening.
     * Input:
     * The default configuration with no SSL
     * Output:
     *
     * <date> MESSAGE BRK-1003 : Shutting down : TCP port 5672
     *
     * Validation Steps:
     *
     * 1. The BRK ID is correct
     * 2. Only TCP is reported with the default configuration with no SSL.
     * 3. The default port is correct
     * 4. The port is not accessible after this message
     *
     * @throws Exception caused by broker startup
     */
    public void testBrokerShutdownListeningTCPDefault() throws Exception
    {
        // This logging startup code only occurs when you run a Java broker,
        // that broker must be started via Main so not an InVM broker.
        if (isJavaBroker() && isExternalBroker())
        {
            String TESTID = "BRK-1003";

            startBroker();

            // Now we can create the monitor as _outputFile will now be defined
            _monitor = new LogMonitor(_outputFile);

            stopBroker();

            //Give broker time to shutdown and flush log
            checkSocketClosed(getPort());

            List<String> results = waitAndFindMatches(BRK_LOG_PREFIX);
            try
            {
                // Validation

                assertTrue("BRKer message not logged", results.size() > 0);

                boolean validation = false;
                boolean foundBRK1001 = false;
                for (String rawLog : results)
                {
                    String log = getLog(rawLog);

                    // Ensure we do not have a BRK-1002 message
                    if (!getMessageID(log).equals(TESTID))
                    {
                        if (getMessageID(log).equals("BRK-1001"))
                        {
                            foundBRK1001 = true;
                        }
                        continue;
                    }

                    assertTrue("BRK-1001 not logged before this message", foundBRK1001);

                    //1
                    validateMessageID(TESTID, log);

                    //2
                    assertEquals("More than one listen message found.",
                                 1, findMatches(TESTID).size());

                    //3
                    String message = getMessageString(log);
                    assertTrue("Expected shutdown log not correct" + message,
                               message.endsWith("TCP port " + getPort()));

                    //4
                    checkSocketClosed(getPort());

                    validation = true;
                }

                assertTrue("Validation not performed: " + TESTID + " not logged", validation);
            }
            catch (AssertionFailedError afe)
            {
                dumpLogs(results, _monitor);

                throw afe;
            }
        }
    }

    /**
     * Description:
     * On startup the broker may listen on a number of ports and protocols. Each of these must be reported as they are made available.
     * Input:
     * The default configuration with SSL enabled
     * Output:
     *
     * <date> MESSAGE BRK-1002 : Starting : Listening on TCP port 5672
     * <date> MESSAGE BRK-1002 : Starting : Listening on TCP/SSL port 8672
     *
     * Constraints:
     * Additional broker configuration will occur between the Startup(BRK-1001) and Starting(BRK-1002) messages depending on what VirtualHosts are configured.
     * Validation Steps:
     *
     * 1. The BRK ID is correct
     * 2. This occurs after the BRK-1001 startup message
     * 3. With SSL enabled in the configuration two BRK-1002 will be printed (order is not specified)
     * 1. One showing values TCP / 5672
     * 2. One showing values TCP/SSL / 5672
     *
     * @throws Exception caused by broker startup
     */
    public void testBrokerShutdownListeningTCPSSL() throws Exception
    {
        // This logging startup code only occurs when you run a Java broker,
        // that broker must be started via Main so not an InVM broker.
        if (isJavaBroker() && isExternalBroker())
        {
            String TESTID = "BRK-1003";

            // Enable SSL on the connection
            setConfigurationProperty("connector.ssl.enabled", "true");
            setConfigurationProperty("connector.ssl.keyStorePath", getConfigurationStringProperty("management.ssl.keyStorePath"));
            setConfigurationProperty("connector.ssl.keyStorePassword", getConfigurationStringProperty("management.ssl.keyStorePassword"));

            Integer sslPort = Integer.parseInt(getConfigurationStringProperty("connector.sslport"));

            startBroker();

            // Now we can create the monitor as _outputFile will now be defined
            _monitor = new LogMonitor(_outputFile);


//            //Clear any startup messages as we don't need them for validation
//            _monitor.reset();
            //Stop the broker to get the log messages for testing
            stopBroker();

            //Give broker time to shutdown and flush log
            checkSocketClosed(getPort());

            List<String> results = waitAndFindMatches(TESTID);
            try
            {
                // Validation

                assertTrue(TESTID + " messages not logged", results.size() > 0);

                String log = getLog(results.get(0));

                //1
                validateMessageID(TESTID, log);

                //2
                List<String> listenMessages = findMatches(TESTID);
                assertEquals("Two shutdown messages should be found.",
                             2, listenMessages.size());

                //3
                String message = getMessageString(getLog(listenMessages.get(0)));
                assertTrue("Expected shutdown log not correct" + message,
                           message.endsWith("TCP port " + getPort()));

                // Check second, ssl, listen.
                message = getMessageString(getLog(listenMessages.get(1)));
                assertTrue("Expected shutdown log not correct" + message,
                           message.endsWith("TCP/SSL port " + sslPort));

                //4
                //Test Port closed
                checkSocketClosed(getPort());
                //Test SSL Port closed
                checkSocketClosed(sslPort);
            }
            catch (AssertionFailedError afe)
            {
                dumpLogs(results, _monitor);

                throw afe;
            }
        }
    }

    /**
     * Description:
     * Input:
     * No input, all clean broker shutdowns will show BRK-1005 messages.
     * Output:
     *
     * <date> MESSAGE BRK-1005 : Stopped
     *
     * Constraints:
     * This is the LAST message the broker will log.
     * Validation Steps:
     *
     * 1. The BRK ID is correct
     * 2. This is the last message the broker will log.
     *
     * @throws Exception caused by broker startup
     */
    public void testBrokerShutdownStopped() throws Exception
    {
        // This logging startup code only occurs when you run a Java broker,
        // that broker must be started via Main so not an InVM broker.
        if (isJavaBroker() && isExternalBroker())
        {
            String TESTID = "BRK-1005";

            startBroker();

            // Now we can create the monitor as _outputFile will now be defined
            _monitor = new LogMonitor(_outputFile);

            getConnection().close();

            stopBroker();

            // Ensure the broker has shutdown before retreving results
            checkSocketClosed(getPort());

            waitAndFindMatches(TESTID);

            List<String> results = waitAndFindMatches(BRK_LOG_PREFIX);
            try
            {
                // Validation

                assertTrue("BRKer message not logged", results.size() > 0);

                boolean validation = false;
                for (String rawLog : results)
                {
                    assertFalse("More broker log statements present after ready message", validation);
                    String log = getLog(rawLog);

                    // Ignore all logs until we get to the test id.
                    if (!getMessageID(log).equals(TESTID))
                    {
                        continue;
                    }

                    //1
                    validateMessageID(TESTID, log);

                    //2
                    assertEquals("More than one ready message found.",
                                 1, findMatches(TESTID).size());

                    //3
                    assertEquals("Stopped message not present", "Stopped", getMessageString(log));

                    validation = true;
                }

                assertTrue("Validation not performed: " + TESTID + " not logged", validation);
            }
            catch (AssertionFailedError afe)
            {
                dumpLogs(results, _monitor);

                throw afe;
            }
        }
    }

    /**
     * Test that a socket on the given port is closed.
     *
     * Does this by attempting to connect to the port and expecting a
     * ConnectionRefused IOException or a ConnectionException
     *
     * @param port the port number
     */
    private void checkSocketClosed(int port)
    {
        try
        {
            Socket socket = new Socket((String) null, port);
            fail("Socket not closed on port:" + port);
        }
        catch (ConnectionException e)
        {
            //normal path
        }
        catch (IOException e)
        {
            if (!e.getMessage().equals("Connection refused"))
            {
                fail("Socket not closed on port:" + port + ":" + e.getMessage());
                // Keep stack trace for diagnosis.
                e.printStackTrace(System.err);
            }
        }
    }

    /**
     * Test that a socket on the given port is open.
     *
     * Does this by attempting to connect to the port and expecting a
     * The connection to succeed.
     * It then closes the socket and expects that to work cleanly.
     *
     * @param port the port number
     */
    private void testSocketOpen(int port)
    {
        try
        {
            Socket socket = new Socket((String) null, port);
            socket.close();
        }
        catch (IOException e)
        {
            fail("Unable to open and close socket to port:" + port
                 + ". Due to:" + e.getMessage());
        }
    }
}