/* Copyright 2002-2022 CS GROUP
 * Licensed to CS GROUP (CS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * CS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.orekit.errors;

import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

import org.junit.Assert;
import org.junit.Test;

public class OrekitMessagesTest {

    private final String[] LANGUAGES_LIST = { "da", "de", "el", "en", "es", "fr", "gl", "it", "no", "ro" };

    @Test
    public void testMessageNumber() {
        Assert.assertEquals(252, OrekitMessages.values().length);
    }

    @Test
    public void testAllKeysPresentInPropertiesFiles() {
        for (final String language : LANGUAGES_LIST) {
            ResourceBundle bundle = ResourceBundle.getBundle("assets/org/orekit/localization/OrekitMessages",
                    new Locale(language), new OrekitMessages.UTF8Control());
            for (OrekitMessages message : OrekitMessages.values()) {
                final String messageKey = message.toString();
                boolean keyPresent = false;
                for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
                    keyPresent |= messageKey.equals(keys.nextElement());
                }
                Assert.assertTrue("missing key \"" + message.name() + "\" for language " + language, keyPresent);
            }
            Assert.assertEquals(language, bundle.getLocale().getLanguage());
        }

    }

    @Test
    public void testAllPropertiesCorrespondToKeys() {
        for (final String language : LANGUAGES_LIST) {
            ResourceBundle bundle = ResourceBundle.getBundle("assets/org/orekit/localization/OrekitMessages",
                    new Locale(language), new OrekitMessages.UTF8Control());
            for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
                final String propertyKey = keys.nextElement();
                try {
                    Assert.assertNotNull(OrekitMessages.valueOf(propertyKey));
                } catch (IllegalArgumentException iae) {
                    Assert.fail("unknown key \"" + propertyKey + "\" in language " + language);
                }
            }
            Assert.assertEquals(language, bundle.getLocale().getLanguage());
        }

    }

    @Test
    public void testNoMissingFrenchTranslation() {
        for (OrekitMessages message : OrekitMessages.values()) {
            String translated = message.getLocalizedString(Locale.FRENCH);
            Assert.assertFalse(message.name(), translated.toLowerCase().contains("missing translation"));
        }
    }

    @Test
    public void testNoOpEnglishTranslation() {
        for (OrekitMessages message : OrekitMessages.values()) {
            String translated = message.getLocalizedString(Locale.ENGLISH);
            Assert.assertEquals(message.getSourceString(), translated);
        }
    }

    @Test
    public void testVariablePartsConsistency() {
        for (final String language : LANGUAGES_LIST) {
            Locale locale = new Locale(language);
            for (OrekitMessages message : OrekitMessages.values()) {
                MessageFormat source = new MessageFormat(message.getSourceString());
                MessageFormat translated = new MessageFormat(message.getLocalizedString(locale));
                Assert.assertEquals(message.name() + " (" + language + ")", source.getFormatsByArgumentIndex().length,
                        translated.getFormatsByArgumentIndex().length);
            }
        }
    }

}

@@ -3051,32 +3051,55 @@
 Translation() %7B%0A
+    %09int errCount = 0;%0A
         for (Ore
@@ -3238,111 +3238,291 @@
     
-Assert.assertFalse(message.name(), translated.toLowerCase().contains(%22missing translation%22));%0A        %7D
+// To detect a missing translation, check if the returned string contains the original text in English.%0A            if (translated.compareTo(message.getSourceString()) == 0) %7B%0A                errCount = errCount + 1;%0A            %7D%0A        %7D%0A        Assert.assertEquals(0, errCount);%0A
 %0A   
@@ -3722,16 +3722,67 @@
 NGLISH);
+%0A%0A            // Check that both texts are the same
 %0A       
@@ -3846,16 +3846,163 @@
 lated);%0A
+            %0A            // Check that the original message is not empty.%0A            Assert.assertFalse(message.getSourceString().length() == 0);%0A
         

Fix issue-889: Unexpected behavior of two tests in OrekitMessagesTest Improved checks to detect missing translations into French or missing messages in the English version.