-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
determine if pattern is starts-with or contains (#1070)
Adds support to PatternMatcher to check if a pattern is a strict starts-with or contains check. This can be useful when mapping to some data stores that have optimizations for those operations that will not work with arbitrary regex. Also adds a method to get a contained string that can be used as an initial filter.
- Loading branch information
1 parent
2b2e651
commit 334a51a
Showing
5 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
spectator-api/src/test/java/com/netflix/spectator/impl/matcher/ContainsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2014-2023 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spectator.impl.matcher; | ||
|
||
import com.netflix.spectator.impl.PatternMatcher; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ContainsTest { | ||
|
||
private PatternMatcher re(String pattern) { | ||
return PatternMatcher.compile(pattern); | ||
} | ||
|
||
private void assertStartsWith(String pattern) { | ||
PatternMatcher m = re(pattern); | ||
Assertions.assertTrue(m.isPrefixMatcher(), pattern); | ||
Assertions.assertTrue(m.isContainsMatcher(), pattern); | ||
} | ||
|
||
private void assertContainsOnly(String pattern) { | ||
PatternMatcher m = re(pattern); | ||
Assertions.assertFalse(m.isPrefixMatcher(), pattern); | ||
Assertions.assertTrue(m.isContainsMatcher(), pattern); | ||
} | ||
|
||
@Test | ||
public void startsWith() { | ||
assertStartsWith("^abc"); | ||
assertStartsWith("^abc[.]def"); | ||
assertStartsWith("^abc\\.def"); | ||
} | ||
|
||
@Test | ||
public void notStartsWith() { | ||
Assertions.assertFalse(re("abc").isPrefixMatcher()); | ||
Assertions.assertFalse(re("ab[cd]").isPrefixMatcher()); | ||
Assertions.assertFalse(re("^abc.def").isPrefixMatcher()); | ||
} | ||
|
||
@Test | ||
public void contains() { | ||
assertContainsOnly("abc"); | ||
assertContainsOnly(".*abc"); | ||
assertContainsOnly("abc\\.def"); | ||
} | ||
|
||
@Test | ||
public void notContains() { | ||
Assertions.assertFalse(re("ab[cd]").isContainsMatcher()); | ||
Assertions.assertFalse(re("abc.def").isContainsMatcher()); | ||
} | ||
|
||
@Test | ||
public void containedString() { | ||
Assertions.assertEquals("abc", re("abc").containedString()); | ||
Assertions.assertEquals("abc", re(".*abc").containedString()); | ||
Assertions.assertEquals("ab", re(".*ab[cd]").containedString()); | ||
Assertions.assertEquals("abc", re("abc.def").containedString()); | ||
Assertions.assertEquals("abc.def", re("abc\\.def").containedString()); | ||
Assertions.assertEquals("abc", re("^abc.def").containedString()); | ||
Assertions.assertEquals("abc.def", re("^abc\\.def").containedString()); | ||
} | ||
} |