aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org
diff options
context:
space:
mode:
authorJaven O'Neal <onealj@apache.org>2016-10-19 23:04:16 +0000
committerJaven O'Neal <onealj@apache.org>2016-10-19 23:04:16 +0000
commitf6346b87a413c9e45fcfa7d059e60bb270ba4584 (patch)
tree6f32d9f41b4284feba5f1da0b9c5f2b516bfc7f5 /src/testcases/org
parente88722f05275c24acb78a1442cd9c09a83cc6c00 (diff)
downloadpoi-f6346b87a413c9e45fcfa7d059e60bb270ba4584.tar.gz
poi-f6346b87a413c9e45fcfa7d059e60bb270ba4584.zip
add StringUtil.count, inspired by Apache Commons Lang StringUtils#countMatches and Python's str.count(substr)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1765730 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org')
-rw-r--r--src/testcases/org/apache/poi/util/TestStringUtil.java19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/util/TestStringUtil.java b/src/testcases/org/apache/poi/util/TestStringUtil.java
index 002c8dcd2d..122ec408d9 100644
--- a/src/testcases/org/apache/poi/util/TestStringUtil.java
+++ b/src/testcases/org/apache/poi/util/TestStringUtil.java
@@ -192,5 +192,24 @@ public class TestStringUtil {
assertEquals("abc|def|ghi", StringUtil.join("|", "abc", "def", "ghi"));
assertEquals("5|8.5|true|string", StringUtil.join("|", 5, 8.5, true, "string")); //assumes Locale prints number decimal point as a period rather than a comma
}
+
+ @Test
+ public void count() {
+ String test = "Apache POI project\n\u00a9 Copyright 2016";
+ // supports search in null or empty string
+ assertEquals("null", 0, StringUtil.countMatches(null, 'A'));
+ assertEquals("empty string", 0, StringUtil.countMatches("", 'A'));
+
+ assertEquals("normal", 2, StringUtil.countMatches(test, 'e'));
+ assertEquals("normal, should not find a in escaped copyright", 1, StringUtil.countMatches(test, 'a'));
+
+ // search for non-printable characters
+ assertEquals("null character", 0, StringUtil.countMatches(test, '\0'));
+ assertEquals("CR", 0, StringUtil.countMatches(test, '\r'));
+ assertEquals("LF", 1, StringUtil.countMatches(test, '\n'));
+
+ // search for unicode characters
+ assertEquals("Unicode", 1, StringUtil.countMatches(test, '\u00a9'));
+ }
}