[BAEL-1536] A Guide to the Resource Bundle (#3602)

* [BAEL-1536] A Guide to the Resource Bundle

The essential code for the article

* [BAEL-1536] A Guide to the Resource Bundle #2

The essential code for the article after the first article review.

* [BAEL-1536] A Guide to the Resource Bundle #3

Essential changes after the second review.

* [BAEL-1536] A Guide to the Resource Bundle #4

Essential changes after the second review:
- Doubles to BigDecimals
This commit is contained in:
Patryk
2018-02-22 21:45:27 +01:00
committed by pauljervis
parent d172168ace
commit 378cae0821
9 changed files with 172 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
package com.baeldung.resourcebundle;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;
import static org.junit.Assert.*;
public class ExampleResourceUnitTest {
@Test
public void whenGetBundleExampleResourceForLocalePlPl_thenItShouldInheritPropertiesGreetingAndLanguage() {
Locale plLocale = new Locale("pl", "PL");
ResourceBundle exampleBundle = ResourceBundle.getBundle("com.baeldung.resourcebundle.ExampleResource", plLocale);
assertTrue(exampleBundle.keySet()
.containsAll(Arrays.asList("toUsdRate", "cities", "greeting", "currency", "language")));
assertEquals(exampleBundle.getString("greeting"), "cześć");
assertEquals(exampleBundle.getObject("toUsdRate"), new BigDecimal("3.401"));
assertArrayEquals(exampleBundle.getStringArray("cities"), new String[] { "Warsaw", "Cracow" });
}
@Test
public void whenGetBundleExampleResourceForLocaleUs_thenItShouldContainOnlyGreeting() {
Locale usLocale = Locale.US;
ResourceBundle exampleBundle = ResourceBundle.getBundle("com.baeldung.resourcebundle.ExampleResource", usLocale);
assertFalse(exampleBundle.keySet()
.containsAll(Arrays.asList("toUsdRate", "cities", "currency", "language")));
assertTrue(exampleBundle.keySet()
.containsAll(Arrays.asList("greeting")));
}
}

View File

@@ -0,0 +1,62 @@
package com.baeldung.resourcebundle;
import org.junit.Test;
import java.util.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class PropertyResourceUnitTest {
@Test
public void givenLocaleUsAsDefualt_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() {
Locale.setDefault(Locale.US);
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL"));
assertTrue(bundle.keySet()
.containsAll(Arrays.asList("backButton", "helloLabel", "cancelButton", "continueButton", "helloLabelNoEncoding")));
}
@Test
public void givenLocaleUsAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldContainKeys1To3AndKey4() {
Locale.setDefault(Locale.US);
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"));
assertTrue(bundle.keySet()
.containsAll(Arrays.asList("deleteButton", "helloLabel", "cancelButton", "continueButton")));
}
@Test
public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldOnlyContainKeys1To3() {
Locale.setDefault(Locale.CHINA);
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"));
assertTrue(bundle.keySet()
.containsAll(Arrays.asList("continueButton", "helloLabel", "cancelButton")));
}
@Test
public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFrAndExampleControl_thenItShouldOnlyContainKey5() {
Locale.setDefault(Locale.CHINA);
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"), new ExampleControl());
assertTrue(bundle.keySet()
.containsAll(Arrays.asList("backButton", "helloLabel")));
}
@Test
public void givenValuesDifferentlyEncoded_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() {
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL"));
assertEquals(bundle.getString("helloLabel"), "cześć");
assertEquals(bundle.getString("helloLabelNoEncoding"), "czeÅ\u009BÄ\u0087");
}
}