1   /* Copyright 2002-2025 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  
20  import java.text.MessageFormat;
21  import java.util.Enumeration;
22  import java.util.Locale;
23  import java.util.ResourceBundle;
24  
25  import org.hipparchus.exception.UTF8Control;
26  import org.junit.jupiter.api.Assertions;
27  import org.junit.jupiter.api.Test;
28  
29  public class OrekitMessagesTest {
30  
31      private final String[] LANGUAGES_LIST = { "ca", "da", "de", "el", "en", "es", "fr", "gl", "it", "no", "ro" };
32  
33      @Test
34      public void testMessageNumber() {
35          Assertions.assertEquals(315, OrekitMessages.values().length);
36      }
37  
38      @Test
39      public void testAllKeysPresentInPropertiesFiles() {
40          for (final String language : LANGUAGES_LIST) {
41              ResourceBundle bundle = ResourceBundle.getBundle("assets/org/orekit/localization/OrekitMessages",
42                                                               Locale.forLanguageTag(language),
43                                                               new UTF8Control());
44              for (OrekitMessages message : OrekitMessages.values()) {
45                  final String messageKey = message.toString();
46                  boolean keyPresent = false;
47                  for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
48                      keyPresent |= messageKey.equals(keys.nextElement());
49                  }
50                  Assertions.assertTrue(keyPresent,"missing key \"" + message.name() + "\" for language " + language);
51              }
52              Assertions.assertEquals(language, bundle.getLocale().getLanguage());
53          }
54  
55      }
56  
57      @Test
58      public void testAllPropertiesCorrespondToKeys() {
59          for (final String language : LANGUAGES_LIST) {
60              ResourceBundle bundle = ResourceBundle.getBundle("assets/org/orekit/localization/OrekitMessages",
61                                                               Locale.forLanguageTag(language),
62                                                               new UTF8Control());
63              for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
64                  final String propertyKey = keys.nextElement();
65                  try {
66                      Assertions.assertNotNull(OrekitMessages.valueOf(propertyKey));
67                  } catch (IllegalArgumentException iae) {
68                      Assertions.fail("unknown key \"" + propertyKey + "\" in language " + language);
69                  }
70              }
71              Assertions.assertEquals(language, bundle.getLocale().getLanguage());
72          }
73  
74      }
75  
76      @Test
77      public void testNoMissingFrenchTranslation() {
78  
79          for (OrekitMessages message : OrekitMessages.values()) {
80              String translated = message.getLocalizedString(Locale.FRENCH);
81              // To detect a missing translation, check if the returned string is the original text in English.
82              Assertions.assertNotEquals(message.name(), translated, message.getSourceString());
83           }
84      }
85  
86      @Test
87      public void testNoOpEnglishTranslation() {
88          for (OrekitMessages message : OrekitMessages.values()) {
89              String translated = message.getLocalizedString(Locale.ENGLISH);
90  
91              // Check that the original message is not empty.
92              Assertions.assertFalse(message.getSourceString().isEmpty(), message.name());
93  
94              // Check that both texts are the same
95              Assertions.assertEquals(message.getSourceString(), translated,message.name());
96  
97          }
98      }
99  
100     @Test
101     public void testVariablePartsConsistency() {
102         for (final String language : LANGUAGES_LIST) {
103             Locale locale = Locale.forLanguageTag(language);
104             for (OrekitMessages message : OrekitMessages.values()) {
105                 MessageFormat source = new MessageFormat(message.getSourceString());
106                 MessageFormat translated = new MessageFormat(message.getLocalizedString(locale));
107                 Assertions.assertEquals(source.getFormatsByArgumentIndex().length,
108                         translated.getFormatsByArgumentIndex().length,message.name() + " (" + language + ")");
109             }
110         }
111     }
112 
113 }