|
| 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