Add test for UuidUtils
This commit is contained in:
parent
c34f00f759
commit
d87fa860e6
@ -12,15 +12,15 @@ public final class UuidUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether the given string as an UUID or null
|
* Returns the given string as an UUID or null.
|
||||||
*
|
*
|
||||||
* @param string the uuid to parse
|
* @param string the uuid to parse
|
||||||
* @return parsed UUID if succeed or null
|
* @return parsed UUID if succeeded or null
|
||||||
*/
|
*/
|
||||||
public static UUID parseUuidSafely(String string) {
|
public static UUID parseUuidSafely(String string) {
|
||||||
try {
|
try {
|
||||||
return UUID.fromString(string);
|
return string == null ? null : UUID.fromString(string);
|
||||||
} catch (IllegalArgumentException | NullPointerException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
29
src/test/java/fr/xephi/authme/util/UuidUtilsTest.java
Normal file
29
src/test/java/fr/xephi/authme/util/UuidUtilsTest.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package fr.xephi.authme.util;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link UuidUtils}.
|
||||||
|
*/
|
||||||
|
public class UuidUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldParseUuidSafely() {
|
||||||
|
// given
|
||||||
|
UUID correctUuid = UUID.fromString("8e0a9aaa-5eda-42ef-8daf-e6c6359f607e");
|
||||||
|
|
||||||
|
// when / then
|
||||||
|
assertThat(UuidUtils.parseUuidSafely("8e0a9aaa-5eda-42ef-8daf-e6c6359f607e"), equalTo(correctUuid));
|
||||||
|
|
||||||
|
assertThat(UuidUtils.parseUuidSafely(null), nullValue());
|
||||||
|
assertThat(UuidUtils.parseUuidSafely(""), nullValue());
|
||||||
|
assertThat(UuidUtils.parseUuidSafely("foo"), nullValue());
|
||||||
|
assertThat(UuidUtils.parseUuidSafely("8e0a9aaa-5eda-42ef-InvalidEnding"), nullValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user