package net.pterodactylus.sone.core;
import static java.util.logging.Logger.getLogger;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+import static net.pterodactylus.sone.utils.NumberParsers.parseLong;
import java.io.InputStream;
import java.util.ArrayList;
import net.pterodactylus.sone.database.PostBuilder;
import net.pterodactylus.sone.database.PostReplyBuilder;
import net.pterodactylus.sone.database.SoneBuilder;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.xml.SimpleXML;
import net.pterodactylus.util.xml.XML;
Integer protocolVersion = null;
String soneProtocolVersion = soneXml.getValue("protocol-version", null);
if (soneProtocolVersion != null) {
- protocolVersion = Numbers.safeParseInteger(soneProtocolVersion);
+ protocolVersion = parseInt(soneProtocolVersion, null);
}
if (protocolVersion == null) {
logger.log(Level.INFO, "No protocol version found, assuming 0.");
String profileFirstName = profileXml.getValue("first-name", null);
String profileMiddleName = profileXml.getValue("middle-name", null);
String profileLastName = profileXml.getValue("last-name", null);
- Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null));
- Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null));
- Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null));
+ Integer profileBirthDay = parseInt(profileXml.getValue("birth-day", ""), null);
+ Integer profileBirthMonth = parseInt(profileXml.getValue("birth-month", ""), null);
+ Integer profileBirthYear = parseInt(profileXml.getValue("birth-year", ""), null);
Profile profile = new Profile(sone).setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName);
profile.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear);
/* avatar is processed after images are loaded. */
logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone));
return null;
}
- long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L);
- int imageWidth = Numbers.safeParseInteger(imageWidthString, 0);
- int imageHeight = Numbers.safeParseInteger(imageHeightString, 0);
+ long creationTime = parseLong(imageCreationTimeString, 0L);
+ int imageWidth = parseInt(imageWidthString, 0);
+ int imageHeight = parseInt(imageHeightString, 0);
if ((imageWidth < 1) || (imageHeight < 1)) {
logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
return null;
package net.pterodactylus.sone.freenet.wot;
import static java.util.logging.Logger.getLogger;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
import java.util.HashMap;
import java.util.HashSet;
import net.pterodactylus.sone.freenet.plugin.PluginConnector;
import net.pterodactylus.sone.freenet.plugin.PluginException;
import net.pterodactylus.sone.freenet.plugin.event.ReceivedReplyEvent;
-import net.pterodactylus.util.number.Numbers;
import com.google.common.base.Optional;
import com.google.common.collect.MapMaker;
DefaultIdentity identity = new DefaultIdentity(id, nickname, requestUri);
identity.setContexts(parseContexts("Contexts" + identityCounter + ".", fields));
identity.setProperties(parseProperties("Properties" + identityCounter + ".", fields));
- Integer trust = Numbers.safeParseInteger(fields.get("Trust" + identityCounter), null);
- int score = Numbers.safeParseInteger(fields.get("Score" + identityCounter), 0);
- int rank = Numbers.safeParseInteger(fields.get("Rank" + identityCounter), 0);
+ Integer trust = parseInt(fields.get("Trust" + identityCounter), null);
+ int score = parseInt(fields.get("Score" + identityCounter), 0);
+ int rank = parseInt(fields.get("Rank" + identityCounter), 0);
identity.setTrust(ownIdentity, new Trust(trust, score, rank));
identities.add(identity);
}
package net.pterodactylus.sone.template;
+import static java.lang.Integer.MAX_VALUE;
+import static java.lang.String.valueOf;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import net.pterodactylus.sone.core.Core;
import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Filter;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
return null;
}
String imageClass = String.valueOf(parameters.get("class"));
- int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE);
- int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE);
+ int maxWidth = parseInt(valueOf(parameters.get("max-width")), MAX_VALUE);
+ int maxHeight = parseInt(valueOf(parameters.get("max-height")), MAX_VALUE);
String mode = String.valueOf(parameters.get("mode"));
String title = String.valueOf(parameters.get("title"));
package net.pterodactylus.sone.template;
+import static java.lang.String.valueOf;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import net.pterodactylus.sone.text.SoneTextParser;
import net.pterodactylus.sone.text.SoneTextParserContext;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Filter;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
@Override
public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
String text = String.valueOf(data);
- int length = Numbers.safeParseInteger(parameters.get("length"), Numbers.safeParseInteger(templateContext.get(String.valueOf(parameters.get("length"))), -1));
- int cutOffLength = Numbers.safeParseInteger(parameters.get("cut-off-length"), Numbers.safeParseInteger(templateContext.get(String.valueOf(parameters.get("cut-off-length"))), length));
+ int length = parseInt(valueOf(parameters.get("length")), -1);
+ int cutOffLength = parseInt(valueOf(parameters.get("cut-off-length")), length);
Object sone = parameters.get("sone");
if (sone instanceof String) {
sone = core.getSone((String) sone).orNull();
--- /dev/null
+package net.pterodactylus.sone.utils;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import com.google.common.primitives.Ints;
+import com.google.common.primitives.Longs;
+
+/**
+ * Parses numbers from strings.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class NumberParsers {
+
+ @Nonnull
+ public static Integer parseInt(@Nullable String text,
+ @Nullable Integer defaultValue) {
+ if (text == null) {
+ return defaultValue;
+ }
+ Integer value = Ints.tryParse(text);
+ return (value == null) ? defaultValue : value;
+ }
+
+ @Nonnull
+ public static Long parseLong(@Nullable String text,
+ @Nullable Long defaultValue) {
+ if (text == null) {
+ return defaultValue;
+ }
+ Long value = Longs.tryParse(text);
+ return (value == null) ? defaultValue : value;
+ }
+
+}
package net.pterodactylus.sone.web;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.collection.Pagination;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
});
List<Post> sortedPosts = new ArrayList<Post>(loadedPosts);
Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
- Pagination<Post> pagination = new Pagination<Post>(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
+ Pagination<Post> pagination = new Pagination<Post>(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(parseInt(request.getHttpRequest().getParam("page"), 0));
templateContext.set("pagination", pagination);
templateContext.set("posts", pagination.getItems());
templateContext.set("postsNotLoaded", allPosts.size() != loadedPosts.size());
package net.pterodactylus.sone.web;
import static net.pterodactylus.sone.text.TextFilter.filter;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
import java.util.List;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.text.TextFilter;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim();
middleName = request.getHttpRequest().getPartAsStringFailsafe("middle-name", 256).trim();
lastName = request.getHttpRequest().getPartAsStringFailsafe("last-name", 256).trim();
- birthDay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim());
- birthMonth = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim());
- birthYear = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim());
+ birthDay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim(), null);
+ birthMonth = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim(), null);
+ birthYear = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim(), null);
avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36);
profile.setFirstName(firstName.length() > 0 ? firstName : null);
profile.setMiddleName(middleName.length() > 0 ? middleName : null);
import static net.pterodactylus.sone.data.Album.FLATTENER;
import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
import static net.pterodactylus.sone.data.Album.TITLE_COMPARATOR;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
import java.net.URI;
import java.util.ArrayList;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.collection.Pagination;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
albums.addAll(from(sone.getRootAlbum().getAlbums()).transformAndConcat(FLATTENER).filter(NOT_EMPTY).toList());
}
Collections.sort(albums, TITLE_COMPARATOR);
- Pagination<Album> albumPagination = new Pagination<Album>(albums, 12).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
+ Pagination<Album> albumPagination = new Pagination<Album>(albums, 12).setPage(parseInt(request.getHttpRequest().getParam("page"), 0));
templateContext.set("albumPagination", albumPagination);
templateContext.set("albums", albumPagination.getItems());
return;
package net.pterodactylus.sone.web;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import net.pterodactylus.sone.notify.ListNotificationFilters;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.collection.Pagination;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
allPosts = Collections2.filter(allPosts, Post.FUTURE_POSTS_FILTER);
List<Post> sortedPosts = new ArrayList<Post>(allPosts);
Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
- Pagination<Post> pagination = new Pagination<Post>(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
+ Pagination<Post> pagination = new Pagination<Post>(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(parseInt(request.getHttpRequest().getParam("page"), 0));
templateContext.set("pagination", pagination);
templateContext.set("posts", pagination.getItems());
}
package net.pterodactylus.sone.web;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.collection.Pagination;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR);
}
}
- Pagination<Sone> sonePagination = new Pagination<Sone>(sortedSones, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
+ Pagination<Sone> sonePagination = new Pagination<Sone>(sortedSones, 25).setPage(parseInt(request.getHttpRequest().getParam("page"), 0));
templateContext.set("pagination", sonePagination);
templateContext.set("knownSones", sonePagination.getItems());
}
package net.pterodactylus.sone.web;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import net.pterodactylus.sone.notify.ListNotificationFilters;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.collection.Pagination;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
/* paginate them. */
- Pagination<Post> pagination = new Pagination<Post>(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
+ Pagination<Post> pagination = new Pagination<Post>(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(parseInt(request.getHttpRequest().getParam("page"), 0));
templateContext.set("pagination", pagination);
templateContext.set("posts", pagination.getItems());
}
package net.pterodactylus.sone.web;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
import java.util.ArrayList;
import java.util.List;
import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
currentSone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(showCustomAvatars));
webInterface.getCore().touchConfiguration();
}
- Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16));
+ Integer insertionDelay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16), null);
if (!preferences.validateInsertionDelay(insertionDelay)) {
fieldErrors.add("insertion-delay");
} else {
preferences.setInsertionDelay(insertionDelay);
}
- Integer postsPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null);
+ Integer postsPerPage = parseInt(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null);
if (!preferences.validatePostsPerPage(postsPerPage)) {
fieldErrors.add("posts-per-page");
} else {
preferences.setPostsPerPage(postsPerPage);
}
- Integer imagesPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4), null);
+ Integer imagesPerPage = parseInt(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4), null);
if (!preferences.validateImagesPerPage(imagesPerPage)) {
fieldErrors.add("images-per-page");
} else {
preferences.setImagesPerPage(imagesPerPage);
}
- Integer charactersPerPost = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null);
+ Integer charactersPerPost = parseInt(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null);
if (!preferences.validateCharactersPerPost(charactersPerPost)) {
fieldErrors.add("characters-per-post");
} else {
preferences.setCharactersPerPost(charactersPerPost);
}
- Integer postCutOffLength = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10), null);
+ Integer postCutOffLength = parseInt(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10), null);
if (!preferences.validatePostCutOffLength(postCutOffLength)) {
fieldErrors.add("post-cut-off-length");
} else {
}
boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access");
preferences.setRequireFullAccess(requireFullAccess);
- Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3));
+ Integer positiveTrust = parseInt(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3), null);
if (!preferences.validatePositiveTrust(positiveTrust)) {
fieldErrors.add("positive-trust");
} else {
preferences.setPositiveTrust(positiveTrust);
}
- Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4));
+ Integer negativeTrust = parseInt(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4), null);
if (!preferences.validateNegativeTrust(negativeTrust)) {
fieldErrors.add("negative-trust");
} else {
preferences.setTrustComment(trustComment);
boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active");
preferences.setFcpInterfaceActive(fcpInterfaceActive);
- Integer fcpFullAccessRequiredInteger = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal());
+ Integer fcpFullAccessRequiredInteger = parseInt(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal());
FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger];
preferences.setFcpFullAccessRequired(fcpFullAccessRequired);
webInterface.getCore().touchConfiguration();
package net.pterodactylus.sone.web;
+import static net.pterodactylus.sone.utils.NumberParsers.parseLong;
+
import net.pterodactylus.sone.core.SoneRescuer;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
SoneRescuer soneRescuer = webInterface.getCore().getSoneRescuer(currentSone);
if (request.getMethod() == Method.POST) {
if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("fetch", 4))) {
- long edition = Numbers.safeParseLong(request.getHttpRequest().getPartAsStringFailsafe("edition", 8), -1L);
+ long edition = parseLong(request.getHttpRequest().getPartAsStringFailsafe("edition", 8), -1L);
if (edition > -1) {
soneRescuer.setEdition(edition);
}
package net.pterodactylus.sone.web;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import net.pterodactylus.sone.template.SoneAccessor;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.collection.Pagination;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
List<Post> sonePosts = sone.get().getPosts();
sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone.get().getId()));
Collections.sort(sonePosts, Post.TIME_COMPARATOR);
- Pagination<Post> postPagination = new Pagination<Post>(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
+ Pagination<Post> postPagination = new Pagination<Post>(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(parseInt(request.getHttpRequest().getParam("postPage"), 0));
templateContext.set("postPagination", postPagination);
templateContext.set("posts", postPagination.getItems());
Set<PostReply> replies = sone.get().getReplies();
});
- Pagination<Post> repliedPostPagination = new Pagination<Post>(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("repliedPostPage"), 0));
+ Pagination<Post> repliedPostPagination = new Pagination<Post>(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(parseInt(request.getHttpRequest().getParam("repliedPostPage"), 0));
templateContext.set("repliedPostPagination", repliedPostPagination);
templateContext.set("repliedPosts", repliedPostPagination.getItems());
}
--- /dev/null
+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());
+ }
+
+}