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