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.junit.jupiter.api.Assertions.assertEquals;
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 DependencyDefinitionTest {
023
024    @Test
025    public void testParsing() {
026        // just group id
027        DependencyDefinition dependencyDefinition = new DependencyDefinition("foo");
028        assertEquals("foo", dependencyDefinition.getGroupId());
029        assertEquals("*", dependencyDefinition.getArtifactId());
030        assertEquals("jar", dependencyDefinition.getType());
031        assertFalse(dependencyDefinition.getClassifier().isPresent());
032
033        // group id and artifact id
034        dependencyDefinition = new DependencyDefinition("foo:bar");
035        assertEquals("foo", dependencyDefinition.getGroupId());
036        assertEquals("bar", dependencyDefinition.getArtifactId());
037        assertEquals("jar", dependencyDefinition.getType());
038        assertFalse(dependencyDefinition.getClassifier().isPresent());
039
040        // group id and type
041        dependencyDefinition = new DependencyDefinition("foo::war");
042        assertEquals("foo", dependencyDefinition.getGroupId());
043        assertEquals("*", dependencyDefinition.getArtifactId());
044        assertEquals("war", dependencyDefinition.getType());
045        assertFalse(dependencyDefinition.getClassifier().isPresent());
046
047        // group id and classifier
048        dependencyDefinition = new DependencyDefinition("foo:::tests");
049        assertEquals("foo", dependencyDefinition.getGroupId());
050        assertEquals("*", dependencyDefinition.getArtifactId());
051        assertEquals("jar", dependencyDefinition.getType());
052        assertTrue(dependencyDefinition.getClassifier().isPresent());
053        assertEquals("tests", dependencyDefinition.getClassifier().get());
054
055        // group id and artifact id and type
056        dependencyDefinition = new DependencyDefinition("foo:bar:war");
057        assertEquals("foo", dependencyDefinition.getGroupId());
058        assertEquals("bar", dependencyDefinition.getArtifactId());
059        assertEquals("war", dependencyDefinition.getType());
060        assertFalse(dependencyDefinition.getClassifier().isPresent());
061
062        // group id and artifact id and classifier
063        dependencyDefinition = new DependencyDefinition("foo:bar::tests");
064        assertEquals("foo", dependencyDefinition.getGroupId());
065        assertEquals("bar", dependencyDefinition.getArtifactId());
066        assertEquals("jar", dependencyDefinition.getType());
067        assertTrue(dependencyDefinition.getClassifier().isPresent());
068        assertEquals("tests", dependencyDefinition.getClassifier().get());
069
070        // group id and artifact id and type and classifier
071        dependencyDefinition = new DependencyDefinition("foo:bar:war:tests");
072        assertEquals("foo", dependencyDefinition.getGroupId());
073        assertEquals("bar", dependencyDefinition.getArtifactId());
074        assertEquals("war", dependencyDefinition.getType());
075        assertTrue(dependencyDefinition.getClassifier().isPresent());
076        assertEquals("tests", dependencyDefinition.getClassifier().get());
077    }
078
079    @Test
080    public void testWildcardMatches() {
081        // wildcard matcher  *:*:jar:<anything>
082        DependencyDefinition matcher = new DependencyDefinition("*");
083        assertEquals("*", matcher.getGroupId());
084        assertEquals("*", matcher.getArtifactId());
085
086        assertTrue(matcher.matches(new DependencyDefinition("foo:bar")));
087        assertTrue(matcher.matches(new DependencyDefinition("com.baz:blo")));
088        assertTrue(matcher.matches(new DependencyDefinition("foo:bar:jar:tests")));
089        // a war is not a jar
090        assertFalse(matcher.matches(new DependencyDefinition("foo:bar:war:tests")));
091
092        // com.foo:*
093        matcher = new DependencyDefinition("com.foo:*");
094        assertTrue(matcher.matches(new DependencyDefinition("com.foo:baz")));
095        assertTrue(matcher.matches(new DependencyDefinition("com.foo:bla.foo")));
096        assertFalse(matcher.matches(new DependencyDefinition("com.foobar:bar")));
097        assertFalse(matcher.matches(new DependencyDefinition("com.foo.bar:bar")));
098        assertFalse(matcher.matches(new DependencyDefinition("org.foo.bar:bar")));
099
100        // com.foo*:*
101        matcher = new DependencyDefinition("com.foo*:*");
102        assertTrue(matcher.matches(new DependencyDefinition("com.foo:baz")));
103        assertTrue(matcher.matches(new DependencyDefinition("com.foo:bla.foo")));
104        assertTrue(matcher.matches(new DependencyDefinition("com.foobar:bar")));
105        assertTrue(matcher.matches(new DependencyDefinition("com.foo.bar:bar")));
106        assertFalse(matcher.matches(new DependencyDefinition("org.foo.bar:bar")));
107
108        // com.foo:bar*
109        matcher = new DependencyDefinition("com.foo:bar*");
110        assertFalse(matcher.matches(new DependencyDefinition("com.foo:baz")));
111        assertFalse(matcher.matches(new DependencyDefinition("com.foo:bla.foo")));
112        assertTrue(matcher.matches(new DependencyDefinition("com.foo:bar")));
113        assertTrue(matcher.matches(new DependencyDefinition("com.foo:bar-foo")));
114        assertTrue(matcher.matches(new DependencyDefinition("com.foo:barfoo")));
115    }
116
117    @Test
118    public void testClassifiers() {
119        // matcher com.foo:bar:jar:tests
120        DependencyDefinition matcher = new DependencyDefinition("com.foo:bar::tests");
121
122        assertTrue(matcher.matches(new DependencyDefinition("com.foo:bar::tests")));
123        assertTrue(matcher.matches(new DependencyDefinition("com.foo:bar:jar:tests")));
124        assertTrue(matcher.matches(new DependencyDefinition("com.foo:bar:test-jar")));
125
126        // matches has a classifier, jar has not
127        assertFalse(matcher.matches(new DependencyDefinition("com.foo:bar")));
128
129        // matcher has different classifier than jar
130        assertFalse(matcher.matches(new DependencyDefinition("com.foo:bar::sources")));
131
132
133        // *:*:jar:<any>
134        matcher = new DependencyDefinition("*");
135        assertTrue(matcher.matches(new DependencyDefinition("com.foo:bar::tests")));
136        assertTrue(matcher.matches(new DependencyDefinition("com.foo:bar::sources")));
137
138    }
139}