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.math4.legacy.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(328, 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      @Test
55      public void testAllPropertiesCorrespondToKeys() {
56          final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/");
57          for (final String language : new String[] {"fr"}) {
58              ResourceBundle bundle =
59                  ResourceBundle.getBundle("assets/" + path, new Locale(language));
60              for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
61                  final String propertyKey = keys.nextElement();
62                  try {
63                      Assert.assertNotNull(LocalizedFormats.valueOf(propertyKey));
64                  } catch (IllegalArgumentException iae) {
65                      Assert.fail("unknown key \"" + propertyKey + "\" in language " + language);
66                  }
67              }
68              Assert.assertEquals(language, bundle.getLocale().getLanguage());
69          }
70      }
71  
72      @Test
73      public void testNoMissingFrenchTranslation() {
74          for (LocalizedFormats message : LocalizedFormats.values()) {
75              String translated = message.getLocalizedString(Locale.FRENCH);
76              Assert.assertFalse(message.name(), translated.toLowerCase().contains("missing translation"));
77          }
78      }
79  
80      @Test
81      public void testNoOpEnglishTranslation() {
82          for (LocalizedFormats message : LocalizedFormats.values()) {
83              String translated = message.getLocalizedString(Locale.ENGLISH);
84              Assert.assertEquals(message.getSourceString(), translated);
85          }
86      }
87  
88      @Test
89      public void testVariablePartsConsistency() {
90          for (final String language : new String[] {"fr"}) {
91              Locale locale = new Locale(language);
92              for (LocalizedFormats message : LocalizedFormats.values()) {
93                  MessageFormat source     = new MessageFormat(message.getSourceString());
94                  MessageFormat translated = new MessageFormat(message.getLocalizedString(locale));
95                  Assert.assertEquals(message.name() + " (" + language + ")",
96                                      source.getFormatsByArgumentIndex().length,
97                                      translated.getFormatsByArgumentIndex().length);
98              }
99          }
100     }
101 }