You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// TODO properly log exception from getting OS name
importjava.util.Locale;
importcom.bookiescrape.app.util.OperatingSystemUtils.OperatingSystem;
finalclassMockOperatingSystemUtils {
/** * Enum representing some popular operating systems. * * @see OperatingSystemUtils * @see OperatingSystemUtils#getDetectedOS() */enumMockOperatingSystem {
/** Mac operating system. */MAC_OS("mac", "darwin"),
/** Windows operating system. */WINDOWS("win"),
/** Ubuntu operating system. */UBUNTU("ubuntu"),
/** Unix operating system. */UNIX("nix"),
/** Linux operating system. */LINUX("nux"),
/** Unrecognized operating system. */OTHER("generic");
privatefinalString[] keys;
/** Constructs an OSType enum. */privateMockOperatingSystem(String... keys) { this.keys = keys; }
publicOperatingSystemgetOperatingSystem() {
switch (this) {
caseMAC_OS:
returnOperatingSystem.MAC_OS;
caseWINDOWS:
returnOperatingSystem.WINDOWS;
caseUBUNTU:
returnOperatingSystem.UBUNTU;
caseUNIX:
returnOperatingSystem.UNIX;
caseLINUX:
returnOperatingSystem.LINUX;
caseOTHER:
default:
returnOperatingSystem.OTHER;
}
}
} // enum OperatingSystempublicMockOperatingSystemUtils() {}
/** The operating system name system property key. */privatestaticfinalStringOS_NAME_KEY = "os.name";
/** * Gets the operating system detected from * {@code System.getProperty("os.name")}. * * @return the operating system detected from * {@code System.getProperty("os.name")} * @see MockOperatingSystem * @see System#getProperty(String) */publicstaticMockOperatingSystemgetDetectedOS() { returngetOperatingSystemType(); }
/** Helper for {@code getDetectedOSType()}. */privatestaticMockOperatingSystemgetOperatingSystemType() {
StringosName = getOSNameProperty();
// return Other if getOSNameProperty() returns a blank stringif (osName.isBlank()) { returnMockOperatingSystem.OTHER; }
// change osName to English lowercaseosName = osName.toLowerCase(Locale.ENGLISH);
// iterate over OS enums and check if osName contains any OS keysfor (MockOperatingSystemos : MockOperatingSystem.values()) {
if (osNameContainsAnOSKey(osName, os)) { returnos; }
}
returnMockOperatingSystem.OTHER;
}
/** * Helper that iterates over an {@code OperatingSystem} enum's keys and * checks if {@code osName} contains a key. * * @param osName - the operating system name from * {@code System.getProperty("os.name")} * @param os - the {@code OperatingSystem} enum to check * @return {@code true} if {@code osName} contains any of {@code os}'s * keys, otherwise {@code false} */privatestaticbooleanosNameContainsAnOSKey(StringosName, MockOperatingSystemos) {
for (Stringkey : os.keys) {
if (osName.indexOf(key) != -1) { returntrue; }
}
returnfalse;
}
/** * Helper to get the operating system name from * {@code System#getProperty("os.name")}. * <p> * If querying the system properties for {@code "os.name"} throws an * exception or results in {@code null}, then the empty string ({@code ""}) * will be returned. * <p> * <b>Note:</b>Any thrown exceptions will be caught by this method and, if * enabled, logged. * * @return the operating system name retrieved from * {@code System#getProperty("os.name")}, or the empty string * ({@code ""}) * @see System#getProperty(String) */privatestaticStringgetOSNameProperty() {
StringosName = null;
try {
osName = System.getProperty(OS_NAME_KEY);
} catch (Exceptione) {
// TODO properly log exception from getting OS nameSystem.err.println(e.getLocalizedMessage());
}
returnosName == null ? "" : osName;
}
}
ewfilemode100644ndex0000000..4a6c77b
++ b/src/test/java/com/bookiescrape/app/util/OperatingSystemUtilsTest.java
038bcd068ad544ce9078e880339b3aab20d68229
The text was updated successfully, but these errors were encountered:
properly log exception from getting OS name
bookie-scrape/src/test/java/com/bookiescrape/app/util/MockOperatingSystemUtils.java
Line 132 in 801c596
038bcd068ad544ce9078e880339b3aab20d68229
The text was updated successfully, but these errors were encountered: