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 */
014package org.basepom.mojo.repack;
015
016import static org.basepom.mojo.repack.Wildcard.wildcardMatch;
017import static org.junit.jupiter.api.Assertions.assertFalse;
018import static org.junit.jupiter.api.Assertions.assertTrue;
019
020import org.junit.jupiter.api.Test;
021
022public class WildcardMatchingTest {
023
024    @Test
025    public void testEmptyPattern() {
026        assertTrue(wildcardMatch("", ""));
027        assertFalse(wildcardMatch("", "x"));
028    }
029
030    @Test
031    public void testDirectMatches() {
032        assertTrue(wildcardMatch("aaa", "aaa"));
033        assertTrue(wildcardMatch("aab", "aab"));
034        assertFalse(wildcardMatch("aab", "ab"));
035        assertFalse(wildcardMatch("ab", "aab"));
036        assertFalse(wildcardMatch("ab", "aba"));
037    }
038
039    @Test
040    public void testSingleWildcards() {
041        assertTrue(wildcardMatch("a?a", "aaa"));
042        assertTrue(wildcardMatch("aa?", "aab"));
043        assertFalse(wildcardMatch("a?b", "ab"));
044        assertFalse(wildcardMatch("?b", "aab"));
045        assertFalse(wildcardMatch("a?b", "aba"));
046    }
047
048    @Test
049    public void testEndGlobs() {
050        assertTrue(wildcardMatch("aa*", "aaa"));
051        assertTrue(wildcardMatch("aab*", "aab"));
052        assertFalse(wildcardMatch("aa*", "ab"));
053        assertFalse(wildcardMatch("ab*", "aab"));
054        assertTrue(wildcardMatch("ab*", "aba"));
055    }
056
057    @Test
058    public void testMiddleGlobs() {
059        assertTrue(wildcardMatch("aa*aa", "aaaa"));
060        assertTrue(wildcardMatch("aa*aa", "aaaaa"));
061        assertTrue(wildcardMatch("aa*aa", "aabaa"));
062        assertTrue(wildcardMatch("ab*ba", "abcsometextherecba"));
063        assertTrue(wildcardMatch("spring*works", "spring_summer_works_for_me_works"));
064
065        assertFalse(wildcardMatch("ab*ba", "abbb"));
066        assertFalse(wildcardMatch("ab*a", "absomelongtextthatendswithc"));
067        assertFalse(wildcardMatch("*abcde", "sometext"));
068    }
069
070    @Test
071    public void testLastChanceSaloon() {
072        assertTrue(wildcardMatch("abc*", "abc"));
073        assertTrue(wildcardMatch("abc****", "abc"));
074        assertFalse(wildcardMatch("abc****d", "abc"));
075        assertFalse(wildcardMatch("abc****?", "abc"));
076    }
077}