View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.commons.text.lookup;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.mockito.Mockito.spy;
25  import static org.mockito.Mockito.when;
26  
27  import java.util.ResourceBundle;
28  
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests {@link ResourceBundleStringLookup}.
33   */
34  class ResourceBundleStringLookupTest {
35  
36      private static final String KEY = "key";
37      private static final String TEST_RESOURCE_BUNDLE = "org.apache.commons.text.example.testResourceBundleLookup";
38  
39      @Test
40      void testAny() {
41          final String bundleName = TEST_RESOURCE_BUNDLE;
42          final String bundleKey = KEY;
43          assertEquals(ResourceBundle.getBundle(bundleName).getString(bundleKey),
44              ResourceBundleStringLookup.INSTANCE.apply(AbstractStringLookup.toLookupKey(bundleName, bundleKey)));
45      }
46  
47      @Test
48      void testBadKey() {
49          final String bundleName = TEST_RESOURCE_BUNDLE;
50          final String bundleKey = "bad_key";
51          assertNull(new ResourceBundleStringLookup(bundleName).apply(bundleKey));
52          assertNull(ResourceBundleStringLookup.INSTANCE.apply(AbstractStringLookup.toLookupKey(bundleName, bundleKey)));
53      }
54  
55      @Test
56      void testBadNames() {
57          assertNull(ResourceBundleStringLookup.INSTANCE
58              .apply(AbstractStringLookup.toLookupKey("BAD_RESOURCE_BUNDLE_NAME", "KEY")));
59      }
60  
61      @Test
62      void testDoubleBundle() {
63          assertThrows(IllegalArgumentException.class, () -> new ResourceBundleStringLookup(TEST_RESOURCE_BUNDLE)
64              .apply(AbstractStringLookup.toLookupKey("OtherBundle", KEY)));
65      }
66  
67      @Test
68      void testExceptionGettingString() {
69          final ResourceBundleStringLookup mockLookup = spy(ResourceBundleStringLookup.class);
70          when(mockLookup.getString(TEST_RESOURCE_BUNDLE, KEY)).thenThrow(ClassCastException.class);
71          assertThrows(IllegalArgumentException.class, () -> mockLookup.apply(AbstractStringLookup.toLookupKey(TEST_RESOURCE_BUNDLE, KEY)));
72      }
73  
74      @Test
75      void testMissingKeyInSpec() {
76          assertThrows(IllegalArgumentException.class, () -> ResourceBundleStringLookup.INSTANCE.apply(TEST_RESOURCE_BUNDLE + ":"));
77      }
78  
79      @Test
80      void testNull() {
81          assertNull(ResourceBundleStringLookup.INSTANCE.apply(null));
82      }
83  
84      @Test
85      void testOne() {
86          assertEquals(ResourceBundle.getBundle(TEST_RESOURCE_BUNDLE).getString(KEY),
87              new ResourceBundleStringLookup(TEST_RESOURCE_BUNDLE).apply(KEY));
88      }
89  
90      @Test
91      void testToString() {
92          // does not blow up and gives some kind of string.
93          assertFalse(ResourceBundleStringLookup.INSTANCE.toString().isEmpty());
94      }
95  
96  }