/* * 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.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"; static final class TestHandler extends Handler { ResourceBundle lastBundle = null; String lastBundleName = null; @Override public void publish(LogRecord record) { lastBundle = record.getResourceBundle(); lastBundleName = record.getResourceBundleName(); } @Override public void flush() { } @Override public void close() throws SecurityException { } } private static enum TestCase { LOGRB1, LOGRB2, LOGRB3, LOGRB4; public void logrb(Logger logger, ResourceBundle bundle) { switch(this) { case LOGRB1: logger.logrb(Level.CONFIG, TestLogrbResourceBundle.class.getName(), "main", bundle, "dummy"); break; case LOGRB2: logger.logrb(Level.CONFIG, TestLogrbResourceBundle.class.getName(), "main", bundle, "dummy", "bar"); break; case LOGRB3: logger.logrb(Level.CONFIG, TestLogrbResourceBundle.class.getName(), "main", bundle, "dummy", new Object[] { "bar", "baz"} ); break; case LOGRB4: logger.logrb(Level.CONFIG, TestLogrbResourceBundle.class.getName(), "main", bundle, "dummy", new Exception("dummy exception") ); break; default: } } } 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 { for (TestCase test : TestCase.values()) { for (ResourceBundle b : new ResourceBundle[] { anonBundle, bundle, bundle_fr, propertyBundle, anonBundle, null, propertyBundle_fr, }) { final String baseName = getBaseName(b); System.out.println("Testing " + test + " with " + baseName); test.logrb(foobar, b); 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); } if (foobar.getResourceBundle() != null) { throw new RuntimeException("Unexpected bundle: " + foobar.getResourceBundle()); } if (foobar.getResourceBundleName() != null) { throw new RuntimeException("Unexpected bundle: " + foobar.getResourceBundleName()); } } } for (ResourceBundle propBundle : new ResourceBundle[] { propertyBundle, propertyBundle_fr, }) { foobar.setResourceBundle(propBundle); 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 (TestCase test : TestCase.values()) { 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); test.logrb(foobar, b); 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); } 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()); } } } } Logger foobaz = Logger.getLogger("foo.bar.baz"); if (foobaz.getResourceBundle() != null) { throw new RuntimeException("Unexpected bundle: " + foobar.getResourceBundle()); } if (foobaz.getResourceBundleName() != null) { throw new RuntimeException("Unexpected bundle: " + foobar.getResourceBundle()); } Locale.setDefault(Locale.GERMAN); // shouldn't change anything... for (TestCase test : TestCase.values()) { 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); test.logrb(foobaz, b); 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); } if (foobaz.getResourceBundle() != null) { throw new RuntimeException("Unexpected bundle: " + foobaz.getResourceBundle()); } if (foobaz.getResourceBundleName() != null) { throw new RuntimeException("Unexpected bundle: " + foobaz.getResourceBundleName()); } } } } finally { Locale.setDefault(defaultLocale); } } }