Don’t use number parsing from the utils package anymore.
[Sone.git] / src / test / java / net / pterodactylus / sone / utils / NumberParsersTest.java
diff --git a/src/test/java/net/pterodactylus/sone/utils/NumberParsersTest.java b/src/test/java/net/pterodactylus/sone/utils/NumberParsersTest.java
new file mode 100644 (file)
index 0000000..00c2263
--- /dev/null
@@ -0,0 +1,84 @@
+package net.pterodactylus.sone.utils;
+
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+import static net.pterodactylus.sone.utils.NumberParsers.parseLong;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link NumberParsers}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class NumberParsersTest {
+
+       @Test
+       // yes, this test is for coverage only.
+       public void constructorCanBeCalled() {
+               new NumberParsers();
+       }
+
+       @Test
+       public void nullIsParsedToDefaultInt() {
+               assertThat(parseInt(null, 17), is(17));
+       }
+
+       @Test
+       public void notANumberIsParsedToDefaultInt() {
+               assertThat(parseInt("not a number", 18), is(18));
+       }
+
+       @Test
+       public void intIsCorrectlyParsed() {
+               assertThat(parseInt("19", 0), is(19));
+       }
+
+       @Test
+       public void valueTooLargeForIntIsParsedToDefault() {
+               assertThat(parseInt("2147483648", 20), is(20));
+       }
+
+       @Test
+       public void valueTooSmallForIntIsParsedToDefault() {
+               assertThat(parseInt("-2147483649", 20), is(20));
+       }
+
+       @Test
+       public void nullCanBeDefaultIntValue() {
+               assertThat(parseInt("not a number", null), nullValue());
+       }
+
+       @Test
+       public void nullIsParsedToDefaultLong() {
+               assertThat(parseLong(null, 17L), is(17L));
+       }
+
+       @Test
+       public void notANumberIsParsedToDefaultLong() {
+               assertThat(parseLong("not a number", 18L), is(18L));
+       }
+
+       @Test
+       public void LongIsCorrectlyParsed() {
+               assertThat(parseLong("19", 0L), is(19L));
+       }
+
+       @Test
+       public void valueTooLargeForLongIsParsedToDefault() {
+               assertThat(parseLong("9223372036854775808", 20L), is(20L));
+       }
+
+       @Test
+       public void valueTooSmallForLongIsParsedToDefault() {
+               assertThat(parseLong("-9223372036854775809", 20L), is(20L));
+       }
+
+       @Test
+       public void nullCanBeDefaultLongValue() {
+               assertThat(parseLong("not a number", null), nullValue());
+       }
+
+}