maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From micha...@apache.org
Subject maven git commit: [MNG-5977] Improve output readability of our MavenTransferListener implementations
Date Wed, 24 Feb 2016 21:24:34 GMT
Repository: maven
Updated Branches:
  refs/heads/master 44729b142 -> 33442c212


[MNG-5977] Improve output readability of our MavenTransferListener implementations

* Reworked the file size formatter which now compacts the progress with
the unit symbol, plus it scales to one decimal place only if the size is
below 10.
* Replaced the concurrent hash map with a synchronized linked hash map
to retain order of the progress meter. It will behave now like a queue.

Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/33442c21
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/33442c21
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/33442c21

Branch: refs/heads/master
Commit: 33442c212b5e5c29feb0101d70a625cea7845ea7
Parents: 44729b1
Author: Michael Osipov <michaelo@apache.org>
Authored: Wed Feb 24 22:23:13 2016 +0100
Committer: Michael Osipov <michaelo@apache.org>
Committed: Wed Feb 24 22:24:12 2016 +0100

----------------------------------------------------------------------
 .../transfer/AbstractMavenTransferListener.java | 180 +++++++++--
 .../transfer/ConsoleMavenTransferListener.java  |  34 +-
 .../transfer/Slf4jMavenTransferListener.java    |   5 +-
 .../maven/cli/transfer/FileSizeFormatTest.java  | 311 +++++++++++++++++++
 4 files changed, 478 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/33442c21/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
index b8af752..e72aa47 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
@@ -22,9 +22,9 @@ package org.apache.maven.cli.transfer;
 import java.io.PrintStream;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
-import java.text.FieldPosition;
 import java.util.Locale;
 
+import org.apache.commons.lang3.Validate;
 import org.eclipse.aether.transfer.AbstractTransferListener;
 import org.eclipse.aether.transfer.TransferCancelledException;
 import org.eclipse.aether.transfer.TransferEvent;
@@ -36,8 +36,9 @@ public abstract class AbstractMavenTransferListener
 
     // CHECKSTYLE_OFF: LineLength
     /**
-     * Formats file length with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a>
prefix
-     * (GB, MB, kB) and using the pattern <code>###0.#</code> by default.
+     * Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a>
prefix
+     * (GB, MB, kB) and using the patterns <code>#0.0</code> for numbers between
1 and 10
+     * and <code>###0</code> for numbers between 10 and 1000+ by default.
      *
      * @see <a href="https://en.wikipedia.org/wiki/Metric_prefix">https://en.wikipedia.org/wiki/Metric_prefix</a>
      * @see <a href="https://en.wikipedia.org/wiki/Binary_prefix">https://en.wikipedia.org/wiki/Binary_prefix</a>
@@ -45,48 +46,163 @@ public abstract class AbstractMavenTransferListener
      *      href="https://en.wikipedia.org/wiki/Octet_%28computing%29">https://en.wikipedia.org/wiki/Octet_(computing)</a>
      */
     // CHECKSTYLE_ON: LineLength
-    static class FileDecimalFormat
-        extends DecimalFormat
+    // TODO Move me to Maven Shared Utils
+    static class FileSizeFormat
     {
-        private static final long serialVersionUID = -684999256062614038L;
-
-        /**
-         * Default constructor
-         *
-         * @param locale
-         */
-        public FileDecimalFormat( Locale locale )
+        static enum ScaleUnit
         {
-            super( "###0.#", new DecimalFormatSymbols( locale ) );
+            BYTE
+            {
+                @Override
+                public long bytes()
+                {
+                    return 1L;
+                }
+
+                @Override
+                public String symbol()
+                {
+                    return "B";
+                }
+            },
+            KILOBYTE
+            {
+                @Override
+                public long bytes()
+                {
+                    return 1000L;
+                }
+
+                @Override
+                public String symbol()
+                {
+                    return "kB";
+                }
+            },
+            MEGABYTE
+            {
+                @Override
+                public long bytes()
+                {
+                    return KILOBYTE.bytes() * KILOBYTE.bytes();
+                }
+
+                @Override
+                public String symbol()
+                {
+                    return "MB";
+                }
+            },
+            GIGABYTE
+            {
+                @Override
+                public long bytes()
+                {
+                    return MEGABYTE.bytes() * KILOBYTE.bytes();
+                };
+
+                @Override
+                public String symbol()
+                {
+                    return "GB";
+                }
+            };
+
+            public abstract long bytes();
+            public abstract String symbol();
+
+            public static ScaleUnit getScaleUnit( long size )
+            {
+                Validate.isTrue( size >= 0, "File size cannot be negative: %s", size );
+
+                if ( size >= GIGABYTE.bytes() )
+                {
+                    return GIGABYTE;
+                }
+                else if ( size >= MEGABYTE.bytes() )
+                {
+                    return MEGABYTE;
+                }
+                else if ( size >= KILOBYTE.bytes() )
+                {
+                    return KILOBYTE;
+                }
+                else
+                {
+                    return BYTE;
+                }
+            }
+        }
+
+        private DecimalFormat smallFormat;
+        private DecimalFormat largeFormat;
+
+        public FileSizeFormat( Locale locale )
+        {
+            smallFormat = new DecimalFormat( "#0.0", new DecimalFormatSymbols( locale ) );
+            largeFormat = new DecimalFormat( "###0", new DecimalFormatSymbols( locale ) );
+        }
+
+        public String format( long size )
+        {
+            return format( size, null );
         }
 
-        /** {@inheritDoc} */
-        public StringBuffer format( long fs, StringBuffer result, FieldPosition fieldPosition
)
+        public String format( long size, ScaleUnit unit )
         {
-            if ( fs > 1000L * 1000L * 1000L )
+            return format( size, unit, false );
+        }
+
+        public String format( long size, ScaleUnit unit, boolean omitSymbol )
+        {
+            Validate.isTrue( size >= 0, "File size cannot be negative: %s", size );
+
+            if ( unit == null )
             {
-                result = super.format( (float) fs / ( 1000L * 1000L * 1000L ), result, fieldPosition
);
-                result.append( " GB" );
-                return result;
+                unit = ScaleUnit.getScaleUnit( size );
             }
 
-            if ( fs > 1000L * 1000L )
+            double scaledSize = (double) size / unit.bytes();
+            String scaledSymbol = " " + unit.symbol();
+
+            if ( omitSymbol )
             {
-                result = super.format( (float) fs / ( 1000L * 1000L ), result, fieldPosition
);
-                result.append( " MB" );
-                return result;
+                scaledSymbol = "";
             }
 
-            if ( fs > 1000L )
+            if ( unit == ScaleUnit.BYTE )
             {
-                result = super.format( (float) fs / ( 1000L ), result, fieldPosition );
-                result.append( " kB" );
-                return result;
+                return largeFormat.format( size ) + scaledSymbol;
             }
 
-            result = super.format( fs, result, fieldPosition );
-            result.append( " B" );
-            return result;
+            if ( scaledSize < 0.05 || scaledSize >= 10.0 )
+            {
+                return largeFormat.format( scaledSize ) + scaledSymbol;
+            }
+            else
+            {
+                return smallFormat.format( scaledSize ) + scaledSymbol;
+            }
+        }
+
+        public String formatProgress( long progressedSize, long size )
+        {
+            Validate.isTrue( progressedSize >= 0L, "Progressed file size cannot be negative:
%s", progressedSize );
+            Validate.isTrue( size >= 0L && progressedSize <= size || size <
0L,
+                "Progressed file size cannot be bigger than size: %s > %s", progressedSize,
size );
+
+            if ( size >= 0 && progressedSize != size )
+            {
+                ScaleUnit unit = ScaleUnit.getScaleUnit( size );
+                String formattedProgressedSize = format( progressedSize, unit, true );
+                String formattedSize = format( size, unit );
+
+                return formattedProgressedSize + "/" + formattedSize;
+            }
+            else
+            {
+                return format( progressedSize );
+            }
         }
     }
 
@@ -121,7 +237,7 @@ public abstract class AbstractMavenTransferListener
         TransferResource resource = event.getResource();
         long contentLength = event.getTransferredBytes();
 
-        DecimalFormat format = new FileDecimalFormat( Locale.ENGLISH );
+        FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
         String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded"
: "Downloaded" );
         String len = format.format( contentLength );
 

http://git-wip-us.apache.org/repos/asf/maven/blob/33442c21/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java
b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java
index f3daeee..5af1821 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java
@@ -20,11 +20,11 @@ package org.apache.maven.cli.transfer;
  */
 
 import java.io.PrintStream;
-import java.text.DecimalFormat;
+import java.util.Collections;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.Locale;
 import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.commons.lang3.StringUtils;
 import org.eclipse.aether.transfer.TransferCancelledException;
@@ -40,7 +40,8 @@ public class ConsoleMavenTransferListener
     extends AbstractMavenTransferListener
 {
 
-    private Map<TransferResource, Long> transfers = new ConcurrentHashMap<>();
+    private Map<TransferResource, Long> transfers = Collections.synchronizedMap(
+                                                        new LinkedHashMap<TransferResource,
Long>() );
 
     private boolean printResourceNames;
     private int lastLength;
@@ -78,16 +79,19 @@ public class ConsoleMavenTransferListener
         StringBuilder buffer = new StringBuilder( 128 );
         buffer.append( "Progress (" ).append(  transfers.size() ).append( "): " );
 
-        Iterator<Map.Entry<TransferResource, Long>> iter = transfers.entrySet().iterator();
-        while ( iter.hasNext() )
+        synchronized( transfers )
         {
-            Map.Entry<TransferResource, Long> entry = iter.next();
-            long total = entry.getKey().getContentLength();
-            Long complete = entry.getValue();
-            buffer.append( getStatus( entry.getKey().getResourceName(), complete, total )
);
-            if ( iter.hasNext() )
+            Iterator<Map.Entry<TransferResource, Long>> entries = transfers.entrySet().iterator();
+            while ( entries.hasNext() )
             {
-                buffer.append( " | " );
+                Map.Entry<TransferResource, Long> entry = entries.next();
+                long total = entry.getKey().getContentLength();
+                Long complete = entry.getValue();
+                buffer.append( getStatus( entry.getKey().getResourceName(), complete, total
) );
+                if ( entries.hasNext() )
+                {
+                    buffer.append( " | " );
+                }
             }
         }
 
@@ -101,7 +105,7 @@ public class ConsoleMavenTransferListener
 
     private String getStatus( String resourceName, long complete, long total )
     {
-        DecimalFormat format = new FileDecimalFormat( Locale.ENGLISH );
+        FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
         StringBuilder status = new StringBuilder();
 
         if ( printResourceNames )
@@ -110,11 +114,7 @@ public class ConsoleMavenTransferListener
             status.append( " (" );
         }
 
-        status.append( format.format( complete ) );
-        if ( total > 0 && complete != total )
-        {
-            status.append( "/" ).append( format.format( total ) );
-        }
+        status.append( format.formatProgress( complete, total ) );
 
         if ( printResourceNames )
         {

http://git-wip-us.apache.org/repos/asf/maven/blob/33442c21/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java
b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java
index 6f50f5e..5bfb7b4 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java
@@ -19,10 +19,9 @@ package org.apache.maven.cli.transfer;
  * under the License.
  */
 
-import java.text.DecimalFormat;
 import java.util.Locale;
 
-import org.apache.maven.cli.transfer.AbstractMavenTransferListener.FileDecimalFormat;
+import org.apache.maven.cli.transfer.AbstractMavenTransferListener.FileSizeFormat;
 import org.eclipse.aether.transfer.AbstractTransferListener;
 import org.eclipse.aether.transfer.TransferCancelledException;
 import org.eclipse.aether.transfer.TransferEvent;
@@ -71,7 +70,7 @@ public class Slf4jMavenTransferListener
         TransferResource resource = event.getResource();
         long contentLength = event.getTransferredBytes();
 
-        DecimalFormat format = new FileDecimalFormat( Locale.ENGLISH );
+        FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
         String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded"
: "Downloaded" );
         String len = format.format( contentLength );
 

http://git-wip-us.apache.org/repos/asf/maven/blob/33442c21/maven-embedder/src/test/java/org/apache/maven/cli/transfer/FileSizeFormatTest.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/test/java/org/apache/maven/cli/transfer/FileSizeFormatTest.java
b/maven-embedder/src/test/java/org/apache/maven/cli/transfer/FileSizeFormatTest.java
new file mode 100644
index 0000000..ff81e93
--- /dev/null
+++ b/maven-embedder/src/test/java/org/apache/maven/cli/transfer/FileSizeFormatTest.java
@@ -0,0 +1,311 @@
+package org.apache.maven.cli.transfer;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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
+ *
+ *  http://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.
+ */
+
+import java.util.Locale;
+
+import org.apache.maven.cli.transfer.AbstractMavenTransferListener.FileSizeFormat;
+import org.apache.maven.cli.transfer.AbstractMavenTransferListener.FileSizeFormat.ScaleUnit;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+
+public class FileSizeFormatTest {
+
+    @Test( expected = IllegalArgumentException.class )
+    public void testNegativeSize()
+    {
+        FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
+
+        long negativeSize = -100L;
+        format.format( negativeSize );
+    }
+
+    @Test
+    public void testSize()
+    {
+        FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
+
+        long _0_bytes = 0L;
+        assertEquals( "0 B", format.format( _0_bytes ) );
+
+        long _5_bytes = 5L;
+        assertEquals( "5 B", format.format( _5_bytes ) );
+
+        long _10_bytes = 10L;
+        assertEquals( "10 B", format.format( _10_bytes ) );
+
+        long _15_bytes = 15L;
+        assertEquals( "15 B", format.format( _15_bytes ) );
+
+        long _999_bytes = 999L;
+        assertEquals( "999 B", format.format( _999_bytes ) );
+
+        long _1000_bytes = 1000L;
+        assertEquals( "1.0 kB", format.format( _1000_bytes ) );
+
+        long _5500_bytes = 5500L;
+        assertEquals( "5.5 kB", format.format( _5500_bytes ) );
+
+        long _10_kilobytes = 10L * 1000L;
+        assertEquals( "10 kB", format.format( _10_kilobytes ) );
+
+        long _15_kilobytes = 15L * 1000L;
+        assertEquals( "15 kB", format.format( _15_kilobytes ) );
+
+        long _999_kilobytes = 999L * 1000L;
+        assertEquals( "999 kB", format.format( _999_kilobytes ) );
+
+        long _1000_kilobytes = 1000L * 1000L;
+        assertEquals( "1.0 MB", format.format( _1000_kilobytes ) );
+
+        long _5500_kilobytes = 5500L * 1000L;
+        assertEquals( "5.5 MB", format.format( _5500_kilobytes ) );
+
+        long _10_megabytes = 10L * 1000L * 1000L;
+        assertEquals( "10 MB", format.format( _10_megabytes ) );
+
+        long _15_megabytes = 15L * 1000L * 1000L;
+        assertEquals( "15 MB", format.format( _15_megabytes ) );
+
+        long _999_megabytes = 999L * 1000L * 1000L;
+        assertEquals( "999 MB", format.format( _999_megabytes ) );
+
+        long _1000_megabytes = 1000L * 1000L * 1000L;
+        assertEquals( "1.0 GB", format.format( _1000_megabytes ) );
+
+        long _5500_megabytes = 5500L * 1000L * 1000L;
+        assertEquals( "5.5 GB", format.format( _5500_megabytes ) );
+
+        long _10_gigabytes = 10L * 1000L * 1000L * 1000L;
+        assertEquals( "10 GB", format.format( _10_gigabytes ) );
+
+        long _15_gigabytes = 15L * 1000L * 1000L * 1000L;
+        assertEquals( "15 GB", format.format( _15_gigabytes ) );
+
+        long _1000_gigabytes = 1000L * 1000L * 1000L * 1000L;
+        assertEquals( "1000 GB", format.format( _1000_gigabytes ) );
+    }
+
+    @Test
+    public void testSizeWithSelectedScaleUnit()
+    {
+        FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
+
+        long _0_bytes = 0L;
+        assertEquals( "0 B", format.format( _0_bytes ) );
+        assertEquals( "0 B", format.format( _0_bytes, ScaleUnit.BYTE ) );
+        assertEquals( "0 kB", format.format( _0_bytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "0 MB", format.format( _0_bytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _0_bytes, ScaleUnit.GIGABYTE ) );
+
+        long _5_bytes = 5L;
+        assertEquals( "5 B", format.format( _5_bytes ) );
+        assertEquals( "5 B", format.format( _5_bytes, ScaleUnit.BYTE ) );
+        assertEquals( "0 kB", format.format( _5_bytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "0 MB", format.format( _5_bytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _5_bytes, ScaleUnit.GIGABYTE ) );
+
+
+        long _49_bytes = 49L;
+        assertEquals( "49 B", format.format( _49_bytes ) );
+        assertEquals( "49 B", format.format( _49_bytes, ScaleUnit.BYTE ) );
+        assertEquals( "0 kB", format.format( _49_bytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "0 MB", format.format( _49_bytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _49_bytes, ScaleUnit.GIGABYTE ) );
+
+        long _50_bytes = 50L;
+        assertEquals( "50 B", format.format( _50_bytes ) );
+        assertEquals( "50 B", format.format( _50_bytes, ScaleUnit.BYTE ) );
+        assertEquals( "0.1 kB", format.format( _50_bytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "0 MB", format.format( _50_bytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _50_bytes, ScaleUnit.GIGABYTE ) );
+
+        long _999_bytes = 999L;
+        assertEquals( "999 B", format.format( _999_bytes ) );
+        assertEquals( "999 B", format.format( _999_bytes, ScaleUnit.BYTE ) );
+        assertEquals( "1.0 kB", format.format( _999_bytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "0 MB", format.format( _999_bytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _999_bytes, ScaleUnit.GIGABYTE ) );
+
+        long _1000_bytes = 1000L;
+        assertEquals( "1.0 kB", format.format( _1000_bytes ) );
+        assertEquals( "1000 B", format.format( _1000_bytes, ScaleUnit.BYTE ) );
+        assertEquals( "1.0 kB", format.format( _1000_bytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "0 MB", format.format( _1000_bytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _1000_bytes, ScaleUnit.GIGABYTE ) );
+
+        long _49_kilobytes = 49L * 1000L;
+        assertEquals( "49 kB", format.format( _49_kilobytes ) );
+        assertEquals( "49000 B", format.format( _49_kilobytes, ScaleUnit.BYTE ) );
+        assertEquals( "49 kB", format.format( _49_kilobytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "0 MB", format.format( _49_kilobytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _49_kilobytes, ScaleUnit.GIGABYTE ) );
+
+        long _50_kilobytes = 50L * 1000L;
+        assertEquals( "50 kB", format.format( _50_kilobytes ) );
+        assertEquals( "50000 B", format.format( _50_kilobytes, ScaleUnit.BYTE ) );
+        assertEquals( "50 kB", format.format( _50_kilobytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "0.1 MB", format.format( _50_kilobytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _50_kilobytes, ScaleUnit.GIGABYTE ) );
+
+        long _999_kilobytes = 999L * 1000L;
+        assertEquals( "999 kB", format.format( _999_kilobytes ) );
+        assertEquals( "999000 B", format.format( _999_kilobytes, ScaleUnit.BYTE ) );
+        assertEquals( "999 kB", format.format( _999_kilobytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "1.0 MB", format.format( _999_kilobytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _999_kilobytes, ScaleUnit.GIGABYTE ) );
+
+        long _1000_kilobytes = 1000L * 1000L;
+        assertEquals( "1.0 MB", format.format( _1000_kilobytes ) );
+        assertEquals( "1000000 B", format.format( _1000_kilobytes, ScaleUnit.BYTE ) );
+        assertEquals( "1000 kB", format.format( _1000_kilobytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "1.0 MB", format.format( _1000_kilobytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _1000_kilobytes, ScaleUnit.GIGABYTE ) );
+
+        long _49_megabytes = 49L * 1000L * 1000L;
+        assertEquals( "49 MB", format.format( _49_megabytes ) );
+        assertEquals( "49000000 B", format.format( _49_megabytes, ScaleUnit.BYTE ) );
+        assertEquals( "49000 kB", format.format( _49_megabytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "49 MB", format.format( _49_megabytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0 GB", format.format( _49_megabytes, ScaleUnit.GIGABYTE ) );
+
+        long _50_megabytes = 50L * 1000L * 1000L;
+        assertEquals( "50 MB", format.format( _50_megabytes ) );
+        assertEquals( "50000000 B", format.format( _50_megabytes, ScaleUnit.BYTE ) );
+        assertEquals( "50000 kB", format.format( _50_megabytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "50 MB", format.format( _50_megabytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "0.1 GB", format.format( _50_megabytes, ScaleUnit.GIGABYTE ) );
+
+        long _999_megabytes = 999L * 1000L * 1000L;
+        assertEquals( "999 MB", format.format( _999_megabytes ) );
+        assertEquals( "999000000 B", format.format( _999_megabytes, ScaleUnit.BYTE ) );
+        assertEquals( "999000 kB", format.format( _999_megabytes, ScaleUnit.KILOBYTE ) );
+        assertEquals( "999 MB", format.format( _999_megabytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "1.0 GB", format.format( _999_megabytes, ScaleUnit.GIGABYTE ) );
+
+        long _1000_megabytes = 1000L * 1000L * 1000L;
+        assertEquals( "1.0 GB", format.format( _1000_megabytes ) );
+        assertEquals( "1000000000 B", format.format( _1000_megabytes, ScaleUnit.BYTE ) );
+        assertEquals( "1000000 kB", format.format( _1000_megabytes, ScaleUnit.KILOBYTE )
);
+        assertEquals( "1000 MB", format.format( _1000_megabytes, ScaleUnit.MEGABYTE ) );
+        assertEquals( "1.0 GB", format.format( _1000_megabytes, ScaleUnit.GIGABYTE ) );
+    }
+
+    @Test( expected = IllegalArgumentException.class )
+    public void testNegativeProgessedSize()
+    {
+        FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
+
+        long negativeProgessedSize = -100L;
+        format.formatProgress( negativeProgessedSize, 10L );
+    }
+
+    @Test( expected = IllegalArgumentException.class )
+    public void testNegativeProgessedSizeBiggerThanSize()
+    {
+        FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
+
+        format.formatProgress( 100L, 10L );
+    }
+
+    @Test
+    public void testProgressedSizeWithoutSize()
+    {
+         FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
+
+         long _0_bytes = 0L;
+         assertEquals( "0 B", format.formatProgress( _0_bytes, -1L ) );
+
+         long _1000_bytes = 1000L;
+         assertEquals( "1.0 kB", format.formatProgress( _1000_bytes, -1L ) );
+
+         long _1000_kilobytes = 1000L * 1000L;
+         assertEquals( "1.0 MB", format.formatProgress( _1000_kilobytes, -1L ) );
+
+         long _1000_megabytes = 1000L * 1000L * 1000L;
+         assertEquals( "1.0 GB", format.formatProgress( _1000_megabytes, -1L ) );
+
+    }
+
+    @Test
+    public void testProgressedSizeWithZeroSize()
+    {
+         FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
+
+         long _0_bytes = 0L;
+         assertEquals( "0 B", format.formatProgress( _0_bytes, _0_bytes ) );
+    }
+
+    @Test
+    public void testProgressedSizeZeroAndSizeZero()
+    {
+         FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
+
+         long _0_bytes = 0L;
+         assertEquals( "0 B", format.formatProgress( _0_bytes, _0_bytes ) );
+    }
+
+    @Test
+    public void testProgressedSizeWithSize()
+    {
+         FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
+
+         long _0_bytes = 0L;
+         long _400_bytes = 400L;
+         long _800_bytes = 2L * _400_bytes;
+         assertEquals( "0/800 B", format.formatProgress( _0_bytes, _800_bytes ) );
+         assertEquals( "400/800 B", format.formatProgress( _400_bytes, _800_bytes ) );
+         assertEquals( "800 B", format.formatProgress( _800_bytes, _800_bytes ) );
+
+         long _4000_bytes = 4000L;
+         long _8000_bytes = 2L * _4000_bytes;
+         long _50_kilobytes = 50000L;
+         assertEquals( "0/8.0 kB", format.formatProgress( _0_bytes, _8000_bytes ) );
+         assertEquals( "0.4/8.0 kB", format.formatProgress( _400_bytes, _8000_bytes ) );
+         assertEquals( "4.0/8.0 kB", format.formatProgress( _4000_bytes, _8000_bytes ) );
+         assertEquals( "8.0 kB", format.formatProgress( _8000_bytes, _8000_bytes ) );
+         assertEquals( "8.0/50 kB", format.formatProgress( _8000_bytes, _50_kilobytes ) );
+         assertEquals( "16/50 kB", format.formatProgress( 2L * _8000_bytes, _50_kilobytes
) );
+         assertEquals( "50 kB", format.formatProgress( _50_kilobytes, _50_kilobytes ) );
+
+         long _500_kilobytes = 500000L;
+         long _1000_kilobytes = 2L * _500_kilobytes;;
+         long _5000_kilobytes = 5L * _1000_kilobytes;
+         long _15_megabytes = 3L * _5000_kilobytes;
+         assertEquals( "0/5.0 MB", format.formatProgress( _0_bytes, _5000_kilobytes ) );
+         assertEquals( "0.5/5.0 MB", format.formatProgress( _500_kilobytes, _5000_kilobytes
) );
+         assertEquals( "1.0/5.0 MB", format.formatProgress( _1000_kilobytes, _5000_kilobytes
) );
+         assertEquals( "5.0 MB", format.formatProgress( _5000_kilobytes, _5000_kilobytes
) );
+         assertEquals( "5.0/15 MB", format.formatProgress( _5000_kilobytes, _15_megabytes
) );
+         assertEquals( "15 MB", format.formatProgress( _15_megabytes, _15_megabytes ) );
+
+         long _500_megabytes = 500000000L;
+         long _1000_megabytes = 2L * _500_megabytes;
+         long _5000_megabytes = 5L * _1000_megabytes;
+         long _15_gigabytes = 3L * _5000_megabytes;
+         assertEquals( "0/500 MB", format.formatProgress( _0_bytes, _500_megabytes ) );
+         assertEquals( "1.0/5.0 GB", format.formatProgress( _1000_megabytes, _5000_megabytes
) );
+         assertEquals( "5.0 GB", format.formatProgress( _5000_megabytes, _5000_megabytes
) );
+         assertEquals( "5.0/15 GB", format.formatProgress( _5000_megabytes, _15_gigabytes
) );
+         assertEquals( "15 GB", format.formatProgress( _15_gigabytes, _15_gigabytes ) );
+    }
+
+}


Mime
View raw message