View Javadoc
1   package org.apache.maven.shared.artifact.filter.resolve.transform;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Collections;
25  
26  import org.apache.maven.shared.artifact.filter.resolve.Node;
27  import org.eclipse.aether.artifact.DefaultArtifact;
28  import org.eclipse.aether.graph.DefaultDependencyNode;
29  import org.eclipse.aether.graph.Dependency;
30  import org.eclipse.aether.graph.DependencyNode;
31  import org.eclipse.aether.graph.Exclusion;
32  import org.junit.Test;
33  
34  import static org.junit.Assert.*;
35  
36  public class EclipseAetherNodeTest
37  {
38      @Test
39      public void testGAV()
40      {
41          Node node = new EclipseAetherNode( newDependencyNode( "g:a:v", null ) );
42          
43          org.apache.maven.model.Dependency mavenDependency = node.getDependency();
44          
45          assertEquals( "g", mavenDependency.getGroupId()  );
46          assertEquals( "a", mavenDependency.getArtifactId()  );
47          assertEquals( "v", mavenDependency.getVersion() );
48          assertEquals( "", mavenDependency.getClassifier() );
49          assertNull( mavenDependency.getType() );
50          assertEquals( "", mavenDependency.getScope() );
51      }
52  
53      @Test
54      public void testClassifier()
55      {
56          Node node = new EclipseAetherNode( newDependencyNode( "g:a::c:v", null ) );
57          
58          org.apache.maven.model.Dependency mavenDependency = node.getDependency();
59          
60          assertEquals( "g", mavenDependency.getGroupId()  );
61          assertEquals( "a", mavenDependency.getArtifactId()  );
62          assertEquals( "v", mavenDependency.getVersion() );
63          assertEquals( "c", mavenDependency.getClassifier() );
64          assertNull( mavenDependency.getType() );
65          assertEquals( "", mavenDependency.getScope() );
66      }
67      
68      @Test
69      public void testScope()
70      {
71          Node node = new EclipseAetherNode( newDependencyNode( "g:a:c:v", "s" ) );
72          
73          org.apache.maven.model.Dependency mavenDependency = node.getDependency();
74          
75          assertEquals( "g", mavenDependency.getGroupId()  );
76          assertEquals( "a", mavenDependency.getArtifactId()  );
77          assertEquals( "v", mavenDependency.getVersion() );
78          assertEquals( "", mavenDependency.getClassifier() );
79          assertNull( mavenDependency.getType() );
80          assertEquals( "s", mavenDependency.getScope() );
81      }
82  
83      @Test
84      public void testOptional()
85      {
86          Node node = new EclipseAetherNode( newDependencyNode( "g:a:v", null, (Boolean) null ) );
87  
88          assertNull( node.getDependency().getOptional() );
89          assertFalse( node.getDependency().isOptional() );
90          
91          node = new EclipseAetherNode( newDependencyNode( "g:a:v", null, true ) );
92          assertEquals( "true", node.getDependency().getOptional()  );
93          assertTrue( node.getDependency().isOptional() );
94  
95          node = new EclipseAetherNode( newDependencyNode( "g:a:v", null, false ) );
96          assertEquals( "false", node.getDependency().getOptional()  );
97          assertFalse( node.getDependency().isOptional() );
98      }
99      
100     @Test
101     public void testExclusions()
102     {
103         Node node = new EclipseAetherNode( newDependencyNode( "g:a:v", null, Collections.singletonList( "eg:ea" ) ) );
104         assertEquals( 1, node.getDependency().getExclusions().size() );
105         
106         org.apache.maven.model.Exclusion mavenExclusion = node.getDependency().getExclusions().get( 0 );
107         assertEquals( "eg", mavenExclusion.getGroupId() );
108         assertEquals( "ea", mavenExclusion.getArtifactId() );
109     }
110 
111     private DependencyNode newDependencyNode( String coor, String scope )
112     {
113         return new DefaultDependencyNode( new Dependency( new DefaultArtifact( coor ), scope ) );
114     }
115     
116     private DependencyNode newDependencyNode( String coor, String scope, Boolean optional )
117     {
118         return new DefaultDependencyNode( new Dependency( new DefaultArtifact( coor ), scope, optional ) );
119     }
120 
121     private DependencyNode newDependencyNode( String coor, String scope, Collection<String> exclusions )
122     {
123         Dependency dependency = new Dependency( new DefaultArtifact( coor ), scope );
124         
125         Collection<Exclusion> aetherExclusions = new ArrayList<>( exclusions.size() );
126         for ( String exclusion : exclusions )
127         {
128             String[] ga = exclusion.split( ":" );
129             aetherExclusions.add( new Exclusion( ga[0], ga[1], null, null ) );
130         }
131         dependency = dependency.setExclusions( aetherExclusions );
132         
133         return new DefaultDependencyNode( dependency );
134     }
135 
136 }