From e625409562b550272764177e92ee81aca09735bc Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 8 May 2022 18:07:40 +0300 Subject: [PATCH] classlib: add ResourceBundle.getBaseBundleName --- .../teavm/classlib/impl/ResourceBundleImpl.java | 4 +++- .../classlib/java/util/TResourceBundle.java | 10 +++++++++- .../classlib/java/util/ResourceBundleTest.java | 13 +++++++++++++ .../META-INF/services/java.util.ResourceBundle | 17 +++++++++++++++++ .../src/test/resources/testBundle_en.properties | 17 +++++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/src/test/resources/META-INF/services/java.util.ResourceBundle create mode 100644 tests/src/test/resources/testBundle_en.properties diff --git a/classlib/src/main/java/org/teavm/classlib/impl/ResourceBundleImpl.java b/classlib/src/main/java/org/teavm/classlib/impl/ResourceBundleImpl.java index 4d562b562..c386e86ac 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/ResourceBundleImpl.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/ResourceBundleImpl.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; @@ -58,7 +59,8 @@ public class ResourceBundleImpl { Set implementations = new LinkedHashSet<>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), + StandardCharsets.UTF_8))) { while (true) { String line = reader.readLine(); if (line == null) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TResourceBundle.java b/classlib/src/main/java/org/teavm/classlib/java/util/TResourceBundle.java index a550295ab..4d90ff4b1 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TResourceBundle.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TResourceBundle.java @@ -51,6 +51,8 @@ public abstract class TResourceBundle { private static final Map> bundleProviders = ResourceBundleImpl.createBundleMap(false); + private String name; + public TResourceBundle() { } @@ -118,6 +120,10 @@ public abstract class TResourceBundle { return locale; } + public String getBaseBundleName() { + return name; + } + public final Object getObject(String key) { TResourceBundle last; TResourceBundle theParent = this; @@ -173,6 +179,7 @@ public abstract class TResourceBundle { } } cache.put(bundleName, bundle); + bundle.name = base; return bundle; } @@ -180,6 +187,7 @@ public abstract class TResourceBundle { bundle = handleGetBundle(base, extension, loadBase); if (bundle != null) { cache.put(bundleName, bundle); + bundle.name = base; return bundle; } } @@ -219,7 +227,7 @@ public abstract class TResourceBundle { } country = name.substring(index + 1, nextIndex); if (nextIndex + 1 < name.length()) { - variant = name.substring(nextIndex + 1, name.length()); + variant = name.substring(nextIndex + 1); } } } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/ResourceBundleTest.java b/tests/src/test/java/org/teavm/classlib/java/util/ResourceBundleTest.java index 170fdecb3..bf3ea486c 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/ResourceBundleTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/ResourceBundleTest.java @@ -72,6 +72,19 @@ public class ResourceBundleTest { } + @Test + public void simpleBundle() { + ResourceBundle bundle = ResourceBundle.getBundle("testBundle", Locale.ENGLISH); + assertEquals("testBundle", bundle.getBaseBundleName()); + assertEquals("Test passed", bundle.getString("a")); + try { + bundle.getString("b"); + fail("MissingResourceException not thrown"); + } catch (MissingResourceException e) { + assertEquals("b", e.getKey()); + } + } + /* * the class and constructor must be public so ResourceBundle has the * possibility to instantiate diff --git a/tests/src/test/resources/META-INF/services/java.util.ResourceBundle b/tests/src/test/resources/META-INF/services/java.util.ResourceBundle new file mode 100644 index 000000000..c661ad8b9 --- /dev/null +++ b/tests/src/test/resources/META-INF/services/java.util.ResourceBundle @@ -0,0 +1,17 @@ +# +# Copyright 2022 Alexey Andreev. +# +# Licensed 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. +# + +testBundle_en \ No newline at end of file diff --git a/tests/src/test/resources/testBundle_en.properties b/tests/src/test/resources/testBundle_en.properties new file mode 100644 index 000000000..d41239956 --- /dev/null +++ b/tests/src/test/resources/testBundle_en.properties @@ -0,0 +1,17 @@ +# +# Copyright 2022 Alexey Andreev. +# +# Licensed 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. +# + +a=Test passed \ No newline at end of file