Skip to content

Commit

Permalink
[MSHARED-1017] null passed to DependencyFilter in EclipseAetherFilter…
Browse files Browse the repository at this point in the history
…TransformerTest
  • Loading branch information
mkoncek authored and michael-o committed Jan 15, 2022
1 parent 6236b1b commit 2679674
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
*/
public class ArtifactIncludeFilterTransformer implements FilterTransformer<ArtifactFilter>
{

private boolean includeNullScope = true;
private boolean actTransitivelyPattern = false;

private boolean actTransitivelyPattern = false;

/**
* Used by {@link #transform(ScopeFilter)}
*
Expand All @@ -66,7 +66,7 @@ public void setIncludeNullScope( boolean includeNullScope )
{
this.includeNullScope = includeNullScope;
}

/**
* Used by {@link #transform(PatternExclusionsFilter)} and {@link #transform(PatternInclusionsFilter)} Determines
* whether the include/exclude patterns will be applied to the transitive path of a given artifact. If {@code true},
Expand Down Expand Up @@ -95,9 +95,9 @@ public boolean include( Artifact artifact )
{
return includeNullScope;
}

boolean isIncluded;

if ( scopeFilter.getIncluded() != null )
{
isIncluded = scopeFilter.getIncluded().contains( artifact.getScope() );
Expand All @@ -106,12 +106,12 @@ public boolean include( Artifact artifact )
{
isIncluded = true;
}

boolean isExcluded;

if ( scopeFilter.getExcluded() != null )
{
isExcluded = scopeFilter.getExcluded().contains( artifact.getScope() );
isExcluded = scopeFilter.getExcluded().contains( artifact.getScope() );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.Objects.requireNonNull;

import org.apache.maven.shared.artifact.filter.resolve.AbstractFilter;
import org.apache.maven.shared.artifact.filter.resolve.AndFilter;
import org.apache.maven.shared.artifact.filter.resolve.ExclusionsFilter;
Expand Down Expand Up @@ -53,11 +55,11 @@ public class EclipseAetherFilterTransformer
implements FilterTransformer<DependencyFilter>
{
/**
* When using as regular expression, group(1) + group(3) will be the coordinate,
* When using as regular expression, group(1) + group(3) will be the coordinate,
* group(2) will be the classifier.
*/
private static final String GAE_C_V = "(.*:.*:.*):(.+)(:.*)";

/** {@inheritDoc} */
@Override
public AndDependencyFilter transform( AndFilter andFilter )
Expand Down Expand Up @@ -95,13 +97,13 @@ public ScopeDependencyFilter transform( ScopeFilter filter )
{
return new ScopeDependencyFilter( filter.getIncluded(), filter.getExcluded() );
}

/** {@inheritDoc} */
@Override
public DependencyFilter transform( PatternExclusionsFilter filter )
{
return new PatternExclusionsDependencyFilter( filter.getExcludes() );
}
}

/** {@inheritDoc} */
@Override
Expand All @@ -110,18 +112,18 @@ public DependencyFilter transform( PatternInclusionsFilter filter )
// if any include contains a classifier:
// split all includes and make it an or-filter for every include
// for the classifier, add an and-filter with a classifierfilter and patterninclusionfilter

for ( String include : filter.getIncludes() )
{
if ( include.matches( GAE_C_V ) )
{
return newAdvancedPatternInclusionFilter( filter.getIncludes() );
}
}

return new PatternInclusionsDependencyFilter( filter.getIncludes() );
}

/** {@inheritDoc} */
@Override
public DependencyFilter transform( final AbstractFilter filter )
Expand All @@ -131,9 +133,12 @@ public DependencyFilter transform( final AbstractFilter filter )
@Override
public boolean accept( DependencyNode node, List<DependencyNode> parents )
{
requireNonNull( node, "node cannot be null" );
requireNonNull( parents, "parents cannot be null" );

return filter.accept( new EclipseAetherNode( node ), null );
}
};
};
}

private DependencyFilter newAdvancedPatternInclusionFilter( Collection<String> includes )
Expand All @@ -150,19 +155,22 @@ private DependencyFilter newAdvancedPatternInclusionFilter( Collection<String> i
new PatternInclusionsDependencyFilter( matcher.group( 1 ) + matcher.group( 3 ) );

final String classifier = matcher.group( 2 );

DependencyFilter classifierFilter = new DependencyFilter()
{
@Override
public boolean accept( DependencyNode node, List<DependencyNode> parents )
{
requireNonNull( node, "node cannot be null" );
requireNonNull( parents, "parents cannot be null" );

String nodeClassifier = node.getArtifact().getClassifier();

if ( nodeClassifier == null )
{
return false;
}
else
else
{
return "*".equals( classifier ) || nodeClassifier.matches( classifier );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public void testTransformAndFilter()
new ExclusionsFilter( Collections.singletonList( "x:a" ) ) ) );

AndDependencyFilter dependencyFilter = (AndDependencyFilter) filter.transform( transformer );

assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "compile" ), null ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v", "compile" ), null ) );
assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "compile" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:v", "test" ), null ) );
assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v", "compile" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v", "test" ), null ) );
assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:v", "test" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v", "test" ), Collections.<DependencyNode>emptyList() ) );
}

@Test
Expand All @@ -78,24 +78,24 @@ public void testTransformExclusionsFilter()

ExclusionsDependencyFilter dependencyFilter = (ExclusionsDependencyFilter) filter.transform( transformer );

assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "compile" ), null ) );
assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "compile" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v", "compile" ), null ) );
assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v", "compile" ), Collections.<DependencyNode>emptyList() ) );
}

@Test
public void testTransformOrFilter()
{
OrFilter filter = new OrFilter( Arrays.<TransformableFilter>asList( ScopeFilter.including( "compile" ),
OrFilter filter = new OrFilter( Arrays.<TransformableFilter>asList( ScopeFilter.including( "compile" ),
ScopeFilter.including( "test" ) ) );

OrDependencyFilter dependencyFilter = (OrDependencyFilter) filter.transform( transformer );

assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "compile" ), null ) );
assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "compile" ), Collections.<DependencyNode>emptyList() ) );

assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "test" ), Collections.<DependencyNode>emptyList() ) );

assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "test" ), null ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:v", "runtime" ), null ) );
assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:v", "runtime" ), Collections.<DependencyNode>emptyList() ) );
}

@Test
Expand All @@ -104,12 +104,12 @@ public void testTransformScopeFilter()
ScopeFilter filter = ScopeFilter.including( Collections.singletonList( "runtime" ) );

ScopeDependencyFilter dependencyFilter = (ScopeDependencyFilter) filter.transform( transformer );

assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "runtime" ), null ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:v", "compile" ), null ) );
assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "runtime" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:v", "test" ), null ) );
assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:v", "compile" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:v", "test" ), Collections.<DependencyNode>emptyList() ) );
}

@Test
Expand All @@ -121,9 +121,9 @@ public void testTransformPatternExclusionsFilter()
PatternExclusionsDependencyFilter dependencyFilter =
(PatternExclusionsDependencyFilter) filter.transform( transformer );

assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "runtime" ), null ) );
assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "runtime" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v", "runtime" ), null ) );
assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v", "runtime" ), Collections.<DependencyNode>emptyList() ) );
}

@Test
Expand All @@ -135,9 +135,9 @@ public void testTransformPatternInclusionsFilter()
PatternInclusionsDependencyFilter dependencyFilter =
(PatternInclusionsDependencyFilter) filter.transform( transformer );

assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "runtime" ), null ) );
assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v", "runtime" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v", "runtime" ), null ) );
assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v", "runtime" ), Collections.<DependencyNode>emptyList() ) );
}

@Test
Expand All @@ -148,11 +148,11 @@ public void testTransformClassifierPatternInclusionsFilter()

DependencyFilter dependencyFilter = filter.transform( transformer );

assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v:c:*", "runtime" ), null ) );
assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:v:c:*", "runtime" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:v", "runtime" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:v", "runtime" ), null ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v:c:*", "runtime" ), null ) );
assertFalse( dependencyFilter.accept( newDependencyNode( "x:a:v:c:*", "runtime" ), Collections.<DependencyNode>emptyList() ) );
}

@Test
Expand All @@ -166,14 +166,14 @@ public boolean accept( Node node, List<Node> parents )
return ArtifactUtils.isSnapshot( node.getDependency().getVersion() );
}
};

DependencyFilter dependencyFilter = snapshotFilter.transform( transformer );

assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:1.0-SNAPSHOT", "compile" ), null ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:1.0", "compile" ), null ) );
assertTrue( dependencyFilter.accept( newDependencyNode( "g:a:1.0-SNAPSHOT", "compile" ), Collections.<DependencyNode>emptyList() ) );

assertFalse( dependencyFilter.accept( newDependencyNode( "g:a:1.0", "compile" ), Collections.<DependencyNode>emptyList() ) );
}

private DependencyNode newDependencyNode( String string, String scope )
{
return new DefaultDependencyNode( new Dependency( new DefaultArtifact( string ), scope ) );
Expand Down

0 comments on commit 2679674

Please sign in to comment.