1   /* Copyright 2002-2022 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS 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.orekit.errors;
18  
19  import java.text.MessageFormat;
20  import java.util.Enumeration;
21  import java.util.Locale;
22  import java.util.ResourceBundle;
23  
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  public class OrekitMessagesTest {
28  
29      private final String[] LANGUAGES_LIST = { "da", "de", "el", "en", "es", "fr", "gl", "it", "no", "ro" };
30  
31      @Test
32      public void testMessageNumber() {
33          Assert.assertEquals(252, OrekitMessages.values().length);
34      }
35  
36      @Test
37      public void testAllKeysPresentInPropertiesFiles() {
38          for (final String language : LANGUAGES_LIST) {
39              ResourceBundle bundle = ResourceBundle.getBundle("assets/org/orekit/localization/OrekitMessages",
40                      new Locale(language), new OrekitMessages.UTF8Control());
41              for (OrekitMessages message : OrekitMessages.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, keyPresent);
48              }
49              Assert.assertEquals(language, bundle.getLocale().getLanguage());
50          }
51  
52      }
53  
54      @Test
55      public void testAllPropertiesCorrespondToKeys() {
56          for (final String language : LANGUAGES_LIST) {
57              ResourceBundle bundle = ResourceBundle.getBundle("assets/org/orekit/localization/OrekitMessages",
58                      new Locale(language), new OrekitMessages.UTF8Control());
59              for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
60                  final String propertyKey = keys.nextElement();
61                  try {
62                      Assert.assertNotNull(OrekitMessages.valueOf(propertyKey));
63                  } catch (IllegalArgumentException iae) {
64                      Assert.fail("unknown key \"" + propertyKey + "\" in language " + language);
65                  }
66              }
67              Assert.assertEquals(language, bundle.getLocale().getLanguage());
68          }
69  
70      }
71  
72      @Test
73      public void testNoMissingFrenchTranslation() {
74          for (OrekitMessages message : OrekitMessages.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 (OrekitMessages message : OrekitMessages.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 : LANGUAGES_LIST) {
91              Locale locale = new Locale(language);
92              for (OrekitMessages message : OrekitMessages.values()) {
93                  MessageFormat source = new MessageFormat(message.getSourceString());
94                  MessageFormat translated = new MessageFormat(message.getLocalizedString(locale));
95                  Assert.assertEquals(message.name() + " (" + language + ")", source.getFormatsByArgumentIndex().length,
96                          translated.getFormatsByArgumentIndex().length);
97              }
98          }
99      }
100 
101 }