summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/LogMessageTest.java
blob: b0cb0ca0ab58006931cdb616c58e0cc8fdfe5f6f (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
/*
 *
 * 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.TestCase;

import org.apache.qpid.server.logging.messages.BrokerMessages;

import java.util.Locale;
import java.util.ResourceBundle;

public class LogMessageTest extends TestCase
{

    /**
     * Test that the US local has a loadable bundle.
     * No longer have a specific en_US bundle so cannot verify that that version
     * is loaded. Can only verify that we get a ResourceBundle loaded.
     */
    public void testBundle()
    {
        Locale usLocal = Locale.US;
        Locale.setDefault(usLocal);
        ResourceBundle _messages = ResourceBundle.getBundle("org.apache.qpid.server.logging.messages.Broker_logmessages",
                                                            usLocal);

        assertNotNull("Unable to load ResourceBundle", _messages);
    }

    /**
     * Test that loading an undefined locale will result in loading of the
     * default US locale.
     */
    public void testUndefinedLocale()
    {
        Locale japanese = Locale.JAPANESE;

        Locale.setDefault(japanese);
        try
        {
            ResourceBundle _messages = ResourceBundle.getBundle("org.apache.qpid.server.logging.messages.Broker_logmessages",
                                                                japanese);

            assertNotNull("Unable to load ResourceBundle", _messages);

            // If we attempt to load an undefined locale it should default to the Root locale.
            assertEquals("Loaded bundle has incorrect locale.", Locale.ROOT, _messages.getLocale());
        }
        catch (Throwable t)
        {
            fail(t.getMessage());
        }
    }

    /**
     * test Simultaneous log message generation.
     * QPID-2137 highlighted that log message generation was not thread-safe.
     * Test to ensure that simultaneous logging is possible and does not throw an exception.
     * @throws InterruptedException if there is a problem joining logging threads.
     */
    public void testSimultaneousLogging() throws InterruptedException
    {
        int LOGGERS = 10;
        int LOG_COUNT = 10;
        LogGenerator[] logGenerators = new LogGenerator[LOGGERS];
        Thread[] threads = new Thread[LOGGERS];

        //Create Loggers
        for (int i = 0; i < LOGGERS; i++)
        {
            logGenerators[i] = new LogGenerator(LOG_COUNT);
            threads[i] = new Thread(logGenerators[i]);
        }

        //Run Loggers
        for (int i = 0; i < LOGGERS; i++)
        {
            threads[i].start();
        }

        //End Loggers
        for (int i = 0; i < LOGGERS; i++)
        {
            threads[i].join();
            Exception e = logGenerators[i].getThrowException();
            // If we have an exception something went wrong.
            // Check and see if it was QPID-2137
            if (e != null)
            {
                // Just log out if we find the usual exception causing QPID-2137
                if (e instanceof StringIndexOutOfBoundsException)
                {
                    System.err.println("Detected QPID-2137");
                }
                fail("Exception thrown during log generation:" + e);
            }
        }
    }

    /**
     * Inner class used by testSimultaneousLogging.
     *
     * This class creates a given number of LogMessages using the BrokerMessages package.
     * CONFIG and LISTENING messages are both created per count.
     *
     * This class is run multiple times simultaneously so that we increase the chance of
     * reproducing QPID-2137. This is reproduced when the pattern string used in the MessageFormat
     * class is changed whilst formatting is taking place.
     *
     */
    class LogGenerator implements Runnable
    {
        private Exception _exception = null;
        private int _count;

        /**
         * @param count The number of Log Messages to generate
         */
        LogGenerator(int count)
        {
            _count = count;
        }

        public void run()
        {
            try
            {
                // try and generate _count iterations of Config & Listening messages.
                for (int i = 0; i < _count; i++)
                {
                    BrokerMessages.CONFIG("Config");
                    BrokerMessages.LISTENING("TCP", 1234);
                }
            }
            catch (Exception e)
            {
                // if something goes wrong recorded it for later analysis.
                _exception = e;
            }
        }

        /**
         * Return any exception that was thrown during the log generation.
         * @return Exception
         */
        public Exception getThrowException()
        {
            return _exception;
        }
    }

}