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  import org.junit.Test;
27  
28  public class LocalizedFormatsTest {
29  
30      @Test
31      public void testMessageNumber() {
32          Assert.assertEquals(320, LocalizedFormats.values().length);
33      }
34  
35      @Test
36      public void testAllKeysPresentInPropertiesFiles() {
37          final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/");
38          for (final String language : new String[] { "fr" } ) {
39              ResourceBundle bundle =
40                  ResourceBundle.getBundle("assets/" + path, new Locale(language));
41              for (LocalizedFormats message : LocalizedFormats.values()) {
42                  final String messageKey = message.toString();
43                  boolean keyPresent = false;
44                  for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
45                      keyPresent |= messageKey.equals(keys.nextElement());
46                  }
47                  Assert.assertTrue("missing key \"" + message.name() + "\" for language " + language,
48                                    keyPresent);
49              }
50              Assert.assertEquals(language, bundle.getLocale().getLanguage());
51          }
52  
53      }
54  
55      @Test
56      public void testAllPropertiesCorrespondToKeys() {
57          final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/");
58          for (final String language : new String[] { "fr" } ) {
59              ResourceBundle bundle =
60                  ResourceBundle.getBundle("assets/" + path, new Locale(language));
61              for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
62                  final String propertyKey = keys.nextElement();
63                  try {
64                      Assert.assertNotNull(LocalizedFormats.valueOf(propertyKey));
65                  } catch (IllegalArgumentException iae) {
66                      Assert.fail("unknown key \"" + propertyKey + "\" in language " + language);
67                  }
68              }
69              Assert.assertEquals(language, bundle.getLocale().getLanguage());
70          }
71  
72      }
73  
74      @Test
75      public void testNoMissingFrenchTranslation() {
76          for (LocalizedFormats message : LocalizedFormats.values()) {
77              String translated = message.getLocalizedString(Locale.FRENCH);
78              Assert.assertFalse(message.name(), translated.toLowerCase().contains("missing translation"));
79          }
80      }
81  
82      @Test
83      public void testNoOpEnglishTranslation() {
84          for (LocalizedFormats message : LocalizedFormats.values()) {
85              String translated = message.getLocalizedString(Locale.ENGLISH);
86              Assert.assertEquals(message.getSourceString(), translated);
87          }
88      }
89  
90      @Test
91      public void testVariablePartsConsistency() {
92          for (final String language : new String[] { "fr" } ) {
93              Locale locale = new Locale(language);
94              for (LocalizedFormats message : LocalizedFormats.values()) {
95                  MessageFormat source     = new MessageFormat(message.getSourceString());
96                  MessageFormat translated = new MessageFormat(message.getLocalizedString(locale));
97                  Assert.assertEquals(message.name() + " (" + language + ")",
98                                      source.getFormatsByArgumentIndex().length,
99                                      translated.getFormatsByArgumentIndex().length);
100             }
101         }
102     }
103 
104 }