1   /* Copyright 2013-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.rugged.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 RuggedMessagesTest {
30  
31      private final String[] LANGUAGES_LIST = { "da", "de", "en", "es", "fr", "gl", "it", "no", "ro" } ;
32      @Test
33      public void testMessageNumber() {
34          Assertions.assertEquals(35, RuggedMessages.values().length);
35      }
36  
37      @Test
38      public void testAllKeysPresentInPropertiesFiles() {
39          for (final String language : LANGUAGES_LIST) {
40              ResourceBundle bundle = ResourceBundle.getBundle("assets/org/orekit/rugged/RuggedMessages",
41                                                        Locale.forLanguageTag(language),
42                                                        new UTF8Control());
43  
44              for (RuggedMessages message : RuggedMessages.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,
51                                    "missing key \"" + message.name() + "\" for language " + language);
52              }
53              Assertions.assertEquals(language, bundle.getLocale().getLanguage());
54          }
55  
56      }
57  
58      @Test
59      public void testAllPropertiesCorrespondToKeys() {
60          for (final String language : LANGUAGES_LIST) {
61              ResourceBundle bundle = ResourceBundle.getBundle("assets/org/orekit/rugged/RuggedMessages",
62                                                        Locale.forLanguageTag(language),
63                                                        new UTF8Control());
64              for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
65                  final String propertyKey = keys.nextElement();
66                  try {
67                      Assertions.assertNotNull(RuggedMessages.valueOf(propertyKey));
68                  } catch (IllegalArgumentException iae) {
69                      Assertions.fail("unknown key \"" + propertyKey + "\" in language " + language);
70                  }
71              }
72              Assertions.assertEquals(language, bundle.getLocale().getLanguage());
73          }
74  
75      }
76  
77      @Test
78      public void testNoMissingFrenchTranslation() {
79          for (RuggedMessages message : RuggedMessages.values()) {
80              String translated = message.getLocalizedString(Locale.FRENCH);
81              Assertions.assertFalse(translated.toLowerCase().contains("missing translation"), message.name());
82          }
83      }
84  
85      @Test
86      public void testNoOpEnglishTranslation() {
87          for (RuggedMessages message : RuggedMessages.values()) {
88              String translated = message.getLocalizedString(Locale.ENGLISH);
89              Assertions.assertEquals(message.getSourceString(), translated);
90          }
91      }
92  
93      @Test
94      public void testMissingLanguageFallback() {
95          for (RuggedMessages message : RuggedMessages.values()) {
96              String translated = message.getLocalizedString(Locale.TRADITIONAL_CHINESE);
97              Assertions.assertEquals(message.getSourceString(), translated);
98          }
99      }
100 
101     @Test
102     public void testMissingLanguageMissingTranslation() {
103         Assertions.assertEquals(RuggedMessages.INTERNAL_ERROR.getSourceString(),
104                             RuggedMessages.INTERNAL_ERROR.getLocalizedString(Locale.KOREAN));
105         Assertions.assertEquals(RuggedMessages.NO_DEM_DATA.getSourceString(),
106                             RuggedMessages.NO_DEM_DATA.getLocalizedString(Locale.KOREAN));
107         Assertions.assertEquals("ABCDEF {0}",
108                             RuggedMessages.UNKNOWN_SENSOR.getLocalizedString(Locale.KOREAN));
109         Assertions.assertEquals(RuggedMessages.EMPTY_TILE.getSourceString(),
110                             RuggedMessages.EMPTY_TILE.getLocalizedString(Locale.KOREAN));
111     }
112 
113     @Test
114     public void testVariablePartsConsistency() {
115         for (final String language : LANGUAGES_LIST) {
116             Locale locale = Locale.forLanguageTag(language);
117             for (RuggedMessages message : RuggedMessages.values()) {
118                 MessageFormat source     = new MessageFormat(message.getSourceString());
119                 MessageFormat translated = new MessageFormat(message.getLocalizedString(locale));
120                 Assertions.assertEquals(source.getFormatsByArgumentIndex().length,
121                                     translated.getFormatsByArgumentIndex().length,
122                                     message.name() + " (" + language + ")");
123             }
124         }
125     }
126 
127 }