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