View Javadoc
1   package com.simpligility.maven.plugins.android;
2   
3   import java.util.Collection;
4   import java.util.LinkedHashSet;
5   
6   import org.apache.maven.artifact.Artifact;
7   import org.junit.Test;
8   
9   import static java.util.Arrays.asList;
10  import static java.util.Collections.emptySet;
11  import static java.util.Collections.singleton;
12  
13  import static com.simpligility.maven.plugins.android.InclusionExclusionResolver.filterArtifacts;
14  import static org.easymock.EasyMock.createMock;
15  import static org.easymock.EasyMock.expect;
16  import static org.easymock.EasyMock.replay;
17  import static org.junit.Assert.assertEquals;
18  
19  public class InclusionExclusionResolverTest
20  {
21  
22      private static final Artifact A1 = artifact( "jar", "G1", "A1", "1.0" );
23      private static final Artifact A2 = artifact( "jar", "G2", "A1", "1.0" );
24      private static final Artifact A3 = artifact( "aar", "G1", "A2", "1.0" );
25      private static final Artifact A4 = artifact( "jar", "G1", "A3", "2.0-rc" );
26      private static final Artifact A5 = artifact( "aar", "G2", "A2", "2.0-rc" );
27  
28      private static final Collection< Artifact > ALL = collect( A1, A2, A3, A4, A5 );
29      private static final Collection< Artifact > NONE = emptySet();
30  
31      @Test
32      public void testSkipDependenciesFalse()
33      {
34          assertEquals(
35                  "No artifacts must be skiped",
36                  ALL,
37                  filterArtifacts( ALL, false, null, null, null, null )
38          );
39  	}
40  
41      @Test
42      public void testSkipDependenciesTrue()
43      {
44          assertEquals(
45                  "All artifacts must be skipped",
46                  NONE,
47                  filterArtifacts( ALL, true, null, null, null, null )
48          );
49      }
50  
51      @Test
52      public void testSkipDependenciesIncludeTypes()
53      {
54          assertEquals(
55                  "All artifacts must be skipped, but AAR artifacts have higher priority",
56                  collect( A3, A5 ),
57                  filterArtifacts( ALL, true, singleton( "aar" ), null, null, null )
58          );
59          assertEquals(
60                  "All artifacts must be skipped, but JAR artifacts have higher priority",
61                  collect( A1, A2, A4 ),
62                  filterArtifacts( ALL, true, singleton( "jar" ), null, null, null )
63          );
64          assertEquals(
65                  "No artifacts must be skipped",
66                  ALL,
67                  filterArtifacts( ALL, false, singleton( "aar" ), null, null, null )
68          );
69          assertEquals(
70                  "No artifacts must be skipped",
71                  ALL,
72                  filterArtifacts( ALL, false, singleton( "jar" ), null, null, null )
73          );
74      }
75  
76      @Test
77      public void testSkipDependenciesExcludeTypes()
78      {
79          assertEquals(
80                  "All artifacts must be skipped, especially AAR artifacts",
81                  NONE,
82                  filterArtifacts(ALL, true, null, singleton( "aar" ), null, null )
83          );
84          assertEquals(
85                  "All artifacts must be skipped, especially JAR artifacts",
86                  NONE,
87                  filterArtifacts( ALL, true, null, singleton( "jar" ), null, null )
88          );
89          assertEquals(
90                  "AAR artifacts must be skipped",
91                  collect( A1, A2, A4 ),
92                  filterArtifacts( ALL, false, null, singleton( "aar" ), null, null )
93          );
94          assertEquals(
95                  "JAR artifacts must be skipped",
96                  collect( A3, A5 ),
97                  filterArtifacts( ALL, false, null, singleton( "jar" ), null, null )
98          );
99          assertEquals(
100                 "All artifacts must be skipped, especially both JAR and AAR artifacts",
101                 NONE,
102                 filterArtifacts( ALL, false, null, asList( "aar", "jar" ), null, null )
103         );
104     }
105 
106     @Test
107     public void testMatchingArtifactTypesIncludeExcludePriority()
108     {
109         assertEquals(
110                 "Include must have higher priority",
111                 ALL,
112                 filterArtifacts( ALL, false, singleton( "jar" ), singleton( "jar" ), null, null )
113         );
114         assertEquals(
115                 "Include must have higher priority",
116                 collect( A1, A2, A4 ),
117                 filterArtifacts( ALL, false, singleton( "jar" ), asList("aar", "jar"), null, null )
118         );
119         assertEquals(
120                 "Include must have higher priority",
121                 collect( A1, A2, A4 ),
122                 filterArtifacts( ALL, true, singleton( "jar" ), singleton( "jar" ), null, null )
123         );
124         assertEquals(
125                 "Include must have higher priority",
126                 collect( A1, A2, A4 ),
127                 filterArtifacts( ALL, true, singleton( "jar" ), asList( "aar", "jar" ), null, null )
128         );
129     }
130 
131     @Test
132     public void testIncludeExcludeByQualifiers()
133     {
134         assertEquals(
135                 "Empty exclude must do nothing",
136                 ALL,
137                 filterArtifacts( ALL, false, null, null, null, singleton( "" ) )
138         );
139         assertEquals(
140                 "Empty include must do nothing",
141                 NONE,
142                 filterArtifacts( ALL, true, null, null, singleton( "" ), null )
143         );
144         assertEquals(
145                 "Skip all and must include all of group G2",
146                 collect( A2, A5 ),
147                 filterArtifacts( ALL, true, null, null, singleton( "G2" ), null )
148         );
149         assertEquals(
150                 "Skip all and must include all of group G2 and artifact A2",
151                 collect( A5 ),
152                 filterArtifacts( ALL, true, null, null, singleton( "G2:A2" ), null )
153         );
154         assertEquals(
155                 "Do not skip and must exclude group G2",
156                 collect( A1, A3, A4 ),
157                 filterArtifacts( ALL, false, null, null, null, singleton( "G2" ) )
158         );
159         assertEquals(
160                 "Do not skip and must exclude group G2 and artifact A2",
161                 collect( A1, A2, A3, A4 ),
162                 filterArtifacts(ALL, false, null, null, null, singleton( "G2:A2" ) )
163         );
164         assertEquals(
165                 "Do not skip and must exclude group G2 and artifact A2 with invalid version",
166                 ALL,
167                 filterArtifacts( ALL, false, null, null, null, singleton( "G2:A2:-" ) )
168         );
169         assertEquals(
170                 "Do not skip and must exclude group G2 and artifact A2 with valid version",
171                 collect( A1, A2, A3, A4 ),
172                 filterArtifacts( ALL, false, null, null, null, singleton( "G2:A2:2.0-rc" ) )
173         );
174     }
175 
176     @Test
177     public void testIncludeExcludeTypeQualifierIntersections()
178     {
179         assertEquals(
180                 "Exclude all JARs but include by artifact qualifiers",
181                 collect( A2, A3, A4, A5 ),
182                 filterArtifacts( ALL, false, null, singleton( "jar" ), asList( "G2:A1", "G1:A3" ), null )
183         );
184         assertEquals(
185                 "Exclude all JARs but include by artifact qualifiers",
186                 collect(A2, A3, A4, A5),
187                 filterArtifacts( ALL, false, null, singleton( "jar" ), asList( "G2:A1", "G1:A3" ), asList( "G2:A1", "G1:A3" ) )
188         );
189         assertEquals(
190                 "Skip all but must include all AAR files despite the concrete artifact exclusion",
191                 collect( A3, A5 ),
192                 filterArtifacts( ALL, true, singleton("aar"), null, null, singleton("G2:A2:2.0-rc"))
193         );
194     }
195 
196     @Test( expected = IllegalArgumentException.class )
197     public void testIllegalQualifier()
198     {
199         filterArtifacts( ALL, false, null, null, singleton( "G1:A1:V:X" ), null );
200     }
201 
202     private static Collection<Artifact> collect( Artifact... artifacts )
203     {
204         return new LinkedHashSet< Artifact >( asList( artifacts ) );
205     }
206 
207     private static Artifact artifact( String type, String groupId, String artifactId, String version )
208     {
209         final Artifact artifact = createMock( Artifact.class );
210         expect( artifact.getType() ).andReturn( type ).anyTimes();
211         expect( artifact.getGroupId() ).andReturn( groupId ).anyTimes();
212         expect( artifact.getArtifactId() ).andReturn( artifactId ).anyTimes();
213         expect( artifact.getVersion() ).andReturn(version).anyTimes();
214         replay(artifact);
215         return artifact;
216     }
217 
218 }