View Javadoc
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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.exception;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import java.lang.reflect.InvocationTargetException;
27  import java.text.MessageFormat;
28  import java.util.Enumeration;
29  import java.util.Locale;
30  import java.util.ResourceBundle;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertFalse;
34  import static org.junit.jupiter.api.Assertions.assertNotNull;
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  import static org.junit.jupiter.api.Assertions.fail;
37  
38  public abstract class LocalizedFormatsAbstractTest {
39  
40      protected abstract Class<? extends Enum<?>> getFormatsClass();
41      protected abstract int getExpectedNumber();
42  
43      private Localizable[] getValues() {
44          Localizable[] localizable = null;
45          try {
46              Object[] a = (Object[]) getFormatsClass().getMethod("values").invoke(null);
47              localizable = new Localizable[a.length];
48              for (int i = 0; i < a.length; ++i) {
49                  localizable[i] = (Localizable) a[i];
50              }
51          } catch (NoSuchMethodException | SecurityException | IllegalAccessException |
52                          IllegalArgumentException | InvocationTargetException e) {
53              fail(e.getLocalizedMessage());
54          }
55          return localizable;
56      }
57  
58      private Localizable valueOf(String s) {
59          Localizable localizable = null;
60          try {
61              localizable = (Localizable) getFormatsClass().getMethod("valueOf", String.class).invoke(null, s);
62          } catch (IllegalArgumentException iae) {
63              localizable = null;
64          } catch (NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException e) {
65              fail(s + " <-> " + e.getLocalizedMessage());
66          }
67          return localizable;
68      }
69  
70      @Test
71      public void testMessageNumber() {
72          assertEquals(getExpectedNumber(), getValues().length);
73      }
74  
75      @Test
76      public void testAllKeysPresentInPropertiesFiles() {
77          Class<? extends Enum<?>> c = getFormatsClass();
78          final String path = c.getName().replaceAll("\\.", "/");
79          for (final String language : new String[] { "fr" } ) {
80              ResourceBundle bundle =
81                  ResourceBundle.getBundle("assets/" + path, new Locale(language), c.getClassLoader());
82              for (Localizable message : getValues()) {
83                  final String messageKey = message.toString();
84                  boolean keyPresent = false;
85                  for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
86                      keyPresent |= messageKey.equals(keys.nextElement());
87                  }
88                  assertTrue(keyPresent,
89                                    "missing key \"" + message.toString() + "\" for language " + language);
90              }
91              assertEquals(language, bundle.getLocale().getLanguage());
92          }
93  
94      }
95  
96      @Test
97      public void testAllPropertiesCorrespondToKeys() {
98          Class<? extends Enum<?>> c = getFormatsClass();
99          final String path = c.getName().replaceAll("\\.", "/");
100         for (final String language : new String[] { "fr" } ) {
101             ResourceBundle bundle =
102                 ResourceBundle.getBundle("assets/" + path, new Locale(language), c.getClassLoader());
103             for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
104                 final String propertyKey = keys.nextElement();
105                 try {
106                     assertNotNull(valueOf(propertyKey));
107                 } catch (IllegalArgumentException iae) {
108                     fail("unknown key \"" + propertyKey + "\" in language " + language);
109                 }
110             }
111             assertEquals(language, bundle.getLocale().getLanguage());
112         }
113 
114     }
115 
116     @Test
117     public void testNoMissingFrenchTranslation() {
118         for (Localizable message : getValues()) {
119             String translated = message.getLocalizedString(Locale.FRENCH);
120             assertFalse(translated.toLowerCase().contains("missing translation"), message.toString());
121         }
122     }
123 
124     @Test
125     public void testNoOpEnglishTranslation() {
126         for (Localizable message : getValues()) {
127             String translated = message.getLocalizedString(Locale.ENGLISH);
128             assertEquals(message.getSourceString(), translated);
129         }
130     }
131 
132     @Test
133     public void testVariablePartsConsistency() {
134         for (final String language : new String[] { "fr" } ) {
135             Locale locale = new Locale(language);
136             for (Localizable message : getValues()) {
137                 MessageFormat source     = new MessageFormat(message.getSourceString());
138                 MessageFormat translated = new MessageFormat(message.getLocalizedString(locale));
139                 assertEquals(source.getFormatsByArgumentIndex().length,
140                                     translated.getFormatsByArgumentIndex().length,
141                                     message.toString() + " (" + language + ")");
142             }
143         }
144     }
145 
146 }