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