From 700c0cb4415df7cce346c7cb76fa4a52d0c54bfa Mon Sep 17 00:00:00 2001 From: earayu Date: Fri, 21 Sep 2018 19:41:52 +0800 Subject: [PATCH] unit test & comments --- .../core/util/matcher/FalseMatcher.java | 5 ++ .../core/util/matcher/RegexMatcher.java | 2 +- .../arthas/core/util/matcher/TrueMatcher.java | 5 ++ .../core/util/matcher/WildcardMatcher.java | 2 +- .../core/util/matcher/EqualsMatcherTest.java | 20 ++++++ .../core/util/matcher/FalseMatcherTest.java | 19 ++++++ .../core/util/matcher/RegexMatcherTest.java | 63 +++++++++++++++++++ .../core/util/matcher/TrueMatcherTest.java | 19 ++++++ 8 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 core/src/test/java/com/taobao/arthas/core/util/matcher/EqualsMatcherTest.java create mode 100644 core/src/test/java/com/taobao/arthas/core/util/matcher/FalseMatcherTest.java create mode 100644 core/src/test/java/com/taobao/arthas/core/util/matcher/RegexMatcherTest.java create mode 100644 core/src/test/java/com/taobao/arthas/core/util/matcher/TrueMatcherTest.java diff --git a/core/src/main/java/com/taobao/arthas/core/util/matcher/FalseMatcher.java b/core/src/main/java/com/taobao/arthas/core/util/matcher/FalseMatcher.java index 4294834f..1754c9c7 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/matcher/FalseMatcher.java +++ b/core/src/main/java/com/taobao/arthas/core/util/matcher/FalseMatcher.java @@ -5,6 +5,11 @@ package com.taobao.arthas.core.util.matcher; */ public class FalseMatcher implements Matcher { + /** + * always return false + * @param target + * @return true/false + */ @Override public boolean matching(T target) { return false; diff --git a/core/src/main/java/com/taobao/arthas/core/util/matcher/RegexMatcher.java b/core/src/main/java/com/taobao/arthas/core/util/matcher/RegexMatcher.java index 4c6673dc..1998265e 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/matcher/RegexMatcher.java +++ b/core/src/main/java/com/taobao/arthas/core/util/matcher/RegexMatcher.java @@ -1,7 +1,7 @@ package com.taobao.arthas.core.util.matcher; /** - * 正则表达式匹配 + * regex matcher * @author ralf0131 2017-01-06 13:16. */ public class RegexMatcher implements Matcher { diff --git a/core/src/main/java/com/taobao/arthas/core/util/matcher/TrueMatcher.java b/core/src/main/java/com/taobao/arthas/core/util/matcher/TrueMatcher.java index 28173b9c..07512005 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/matcher/TrueMatcher.java +++ b/core/src/main/java/com/taobao/arthas/core/util/matcher/TrueMatcher.java @@ -5,6 +5,11 @@ package com.taobao.arthas.core.util.matcher; */ public final class TrueMatcher implements Matcher { + /** + * always return true + * @param target + * @return + */ @Override public boolean matching(T target) { return true; diff --git a/core/src/main/java/com/taobao/arthas/core/util/matcher/WildcardMatcher.java b/core/src/main/java/com/taobao/arthas/core/util/matcher/WildcardMatcher.java index e46885c2..3ab53cd4 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/matcher/WildcardMatcher.java +++ b/core/src/main/java/com/taobao/arthas/core/util/matcher/WildcardMatcher.java @@ -1,7 +1,7 @@ package com.taobao.arthas.core.util.matcher; /** - * 通配符表达式匹配 + * wildcard matcher * @author ralf0131 2017-01-06 13:17. */ public class WildcardMatcher implements Matcher { diff --git a/core/src/test/java/com/taobao/arthas/core/util/matcher/EqualsMatcherTest.java b/core/src/test/java/com/taobao/arthas/core/util/matcher/EqualsMatcherTest.java new file mode 100644 index 00000000..6a4191d5 --- /dev/null +++ b/core/src/test/java/com/taobao/arthas/core/util/matcher/EqualsMatcherTest.java @@ -0,0 +1,20 @@ +package com.taobao.arthas.core.util.matcher; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author earayu + */ +public class EqualsMatcherTest { + + @Test + public void testMatching(){ + Assert.assertTrue(new EqualsMatcher(null).matching(null)); + Assert.assertTrue(new EqualsMatcher("").matching("")); + Assert.assertTrue(new EqualsMatcher("foobar").matching("foobar")); + Assert.assertFalse(new EqualsMatcher("").matching(null)); + Assert.assertFalse(new EqualsMatcher("abc").matching("def")); + } + +} diff --git a/core/src/test/java/com/taobao/arthas/core/util/matcher/FalseMatcherTest.java b/core/src/test/java/com/taobao/arthas/core/util/matcher/FalseMatcherTest.java new file mode 100644 index 00000000..5314df7e --- /dev/null +++ b/core/src/test/java/com/taobao/arthas/core/util/matcher/FalseMatcherTest.java @@ -0,0 +1,19 @@ +package com.taobao.arthas.core.util.matcher; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author earayu + */ +public class FalseMatcherTest { + + @Test + public void testMatching(){ + Assert.assertFalse(new FalseMatcher().matching(null)); + Assert.assertFalse(new FalseMatcher().matching(1)); + Assert.assertFalse(new FalseMatcher().matching("")); + Assert.assertFalse(new FalseMatcher().matching("foobar")); + } + +} diff --git a/core/src/test/java/com/taobao/arthas/core/util/matcher/RegexMatcherTest.java b/core/src/test/java/com/taobao/arthas/core/util/matcher/RegexMatcherTest.java new file mode 100644 index 00000000..f272ae27 --- /dev/null +++ b/core/src/test/java/com/taobao/arthas/core/util/matcher/RegexMatcherTest.java @@ -0,0 +1,63 @@ +package com.taobao.arthas.core.util.matcher; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author earayu + */ +public class RegexMatcherTest { + + @Test + public void testMatchingWithNullInputs(){ + Assert.assertFalse(new RegexMatcher(null).matching(null)); + Assert.assertFalse(new RegexMatcher(null).matching("foobar")); + Assert.assertFalse(new RegexMatcher("foobar").matching(null)); + Assert.assertTrue(new RegexMatcher("foobar").matching("foobar")); + } + + /** + * test regux with . | * + ? \s \S \w \W and so on... + */ + @Test + public void testMatchingWithRegularGrammar(){ + Assert.assertTrue(new RegexMatcher("foo?").matching("fo")); + Assert.assertTrue(new RegexMatcher("foo?").matching("foo")); + Assert.assertTrue(new RegexMatcher("foo.").matching("fooo")); + Assert.assertTrue(new RegexMatcher("foo*").matching("fooooo")); + Assert.assertTrue(new RegexMatcher("foo.*").matching("foobarbarbar")); + Assert.assertFalse(new RegexMatcher("foo+").matching("fo")); + Assert.assertTrue(new RegexMatcher("foo+").matching("fooooo")); + + Assert.assertTrue(new RegexMatcher("foo\\s").matching("foo ")); + Assert.assertFalse(new RegexMatcher("foo\\S").matching("foo ")); + Assert.assertTrue(new RegexMatcher("foo\\w").matching("fooo")); + Assert.assertTrue(new RegexMatcher("foo\\W").matching("foo ")); + Assert.assertFalse(new RegexMatcher("foo\\W").matching("fooo")); + + + Assert.assertTrue(new RegexMatcher("foo[1234]").matching("foo1")); + Assert.assertFalse(new RegexMatcher("foo[1234]").matching("foo5")); + Assert.assertTrue(new RegexMatcher("foo\\\\").matching("foo\\")); + Assert.assertTrue(new RegexMatcher("foo\\d").matching("foo5")); + Assert.assertTrue(new RegexMatcher("fo{1,3}").matching("fo")); + Assert.assertFalse(new RegexMatcher("fo{1,3}").matching("foooo")); + } + + @Test + public void testMatchingComplexRegex(){ + String ipAddressPattern = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"; + Assert.assertTrue(new RegexMatcher(ipAddressPattern).matching("1.1.1.1")); + Assert.assertFalse(new RegexMatcher(ipAddressPattern).matching("255.256.255.0")); + Assert.assertFalse(new RegexMatcher(ipAddressPattern).matching("1.1.1")); + + Assert.assertTrue(new RegexMatcher("^foobar$").matching("foobar")); + Assert.assertFalse(new RegexMatcher("^foobar$").matching("\nfoobar")); + Assert.assertFalse(new RegexMatcher("^foobar$").matching("foobar\n")); + + String emailAddressPattern = "[a-z\\d]+(\\.[a-z\\d]+)*@([\\da-z](-[\\da-z])?)+(\\.{1,2}[a-z]+)+"; + Assert.assertTrue(new RegexMatcher(emailAddressPattern).matching("foo@bar.com")); + Assert.assertFalse(new RegexMatcher(emailAddressPattern).matching("asdfghjkl")); + } + +} diff --git a/core/src/test/java/com/taobao/arthas/core/util/matcher/TrueMatcherTest.java b/core/src/test/java/com/taobao/arthas/core/util/matcher/TrueMatcherTest.java new file mode 100644 index 00000000..1cfc6fbd --- /dev/null +++ b/core/src/test/java/com/taobao/arthas/core/util/matcher/TrueMatcherTest.java @@ -0,0 +1,19 @@ +package com.taobao.arthas.core.util.matcher; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author earayu + */ +public class TrueMatcherTest { + + @Test + public void testMatching(){ + Assert.assertTrue(new TrueMatcher().matching(null)); + Assert.assertTrue(new TrueMatcher().matching(1)); + Assert.assertTrue(new TrueMatcher().matching("")); + Assert.assertTrue(new TrueMatcher().matching("foobar")); + } + +}