View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.artifact.filter.collection;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.plugin.testing.ArtifactStubFactory;
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  
35  /**
36   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
37   */
38  public class TestTypeFilter {
39      Set<Artifact> artifacts;
40  
41      @Before
42      public void setUp() throws Exception {
43          ArtifactStubFactory factory = new ArtifactStubFactory(null, false);
44          artifacts = factory.getTypedArtifacts();
45      }
46  
47      @Test
48      public void testTypeParsing() {
49          TypeFilter filter = new TypeFilter("war,jar", "sources,zip,");
50          List<String> includes = filter.getIncludes();
51          List<String> excludes = filter.getExcludes();
52  
53          assertEquals(2, includes.size());
54          assertEquals(2, excludes.size());
55          assertEquals("war", includes.get(0));
56          assertEquals("jar", includes.get(1));
57          assertEquals("sources", excludes.get(0));
58          assertEquals("zip", excludes.get(1));
59      }
60  
61      @Test
62      public void testFiltering() {
63          TypeFilter filter = new TypeFilter("war,jar", "war,zip,");
64          Set<Artifact> result = filter.filter(artifacts);
65          assertEquals(1, result.size());
66  
67          for (Artifact artifact : result) {
68              assertEquals("jar", artifact.getType());
69          }
70      }
71  
72      @Test
73      public void testFiltering2() {
74          TypeFilter filter = new TypeFilter(null, "war,jar,");
75          Set<Artifact> result = filter.filter(artifacts);
76          assertEquals(3, result.size());
77  
78          for (Artifact artifact : result) {
79              assertTrue(!artifact.getType().equals("war") && !artifact.getType().equals("jar"));
80          }
81      }
82  
83      @Test
84      public void testFiltering3() {
85          TypeFilter filter = new TypeFilter(null, null);
86          Set<Artifact> result = filter.filter(artifacts);
87          assertEquals(5, result.size());
88      }
89  
90      @Test
91      public void testFilteringOrder() throws IOException {
92          TypeFilter filter = new TypeFilter("war,jar", "zip");
93          Set<Artifact> artifacts = new LinkedHashSet<>();
94  
95          ArtifactStubFactory factory = new ArtifactStubFactory(null, false);
96          artifacts.add(factory.createArtifact("g", "a", "1.0", Artifact.SCOPE_COMPILE, "jar", null));
97          artifacts.add(factory.createArtifact("g", "b", "1.0", Artifact.SCOPE_COMPILE, "zip", null));
98          artifacts.add(factory.createArtifact("g", "c", "1.0", Artifact.SCOPE_COMPILE, "war", null));
99  
100         Set<Artifact> result = filter.filter(artifacts);
101 
102         assertEquals(2, result.size());
103 
104         List<Artifact> resultList = new ArrayList<>(result);
105 
106         assertEquals("a", resultList.get(0).getArtifactId());
107         assertEquals("c", resultList.get(1).getArtifactId());
108     }
109 }