1/*
2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23/*
24 * @test
25 * @bug 5102289
26 * @summary Test if ResourceBundle.getBundle detects bad Control implementations.
27 */
28
29import java.io.*;
30import java.util.*;
31import static java.util.ResourceBundle.Control.*;
32
33public class BadControlsTest {
34    public static void main(String[] args) {
35        ResourceBundle.Control control;
36
37        control = new ResourceBundle.Control() {
38                public List<String> getFormats(String name) {
39                    return null;
40                }
41            };
42        testControl(control, "getFormats returns null");
43
44        control = new ResourceBundle.Control() {
45                public List<String> getFormats(String name) {
46                    return Arrays.asList("java.class", null, "java.properties");
47                }
48            };
49        testControl(control, "getFormats returns a List containing null");
50
51        control = new ResourceBundle.Control() {
52                public List<Locale> getCandidateLocales(String name, Locale loc) {
53                    return null;
54                }
55            };
56        testControl(control, "getCandidateLocales returns null");
57
58        control = new ResourceBundle.Control() {
59                public List<Locale> getCandidateLocales(String name, Locale loc) {
60                    return Arrays.asList(Locale.US, null, Locale.ENGLISH);
61                }
62            };
63        testControl(control, "getCandidateLocales returns a List containing null");
64
65        long[] badTtls = {
66            TTL_NO_EXPIRATION_CONTROL - 1,
67            -10000,
68            Long.MIN_VALUE
69        };
70        for (final long ttl : badTtls) {
71            control = new ResourceBundle.Control() {
72                    public long getTimeToLive(String name, Locale loc) {
73                        return ttl;
74                    }
75                };
76            testControl(control, "getTimeToLive returns " + ttl);
77        }
78
79        control = new ResourceBundle.Control() {
80                public String toBundleName(String name, Locale loc) {
81                    return null;
82                }
83            };
84        try {
85            ResourceBundle rb = ResourceBundle.getBundle("StressOut", control);
86            throw new RuntimeException("toBundleName returns null");
87        } catch (MissingResourceException e) {
88            if (!(e.getCause() instanceof NullPointerException)) {
89                throw new RuntimeException("toBundleName returns null. The cause isn't NPE.");
90            }
91        }
92
93        // null Control tests
94        control = null;
95        try {
96            ResourceBundle rb = ResourceBundle.getBundle("StressOut", control);
97            throw new RuntimeException("getBundle doesn't throw NPE with null Control");
98        } catch (NullPointerException e) {
99            // OK
100        }
101    }
102
103    private static void testControl(ResourceBundle.Control control, String testTitle) {
104        try {
105            ResourceBundle rb = ResourceBundle.getBundle("StressOut", control);
106            throw new RuntimeException(testTitle);
107        } catch (IllegalArgumentException e) {
108            System.out.println(testTitle + ": PASSED (" + e.getMessage() + ")");
109        }
110    }
111}
112