|
| 1 | +/* |
| 2 | + * Copyright (C) The Prometheus jmx_exporter Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.prometheus.jmx.test.support.metrics; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.util.stream.Stream; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.Arguments; |
| 25 | +import org.junit.jupiter.params.provider.MethodSource; |
| 26 | + |
| 27 | +class GlobMatcherTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void singleWildcardMatchesEverything() { |
| 31 | + assertThat(MetricsAssertions.matchesGlob("*", "")).isTrue(); |
| 32 | + assertThat(MetricsAssertions.matchesGlob("*", "anything")).isTrue(); |
| 33 | + assertThat(MetricsAssertions.matchesGlob("*", "with spaces")).isTrue(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void exactMatch() { |
| 38 | + assertThat(MetricsAssertions.matchesGlob("hello", "hello")).isTrue(); |
| 39 | + assertThat(MetricsAssertions.matchesGlob("hello", "hell")).isFalse(); |
| 40 | + assertThat(MetricsAssertions.matchesGlob("hello", "helloo")).isFalse(); |
| 41 | + assertThat(MetricsAssertions.matchesGlob("hello", "Hello")).isFalse(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void prefixWildcard() { |
| 46 | + assertThat(MetricsAssertions.matchesGlob("pre*", "prefix")).isTrue(); |
| 47 | + assertThat(MetricsAssertions.matchesGlob("pre*", "pre")).isTrue(); |
| 48 | + assertThat(MetricsAssertions.matchesGlob("pre*", "pr")).isFalse(); |
| 49 | + assertThat(MetricsAssertions.matchesGlob("pre*", "other")).isFalse(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void suffixWildcard() { |
| 54 | + assertThat(MetricsAssertions.matchesGlob("*suf", "endsuf")).isTrue(); |
| 55 | + assertThat(MetricsAssertions.matchesGlob("*suf", "suf")).isTrue(); |
| 56 | + assertThat(MetricsAssertions.matchesGlob("*suf", "suffix")).isFalse(); |
| 57 | + assertThat(MetricsAssertions.matchesGlob("*suf", "other")).isFalse(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void containsWildcard() { |
| 62 | + assertThat(MetricsAssertions.matchesGlob("*mid*", "has middle here")).isTrue(); |
| 63 | + assertThat(MetricsAssertions.matchesGlob("*mid*", "middle")).isTrue(); |
| 64 | + assertThat(MetricsAssertions.matchesGlob("*mid*", "mid")).isTrue(); |
| 65 | + assertThat(MetricsAssertions.matchesGlob("*mid*", "no match")).isFalse(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void prefixAndSuffix() { |
| 70 | + assertThat(MetricsAssertions.matchesGlob("a*z", "az")).isTrue(); |
| 71 | + assertThat(MetricsAssertions.matchesGlob("a*z", "abz")).isTrue(); |
| 72 | + assertThat(MetricsAssertions.matchesGlob("a*z", "axyz")).isTrue(); |
| 73 | + assertThat(MetricsAssertions.matchesGlob("a*z", "a")).isFalse(); |
| 74 | + assertThat(MetricsAssertions.matchesGlob("a*z", "z")).isFalse(); |
| 75 | + assertThat(MetricsAssertions.matchesGlob("a*z", "azb")).isFalse(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void multipleWildcards() { |
| 80 | + assertThat(MetricsAssertions.matchesGlob("*foo*bar", "xfooybar")).isTrue(); |
| 81 | + assertThat(MetricsAssertions.matchesGlob("*foo*bar", "foobar")).isTrue(); |
| 82 | + assertThat(MetricsAssertions.matchesGlob("*foo*bar", "fooxxx")).isFalse(); |
| 83 | + assertThat(MetricsAssertions.matchesGlob("*foo*bar", "barfoo")).isFalse(); |
| 84 | + |
| 85 | + assertThat(MetricsAssertions.matchesGlob("foo*bar*", "foobarx")).isTrue(); |
| 86 | + assertThat(MetricsAssertions.matchesGlob("foo*bar*", "foobar")).isTrue(); |
| 87 | + assertThat(MetricsAssertions.matchesGlob("foo*bar*", "fooxbar")).isTrue(); |
| 88 | + assertThat(MetricsAssertions.matchesGlob("foo*bar*", "barfoo")).isFalse(); |
| 89 | + |
| 90 | + assertThat(MetricsAssertions.matchesGlob("*foo*bar*", "xfooybarz")).isTrue(); |
| 91 | + assertThat(MetricsAssertions.matchesGlob("*foo*bar*", "foobar")).isTrue(); |
| 92 | + assertThat(MetricsAssertions.matchesGlob("*foo*bar*", "fooxbar")).isTrue(); |
| 93 | + assertThat(MetricsAssertions.matchesGlob("*foo*bar*", "barfoobaz")).isFalse(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void threeWildcards() { |
| 98 | + assertThat(MetricsAssertions.matchesGlob("a*b*c*d", "aXbYcZd")).isTrue(); |
| 99 | + assertThat(MetricsAssertions.matchesGlob("a*b*c*d", "abcd")).isTrue(); |
| 100 | + assertThat(MetricsAssertions.matchesGlob("a*b*c*d", "aXbYc")).isFalse(); |
| 101 | + assertThat(MetricsAssertions.matchesGlob("a*b*c*d", "bYcZd")).isFalse(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void allWildcards() { |
| 106 | + assertThat(MetricsAssertions.matchesGlob("*a*b*c*", "XaYbZcW")).isTrue(); |
| 107 | + assertThat(MetricsAssertions.matchesGlob("*a*b*c*", "abc")).isTrue(); |
| 108 | + assertThat(MetricsAssertions.matchesGlob("*a*b*c*", "aXbYc")).isTrue(); |
| 109 | + assertThat(MetricsAssertions.matchesGlob("*a*b*c*", "abcW")).isTrue(); |
| 110 | + assertThat(MetricsAssertions.matchesGlob("*a*b*c*", "XaYcZbW")).isFalse(); |
| 111 | + assertThat(MetricsAssertions.matchesGlob("*a*b*c*", "xxx")).isFalse(); |
| 112 | + assertThat(MetricsAssertions.matchesGlob("*a*b*c*", "cba")).isFalse(); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void emptySegments() { |
| 117 | + assertThat(MetricsAssertions.matchesGlob("**", "anything")).isTrue(); |
| 118 | + assertThat(MetricsAssertions.matchesGlob("a**b", "axb")).isTrue(); |
| 119 | + assertThat(MetricsAssertions.matchesGlob("a**b", "ab")).isTrue(); |
| 120 | + } |
| 121 | + |
| 122 | + @ParameterizedTest |
| 123 | + @MethodSource("jvmVersionExamples") |
| 124 | + void jvmVersionGlobPatterns(String pattern, String value, boolean expected) { |
| 125 | + assertThat(MetricsAssertions.matchesGlob(pattern, value)).isEqualTo(expected); |
| 126 | + } |
| 127 | + |
| 128 | + static Stream<Arguments> jvmVersionGlobPatterns() { |
| 129 | + return Stream.of( |
| 130 | + // Exact version (no wildcards) |
| 131 | + Arguments.of("1.8.0_492-b09", "1.8.0_492-b09", true), |
| 132 | + Arguments.of("1.8.0_492-b09", "1.8.0_502-b09", false), |
| 133 | + |
| 134 | + // Any version |
| 135 | + Arguments.of("*", "25.0.3+9-LTS", true), |
| 136 | + |
| 137 | + // Major version prefix |
| 138 | + Arguments.of("1.8*", "1.8.0_492-b09", true), |
| 139 | + Arguments.of("1.8*", "11.0.31+11-LTS", false), |
| 140 | + |
| 141 | + // LTS suffix |
| 142 | + Arguments.of("*-LTS", "25.0.3+9-LTS", true), |
| 143 | + Arguments.of("*-LTS", "25.0.3+9", false), |
| 144 | + |
| 145 | + // Contains jvmci |
| 146 | + Arguments.of("*jvmci*", "17.0.4+8-jvmci-22.3-b22", true), |
| 147 | + Arguments.of("*jvmci*", "17.0.4+8", false), |
| 148 | + |
| 149 | + // Starts with major, ends with build |
| 150 | + Arguments.of("17*b09", "17.0.19+10-b09", true), |
| 151 | + Arguments.of("17*b09", "17.0.19+10-b10", false), |
| 152 | + |
| 153 | + // Multiple wildcards |
| 154 | + Arguments.of("*0*LTS", "21.0.11+10-LTS", true), |
| 155 | + Arguments.of("*0*LTS", "21.0.11+10", false)); |
| 156 | + } |
| 157 | +} |
0 commit comments