From fcb003f440add7ec0fad6848dc8c2a3b02112aeb Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 9 Feb 2016 15:02:41 +0000 Subject: [PATCH] Don't match numbers that are too long --- source/net/filebot/util/StringUtilities.java | 12 +++++++-- .../net/filebot/util/StringUtilitiesTest.java | 25 +++++++++++++++++++ test/net/filebot/util/UtilTestSuite.java | 5 +--- 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 test/net/filebot/util/StringUtilitiesTest.java diff --git a/source/net/filebot/util/StringUtilities.java b/source/net/filebot/util/StringUtilities.java index 03f968e9..983541dd 100644 --- a/source/net/filebot/util/StringUtilities.java +++ b/source/net/filebot/util/StringUtilities.java @@ -24,7 +24,11 @@ public final class StringUtilities { List numbers = new ArrayList(); Matcher matcher = DIGIT.matcher(s); while (matcher.find()) { - numbers.add(new Integer(matcher.group())); + try { + numbers.add(new Integer(matcher.group())); + } catch (NumberFormatException e) { + // ignore + } } return numbers; } @@ -36,7 +40,11 @@ public final class StringUtilities { Matcher matcher = DIGIT.matcher(s); if (matcher.find()) { - return new Integer(matcher.group()); + try { + return new Integer(matcher.group()); + } catch (NumberFormatException e) { + // ignore + } } return null; } diff --git a/test/net/filebot/util/StringUtilitiesTest.java b/test/net/filebot/util/StringUtilitiesTest.java new file mode 100644 index 00000000..5ee24855 --- /dev/null +++ b/test/net/filebot/util/StringUtilitiesTest.java @@ -0,0 +1,25 @@ +package net.filebot.util; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.Test; + +public class StringUtilitiesTest { + + @Test + public void matchInteger() { + Integer n = StringUtilities.matchInteger("1091_20150217210000"); + + assertEquals("1091", n.toString()); + } + + @Test + public void matchIntegers() { + List n = StringUtilities.matchIntegers("1091_20150217210000"); + + assertEquals("[1091]", n.toString()); + } + +} diff --git a/test/net/filebot/util/UtilTestSuite.java b/test/net/filebot/util/UtilTestSuite.java index 905d558f..d2667e49 100644 --- a/test/net/filebot/util/UtilTestSuite.java +++ b/test/net/filebot/util/UtilTestSuite.java @@ -1,14 +1,11 @@ - package net.filebot.util; - import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; - @RunWith(Suite.class) -@SuiteClasses( { FileUtilitiesTest.class, ByteBufferOutputStreamTest.class, PreferencesMapTest.class, PreferencesListTest.class, TreeIteratorTest.class, FilterIteratorTest.class }) +@SuiteClasses({ FileUtilitiesTest.class, ByteBufferOutputStreamTest.class, PreferencesMapTest.class, PreferencesListTest.class, TreeIteratorTest.class, FilterIteratorTest.class, StringUtilities.class }) public class UtilTestSuite { }