Skip to content

Commit 378cae0

Browse files
patryk-kucharzpauljervis
authored andcommitted
[BAEL-1536] A Guide to the Resource Bundle (eugenp#3602)
* [BAEL-1536] A Guide to the Resource Bundle The essential code for the article * [BAEL-1536] A Guide to the Resource Bundle eugenp#2 The essential code for the article after the first article review. * [BAEL-1536] A Guide to the Resource Bundle eugenp#3 Essential changes after the second review. * [BAEL-1536] A Guide to the Resource Bundle eugenp#4 Essential changes after the second review: - Doubles to BigDecimals
1 parent d172168 commit 378cae0

9 files changed

Lines changed: 172 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.resourcebundle;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Locale;
6+
import java.util.ResourceBundle;
7+
8+
public class ExampleControl extends ResourceBundle.Control {
9+
10+
@Override
11+
public List<Locale> getCandidateLocales(String s, Locale locale) {
12+
return Arrays.asList(new Locale("pl", "PL"));
13+
}
14+
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.resourcebundle;
2+
3+
import java.util.ListResourceBundle;
4+
5+
public class ExampleResource extends ListResourceBundle {
6+
7+
@Override
8+
protected Object[][] getContents() {
9+
return new Object[][] {
10+
{ "greeting", "hello" }
11+
};
12+
}
13+
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.resourcebundle;
2+
3+
import java.util.ListResourceBundle;
4+
5+
public class ExampleResource_pl extends ListResourceBundle {
6+
7+
@Override
8+
protected Object[][] getContents() {
9+
return new Object[][] {
10+
{ "greeting", "cześć" },
11+
{ "language", "polish" },
12+
};
13+
}
14+
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.resourcebundle;
2+
3+
import java.math.BigDecimal;
4+
import java.util.ListResourceBundle;
5+
6+
public class ExampleResource_pl_PL extends ListResourceBundle {
7+
8+
@Override
9+
protected Object[][] getContents() {
10+
return new Object[][] {
11+
{ "currency", "polish zloty" },
12+
{ "toUsdRate", new BigDecimal("3.401") },
13+
{ "cities", new String[] { "Warsaw", "Cracow" } }
14+
};
15+
}
16+
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Buttons
2+
cancelButton=cancel
3+
continueButton continue
4+
5+
! Labels
6+
helloLabel:hello
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
deleteButton=delete
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
backButton=cofnij
2+
helloLabel=cze\u015b\u0107
3+
helloLabelNoEncoding=cześć
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.resourcebundle;
2+
3+
import org.junit.Test;
4+
5+
import java.math.BigDecimal;
6+
import java.util.Arrays;
7+
import java.util.Locale;
8+
import java.util.ResourceBundle;
9+
10+
import static org.junit.Assert.*;
11+
12+
public class ExampleResourceUnitTest {
13+
14+
@Test
15+
public void whenGetBundleExampleResourceForLocalePlPl_thenItShouldInheritPropertiesGreetingAndLanguage() {
16+
Locale plLocale = new Locale("pl", "PL");
17+
18+
ResourceBundle exampleBundle = ResourceBundle.getBundle("com.baeldung.resourcebundle.ExampleResource", plLocale);
19+
20+
assertTrue(exampleBundle.keySet()
21+
.containsAll(Arrays.asList("toUsdRate", "cities", "greeting", "currency", "language")));
22+
assertEquals(exampleBundle.getString("greeting"), "cześć");
23+
assertEquals(exampleBundle.getObject("toUsdRate"), new BigDecimal("3.401"));
24+
assertArrayEquals(exampleBundle.getStringArray("cities"), new String[] { "Warsaw", "Cracow" });
25+
}
26+
27+
@Test
28+
public void whenGetBundleExampleResourceForLocaleUs_thenItShouldContainOnlyGreeting() {
29+
Locale usLocale = Locale.US;
30+
31+
ResourceBundle exampleBundle = ResourceBundle.getBundle("com.baeldung.resourcebundle.ExampleResource", usLocale);
32+
33+
assertFalse(exampleBundle.keySet()
34+
.containsAll(Arrays.asList("toUsdRate", "cities", "currency", "language")));
35+
assertTrue(exampleBundle.keySet()
36+
.containsAll(Arrays.asList("greeting")));
37+
}
38+
39+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.resourcebundle;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.Locale;
7+
import java.util.ResourceBundle;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertTrue;
11+
12+
public class PropertyResourceUnitTest {
13+
14+
@Test
15+
public void givenLocaleUsAsDefualt_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() {
16+
Locale.setDefault(Locale.US);
17+
18+
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL"));
19+
20+
assertTrue(bundle.keySet()
21+
.containsAll(Arrays.asList("backButton", "helloLabel", "cancelButton", "continueButton", "helloLabelNoEncoding")));
22+
}
23+
24+
@Test
25+
public void givenLocaleUsAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldContainKeys1To3AndKey4() {
26+
Locale.setDefault(Locale.US);
27+
28+
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"));
29+
30+
assertTrue(bundle.keySet()
31+
.containsAll(Arrays.asList("deleteButton", "helloLabel", "cancelButton", "continueButton")));
32+
}
33+
34+
@Test
35+
public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldOnlyContainKeys1To3() {
36+
Locale.setDefault(Locale.CHINA);
37+
38+
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"));
39+
40+
assertTrue(bundle.keySet()
41+
.containsAll(Arrays.asList("continueButton", "helloLabel", "cancelButton")));
42+
}
43+
44+
@Test
45+
public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFrAndExampleControl_thenItShouldOnlyContainKey5() {
46+
Locale.setDefault(Locale.CHINA);
47+
48+
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"), new ExampleControl());
49+
50+
assertTrue(bundle.keySet()
51+
.containsAll(Arrays.asList("backButton", "helloLabel")));
52+
}
53+
54+
@Test
55+
public void givenValuesDifferentlyEncoded_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() {
56+
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL"));
57+
58+
assertEquals(bundle.getString("helloLabel"), "cześć");
59+
assertEquals(bundle.getString("helloLabelNoEncoding"), "czeÅ\u009BÄ\u0087");
60+
}
61+
62+
}

0 commit comments

Comments
 (0)