TagFilter { protected Result matches(Map<String, String> tags) { List<String> log = new ArrayList<>(); boolean matched = true; for (String filterkey : filter.keySet()) { String filterval = filter.get(filterkey); String itemval = tags.get(filterkey); String detail = "filter(" + filterkey + ((filterval != null) ? ":" + filterval : "") + ") " + "tag(" + ((tags.containsKey(filterkey) ? filterkey : "") + (((tags.get(filterkey) != null) ? ":" + tags.get(filterkey) : ""))) + ")"; if (filterval == null) { if (tags.containsKey(filterkey)) { log.add("(☑, ) " + detail + ": matched names"); } else { log.add("(☐, ) " + detail + ": did not match)"); matched = false; } } else { Pattern filterpattern = Pattern.compile("^" + filterval + "$"); if (itemval == null) { log.add("(☑,☐) " + detail + ": null tag value did not match '" + filterpattern + "'"); matched = false; } else if (filterpattern.matcher(itemval).matches()) { log.add("(☑,☑) " + detail + ": matched pattern '" + filterpattern + "'"); } else { log.add("(☑,☐) " + detail + ": did not match '" + filterpattern + "'"); matched = false; } } } return new Result(matched, log); } TagFilter(String filterSpec); Result matchesMap(Map<String, String> tags); Result matchesTaggedResult(Tagged item); boolean matchesTagged(Tagged item); Map<String, String> getMap(); static TagFilter MATCH_ALL; }
@Test public void testEmptyTagFilterDoesMatch() { Map<String,String> itemtags = new HashMap<String,String>() {{ put("a","tag"); }}; TagFilter tf = new TagFilter(""); assertThat(tf.matches(itemtags).matched()).isTrue(); }
@Test public void testSomeFilterTagsNoItemTagsDoesNotMatch() { Map<String,String> itemtags = new HashMap<String,String>() {{ }}; TagFilter tf = new TagFilter("tag=foo"); assertThat(tf.matches(itemtags).matched()).isFalse(); }
@Test public void testEmptyTagFilterValueDoesMatch() { Map<String,String> itemtags = new HashMap<String,String>() {{ put("one","two"); }}; TagFilter tf = new TagFilter(""); assertThat(tf.matches(itemtags).matched()).isTrue(); }
@Test public void testMatchingTagKeyValueDoesMatch() { Map<String,String> itemtags = new HashMap<String,String>() {{ put("one","two"); }}; TagFilter tf = new TagFilter("one"); TagFilter.Result result = tf.matches(itemtags); assertThat(result.matched()).isTrue(); Map<String,String> itemtags2 = new HashMap<String,String>() {{ put("one",null); }}; assertThat(tf.matches(itemtags2).matched()).isTrue(); }
@Test public void testMatchingKeyMismatchingValueDoesNotMatch() { Map<String,String> itemtags = new HashMap<String,String>() {{ put("one","four"); }}; TagFilter tf = new TagFilter("one:two"); TagFilter.Result result = tf.matches(itemtags); assertThat(result.matched()).isFalse(); }
@Test public void testMatchingKeyAndValueDoesMatch() { Map<String,String> itemtags = new HashMap<String,String>() {{ put("one","four"); }}; TagFilter tf = new TagFilter("one:four"); assertThat(tf.matches(itemtags).matched()).isTrue(); }
@Test public void testMatchingKeyAndValueRegexDoesMatch() { Map<String,String> itemtags = new HashMap<String,String>() {{ put("one","four-five-six"); }}; TagFilter tfLeft = new TagFilter("one:'four-.*'"); assertThat(tfLeft.matches(itemtags).matched()).isTrue(); TagFilter tfInner = new TagFilter("one:'.*-five-.*'"); assertThat(tfInner.matches(itemtags).matched()).isTrue(); TagFilter tfRight = new TagFilter("one:'.*-six'"); assertThat(tfRight.matches(itemtags).matched()).isTrue(); }
@Test public void testRawSubstringDoesNotMatchRegex() { Map<String,String> itemtags = new HashMap<String,String>() {{ put("one","four-five-six"); }}; TagFilter tf = new TagFilter("one:'five'"); assertThat(tf.matches(itemtags).matched()).isFalse(); }
@Test public void testAlternation() { Map<String,String> itemtags = new HashMap<String,String>() {{ put("one","four-five-six"); }}; TagFilter tf = new TagFilter("one:'four.*|seven'"); assertThat(tf.matches(itemtags).matched()).isTrue(); }