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.
This commit is contained in:
Mark Iantorno 2020-08-06 07:28:17 -04:00 committed by GitHub
parent f54e56e74c
commit aa982232ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 20 deletions

View File

@ -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;
}
}

View File

@ -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));
}
}