1 package org.apache.maven.shared.artifact.filter.collection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34 public class TestScopeFilter
35 {
36 Set<Artifact> artifacts;
37
38 @Before
39 public void setUp()
40 throws Exception
41 {
42 ArtifactStubFactory factory = new ArtifactStubFactory( null, false );
43 artifacts = factory.getScopedArtifacts();
44 }
45
46 @Test
47 public void testScopeCompile()
48 throws ArtifactFilterException
49 {
50 ScopeFilter filter = new ScopeFilter( Artifact.SCOPE_COMPILE, null );
51 Set<Artifact> result = filter.filter( artifacts );
52 assertEquals( 3, result.size() );
53
54 }
55
56 @Test
57 public void testScopeRuntime()
58 throws ArtifactFilterException
59 {
60 ScopeFilter filter = new ScopeFilter( Artifact.SCOPE_RUNTIME, null );
61 Set<Artifact> result = filter.filter( artifacts );
62 assertEquals( 2, result.size() );
63 }
64
65 @Test
66 public void testScopeTest()
67 throws ArtifactFilterException
68 {
69 ScopeFilter filter = new ScopeFilter( Artifact.SCOPE_TEST, null );
70 Set<Artifact> result = filter.filter( artifacts );
71 assertEquals( 5, result.size() );
72 }
73
74 @Test
75 public void testScopeProvided()
76 throws ArtifactFilterException
77 {
78 ScopeFilter filter = new ScopeFilter( Artifact.SCOPE_PROVIDED, null );
79 Set<Artifact> result = filter.filter( artifacts );
80 assertTrue( result.size() > 0 );
81 for ( Artifact artifact : result )
82 {
83 assertEquals( Artifact.SCOPE_PROVIDED, artifact.getScope() );
84 }
85 }
86
87 @Test
88 public void testScopeSystem()
89 throws ArtifactFilterException
90 {
91 ScopeFilter filter = new ScopeFilter( Artifact.SCOPE_SYSTEM, null );
92 Set<Artifact> result = filter.filter( artifacts );
93 assertTrue( result.size() > 0 );
94 for ( Artifact artifact : result )
95 {
96 assertEquals( Artifact.SCOPE_SYSTEM, artifact.getScope() );
97 }
98 }
99
100 @Test
101 public void testScopeFilterNull()
102 throws ArtifactFilterException
103 {
104 ScopeFilter filter = new ScopeFilter( null, null );
105 Set<Artifact> result = filter.filter( artifacts );
106 assertEquals( 5, result.size() );
107 }
108
109 @Test
110 public void testScopeFilterEmpty()
111 throws ArtifactFilterException
112 {
113 ScopeFilter filter = new ScopeFilter( "", "" );
114 Set<Artifact> result = filter.filter( artifacts );
115 assertEquals( 5, result.size() );
116 }
117
118 @Test
119 public void testExcludeProvided()
120 throws ArtifactFilterException
121 {
122 ScopeFilter filter = new ScopeFilter( "", Artifact.SCOPE_PROVIDED );
123 Set<Artifact> result = filter.filter( artifacts );
124 assertNotNull( result );
125 assertTrue( result.size() > 0 );
126 for ( Artifact artifact : result )
127 {
128 assertFalse( Artifact.SCOPE_PROVIDED.equalsIgnoreCase( artifact.getScope() ) );
129 }
130 }
131
132 @Test
133 public void testExcludeSystem()
134 throws ArtifactFilterException
135 {
136 ScopeFilter filter = new ScopeFilter( "", Artifact.SCOPE_SYSTEM );
137 Set<Artifact> result = filter.filter( artifacts );
138 assertNotNull( result );
139 assertTrue( result.size() > 0 );
140 for ( Artifact artifact : result )
141 {
142 assertFalse( Artifact.SCOPE_SYSTEM.equalsIgnoreCase( artifact.getScope() ) );
143 }
144 }
145
146 @Test
147 public void testExcludeCompile()
148 throws ArtifactFilterException
149 {
150 ScopeFilter filter = new ScopeFilter( "", Artifact.SCOPE_COMPILE );
151 Set<Artifact> result = filter.filter( artifacts );
152 assertEquals( 2, result.size() );
153 }
154
155 @Test
156 public void testExcludeTest()
157 {
158 try
159 {
160 ScopeFilter filter = new ScopeFilter( "", Artifact.SCOPE_TEST );
161 filter.filter( artifacts );
162 Assert.fail( "Expected an Exception" );
163 }
164 catch ( ArtifactFilterException ignored )
165 {
166 }
167 }
168
169 @Test
170 public void testBadScope()
171 {
172 ScopeFilter filter = new ScopeFilter( "cOmpile", "" );
173 try
174 {
175 filter.filter( artifacts );
176 Assert.fail( "Expected an Exception" );
177 }
178 catch ( ArtifactFilterException ignored )
179 {
180
181 }
182 try
183 {
184 filter = new ScopeFilter( "", "coMpile" );
185 filter.filter( artifacts );
186 Assert.fail( "Expected an Exception" );
187 }
188 catch ( ArtifactFilterException ignored )
189 {
190
191 }
192 }
193
194 @Test
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 }