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.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.mockito.Mockito.spy;
23  import static org.mockito.Mockito.when;
24  
25  import java.util.ResourceBundle;
26  
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link ResourceBundleStringLookup}.
32   */
33  class ResourceBundleStringLookupTest {
34  
35      private static final String KEY = "key";
36      private static final String TEST_RESOURCE_BUNDLE = "org.apache.commons.text.example.testResourceBundleLookup";
37  
38      @Test
39      void testAny() {
40          final String bundleName = TEST_RESOURCE_BUNDLE;
41          final String bundleKey = KEY;
42          Assertions.assertEquals(ResourceBundle.getBundle(bundleName).getString(bundleKey),
43              ResourceBundleStringLookup.INSTANCE.apply(AbstractStringLookup.toLookupKey(bundleName, bundleKey)));
44      }
45  
46      @Test
47      void testBadKey() {
48          final String bundleName = TEST_RESOURCE_BUNDLE;
49          final String bundleKey = "bad_key";
50          assertNull(new ResourceBundleStringLookup(bundleName).apply(bundleKey));
51          assertNull(ResourceBundleStringLookup.INSTANCE.apply(AbstractStringLookup.toLookupKey(bundleName, bundleKey)));
52      }
53  
54      @Test
55      void testBadNames() {
56          assertNull(ResourceBundleStringLookup.INSTANCE
57              .apply(AbstractStringLookup.toLookupKey("BAD_RESOURCE_BUNDLE_NAME", "KEY")));
58      }
59  
60      @Test
61      void testDoubleBundle() {
62          assertThrows(IllegalArgumentException.class, () -> new ResourceBundleStringLookup(TEST_RESOURCE_BUNDLE)
63              .apply(AbstractStringLookup.toLookupKey("OtherBundle", KEY)));
64      }
65  
66      @Test
67      void testExceptionGettingString() {
68          final ResourceBundleStringLookup mockLookup = spy(ResourceBundleStringLookup.class);
69          when(mockLookup.getString(TEST_RESOURCE_BUNDLE, KEY)).thenThrow(ClassCastException.class);
70          assertThrows(IllegalArgumentException.class, () -> mockLookup.apply(AbstractStringLookup.toLookupKey(TEST_RESOURCE_BUNDLE, KEY)));
71      }
72  
73      @Test
74      void testMissingKeyInSpec() {
75          assertThrows(IllegalArgumentException.class, () -> ResourceBundleStringLookup.INSTANCE.apply(TEST_RESOURCE_BUNDLE + ":"));
76      }
77  
78      @Test
79      void testNull() {
80          Assertions.assertNull(ResourceBundleStringLookup.INSTANCE.apply(null));
81      }
82  
83      @Test
84      void testOne() {
85          Assertions.assertEquals(ResourceBundle.getBundle(TEST_RESOURCE_BUNDLE).getString(KEY),
86              new ResourceBundleStringLookup(TEST_RESOURCE_BUNDLE).apply(KEY));
87      }
88  
89      @Test
90      void testToString() {
91          // does not blow up and gives some kind of string.
92          Assertions.assertFalse(ResourceBundleStringLookup.INSTANCE.toString().isEmpty());
93      }
94  
95  }