You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by cl...@apache.org on 2020/07/08 15:38:15 UTC

[activemq-artemis] branch master updated: ARTEMIS-2801 make HumanReadableByteCountTest locale sensitive

This is an automated email from the ASF dual-hosted git repository.

clebertsuconic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/master by this push:
     new 6b589b1  ARTEMIS-2801 make HumanReadableByteCountTest locale sensitive
     new 47fefae  This closes #3178
6b589b1 is described below

commit 6b589b1e7807744169f1e292dc723c7ea611ba92
Author: Justin Bertram <jb...@apache.org>
AuthorDate: Thu Jun 11 12:29:38 2020 -0500

    ARTEMIS-2801 make HumanReadableByteCountTest locale sensitive
---
 .../artemis/utils/HumanReadableByteCountTest.java  | 59 ++++++++++++++++------
 1 file changed, 44 insertions(+), 15 deletions(-)

diff --git a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/HumanReadableByteCountTest.java b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/HumanReadableByteCountTest.java
index 028714d..5c16835 100644
--- a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/HumanReadableByteCountTest.java
+++ b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/HumanReadableByteCountTest.java
@@ -17,6 +17,10 @@
 
 package org.apache.activemq.artemis.utils;
 
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -24,21 +28,46 @@ import static org.junit.Assert.assertEquals;
 public class HumanReadableByteCountTest {
 
    @Test
-   public void test() {
-      String[] suffixes = new String[] {"K", "M", "G", "T", "P", "E"};
-
-      assertEquals("0B", ByteUtil.getHumanReadableByteCount(0));
-      assertEquals("999.0B", ByteUtil.getHumanReadableByteCount(999));
-      assertEquals("500.0B", ByteUtil.getHumanReadableByteCount(500));
-
-      for (int i = 0, j = 3; i < 6; i++, j += 3) {
-         final long magnitude = (long) Math.pow(10, j);
-         assertEquals("1.0" + suffixes[i] + "B", ByteUtil.getHumanReadableByteCount(magnitude));
-         assertEquals("1.3" + suffixes[i] + "B", ByteUtil.getHumanReadableByteCount(magnitude + (long) (.25 * magnitude)));
-         assertEquals("1.5" + suffixes[i] + "B", ByteUtil.getHumanReadableByteCount(magnitude + (long) (.5 * magnitude)));
-         assertEquals("1.9" + suffixes[i] + "B", ByteUtil.getHumanReadableByteCount(magnitude + (long) (.9 * magnitude)));
-         assertEquals("4.2" + suffixes[i] + "B", ByteUtil.getHumanReadableByteCount(magnitude + (long) (3.2 * magnitude)));
-      }
+   public void testDefaultLocale() {
+      internalTest(Locale.getDefault());
+   }
+
+   @Test
+   public void testEnglishLocale() {
+      internalTest(Locale.ENGLISH);
+   }
+
+   @Test
+   public void testFrenchLocale() {
+      // This locale will use commas instead of periods
+      internalTest(Locale.FRENCH);
    }
 
+   private void internalTest(final Locale testLocale) {
+      // track the default
+      Locale defaultLocale = Locale.getDefault();
+
+      Locale.setDefault(testLocale);
+      DecimalFormat decimalFormat = new DecimalFormat("###.0", DecimalFormatSymbols.getInstance(testLocale));
+
+      try {
+         String[] suffixes = new String[]{"K", "M", "G", "T", "P", "E"};
+
+         assertEquals("0B", ByteUtil.getHumanReadableByteCount(0));
+         assertEquals(decimalFormat.format(999.0) + "B", ByteUtil.getHumanReadableByteCount(999));
+         assertEquals(decimalFormat.format(500.0) + "B", ByteUtil.getHumanReadableByteCount(500));
+
+         for (int i = 0, j = 3; i < 6; i++, j += 3) {
+            final long magnitude = (long) Math.pow(10, j);
+            assertEquals(decimalFormat.format(1.0) + suffixes[i] + "B", ByteUtil.getHumanReadableByteCount(magnitude));
+            assertEquals(decimalFormat.format(1.3) + suffixes[i] + "B", ByteUtil.getHumanReadableByteCount(magnitude + (long) (.25 * magnitude)));
+            assertEquals(decimalFormat.format(1.5) + suffixes[i] + "B", ByteUtil.getHumanReadableByteCount(magnitude + (long) (.5 * magnitude)));
+            assertEquals(decimalFormat.format(1.9) + suffixes[i] + "B", ByteUtil.getHumanReadableByteCount(magnitude + (long) (.9 * magnitude)));
+            assertEquals(decimalFormat.format(4.2) + suffixes[i] + "B", ByteUtil.getHumanReadableByteCount(magnitude + (long) (3.2 * magnitude)));
+         }
+      } finally {
+         // reset the default once the test is over
+         Locale.setDefault(defaultLocale);
+      }
+   }
 }