From 66e53f0cfce32219874b84d3394b8019cb4fc53a Mon Sep 17 00:00:00 2001 From: Roman Leventov Date: Sun, 23 Dec 2018 12:53:52 +0100 Subject: [PATCH] [NUMBERS-88] Don't use the default locale in message formatting --- .../combinatorics/CombinatoricsException.java | 3 +- .../commons/numbers/core/ArithmeticUtils.java | 3 +- .../numbers/fraction/FractionException.java | 3 +- .../fraction/FractionParseException.java | 7 ++-- .../fraction/BigFractionFormatTest.java | 10 +++--- .../numbers/fraction/FractionFormatTest.java | 10 +++--- .../commons/numbers/gamma/GammaException.java | 3 +- .../commons/numbers/gamma/DigammaTest.java | 4 ++- .../commons/numbers/gamma/TrigammaTest.java | 4 ++- .../apache/commons/numbers/primes/Primes.java | 12 +++---- .../commons/numbers/primes/PrimesTest.java | 9 +++-- pom.xml | 33 +++++++++++++++++++ .../forbidden-apis/forbidden-apis.txt | 0 13 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 src/main/resources/forbidden-apis/forbidden-apis.txt diff --git a/commons-numbers-combinatorics/src/main/java/org/apache/commons/numbers/combinatorics/CombinatoricsException.java b/commons-numbers-combinatorics/src/main/java/org/apache/commons/numbers/combinatorics/CombinatoricsException.java index 651429358..ee0e057b4 100644 --- a/commons-numbers-combinatorics/src/main/java/org/apache/commons/numbers/combinatorics/CombinatoricsException.java +++ b/commons-numbers-combinatorics/src/main/java/org/apache/commons/numbers/combinatorics/CombinatoricsException.java @@ -17,6 +17,7 @@ package org.apache.commons.numbers.combinatorics; import java.text.MessageFormat; +import java.util.Locale; /** * Package private exception class with constants for frequently used messages. @@ -50,6 +51,6 @@ class CombinatoricsException extends IllegalArgumentException { /** {@inheritDoc} */ @Override public String getMessage() { - return MessageFormat.format(super.getMessage(), formatArguments); + return new MessageFormat(super.getMessage(), Locale.ENGLISH).format(formatArguments); } } diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java index 36a84f520..52b5ebed3 100644 --- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java +++ b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java @@ -18,6 +18,7 @@ import java.math.BigInteger; import java.text.MessageFormat; +import java.util.Locale; /** * Some useful, arithmetics related, additions to the built-in functions in @@ -779,7 +780,7 @@ private static class NumbersArithmeticException extends ArithmeticException { /** {@inheritDoc} */ @Override public String getMessage() { - return MessageFormat.format(super.getMessage(), formatArguments); + return new MessageFormat(super.getMessage(), Locale.ENGLISH).format(formatArguments); } } } diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/FractionException.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/FractionException.java index b89bb42e0..4c4f500f9 100644 --- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/FractionException.java +++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/FractionException.java @@ -17,6 +17,7 @@ package org.apache.commons.numbers.fraction; import java.text.MessageFormat; +import java.util.Locale; /** * Package private exception class with constants for frequently used messages. @@ -53,7 +54,7 @@ class FractionException extends ArithmeticException { /** {@inheritDoc} */ @Override public String getMessage() { - return MessageFormat.format(super.getMessage(), formatArguments); + return new MessageFormat(super.getMessage(), Locale.ENGLISH).format(formatArguments); } diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/FractionParseException.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/FractionParseException.java index 08886e040..99fbf5933 100644 --- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/FractionParseException.java +++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/FractionParseException.java @@ -17,8 +17,8 @@ package org.apache.commons.numbers.fraction; -import java.text.MessageFormat; import java.text.ParseException; +import java.util.Locale; /** * Error thrown when a string cannot be parsed into a fraction. @@ -36,8 +36,9 @@ class FractionParseException extends ParseException { * @param type type of target object */ FractionParseException(String source, int position, Class type) { - super(MessageFormat.format("string \"{0}\" unparseable (from position {1}) as an object of type {2}", - source, position, type), + super(String.format(Locale.ENGLISH, + "string \"%s\" unparseable (from position %d) as an object of type %s", + source, position, type), position); } } diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java index e8c56d6a6..3c7b8989c 100644 --- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java +++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java @@ -277,14 +277,14 @@ public void testParseBig() throws Exception { @Test public void testNumeratorFormat() { NumberFormat old = properFormat.getNumeratorFormat(); - NumberFormat nf = NumberFormat.getInstance(); + NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); properFormat.setNumeratorFormat(nf); Assert.assertEquals(nf, properFormat.getNumeratorFormat()); properFormat.setNumeratorFormat(old); old = improperFormat.getNumeratorFormat(); - nf = NumberFormat.getInstance(); + nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); improperFormat.setNumeratorFormat(nf); Assert.assertEquals(nf, improperFormat.getNumeratorFormat()); @@ -294,14 +294,14 @@ public void testNumeratorFormat() { @Test public void testDenominatorFormat() { NumberFormat old = properFormat.getDenominatorFormat(); - NumberFormat nf = NumberFormat.getInstance(); + NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); properFormat.setDenominatorFormat(nf); Assert.assertEquals(nf, properFormat.getDenominatorFormat()); properFormat.setDenominatorFormat(old); old = improperFormat.getDenominatorFormat(); - nf = NumberFormat.getInstance(); + nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); improperFormat.setDenominatorFormat(nf); Assert.assertEquals(nf, improperFormat.getDenominatorFormat()); @@ -313,7 +313,7 @@ public void testWholeFormat() { ProperBigFractionFormat format = (ProperBigFractionFormat)properFormat; NumberFormat old = format.getWholeFormat(); - NumberFormat nf = NumberFormat.getInstance(); + NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); format.setWholeFormat(nf); Assert.assertEquals(nf, format.getWholeFormat()); diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java index 1cb3d6d6c..b419d94ef 100644 --- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java +++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java @@ -295,14 +295,14 @@ public void testParseProperInvalidMinus() { @Test public void testNumeratorFormat() { NumberFormat old = properFormat.getNumeratorFormat(); - NumberFormat nf = NumberFormat.getInstance(); + NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); properFormat.setNumeratorFormat(nf); Assert.assertEquals(nf, properFormat.getNumeratorFormat()); properFormat.setNumeratorFormat(old); old = improperFormat.getNumeratorFormat(); - nf = NumberFormat.getInstance(); + nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); improperFormat.setNumeratorFormat(nf); Assert.assertEquals(nf, improperFormat.getNumeratorFormat()); @@ -312,14 +312,14 @@ public void testNumeratorFormat() { @Test public void testDenominatorFormat() { NumberFormat old = properFormat.getDenominatorFormat(); - NumberFormat nf = NumberFormat.getInstance(); + NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); properFormat.setDenominatorFormat(nf); Assert.assertEquals(nf, properFormat.getDenominatorFormat()); properFormat.setDenominatorFormat(old); old = improperFormat.getDenominatorFormat(); - nf = NumberFormat.getInstance(); + nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); improperFormat.setDenominatorFormat(nf); Assert.assertEquals(nf, improperFormat.getDenominatorFormat()); @@ -331,7 +331,7 @@ public void testWholeFormat() { ProperFractionFormat format = (ProperFractionFormat)properFormat; NumberFormat old = format.getWholeFormat(); - NumberFormat nf = NumberFormat.getInstance(); + NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); format.setWholeFormat(nf); Assert.assertEquals(nf, format.getWholeFormat()); diff --git a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/GammaException.java b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/GammaException.java index 7122f2eb7..cfad68b84 100644 --- a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/GammaException.java +++ b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/GammaException.java @@ -17,6 +17,7 @@ package org.apache.commons.numbers.gamma; import java.text.MessageFormat; +import java.util.Locale; /** * Package private exception class with constants for frequently used messages. @@ -47,6 +48,6 @@ class GammaException extends IllegalArgumentException { @Override public String getMessage() { - return MessageFormat.format(super.getMessage(), formatArguments); + return new MessageFormat(super.getMessage(), Locale.ENGLISH).format(formatArguments); } } diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/DigammaTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/DigammaTest.java index 17b459aae..f6621abbb 100644 --- a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/DigammaTest.java +++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/DigammaTest.java @@ -19,6 +19,8 @@ import org.junit.Assert; import org.junit.Test; +import java.util.Locale; + /** * Tests for {@link Digamma}. */ @@ -51,7 +53,7 @@ public void testDigammaSmallArgs() { -1e+17, -1e+18, -1e+19, -1e+20, -1e+21, -1e+22, -1e+23, -1e+24, -1e+25, -1e+26, -1e+27, -1e+28, -1e+29, -1e+30}; for (double n = 1; n < 30; n++) { - checkRelativeError(String.format("Test %.0f: ", n), expected[(int) (n - 1)], Digamma.value(Math.pow(10.0, -n)), 1e-8); + checkRelativeError(String.format(Locale.ENGLISH, "Test %.0f: ", n), expected[(int) (n - 1)], Digamma.value(Math.pow(10.0, -n)), 1e-8); } } diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java index 50caf0b6f..f64cfddc2 100644 --- a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java +++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java @@ -19,6 +19,8 @@ import org.junit.Assert; import org.junit.Test; +import java.util.Locale; + /** * Tests for {@link Trigamma}. */ @@ -45,7 +47,7 @@ public void testTrigamma() { 100, 0.010050166663333571395 }; for (int i = data.length - 2; i >= 0; i -= 2) { - Assert.assertEquals(String.format("trigamma %.0f", data[i]), data[i + 1], Trigamma.value(data[i]), eps); + Assert.assertEquals(String.format(Locale.ENGLISH, "trigamma %.0f", data[i]), data[i + 1], Trigamma.value(data[i]), eps); } } diff --git a/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java b/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java index a2bdc8cd3..a296351e8 100644 --- a/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java +++ b/commons-numbers-primes/src/main/java/org/apache/commons/numbers/primes/Primes.java @@ -16,7 +16,6 @@ */ package org.apache.commons.numbers.primes; -import java.text.MessageFormat; import java.util.List; @@ -30,8 +29,9 @@ */ public class Primes { - /** Exception message format when an argument is too small. */ - static final String NUMBER_TOO_SMALL = "{0} is smaller than the minimum ({1})"; + static String numberTooSmallMessage(int x, int min) { + return x + " is smaller than the minimum (" + min + ")"; + } /** * Hide utility class. @@ -71,8 +71,7 @@ public static boolean isPrime(int n) { */ public static int nextPrime(int n) { if (n < 0) { - throw new IllegalArgumentException( - MessageFormat.format(NUMBER_TOO_SMALL, n, 0)); + throw new IllegalArgumentException(numberTooSmallMessage(n, 0)); } if (n == 2) { return 2; @@ -117,8 +116,7 @@ public static int nextPrime(int n) { public static List primeFactors(int n) { if (n < 2) { - throw new IllegalArgumentException( - MessageFormat.format(NUMBER_TOO_SMALL, n, 2)); + throw new IllegalArgumentException(numberTooSmallMessage(n, 2)); } return SmallPrimes.trialDivision(n); diff --git a/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/PrimesTest.java b/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/PrimesTest.java index 983dd119d..f3f138764 100644 --- a/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/PrimesTest.java +++ b/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/PrimesTest.java @@ -17,7 +17,6 @@ package org.apache.commons.numbers.primes; -import java.text.MessageFormat; import java.util.HashSet; import java.util.List; @@ -99,9 +98,9 @@ public void testNextPrime() { Assert.assertEquals(Integer.MAX_VALUE, Primes.nextPrime(Integer.MAX_VALUE - 1)); Assert.assertEquals(Integer.MAX_VALUE, Primes.nextPrime(Integer.MAX_VALUE)); - assertNextPrimeException(Integer.MIN_VALUE, MessageFormat.format(Primes.NUMBER_TOO_SMALL,Integer.MIN_VALUE,0)); - assertNextPrimeException(-1, MessageFormat.format(Primes.NUMBER_TOO_SMALL,-1,0)); - assertNextPrimeException(-13, MessageFormat.format(Primes.NUMBER_TOO_SMALL,-13,0)); + assertNextPrimeException(Integer.MIN_VALUE, Primes.numberTooSmallMessage(Integer.MIN_VALUE,0)); + assertNextPrimeException(-1, Primes.numberTooSmallMessage(-1,0)); + assertNextPrimeException(-13, Primes.numberTooSmallMessage(-13,0)); } @Test @@ -148,7 +147,7 @@ static void checkPrimeFactors(List factors){ @Test public void testPrimeFactors() throws Exception { for (int i : BELOW_2) { - assertPrimeFactorsException(i, MessageFormat.format(Primes.NUMBER_TOO_SMALL,i,2)); + assertPrimeFactorsException(i, Primes.numberTooSmallMessage(i,2)); } for (int i : NOT_PRIMES) { List factors = Primes.primeFactors(i); diff --git a/pom.xml b/pom.xml index 96435680b..3ab502397 100644 --- a/pom.xml +++ b/pom.xml @@ -131,6 +131,7 @@ 1.8 3.9.0 3.0.0 + 2.6 2.7.2 ${basedir} @@ -235,6 +236,18 @@ + + de.thetaphi + forbiddenapis + + + + check + testCheck + + + + com.github.spotbugs spotbugs-maven-plugin @@ -289,6 +302,26 @@ NOTICE.txt,LICENSE.txt + + de.thetaphi + forbiddenapis + ${numbers.forbiddenapis.version} + + + + jdk-unsafe + jdk-deprecated + + jdk-non-portable + + + ${numbers.parent.dir}/src/main/resources/forbidden-apis/forbidden-apis.txt + + + diff --git a/src/main/resources/forbidden-apis/forbidden-apis.txt b/src/main/resources/forbidden-apis/forbidden-apis.txt new file mode 100644 index 000000000..e69de29bb