Skip to content

Commit

Permalink
Improved test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
maisonobe committed Jul 30, 2024
1 parent 2acfdbd commit 2104484
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.io.Serializable;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
Expand Down Expand Up @@ -58,19 +57,15 @@ public interface Localizable extends Serializable {
*/
default String getLocalizedString(final String baseName, final String key, final Locale locale) {

try {
final ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale, new UTF8Control());
if (bundle.getLocale().getLanguage().equals(locale.getLanguage()))
final ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale, new UTF8Control());
if (bundle.getLocale().getLanguage().equals(locale.getLanguage()))
{
final String translated = bundle.getString(key);
if (!(translated.isEmpty() || translated.toLowerCase().contains("missing translation")))
{
final String translated = bundle.getString(key);
if (!(translated.isEmpty() || translated.toLowerCase().contains("missing translation")))
{
// the value of the resource is the translated format
return translated;
}
// the value of the resource is the translated format
return translated;
}
} catch (MissingResourceException mre) { // NOPMD
// do nothing here
}

// either the locale is not supported or the resource is not translated, or
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Hipparchus project under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The Hipparchus project 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
*
* https://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.hipparchus.exception;

import java.util.Locale;

/**
* Intentionally incomplete localizable, for testing purposes
* @author Luc Maisonobe
*/
enum IntentionallyIncompleteLocalizable implements Localizable
{
MESSAGE_WITHOUT_ARGUMENT("message without argument"),

MESSAGE_WITH_ONE_ARGUMENT("message with one argument {0}"),

MESSAGE_WITH_TWO_ARGUMENTS("message with two arguments {0}, {1}");

/**
* Simple constructor.
* @param sourceFormat source English format to use when no
* localized version is available
*/
IntentionallyIncompleteLocalizable(final String sourceFormat)
{
this.sourceFormat = sourceFormat;
}

/** Source English format. */
private final String sourceFormat;

/** {@inheritDoc} */
@Override
public String getSourceString()
{
return sourceFormat;
}

/** {@inheritDoc} */
@Override
public String getLocalizedString(final Locale locale)
{
return getLocalizedString("assets/" + IntentionallyIncompleteLocalizable.class.getName().replaceAll("\\.", "/"),
name(), locale);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Hipparchus project under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The Hipparchus project 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
*
* https://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.hipparchus.exception;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Locale;

/**
* Intentionally incomplete localizable, for testing purposes
* @author Luc Maisonobe
*/
public class IntentionallyIncompleteLocalizableTest
{
@Test
public void testFixedLocale() {
Assertions.assertEquals("message without argument",
IntentionallyIncompleteLocalizable.MESSAGE_WITHOUT_ARGUMENT.getLocalizedString(Locale.ENGLISH));
Assertions.assertEquals("message with one argument {0}",
IntentionallyIncompleteLocalizable.MESSAGE_WITH_ONE_ARGUMENT.getLocalizedString(Locale.ENGLISH));
Assertions.assertEquals("message with two arguments {0}, {1}",
IntentionallyIncompleteLocalizable.MESSAGE_WITH_TWO_ARGUMENTS.getLocalizedString(Locale.ENGLISH));
}

@Test
public void testUnsupportedLocale() {
Assertions.assertEquals("message without argument",
IntentionallyIncompleteLocalizable.MESSAGE_WITHOUT_ARGUMENT.getLocalizedString(Locale.TRADITIONAL_CHINESE));
Assertions.assertEquals("message with one argument {0}",
IntentionallyIncompleteLocalizable.MESSAGE_WITH_ONE_ARGUMENT.getLocalizedString(Locale.TRADITIONAL_CHINESE));
Assertions.assertEquals("message with two arguments {0}, {1}",
IntentionallyIncompleteLocalizable.MESSAGE_WITH_TWO_ARGUMENTS.getLocalizedString(Locale.TRADITIONAL_CHINESE));
}

@Test
public void testIncompleteTranslations() {
Assertions.assertEquals("message sans argument",
IntentionallyIncompleteLocalizable.MESSAGE_WITHOUT_ARGUMENT.getLocalizedString(Locale.FRENCH));
Assertions.assertEquals("message with one argument {0}",
IntentionallyIncompleteLocalizable.MESSAGE_WITH_ONE_ARGUMENT.getLocalizedString(Locale.FRENCH));
Assertions.assertEquals("message with two arguments {0}, {1}",
IntentionallyIncompleteLocalizable.MESSAGE_WITH_TWO_ARGUMENTS.getLocalizedString(Locale.FRENCH));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MESSAGE_WITHOUT_ARGUMENT = message without argument
MESSAGE_WITH_ONE_ARGUMENT = message with one argument {0}
MESSAGE_WITH_TWO_ARGUMENTS = message with two arguments {0}, {1}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MESSAGE_WITHOUT_ARGUMENT = message sans argument
MESSAGE_WITH_ONE_ARGUMENT = <MISSING TRANSLATION>
MESSAGE_WITH_TWO_ARGUMENTS =
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ void testAllTechniquesEvaluation() {

@Test
void testPercentileWithTechnique() {
reset (50, Percentile.EstimationType.LEGACY);;
reset (50, Percentile.EstimationType.LEGACY);
final Percentile p = getUnivariateStatistic();
assertEquals(Percentile.EstimationType.LEGACY, p.getEstimationType());
assertNotEquals(Percentile.EstimationType.R_1, p.getEstimationType());
Expand Down

0 comments on commit 2104484

Please sign in to comment.