1 package net.opentsdb.query.filter; 2 3 import static org.junit.Assert.assertEquals; 4 import static org.junit.Assert.assertFalse; 5 import static org.junit.Assert.assertTrue; 6 7 import java.util.HashMap; 8 import java.util.Map; 9 import java.util.regex.PatternSyntaxException; 10 11 import org.junit.Before; 12 import org.junit.Test; 13 14 public class TestTagVRegexFilter { 15 private static final String TAGK = "host"; 16 private Map<String, String> tags; 17 18 @Before before()19 public void before() throws Exception { 20 tags = new HashMap<String, String>(1); 21 tags.put(TAGK, "ogg-01.ops.ankh.morpork.com"); 22 } 23 24 @Test matchExact()25 public void matchExact() throws Exception { 26 TagVFilter filter = new TagVRegexFilter(TAGK, "ogg-01.ops.ankh.morpork.com"); 27 assertTrue(filter.match(tags).join()); 28 } 29 30 @Test matchPostfix()31 public void matchPostfix() throws Exception { 32 TagVFilter filter = new TagVRegexFilter(TAGK, ".*.ops.ankh.morpork.com"); 33 assertTrue(filter.match(tags).join()); 34 } 35 36 @Test matchPrefix()37 public void matchPrefix() throws Exception { 38 TagVFilter filter = new TagVRegexFilter(TAGK, "ogg-01.ops.ankh.*"); 39 assertTrue(filter.match(tags).join()); 40 } 41 42 @Test matchAnything()43 public void matchAnything() throws Exception { 44 TagVFilter filter = new TagVRegexFilter(TAGK, ".*"); 45 assertTrue(filter.match(tags).join()); 46 } 47 48 @Test matchFailed()49 public void matchFailed() throws Exception { 50 TagVFilter filter = new TagVRegexFilter(TAGK, "ogg-01.ops.qurim.*"); 51 assertFalse(filter.match(tags).join()); 52 } 53 54 @Test matchGrouping()55 public void matchGrouping() throws Exception { 56 TagVFilter filter = new TagVRegexFilter(TAGK, 57 "ogg-01.ops.(ankh|quirm|tsort).morpork.com"); 58 assertTrue(filter.match(tags).join()); 59 } 60 61 @Test matchNumbers()62 public void matchNumbers() throws Exception { 63 TagVFilter filter = new TagVRegexFilter(TAGK, 64 "ogg-\\d+.ops.ankh.morpork.com"); 65 assertTrue(filter.match(tags).join()); 66 } 67 68 @Test matchNotEnoughNumbers()69 public void matchNotEnoughNumbers() throws Exception { 70 TagVFilter filter = new TagVRegexFilter(TAGK, 71 "ogg-\\d(3).ops.ankh.morpork.com"); 72 assertFalse(filter.match(tags).join()); 73 } 74 75 @Test (expected = IllegalArgumentException.class) ctorNullTagk()76 public void ctorNullTagk() throws Exception { 77 new TagVRegexFilter(null, "ogg-01.ops.qurim.*"); 78 } 79 80 @Test (expected = IllegalArgumentException.class) ctorEmptyTagk()81 public void ctorEmptyTagk() throws Exception { 82 new TagVRegexFilter("", "ogg-01.ops.qurim.*"); 83 } 84 85 @Test (expected = IllegalArgumentException.class) ctorNullFilter()86 public void ctorNullFilter() throws Exception { 87 new TagVRegexFilter(TAGK, null); 88 } 89 90 @Test (expected = IllegalArgumentException.class) ctorEmptyFilter()91 public void ctorEmptyFilter() throws Exception { 92 new TagVRegexFilter(TAGK, ""); 93 } 94 95 @Test (expected = PatternSyntaxException.class) ctorBadRegex()96 public void ctorBadRegex() throws Exception { 97 new TagVRegexFilter(TAGK, "ogg-\\d(3.ops.ankh.morpork.com"); 98 } 99 100 @Test toStringTest()101 public void toStringTest() throws Exception { 102 TagVFilter filter = new TagVRegexFilter(TAGK, 103 "ogg-\\d+.ops.ankh.morpork.com"); 104 assertTrue(filter.toString().contains("regex")); 105 } 106 107 @Test hashCodeAndEqualsTest()108 public void hashCodeAndEqualsTest() throws Exception { 109 TagVFilter filter_a = new TagVRegexFilter(TAGK, 110 "ogg-\\d+.ops.ankh.morpork.com"); 111 TagVFilter filter_b = new TagVRegexFilter(TAGK, 112 "ogg-\\d+.ops.ankh.morpork.com"); 113 TagVFilter filter_c = new TagVRegexFilter(TAGK, 114 "ogg-\\d.ops.ankh.morpork.com"); 115 TagVFilter filter_d = new TagVRegexFilter(TAGK, 116 "ogg-\\d+.ops.ankh.morpork.co"); 117 118 assertEquals(filter_a.hashCode(), filter_b.hashCode()); 119 assertFalse(filter_a.hashCode() == filter_c.hashCode()); 120 assertFalse(filter_a.hashCode() == filter_d.hashCode()); 121 122 assertEquals(filter_a, filter_b); 123 assertFalse(filter_a.equals(filter_c)); 124 assertFalse(filter_a.equals(filter_d)); 125 } 126 } 127