|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.buildcache; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | + |
| 26 | +class ArrayFilterUtilsTest { |
| 27 | + |
| 28 | + @AfterEach |
| 29 | + void clearCache() { |
| 30 | + ArrayFilterUtils.clearPatternCache(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void testFilterAndStringifyArrayWithNullPattern() { |
| 35 | + String[] array = new String[] {"--module-version", "1.0.0", "-g"}; |
| 36 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, null); |
| 37 | + assertEquals("{--module-version,1.0.0,-g}", result); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void testFilterAndStringifyArrayWithMatchingPattern() { |
| 42 | + String[] array = new String[] {"--module-version", "1.0.0", "-g", "--module-version"}; |
| 43 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, "--module-version"); |
| 44 | + assertEquals("[1.0.0, -g]", result); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testFilterAndStringifyArrayWithPartialMatchPattern() { |
| 49 | + // Test that matches() is used instead of find() |
| 50 | + // Pattern "\\d+" should only match pure numbers like "123", not "Maven4" |
| 51 | + String[] array = new String[] {"123", "Maven4", "456", "abc"}; |
| 52 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, "\\d+"); |
| 53 | + // Should keep "Maven4" and "abc" because they don't fully match \d+ |
| 54 | + assertEquals("[Maven4, abc]", result); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testFilterAndStringifyArrayWithRegexPattern() { |
| 59 | + String[] array = new String[] {"--module-version", "1.0.0", "--release", "21", "-g"}; |
| 60 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, "--module-.*"); |
| 61 | + assertEquals("[1.0.0, --release, 21, -g]", result); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testFilterAndStringifyArrayRemovesAllElements() { |
| 66 | + String[] array = new String[] {"test", "test", "test"}; |
| 67 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, "test"); |
| 68 | + assertEquals("[]", result); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testFilterAndStringifyArrayWithNoMatches() { |
| 73 | + String[] array = new String[] {"-g", "-verbose", "-parameters"}; |
| 74 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, "--module-version"); |
| 75 | + assertEquals("[-g, -verbose, -parameters]", result); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testFilterAndStringifyArrayWithEmptyArray() { |
| 80 | + String[] array = new String[] {}; |
| 81 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, "--module-version"); |
| 82 | + assertEquals("[]", result); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testFilterAndStringifyArrayWithIntArray() { |
| 87 | + int[] array = new int[] {1, 2, 3, 4, 5}; |
| 88 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, "3"); |
| 89 | + assertEquals("[1, 2, 4, 5]", result); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testFilterAndStringifyArrayWithComplexPattern() { |
| 94 | + String[] array = new String[] {"--module-version", "1.0.0", "--add-exports", "module/package"}; |
| 95 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, "--module-version|--add-exports"); |
| 96 | + assertEquals("[1.0.0, module/package]", result); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testPatternCaching() { |
| 101 | + // First call should cache the pattern |
| 102 | + String[] array1 = new String[] {"--module-version", "1.0.0"}; |
| 103 | + String result1 = ArrayFilterUtils.filterAndStringifyArray(array1, "--module-version"); |
| 104 | + |
| 105 | + // Second call with same pattern should use cached version |
| 106 | + String[] array2 = new String[] {"--module-version", "2.0.0", "-g"}; |
| 107 | + String result2 = ArrayFilterUtils.filterAndStringifyArray(array2, "--module-version"); |
| 108 | + |
| 109 | + assertEquals("[1.0.0]", result1); |
| 110 | + assertEquals("[2.0.0, -g]", result2); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testMatchesVsFindBehavior() { |
| 115 | + // Test that pattern must match the entire string, not just find a substring |
| 116 | + String[] array = new String[] {"--module-version-extended", "--module-version", "abc"}; |
| 117 | + |
| 118 | + // Pattern should only match exact "--module-version", not "--module-version-extended" |
| 119 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, "--module-version"); |
| 120 | + |
| 121 | + // Using matches(), only exact match is excluded |
| 122 | + assertEquals("[--module-version-extended, abc]", result); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testNumericPatternExactMatch() { |
| 127 | + // Regression test for reviewer's concern about \\d+ matching "Maven4" |
| 128 | + String[] array = new String[] {"123", "Maven4", "7", "test123"}; |
| 129 | + String result = ArrayFilterUtils.filterAndStringifyArray(array, "\\d+"); |
| 130 | + |
| 131 | + // \\d+ with matches() should only exclude pure numbers "123" and "7" |
| 132 | + // Should keep "Maven4" and "test123" since they don't fully match |
| 133 | + assertEquals("[Maven4, test123]", result); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void testMaven4ModuleVersionUseCase() { |
| 138 | + // Real-world test case from the PR description |
| 139 | + String[] compilerArgs = new String[] { |
| 140 | + "-parameters", |
| 141 | + "--module-version", |
| 142 | + "1.0.0-SNAPSHOT", |
| 143 | + "-g" |
| 144 | + }; |
| 145 | + |
| 146 | + String result = ArrayFilterUtils.filterAndStringifyArray(compilerArgs, "--module-version|.*SNAPSHOT.*"); |
| 147 | + |
| 148 | + // Should filter out both "--module-version" and "1.0.0-SNAPSHOT" |
| 149 | + assertEquals("[-parameters, -g]", result); |
| 150 | + } |
| 151 | +} |
0 commit comments