1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
93 assertFalse(ResourceBundleStringLookup.INSTANCE.toString().isEmpty());
94 }
95
96 }