Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isAtLeast(OS) method #5

Merged
merged 3 commits into from May 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/io/github/cegredev/josi/OS.java
Expand Up @@ -582,6 +582,22 @@ public boolean is(OS... operatingSystems) {
return Arrays.asList(operatingSystems).contains(this);
}

/**
* Checks if this OS is at the same or later version of the given OS. Only relevant for Windows and Mac.
*
* @param operatingSystem An operating system with version.
* @return If this OS is the same version as the given OS or a later one of the same family.
*/
public boolean isAtLeast(OS operatingSystem) {
// Restrict to families with versions
enforceFamily(Family.MAC, Family.WINDOWS);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized later perhaps we can allow other OS's to equal each other if the family matches so this should probably be changed to enforceNotFamily(Family.UNKNOWN)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even in the case of the family being unknown the OSs could still equal each other though, right? As I explained in my unfortunately timed comment below I am generally not a fan of the use of enforce here anyways, but more on that in the comment! ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that "other" is a catch all for any other OS that doesn't parse/isn't implemented. So you could have FreeBSD equal to OpenBSD, for example, which is not the case.

// Verify family matches
enforceFamily(operatingSystem.getFamily());
// True if this is equal to or after in enum order
// Assumes "unknown" is a newer version
return this.compareTo(operatingSystem) >= 0;
}

/**
* Throws an {@link UnsupportedOSException} if this operating system is part of the given array.
*
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/io/github/cegredev/josi/OSCheckTests.java
Expand Up @@ -39,6 +39,25 @@ public void testIsOS() {
assertFalse(OS.WIN_8.is(OS.WIN_95, OS.WIN_10, OS.MAC_UNKNOWN), message);
}

@Test
public void testIsAtLeastOS() {
String message = "OS was not recognized!";
assertTrue(OS.WIN_10.isAtLeast(OS.WIN_95), message);
assertTrue(OS.WIN_7.isAtLeast(OS.WIN_7), message);
assertFalse(OS.WIN_8.isAtLeast(OS.WIN_10), message);
assertTrue(OS.MAC_OS_BIG_SUR.isAtLeast(OS.MAC_OS_SIERRA), message);
assertTrue(OS.MAC_OS_CATALINA.isAtLeast(OS.MAC_OS_CATALINA), message);
assertFalse(OS.MAC_OSX_LION.isAtLeast(OS.MAC_OSX_MOUNTAIN_LION), message);

String notThrowMessage = "Did not throw even though OS was not allowed!";
assertThrows(UnsupportedOSException.class, () -> OS.UBUNTU.isAtLeast(OS.UBUNTU),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should maybe be changed to be "true".

notThrowMessage);
assertThrows(UnsupportedOSException.class, () -> OS.WIN_10.isAtLeast(OS.MAC_OS_SIERRA),
notThrowMessage);
assertThrows(UnsupportedOSException.class, () -> OS.MAC_OS_BIG_SUR.isAtLeast(OS.WIN_7),
notThrowMessage);
}

@Test
public void testIsFamily() {
String message = "OS family was not recognized!";
Expand Down