aboutsummaryrefslogtreecommitdiff
path: root/test/java/util/logging/Logger/logrb/TestLogrbResourceBundle.java
blob: f47219492f293f6e0e5e1aae046474d53590035d (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
/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import resources.ListBundle;

/**
 * @test
 * @bug 8013839
 * @summary tests Logger.logrb(..., ResourceBundle);
 * @build TestLogrbResourceBundle resources.ListBundle resources.ListBundle_fr
 * @run main TestLogrbResourceBundle
 * @author danielfuchs
 */
public class TestLogrbResourceBundle {

    final static String LIST_BUNDLE_NAME = "resources.ListBundle";
    final static String PROPERTY_BUNDLE_NAME = "resources.PropertyBundle";

    /**
     * A dummy handler class that we can use to check the bundle/bundle name
     * that was present in the last LogRecord instance published.
     */
    static final class TestHandler extends Handler {
        ResourceBundle lastBundle = null;
        String lastBundleName = null;
        Object[] lastParams = null;
        Throwable lastThrown = null;
        String lastMessage = null;
        @Override
        public void publish(LogRecord record) {
            lastBundle = record.getResourceBundle();
            lastBundleName = record.getResourceBundleName();
            lastParams = record.getParameters();
            lastThrown = record.getThrown();
            lastMessage = record.getMessage();
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() throws SecurityException {
        }
    }

    /**
     * We're going to do the same test with each of the different new logrb
     * forms.
     * <ul>
     *    <li> LOGRB_NO_ARGS: calling logrb with no message argument.
     *    <li> LOGRB_SINGLE_ARG: calling logrb with a single message argument.
     *    <li> LOGRB_ARG_ARRAY: calling logrb with an array of message arguments.
     *    <li> LOGRB_VARARGS: calling logrb with a variable list of message arguments.
     *    <li> LOGRB_THROWABLE: calling logrb with an exception.
     * </ul>
     */
    private static enum TestCase {
        LOGRB_NO_ARGS, LOGRB_SINGLE_ARG, LOGRB_ARG_ARRAY, LOGRB_VARARGS, LOGRB_THROWABLE;

        public void logrb(Logger logger, ResourceBundle bundle) {
            switch(this) {
                case LOGRB_NO_ARGS:
                    logger.logrb(Level.CONFIG,
                            TestLogrbResourceBundle.class.getName(),
                            "main", bundle, "dummy");
                    break;
                case LOGRB_SINGLE_ARG:
                    logger.logrb(Level.CONFIG,
                            TestLogrbResourceBundle.class.getName(),
                            "main", bundle, "dummy", "bar");
                    break;
                case LOGRB_ARG_ARRAY:
                    logger.logrb(Level.CONFIG,
                            TestLogrbResourceBundle.class.getName(),
                            "main", bundle, "dummy",
                            new Object[] { "bar", "baz"} );
                    break;
                case LOGRB_VARARGS:
                    logger.logrb(Level.CONFIG,
                            TestLogrbResourceBundle.class.getName(),
                            "main", bundle, "dummy",
                            "bar", "baz" );
                    break;
                case LOGRB_THROWABLE:
                    logger.logrb(Level.CONFIG,
                            TestLogrbResourceBundle.class.getName(),
                            "main", bundle, "dummy",
                            new Exception("dummy exception") );
                    break;
                default:
            }
        }

        /**
         * Checks that the last published logged record had the expected data.
         * @param handler the TestHandler through which the record was published.
         */
        public void checkLogged(TestHandler handler) {
            checkLogged(handler.lastMessage, handler.lastParams, handler.lastThrown);
        }

        private void checkLogged(String message, Object[] parameters, Throwable thrown) {
            switch(this) {
                case LOGRB_NO_ARGS:
                    if ("dummy".equals(message) && thrown == null
                            && (parameters == null || parameters.length == 0)) {
                        return; // OK: all was as expected.
                    }
                    break;
                case LOGRB_SINGLE_ARG:
                    if ("dummy".equals(message) && thrown == null
                            && parameters != null
                            && parameters.length == 1
                            && "bar".equals(parameters[0])) {
                        return; // OK: all was as expected.
                    }
                    break;
                case LOGRB_VARARGS:
                case LOGRB_ARG_ARRAY:
                    if ("dummy".equals(message) && thrown == null
                            && parameters != null
                            && parameters.length > 1
                            && Arrays.deepEquals(new Object[] { "bar", "baz"},
                                    parameters)) {
                        return; // OK: all was as expected.
                    }
                    break;
                case LOGRB_THROWABLE:
                    if ("dummy".equals(message) && thrown != null
                            && thrown.getClass() == Exception.class
                            && "dummy exception".equals(thrown.getMessage())) {
                        return; // OK: all was as expected.
                    }
                    break;
                default:
            }

            // We had some unexpected stuff: throw exception.
            throw new RuntimeException(this + ": "
                    + "Unexpected content in last published log record: "
                    + "\n\tmessage=\"" + message + "\""
                    + "\n\tparameters=" + Arrays.toString(parameters)
                    + "\n\tthrown=" + thrown);
        }
    }

    static String getBaseName(ResourceBundle bundle) {
        return bundle == null ? null : bundle.getBaseBundleName();
    }

    public static void main(String... args) throws Exception {

        Locale defaultLocale = Locale.getDefault();

        final ResourceBundle bundle = ResourceBundle.getBundle(LIST_BUNDLE_NAME);
        final ResourceBundle bundle_fr =
                    ResourceBundle.getBundle(LIST_BUNDLE_NAME, Locale.FRENCH);
        final ResourceBundle propertyBundle = ResourceBundle.getBundle(PROPERTY_BUNDLE_NAME);
        final ResourceBundle propertyBundle_fr =
                    ResourceBundle.getBundle(PROPERTY_BUNDLE_NAME, Locale.FRENCH);
        Logger foobar = Logger.getLogger("foo.bar");
        final TestHandler handler = new TestHandler();
        foobar.addHandler(handler);
        foobar.setLevel(Level.CONFIG);

        final ResourceBundle anonBundle = new ListBundle();
        try {
            // First we're going to call logrb on a logger that
            // has no bundle set...

            // For each possible logrb form...
            for (TestCase test : TestCase.values()) {
                // For various resource bundles
                for (ResourceBundle b : new ResourceBundle[] {
                    anonBundle, bundle, bundle_fr, propertyBundle,
                    anonBundle, null, propertyBundle_fr,
                }) {
                    // Prints the resource bundle base name (can be null,
                    //   we don't enforce non-null names in logrb.
                    final String baseName = getBaseName(b);
                    System.out.println("Testing " + test + " with " + baseName);

                    // log in the 'foobar' logger using bundle 'b'
                    test.logrb(foobar, b);

                    // check that the correct bundle was set in the published
                    // LogRecord
                    if (handler.lastBundle != b) {
                        throw new RuntimeException("Unexpected bundle: "
                                + handler.lastBundle);
                    }

                    // check that the correct bundle name was set in the published
                    // LogRecord
                    if (!Objects.equals(handler.lastBundleName, baseName)) {
                        throw new RuntimeException("Unexpected bundle name: "
                                + handler.lastBundleName);
                    }

                    // check that calling foobar.logrb() had no side effect on
                    // the bundle used by foobar. foobar should still have no
                    // bundle set.
                    if (foobar.getResourceBundle() != null) {
                        throw new RuntimeException("Unexpected bundle: "
                            + foobar.getResourceBundle());
                    }
                    if (foobar.getResourceBundleName() != null) {
                        throw new RuntimeException("Unexpected bundle: "
                            + foobar.getResourceBundleName());
                    }

                    // Test that the last published log record had all the
                    // data that this test case had logged (message, parameters,
                    // thrown...
                    test.checkLogged(handler);
                }
            }

            // No we're going to set a resource bundle on the foobar logger
            // and do it all again...

            // For the same bundle in two different locales
            for (ResourceBundle propBundle : new ResourceBundle[] {
                propertyBundle, propertyBundle_fr,
            }) {

                // set the bundle on foobar...
                foobar.setResourceBundle(propBundle);

                // check the bundle was correctly set...
                if (!propBundle.equals(foobar.getResourceBundle())) {
                    throw new RuntimeException("Unexpected bundle: "
                            + foobar.getResourceBundle());
                }
                if (!Objects.equals(getBaseName(propBundle), foobar.getResourceBundleName())) {
                    throw new RuntimeException("Unexpected bundle name: "
                            + foobar.getResourceBundleName());
                }

                System.out.println("Configuring " + foobar.getName() + " with "
                        + propBundle);

                // for each possible logrb form...
                for (TestCase test : TestCase.values()) {

                    // for various resource bundles
                    for (ResourceBundle b : new ResourceBundle[] {
                        anonBundle, bundle, null, bundle_fr, propertyBundle,
                        anonBundle, propertyBundle_fr,
                    }) {

                        final String baseName = getBaseName(b);
                        System.out.println("Testing " + test + " with " + baseName);

                        // call foobar.logrb
                        test.logrb(foobar, b);

                        // check which resource bundle was used (should be
                        // the one passed to logrb)
                        if (handler.lastBundle != b) {
                            throw new RuntimeException("Unexpected bundle: "
                                    + handler.lastBundle);
                        }
                        if (!Objects.equals(handler.lastBundleName, baseName)) {
                            throw new RuntimeException("Unexpected bundle name: "
                                    + handler.lastBundleName);
                        }

                        // Verify there was no side effect on the bundle that
                        // had been previously set on the logger...
                        if (foobar.getResourceBundle() != propBundle) {
                            throw new RuntimeException("Unexpected bundle: "
                                + foobar.getResourceBundle());
                        }
                        if (!Objects.equals(getBaseName(propBundle),
                                foobar.getResourceBundleName())) {
                            throw new RuntimeException("Unexpected bundle name: "
                                + foobar.getResourceBundleName());
                        }

                        // Checked that the published LogRecord had the
                        // expected content logged by this test case.
                        test.checkLogged(handler);
                    }
                }
            }

            // Now we're going to the same thing, but with a logger which
            // has an inherited resource bundle.
            Logger foobaz = Logger.getLogger("foo.bar.baz");

            // check that foobaz has no bundle set locally.
            if (foobaz.getResourceBundle() != null) {
                throw new RuntimeException("Unexpected bundle: "
                        + foobaz.getResourceBundle());
            }
            if (foobaz.getResourceBundleName() != null) {
                throw new RuntimeException("Unexpected bundle: "
                        + foobaz.getResourceBundle());
            }

            // The current locale should have no effect on logrb.
            Locale.setDefault(Locale.GERMAN); // shouldn't change anything...

            // for each possible logrb form
            for (TestCase test : TestCase.values()) {

                // for various resource bundle
                for (ResourceBundle b : new ResourceBundle[] {
                    anonBundle, bundle, bundle_fr, propertyBundle, null,
                     anonBundle, propertyBundle_fr,
                }) {
                    final String baseName = getBaseName(b);
                    System.out.println("Testing " + test + " with "
                            + foobaz.getName() + " and "
                            + baseName);

                    // call foobaz.logrb with the bundle
                    test.logrb(foobaz, b);

                    // check that the bundle passed to logrb was used.
                    if (handler.lastBundle != b) {
                        throw new RuntimeException("Unexpected bundle: "
                                + handler.lastBundle);
                    }
                    if (!Objects.equals(handler.lastBundleName, baseName)) {
                        throw new RuntimeException("Unexpected bundle name: "
                                + handler.lastBundleName);
                    }

                    // check that there was no effect on the bundle set
                    // on foobaz: it should still be null.
                    if (foobaz.getResourceBundle() != null) {
                        throw new RuntimeException("Unexpected bundle: "
                            + foobaz.getResourceBundle());
                    }
                    if (foobaz.getResourceBundleName() != null) {
                        throw new RuntimeException("Unexpected bundle: "
                            + foobaz.getResourceBundleName());
                    }

                    // check that the last published log record had all the
                    // data that was logged by this testcase.
                    test.checkLogged(handler);
                }
            }

        } finally {
            Locale.setDefault(defaultLocale);
        }

    }
}