View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math3.exception.util;
18  
19  
20  import java.text.MessageFormat;
21  import java.util.Enumeration;
22  import java.util.Locale;
23  import java.util.ResourceBundle;
24  
25  import org.junit.Assert;
26  
27  import org.junit.Test;
28  
29  public class LocalizedFormatsTest {
30  
31      @Test
32      public void testMessageNumber() {
33          Assert.assertEquals(318, LocalizedFormats.values().length);
34      }
35  
36      @Test
37      public void testAllKeysPresentInPropertiesFiles() {
38          final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/");
39          for (final String language : new String[] { "fr" } ) {
40              ResourceBundle bundle =
41                  ResourceBundle.getBundle("assets/" + path, new Locale(language));
42              for (LocalizedFormats message : LocalizedFormats.values()) {
43                  final String messageKey = message.toString();
44                  boolean keyPresent = false;
45                  for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
46                      keyPresent |= messageKey.equals(keys.nextElement());
47                  }
48                  Assert.assertTrue("missing key \"" + message.name() + "\" for language " + language,
49                                    keyPresent);
50              }
51              Assert.assertEquals(language, bundle.getLocale().getLanguage());
52          }
53  
54      }
55  
56      @Test
57      public void testAllPropertiesCorrespondToKeys() {
58          final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/");
59          for (final String language : new String[] { "fr" } ) {
60              ResourceBundle bundle =
61                  ResourceBundle.getBundle("assets/" + path, new Locale(language));
62              for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
63                  final String propertyKey = keys.nextElement();
64                  try {
65                      Assert.assertNotNull(LocalizedFormats.valueOf(propertyKey));
66                  } catch (IllegalArgumentException iae) {
67                      Assert.fail("unknown key \"" + propertyKey + "\" in language " + language);
68                  }
69              }
70              Assert.assertEquals(language, bundle.getLocale().getLanguage());
71          }
72  
73      }
74  
75      @Test
76      public void testNoMissingFrenchTranslation() {
77          for (LocalizedFormats message : LocalizedFormats.values()) {
78              String translated = message.getLocalizedString(Locale.FRENCH);
79              Assert.assertFalse(message.name(), translated.toLowerCase().contains("missing translation"));
80          }
81      }
82  
83      @Test
84      public void testNoOpEnglishTranslation() {
85          for (LocalizedFormats message : LocalizedFormats.values()) {
86              String translated = message.getLocalizedString(Locale.ENGLISH);
87              Assert.assertEquals(message.getSourceString(), translated);
88          }
89      }
90  
91      @Test
92      public void testVariablePartsConsistency() {
93          for (final String language : new String[] { "fr" } ) {
94              Locale locale = new Locale(language);
95              for (LocalizedFormats message : LocalizedFormats.values()) {
96                  MessageFormat source     = new MessageFormat(message.getSourceString());
97                  MessageFormat translated = new MessageFormat(message.getLocalizedString(locale));
98                  Assert.assertEquals(message.name() + " (" + language + ")",
99                                      source.getFormatsByArgumentIndex().length,
100                                     translated.getFormatsByArgumentIndex().length);
101             }
102         }
103     }
104 
105 }