001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math3.exception.util;
018
019
020 import java.text.MessageFormat;
021 import java.util.Enumeration;
022 import java.util.Locale;
023 import java.util.ResourceBundle;
024
025 import org.junit.Assert;
026
027 import org.junit.Test;
028
029 public class LocalizedFormatsTest {
030
031 @Test
032 public void testMessageNumber() {
033 Assert.assertEquals(313, LocalizedFormats.values().length);
034 }
035
036 @Test
037 public void testAllKeysPresentInPropertiesFiles() {
038 final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/");
039 for (final String language : new String[] { "fr" } ) {
040 ResourceBundle bundle =
041 ResourceBundle.getBundle("assets/" + path, new Locale(language));
042 for (LocalizedFormats message : LocalizedFormats.values()) {
043 final String messageKey = message.toString();
044 boolean keyPresent = false;
045 for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
046 keyPresent |= messageKey.equals(keys.nextElement());
047 }
048 Assert.assertTrue("missing key \"" + message.name() + "\" for language " + language,
049 keyPresent);
050 }
051 Assert.assertEquals(language, bundle.getLocale().getLanguage());
052 }
053
054 }
055
056 @Test
057 public void testAllPropertiesCorrespondToKeys() {
058 final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/");
059 for (final String language : new String[] { "fr" } ) {
060 ResourceBundle bundle =
061 ResourceBundle.getBundle("assets/" + path, new Locale(language));
062 for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
063 final String propertyKey = keys.nextElement();
064 try {
065 Assert.assertNotNull(LocalizedFormats.valueOf(propertyKey));
066 } catch (IllegalArgumentException iae) {
067 Assert.fail("unknown key \"" + propertyKey + "\" in language " + language);
068 }
069 }
070 Assert.assertEquals(language, bundle.getLocale().getLanguage());
071 }
072
073 }
074
075 @Test
076 public void testNoMissingFrenchTranslation() {
077 for (LocalizedFormats message : LocalizedFormats.values()) {
078 String translated = message.getLocalizedString(Locale.FRENCH);
079 Assert.assertFalse(message.name(), translated.toLowerCase().contains("missing translation"));
080 }
081 }
082
083 @Test
084 public void testNoOpEnglishTranslation() {
085 for (LocalizedFormats message : LocalizedFormats.values()) {
086 String translated = message.getLocalizedString(Locale.ENGLISH);
087 Assert.assertEquals(message.getSourceString(), translated);
088 }
089 }
090
091 @Test
092 public void testVariablePartsConsistency() {
093 for (final String language : new String[] { "fr" } ) {
094 Locale locale = new Locale(language);
095 for (LocalizedFormats message : LocalizedFormats.values()) {
096 MessageFormat source = new MessageFormat(message.getSourceString());
097 MessageFormat translated = new MessageFormat(message.getLocalizedString(locale));
098 Assert.assertEquals(message.name() + " (" + language + ")",
099 source.getFormatsByArgumentIndex().length,
100 translated.getFormatsByArgumentIndex().length);
101 }
102 }
103 }
104
105 }