From aa982232ce3d45969d03228f77e9eaa7fe13b6c2 Mon Sep 17 00:00:00 2001 From: Mark Iantorno Date: Thu, 6 Aug 2020 07:28:17 -0400 Subject: [PATCH] I had the code written already, so let's put it in. (#293) * I had the code written already, so let's put it in. * renaming and moving Utilities tests class to conform to best practices. --- .../org/hl7/fhir/utilities/Utilities.java | 26 ++++---- ...UtilitiesTests.java => UtilitiesTest.java} | 65 ++++++++++++++++--- 2 files changed, 71 insertions(+), 20 deletions(-) rename org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/{tests/UtilitiesTests.java => UtilitiesTest.java} (56%) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java index de5aa1d6f..d8b50d7c9 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java @@ -1308,20 +1308,24 @@ public class Utilities { return false; } + public static final int ONE_MB = 1024; + public static final String GB = "Gb"; + public static final String MB = "Mb"; + public static final String KB = "Kb"; + public static final String BT = "b"; + public static String describeSize(int length) { - if (length > 1024 * 1024 * 1024) { - return ""+length / (1024 * 1024 * 1024)+"Gb"; + if (length < 0) throw new IllegalArgumentException("File length of < 0 passed in..."); + + if (length > Math.pow(ONE_MB, 3)) { + return length / ((long) Math.pow(ONE_MB, 3)) + GB; } - if (length > 1024 * 1024) { - return ""+length / (1024 * 1024)+"Mb"; + if (length > Math.pow(ONE_MB, 2)) { + return length / ((long) Math.pow(ONE_MB, 2)) + MB; } - if (length > 1024) { - return ""+length / (1024)+"kb"; + if (length > ONE_MB) { + return length / (ONE_MB) + KB; } - return ""+length +"b"; + return length + BT; } - - - - } \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/UtilitiesTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java similarity index 56% rename from org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/UtilitiesTests.java rename to org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java index 8dd6ffc12..f850a1949 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/UtilitiesTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java @@ -1,16 +1,17 @@ -package org.hl7.fhir.utilities.tests; - -import java.io.File; -import java.io.IOException; +package org.hl7.fhir.utilities; import org.apache.commons.lang3.SystemUtils; -import org.hl7.fhir.utilities.Utilities; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -public class UtilitiesTests { +import java.io.File; +import java.io.IOException; +import java.util.Random; + +import static org.junit.jupiter.api.Assertions.*; + +class UtilitiesTest { public static final String OSX = "OS X"; public static final String MAC = "MAC"; @@ -83,9 +84,9 @@ public class UtilitiesTests { } else if (os.toUpperCase().contains(WINDOWS)) { File tmp = new File("c:\\temp"); if(tmp.exists()) { - return WIN_TEMP_DIR; + return WIN_TEMP_DIR; } else { - return System.getProperty("java.io.tmpdir"); + return System.getProperty("java.io.tmpdir"); } } else { throw new IllegalStateException("OS not recognized...cannot verify created directories."); @@ -103,4 +104,50 @@ public class UtilitiesTests { File file = File.createTempFile("throwaway", ".file"); return file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('/')) + '/'; } + + public static final int BOUND = 500; + public static final Random RAND = new Random(); + + public static final int GB_MEASURE_JUST_OVER = (int) Math.pow(Utilities.ONE_MB, 3) + RAND.nextInt(BOUND); + public static final int GB_MEASURE_EXACT = (int) Math.pow(Utilities.ONE_MB, 3); + public static final int GB_MEASURE_JUST_UNDER = (int) Math.pow(Utilities.ONE_MB, 3) - RAND.nextInt(BOUND); + + public static final int MB_MEASURE_JUST_OVER = (int) Math.pow(Utilities.ONE_MB, 2) + RAND.nextInt(BOUND); + public static final int MB_MEASURE_EXACT = (int) Math.pow(Utilities.ONE_MB, 2); + public static final int MB_MEASURE_JUST_UNDER = (int) Math.pow(Utilities.ONE_MB, 2) - RAND.nextInt(BOUND); + + public static final int KB_MEASURE_JUST_OVER = Utilities.ONE_MB + RAND.nextInt(BOUND); + public static final int KB_MEASURE_EXACT = Utilities.ONE_MB; + public static final int KB_MEASURE_JUST_UNDER = Utilities.ONE_MB - RAND.nextInt(BOUND); + + public static final int BT_MEASURE = Utilities.ONE_MB + RAND.nextInt(BOUND); + public static final int EMPTY = 0; + + public static final int BIG_NEG = Utilities.ONE_MB * -1; + + @Test + @DisplayName("Test size bounds on file size utility.") + void describeSizeTest() { + Assertions.assertAll("GB Measure Limits", + () -> assertTrue(Utilities.describeSize(GB_MEASURE_JUST_OVER).contains(Utilities.GB)), + () -> assertTrue(Utilities.describeSize(GB_MEASURE_EXACT).contains(Utilities.MB)), + () -> assertTrue(Utilities.describeSize(GB_MEASURE_JUST_UNDER).contains(Utilities.MB)) + ); + Assertions.assertAll("MB Measure Limits", + () -> assertTrue(Utilities.describeSize(MB_MEASURE_JUST_OVER).contains(Utilities.MB)), + () -> assertTrue(Utilities.describeSize(MB_MEASURE_EXACT).contains(Utilities.KB)), + () -> assertTrue(Utilities.describeSize(MB_MEASURE_JUST_UNDER).contains(Utilities.KB)) + ); + Assertions.assertAll("KB Measure Limits", + () -> assertTrue(Utilities.describeSize(KB_MEASURE_JUST_OVER).contains(Utilities.KB)), + () -> assertTrue(Utilities.describeSize(KB_MEASURE_EXACT).contains(Utilities.BT)), + () -> assertTrue(Utilities.describeSize(KB_MEASURE_JUST_UNDER).contains(Utilities.BT)) + ); + Assertions.assertAll("BT Measure Limits", + () -> assertTrue(Utilities.describeSize(BT_MEASURE).contains(Utilities.BT)), + () -> assertTrue(Utilities.describeSize(EMPTY).contains(Utilities.BT)) + ); + Assertions.assertThrows(IllegalArgumentException.class, () -> Utilities.describeSize(BIG_NEG)); + } + } \ No newline at end of file