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  /**
18   * This file is an adapted version of OrekitMessagesTest test class
19   * for the <a href="https://www.orekit.org/forge/projects/orekit">orekit</a> library.
20   * The original file is distributed under the terms of the Apache 2 license and is:
21   * Copyright 2010 CS Communication & Systèmes
22   */
23  package org.apache.commons.nabla;
24  
25  
26  import java.text.MessageFormat;
27  import java.util.Enumeration;
28  import java.util.Locale;
29  import java.util.ResourceBundle;
30  
31  import junit.framework.Assert;
32  
33  import org.junit.Test;
34  
35  public class NablaMessagesTest {
36  
37      @Test
38      public void testMessageNumber() {
39          Assert.assertEquals(17, NablaMessages.values().length);
40      }
41  
42      @Test
43      public void testAllKeysPresentInPropertiesFiles() {
44          final String path = NablaMessages.class.getName().replaceAll("\\.", "/");
45          for (final String language : new String[] { "fr" } ) {
46              ResourceBundle bundle =
47                  ResourceBundle.getBundle("assets/" + path, new Locale(language));
48              for (NablaMessages message : NablaMessages.values()) {
49                  final String messageKey = message.toString();
50                  boolean keyPresent = false;
51                  for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
52                      keyPresent |= messageKey.equals(keys.nextElement());
53                  }
54                  Assert.assertTrue("missing key \"" + message.name() + "\" for language " + language,
55                                    keyPresent);
56              }
57              Assert.assertEquals(language, bundle.getLocale().getLanguage());
58          }
59  
60      }
61  
62      @Test
63      public void testAllPropertiesCorrespondToKeys() {
64          final String path = NablaMessages.class.getName().replaceAll("\\.", "/");
65          for (final String language : new String[] { "fr" } ) {
66              ResourceBundle bundle =
67                  ResourceBundle.getBundle("assets/" + path, new Locale(language));
68              for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
69                  final String propertyKey = keys.nextElement();
70                  try {
71                      Assert.assertNotNull(NablaMessages.valueOf(propertyKey));
72                  } catch (IllegalArgumentException iae) {
73                      Assert.fail("unknown key \"" + propertyKey + "\" in language " + language);
74                  }
75              }
76              Assert.assertEquals(language, bundle.getLocale().getLanguage());
77          }
78  
79      }
80  
81      @Test
82      public void testNoMissingFrenchTranslation() {
83          for (NablaMessages message : NablaMessages.values()) {
84              String translated = message.getLocalizedString(Locale.FRENCH);
85              Assert.assertFalse(message.name(), translated.toLowerCase().contains("missing translation"));
86          }
87      }
88  
89      @Test
90      public void testNoOpEnglishTranslation() {
91          for (NablaMessages message : NablaMessages.values()) {
92              String translated = message.getLocalizedString(Locale.ENGLISH);
93              Assert.assertEquals(message.getSourceString(), translated);
94          }
95      }
96  
97      @Test
98      public void testVariablePartsConsistency() {
99          for (final String language : new String[] { "fr" } ) {
100             Locale locale = new Locale(language);
101             for (NablaMessages message : NablaMessages.values()) {
102                 MessageFormat source     = new MessageFormat(message.getSourceString());
103                 MessageFormat translated = new MessageFormat(message.getLocalizedString(locale));
104                 Assert.assertEquals(message.name() + " (" + language + ")",
105                                     source.getFormatsByArgumentIndex().length,
106                                     translated.getFormatsByArgumentIndex().length);
107             }
108         }
109     }
110 
111 }