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