001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015package org.basepom.mojo.dvc; 016 017import static org.junit.jupiter.api.Assertions.assertFalse; 018import static org.junit.jupiter.api.Assertions.assertTrue; 019 020import java.util.regex.Pattern; 021 022import org.junit.jupiter.api.Test; 023 024public class QualifiedNameMatcherTest { 025 026 @Test 027 public void testEmpty() { 028 Pattern matchEmpty = QualifiedNameMatcher.compileWildcard(""); 029 030 assertTrue(matchEmpty.matcher("").matches()); 031 assertTrue(matchEmpty.matcher("abc").matches()); 032 assertTrue(matchEmpty.matcher("Hello, World").matches()); 033 } 034 035 @Test 036 public void testWildcard() { 037 Pattern matchWildcard = QualifiedNameMatcher.compileWildcard("*"); 038 039 assertTrue(matchWildcard.matcher("").matches()); 040 assertTrue(matchWildcard.matcher("abc").matches()); 041 assertTrue(matchWildcard.matcher("Hello, World").matches()); 042 } 043 044 @Test 045 public void testTailWildcard() { 046 Pattern matchTail = QualifiedNameMatcher.compileWildcard("a*"); 047 048 assertFalse(matchTail.matcher("").matches()); 049 assertTrue(matchTail.matcher("abc").matches()); 050 assertFalse(matchTail.matcher("Hello, World").matches()); 051 assertFalse(matchTail.matcher("Stuff before the a and after").matches()); 052 } 053 054 @Test 055 public void testHeadWildcard() { 056 Pattern matchHead = QualifiedNameMatcher.compileWildcard("*d"); 057 058 assertFalse(matchHead.matcher("").matches()); 059 assertFalse(matchHead.matcher("abc").matches()); 060 assertTrue(matchHead.matcher("Hello, World").matches()); 061 062 assertFalse(matchHead.matcher("this is text before d and after it").matches()); 063 } 064 065 @Test 066 public void testMiddleWildcard() { 067 Pattern matchMiddle = QualifiedNameMatcher.compileWildcard("H*d"); 068 069 assertFalse(matchMiddle.matcher("").matches()); 070 assertFalse(matchMiddle.matcher("abc").matches()); 071 assertTrue(matchMiddle.matcher("Hello, World").matches()); 072 } 073 074 @Test 075 public void testDoubleWildcard() { 076 Pattern matchDouble = QualifiedNameMatcher.compileWildcard("H*o, W*d"); 077 078 assertFalse(matchDouble.matcher("").matches()); 079 assertFalse(matchDouble.matcher("abc").matches()); 080 assertTrue(matchDouble.matcher("Hello, World").matches()); 081 assertTrue(matchDouble.matcher("Ho, Wd").matches()); 082 assertFalse(matchDouble.matcher("Yoh, World").matches()); 083 } 084 085 @Test 086 public void testDots() { 087 Pattern matchDots = QualifiedNameMatcher.compileWildcard("org.apache"); 088 089 assertFalse(matchDots.matcher("org_apache").matches()); 090 assertTrue(matchDots.matcher("org.apache").matches()); 091 assertFalse(matchDots.matcher("somewhere in org.apache").matches()); 092 } 093 094 @Test 095 public void testQualifiedNameMatchers() { 096 QualifiedName name = new QualifiedName("the.test.group", "just-an-artifact", null, null); 097 098 QualifiedNameMatcher exact = QualifiedNameMatcher.fromQualifiedName(name); 099 assertTrue(exact.matches(name)); 100 101 QualifiedNameMatcher groupOnly = new QualifiedNameMatcher(name.getGroupId()); 102 assertTrue(groupOnly.matches(name)); 103 104 QualifiedNameMatcher groupAndWildcard = new QualifiedNameMatcher(name.getGroupId() + ":*"); 105 assertTrue(groupAndWildcard.matches(name)); 106 107 QualifiedNameMatcher groupWildcard = new QualifiedNameMatcher(name.getGroupId() + "*:*"); 108 assertTrue(groupWildcard.matches(name)); 109 110 QualifiedNameMatcher allWildcard = new QualifiedNameMatcher("*"); 111 assertTrue(allWildcard.matches(name)); 112 } 113}