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.util.Set;
22  
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.plugin.testing.ArtifactStubFactory;
25  import org.junit.Assert;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.*;
30  
31  /**
32   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
33   */
34  public class TestScopeFilter {
35      Set<Artifact> artifacts;
36  
37      @Before
38      public void setUp() throws Exception {
39          ArtifactStubFactory factory = new ArtifactStubFactory(null, false);
40          artifacts = factory.getScopedArtifacts();
41      }
42  
43      @Test
44      public void testScopeCompile() throws ArtifactFilterException {
45          ScopeFilter filter = new ScopeFilter(Artifact.SCOPE_COMPILE, null);
46          Set<Artifact> result = filter.filter(artifacts);
47          assertEquals(3, result.size());
48      }
49  
50      @Test
51      public void testScopeRuntime() throws ArtifactFilterException {
52          ScopeFilter filter = new ScopeFilter(Artifact.SCOPE_RUNTIME, null);
53          Set<Artifact> result = filter.filter(artifacts);
54          assertEquals(2, result.size());
55      }
56  
57      @Test
58      public void testScopeTest() throws ArtifactFilterException {
59          ScopeFilter filter = new ScopeFilter(Artifact.SCOPE_TEST, null);
60          Set<Artifact> result = filter.filter(artifacts);
61          assertEquals(5, result.size());
62      }
63  
64      @Test
65      public void testScopeProvided() throws ArtifactFilterException {
66          ScopeFilter filter = new ScopeFilter(Artifact.SCOPE_PROVIDED, null);
67          Set<Artifact> result = filter.filter(artifacts);
68          assertTrue(result.size() > 0);
69          for (Artifact artifact : result) {
70              assertEquals(Artifact.SCOPE_PROVIDED, artifact.getScope());
71          }
72      }
73  
74      @Test
75      public void testScopeSystem() throws ArtifactFilterException {
76          ScopeFilter filter = new ScopeFilter(Artifact.SCOPE_SYSTEM, null);
77          Set<Artifact> result = filter.filter(artifacts);
78          assertTrue(result.size() > 0);
79          for (Artifact artifact : result) {
80              assertEquals(Artifact.SCOPE_SYSTEM, artifact.getScope());
81          }
82      }
83  
84      @Test
85      public void testScopeFilterNull() throws ArtifactFilterException {
86          ScopeFilter filter = new ScopeFilter(null, null);
87          Set<Artifact> result = filter.filter(artifacts);
88          assertEquals(5, result.size());
89      }
90  
91      @Test
92      public void testScopeFilterEmpty() throws ArtifactFilterException {
93          ScopeFilter filter = new ScopeFilter("", "");
94          Set<Artifact> result = filter.filter(artifacts);
95          assertEquals(5, result.size());
96      }
97  
98      @Test
99      public void testExcludeProvided() throws ArtifactFilterException {
100         ScopeFilter filter = new ScopeFilter("", Artifact.SCOPE_PROVIDED);
101         Set<Artifact> result = filter.filter(artifacts);
102         assertNotNull(result);
103         assertTrue(result.size() > 0);
104         for (Artifact artifact : result) {
105             assertFalse(Artifact.SCOPE_PROVIDED.equalsIgnoreCase(artifact.getScope()));
106         }
107     }
108 
109     @Test
110     public void testExcludeSystem() throws ArtifactFilterException {
111         ScopeFilter filter = new ScopeFilter("", Artifact.SCOPE_SYSTEM);
112         Set<Artifact> result = filter.filter(artifacts);
113         assertNotNull(result);
114         assertTrue(result.size() > 0);
115         for (Artifact artifact : result) {
116             assertFalse(Artifact.SCOPE_SYSTEM.equalsIgnoreCase(artifact.getScope()));
117         }
118     }
119 
120     @Test
121     public void testExcludeCompile() throws ArtifactFilterException {
122         ScopeFilter filter = new ScopeFilter("", Artifact.SCOPE_COMPILE);
123         Set<Artifact> result = filter.filter(artifacts);
124         assertEquals(2, result.size());
125     }
126 
127     @Test
128     public void testExcludeTest() {
129         try {
130             ScopeFilter filter = new ScopeFilter("", Artifact.SCOPE_TEST);
131             filter.filter(artifacts);
132             Assert.fail("Expected an Exception");
133         } catch (ArtifactFilterException ignored) {
134         }
135     }
136 
137     @Test
138     public void testBadScope() {
139         ScopeFilter filter = new ScopeFilter("cOmpile", "");
140         try {
141             filter.filter(artifacts);
142             Assert.fail("Expected an Exception");
143         } catch (ArtifactFilterException ignored) {
144 
145         }
146         try {
147             filter = new ScopeFilter("", "coMpile");
148             filter.filter(artifacts);
149             Assert.fail("Expected an Exception");
150         } catch (ArtifactFilterException ignored) {
151 
152         }
153     }
154 
155     @Test
156     public void testSettersGetters() {
157         ScopeFilter filter = new ScopeFilter("include", "exclude");
158         assertEquals("include", filter.getIncludeScope());
159         assertEquals("exclude", filter.getExcludeScope());
160 
161         filter.setExcludeScope("a");
162         filter.setIncludeScope("b");
163         assertEquals("b", filter.getIncludeScope());
164         assertEquals("a", filter.getExcludeScope());
165     }
166 }