View Javadoc
1   package org.itracker.core.resources;
2   
3   import org.itracker.model.Configuration;
4   import org.itracker.services.AbstractServicesIntegrationTest;
5   import org.itracker.services.ConfigurationService;
6   import org.junit.Assert;
7   import org.junit.Before;
8   import org.junit.Test;
9   
10  import java.util.Enumeration;
11  import java.util.Locale;
12  import java.util.MissingResourceException;
13  import java.util.ResourceBundle;
14  
15  import static junit.framework.Assert.fail;
16  import static org.itracker.Assert.assertTrue;
17  
18  
19  public class ItrackerResourcesIT extends AbstractServicesIntegrationTest {
20  
21      private ConfigurationService configurationService;
22  
23      @Test
24      public void testInitialized() {
25          assertTrue(ITrackerResources.isInitialized());
26      }
27  
28      @Test
29      public void testGetBundleEmptyString() {
30          ResourceBundle resourceBundle = ITrackerResources.getBundle("");
31          Assert.assertNotNull(resourceBundle);
32          Assert.assertEquals(ITrackerResources.getBundle(), resourceBundle);
33      }
34  
35      @Test
36      public void testGetBundleStringParameter() {
37          ResourceBundle resourceBundle = ITrackerResources.getBundle(ITrackerResources.getLocale());
38          Assert.assertNotNull(resourceBundle);
39          Assert.assertEquals(ITrackerResources.getBundle(), resourceBundle);
40      }
41  
42      @Test
43      public void testGetBundleNullLocale() {
44          ResourceBundle resourceBundle = ITrackerResources.getBundle((Locale) null);
45          Assert.assertNotNull(resourceBundle);
46          Assert.assertEquals(ITrackerResources.getBundle(), resourceBundle);
47      }
48  
49      @Test
50      public void testGetStringWithLocaleWithDirtyKey() {
51          ResourceBundle b = ITrackerResources.getBundle(Locale.ENGLISH);
52          final String key = "itracker.web.attr.admin";
53          ITrackerResources.clearKeyFromBundles(key, true);
54  
55          final String value_1 = ITrackerResources.getString(key, ITrackerResources.getLocale());
56          Assert.assertEquals("Admin", value_1);
57          ((ITrackerResourceBundle) b).removeValue(key, true);
58          String value_2 = ITrackerResources.getString(key, ITrackerResources.getLocale());
59  
60  
61          // When a language items is removed, the default is loaded from properties.
62          Assert.assertEquals("Admin", value_2);
63      }
64  
65      @Test
66      public void testGetStringWithLocaleWithDirtyKey_GB() {
67          final String key = "en_key";
68  
69          final ResourceBundle b = ITrackerResources.getBundle("en_GB");
70          final String value_1 = ITrackerResources.getString(key, b.getLocale());
71          Assert.assertEquals("en_value-en_GB", value_1);
72          ((ITrackerResourceBundle) b).removeValue(key, true);
73          String value_2 = ITrackerResources.getString(key, b.getLocale());
74  
75          // When a language items is removed, the default is loaded from database.
76          Assert.assertEquals("en_value-en_GB", value_2);
77      }
78  
79      @Test
80      public void testGetStringWithLocaleWithDirtyKey_removed_en() {
81          final String key = "itracker.resolution.1";
82          ITrackerResources.clearKeyFromBundles(key, true);
83  
84          final Locale en_l = new Locale("en");
85          final ResourceBundle en = ITrackerResources.getBundle(en_l);
86          final String en_1 = ITrackerResources.getString(key, en_l);
87          Assert.assertEquals("en_1", "Fixed_en", en_1);
88          ((ITrackerResourceBundle) en).removeValue(key, true);
89          final String en_2 = ITrackerResources.getString(key, en_l);
90          // When a language items is removed, the default is loaded from database.
91          Assert.assertEquals("en_2", "Fixed_en", en_2);
92  
93          // When a language items is deleted, the default is loaded from properties.
94          configurationService.removeLanguageKey(key);
95  
96          try {
97              configurationService.getLanguageEntry(key, en_l);
98              fail("should throw MissingResourceException");
99          } catch (MissingResourceException e) {
100             // fine
101         }
102         ITrackerResources.clearKeyFromBundles(key, true);        final String en_3 = ITrackerResources.getString(key, en_l);
103         Assert.assertEquals("en_3", "Fixed", en_3);
104 
105     }
106 
107     @Test
108     public void testGetStringWithLocaleWithDirtyKey_removed_GB() {
109         final String key = "itracker.resolution.1";
110 
111         ITrackerResources.clearKeyFromBundles(key, true);
112 
113         final Locale GB_l = new Locale("en", "GB");
114         final ResourceBundle GB = ITrackerResources.getBundle(GB_l);
115         final String GB_1 = ITrackerResources.getString(key, GB_l);
116         ((ITrackerResourceBundle) GB).removeValue(key, true);
117         Assert.assertEquals("GB_1", "Fixed_GB", GB_1);
118         final String GB_2 = ITrackerResources.getString(key, GB_l);
119         // When a language items is removed, the default is loaded from database.
120         Assert.assertEquals("GB_2", "Fixed_GB", GB_2);
121 
122         // When a language items is deleted, the default is loaded from properties.
123         configurationService.removeLanguageKey(key);
124 
125         try {
126             configurationService.getLanguageEntry(key, GB_l);
127             fail("should throw MissingResourceException");
128         } catch (MissingResourceException e) {
129             // fine
130         }
131         ITrackerResources.clearKeyFromBundles(key, true);
132         final String GB_3 = ITrackerResources.getString(key, GB_l);
133         Assert.assertEquals("GB_3", "Fixed", GB_3);
134     }
135 
136     @Test
137     public void testGetStringWithLocaleWithDirtyKey_removed_es() throws Exception {
138         final String key = "itracker.resolution.1";
139 
140         ITrackerResources.clearKeyFromBundles(key, true);
141 
142         final Locale es_l = new Locale("es");
143         final ResourceBundle es = ITrackerResources.getBundle(es_l);
144         final String es_1 = ITrackerResources.getString(key, es_l);
145         Assert.assertEquals("es_1", "Fixed_es", es_1);
146         ((ITrackerResourceBundle) es).removeValue(key, true);
147         final String es_2 = ITrackerResources.getString(key, es_l);
148         // When a language items is removed, the default is loaded from database.
149         Assert.assertEquals("es_2", "Fixed_es", es_2);
150 
151         // When a language items is deleted, the default is loaded from properties.
152         configurationService.removeLanguageKey(key);
153 
154         try {
155             configurationService.getLanguageEntry(key, es_l);
156             fail("should throw MissingResourceException");
157         } catch (MissingResourceException e) {
158             // fine
159         }
160         ITrackerResources.clearKeyFromBundles(key, true);
161         //((ITrackerResourceBundle) es).removeValue(key, true);
162         final String es_3 = ITrackerResources.getString(key, es_l);
163         Assert.assertEquals("es_3", "Resulta", es_3);
164 
165     }
166 
167     @Test
168     public void testGetStringWithLocaleWithDirtyKey_removed_MX() {
169         final String key = "itracker.resolution.1";
170 
171         ITrackerResources.clearKeyFromBundles(key, true);
172 
173         final Locale MX_l = new Locale("es", "MX");
174         final ResourceBundle MX = ITrackerResources.getBundle(MX_l);
175         final String MX_1 = ITrackerResources.getString(key, MX_l);
176         Assert.assertEquals("MX_1", "Fixed_MX", MX_1);
177         ((ITrackerResourceBundle) MX).removeValue(key, true);
178         final String MX_2 = ITrackerResources.getString(key, MX_l);
179         // When a language items is removed, the default is loaded from database.
180         Assert.assertEquals("MX_2", "Fixed_MX", MX_2);
181 
182         // When a language items is deleted, the default is loaded from properties.
183         configurationService.removeLanguageKey(key);
184 
185         try {
186             configurationService.getLanguageEntry(key, MX_l);
187             fail("should throw MissingResourceException");
188         } catch (MissingResourceException e) {
189             // fine
190         }
191         ITrackerResources.clearKeyFromBundles(key, true);
192         final String MX_3 = ITrackerResources.getString(key, MX_l);
193         // When a language items is deleted, the default is loaded from properties.
194         Assert.assertEquals("MX_3", "Reparado", MX_3);
195 
196     }
197 
198     @Test
199     public void testGetEditBundleNullLocale() {
200         //TODO: set languageDAO of ConfigurationService
201         ResourceBundle resourceBundle = ITrackerResources.getEditBundle(null);
202         Assert.assertNotNull(resourceBundle);
203         Assert.assertEquals(ITrackerResources.getLocale(), resourceBundle.getLocale());
204         Enumeration<String> keys = resourceBundle.getKeys(); // keys of copy bundle
205         while (keys.hasMoreElements()) {
206             String key = keys.nextElement();
207             Assert.assertEquals(ITrackerResources.getString(key), resourceBundle.getString(key));
208         }
209         keys = ITrackerResources.getBundle().getKeys(); // keys of original bundle
210         while (keys.hasMoreElements()) {
211             String key = keys.nextElement();
212             Assert.assertEquals(ITrackerResources.getString(key), resourceBundle.getString(key));
213         }
214     }
215 
216     @Before
217     public void setup() {
218         this.configurationService =
219                 (ConfigurationService) applicationContext.getBean("configurationService");
220         configurationService.resetConfigurationCache(Configuration.Type.locale);
221 
222     }
223 
224     @Override
225     protected String[] getDataSetFiles() {
226         return new String[]{
227                 "dataset/languagebean_dataset.xml"
228         };
229     }
230 
231 }