]> source.dussan.org Git - archiva.git/blob
cf7d7726f169cbaffa6c8c13b634e22da91f464e
[archiva.git] /
1 package org.apache.maven.archiva.repository.scanner;
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 import org.apache.commons.lang.SystemUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
26 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
28 import org.easymock.MockControl;
29
30 import java.io.File;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Date;
34 import java.util.List;
35 import java.util.Map;
36
37 /**
38  * RepositoryContentConsumersTest
39  *
40  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
41  * @version $Id$
42  */
43 public class RepositoryContentConsumersTest
44     extends AbstractRepositoryLayerTestCase
45 {
46     private RepositoryContentConsumers lookupRepositoryConsumers()
47         throws Exception
48     {
49         RepositoryContentConsumers consumerUtilStub = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
50             .getName(), "test" );
51         ArchivaConfiguration archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.ROLE );        
52         RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
53                                                                                            .getName() );
54         
55         consumerUtilStub.setAvailableKnownConsumers( consumerUtil.getAvailableKnownConsumers() );
56         consumerUtilStub.setAvailableInvalidConsumers( consumerUtil.getAvailableInvalidConsumers() );
57         consumerUtilStub.setSelectedInvalidConsumers( consumerUtil.getSelectedInvalidConsumers() );
58         consumerUtilStub.setSelectedKnownConsumers( consumerUtil.getSelectedKnownConsumers() );        
59         consumerUtilStub.setArchivaConfiguration( archivaConfiguration );
60         
61         assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtilStub );
62         
63         return consumerUtilStub;
64     }
65
66     public void testGetSelectedKnownIds()
67         throws Exception
68     {
69         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
70
71         String expectedKnownIds[] = new String[] {
72             "update-db-artifact",
73             "create-missing-checksums",
74             "update-db-repository-metadata",
75             "validate-checksum",
76             "validate-signature",
77             "index-content",
78             "auto-remove",
79             "auto-rename" };
80
81         List<String> knownConsumers = consumerutil.getSelectedKnownConsumerIds();
82         assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
83         assertEquals( "Known Consumer IDs.size", expectedKnownIds.length, knownConsumers.size() );
84
85         for ( String expectedId : expectedKnownIds )
86         {
87             assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
88         }
89     }
90
91     public void testGetSelectedInvalidIds()
92         throws Exception
93     {
94         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
95
96         String expectedInvalidIds[] = new String[] { "update-db-bad-content" };
97
98         List<String> invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
99         assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
100         assertEquals( "Invalid Consumer IDs.size", expectedInvalidIds.length, invalidConsumers.size() );
101
102         for ( String expectedId : expectedInvalidIds )
103         {
104             assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
105         }
106     }
107
108     public void testGetSelectedKnownConsumerMap()
109         throws Exception
110     {
111         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
112
113         String expectedSelectedKnownIds[] = new String[] {
114             "update-db-artifact",
115             "create-missing-checksums",
116             "update-db-repository-metadata",
117             "validate-checksum",
118             "index-content",
119             "auto-remove",
120             "auto-rename" };
121
122         Map<String, KnownRepositoryContentConsumer> knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
123         assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
124         assertEquals( "Known Consumer Map.size", expectedSelectedKnownIds.length, knownConsumerMap.size() );
125
126         for ( String expectedId : expectedSelectedKnownIds )
127         {
128             KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
129             assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
130             assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
131         }
132     }
133
134     public void testGetSelectedInvalidConsumerMap()
135         throws Exception
136     {
137         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
138
139         String expectedSelectedInvalidIds[] = new String[] { "update-db-bad-content" };
140
141         Map<String, InvalidRepositoryContentConsumer> invalidConsumerMap = consumerutil
142             .getSelectedInvalidConsumersMap();
143         assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
144         assertEquals( "Invalid Consumer Map.size", expectedSelectedInvalidIds.length, invalidConsumerMap.size() );
145
146         for ( String expectedId : expectedSelectedInvalidIds )
147         {
148             InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
149             assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
150             assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
151         }
152     }
153
154     public void testGetAvailableKnownList()
155         throws Exception
156     {
157         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
158
159         String expectedKnownIds[] = new String[] {
160             "update-db-artifact",
161             "create-missing-checksums",
162             "update-db-repository-metadata",
163             "validate-checksum",
164             "index-content",
165             "auto-remove",
166             "auto-rename",
167             "available-but-unselected" };
168
169         List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers();
170         assertNotNull( "known consumers should not be null.", knownConsumers );
171         assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size() );
172
173         List<String> expectedIds = Arrays.asList( expectedKnownIds );
174         for ( KnownRepositoryContentConsumer consumer : knownConsumers )
175         {
176             assertTrue( "Consumer [" + consumer.getId() + "] returned by .getAvailableKnownConsumers() is unexpected.",
177                         expectedIds.contains( consumer.getId() ) );
178         }
179     }
180
181     public void testGetAvailableInvalidList()
182         throws Exception
183     {
184         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
185
186         String expectedInvalidIds[] = new String[] { "update-db-bad-content", "move-to-trash-then-notify" };
187
188         List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers();
189         assertNotNull( "invalid consumers should not be null.", invalidConsumers );
190         assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size() );
191
192         List<String> expectedIds = Arrays.asList( expectedInvalidIds );
193         for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
194         {
195             assertTrue( "Consumer [" + consumer.getId()
196                 + "] returned by .getAvailableInvalidConsumers() is unexpected.", expectedIds.contains( consumer
197                 .getId() ) );
198         }
199     }
200
201     public void testExecution()
202         throws Exception
203     {
204         MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
205         RepositoryContentConsumers consumers = lookupRepositoryConsumers();
206         KnownRepositoryContentConsumer selectedKnownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
207         KnownRepositoryContentConsumer unselectedKnownConsumer =
208             (KnownRepositoryContentConsumer) MockControl.createNiceControl(
209                 KnownRepositoryContentConsumer.class ).getMock();
210         consumers.setAvailableKnownConsumers( Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ) );
211         consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
212
213         MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
214         InvalidRepositoryContentConsumer selectedInvalidConsumer =
215             (InvalidRepositoryContentConsumer) invalidControl.getMock();
216         InvalidRepositoryContentConsumer unselectedInvalidConsumer =
217             (InvalidRepositoryContentConsumer) MockControl.createControl(
218                 InvalidRepositoryContentConsumer.class ).getMock();
219         consumers.setAvailableInvalidConsumers( Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer ) );
220         consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
221
222         ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
223         File testFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
224
225         Date startTime = new Date( System.currentTimeMillis() );
226         
227         selectedKnownConsumer.beginScan( repo, startTime );
228         selectedKnownConsumer.getExcludes();
229         knownControl.setReturnValue( Collections.EMPTY_LIST );
230         selectedKnownConsumer.getIncludes();
231         knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
232         selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ) );
233         //        knownConsumer.completeScan();
234         knownControl.replay();
235
236         selectedInvalidConsumer.beginScan( repo, startTime );
237         //        invalidConsumer.completeScan();
238         invalidControl.replay();
239
240         consumers.setStartTime( startTime );
241         consumers.executeConsumers( repo, testFile );
242
243         knownControl.verify();
244         invalidControl.verify();
245
246         knownControl.reset();
247         invalidControl.reset();
248
249         File notIncludedTestFile = getTestFile( "target/test-repo/path/to/test-file.xml" );
250
251         selectedKnownConsumer.beginScan( repo, startTime );
252         selectedKnownConsumer.getExcludes();
253         knownControl.setReturnValue( Collections.EMPTY_LIST );
254         selectedKnownConsumer.getIncludes();
255         knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
256         //        knownConsumer.completeScan();
257         knownControl.replay();
258
259         selectedInvalidConsumer.beginScan( repo, startTime );
260         selectedInvalidConsumer.processFile( _OS( "path/to/test-file.xml" ) );
261         selectedInvalidConsumer.getId();
262         invalidControl.setReturnValue( "invalid" );
263         //        invalidConsumer.completeScan();
264         invalidControl.replay();
265
266         consumers.executeConsumers( repo, notIncludedTestFile );
267
268         knownControl.verify();
269         invalidControl.verify();
270
271         knownControl.reset();
272         invalidControl.reset();
273
274         File excludedTestFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
275
276         selectedKnownConsumer.beginScan( repo, startTime );
277         selectedKnownConsumer.getExcludes();
278         knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
279         //        knownConsumer.completeScan();
280         knownControl.replay();
281
282         selectedInvalidConsumer.beginScan( repo, startTime );
283         selectedInvalidConsumer.processFile( _OS( "path/to/test-file.txt" ) );
284         selectedInvalidConsumer.getId();
285         invalidControl.setReturnValue( "invalid" );
286         //        invalidConsumer.completeScan();
287         invalidControl.replay();
288
289         consumers.executeConsumers( repo, excludedTestFile );
290
291         knownControl.verify();
292         invalidControl.verify();
293     }
294
295     /**
296      * Create an OS specific version of the filepath.
297      * Provide path in unix "/" format.
298      */
299     private String _OS( String path )
300     {
301         if ( SystemUtils.IS_OS_WINDOWS )
302         {
303             return path.replace( '/', '\\' );
304         }
305         return path;
306     }
307 }