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.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
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
92 Assertions.assertFalse(ResourceBundleStringLookup.INSTANCE.toString().isEmpty());
93 }
94
95 }