]> source.dussan.org Git - archiva.git/blob
5717b2de71973aedc0dda934d65877ac8645ce13
[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 java.io.IOException;
23 import java.util.Locale;
24 import org.apache.commons.lang.SystemUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
28 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
30 import org.easymock.MockControl;
31
32 import java.io.File;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.Date;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import org.springframework.beans.BeansException;
40 import org.springframework.beans.factory.BeanFactory;
41 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
42 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.context.ApplicationEvent;
45 import org.springframework.context.MessageSourceResolvable;
46 import org.springframework.context.NoSuchMessageException;
47 import org.springframework.core.io.Resource;
48
49 /**
50  * RepositoryContentConsumersTest
51  *
52  * @version $Id$
53  */
54 public class RepositoryContentConsumersTest
55     extends AbstractRepositoryLayerTestCase
56 {
57     private RepositoryContentConsumers lookupRepositoryConsumers()
58         throws Exception
59     {
60         ArchivaConfiguration configuration = (ArchivaConfiguration)lookup(ArchivaConfiguration.class);
61
62         RepositoryContentConsumers consumerUtilStub = new RepositoryContentConsumersStub(configuration);
63                
64         RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
65                                                                                            .getName() );
66         ApplicationContext context = new MockApplicationContext(consumerUtil.getAvailableKnownConsumers(), consumerUtil.getAvailableInvalidConsumers());
67
68         consumerUtilStub.setApplicationContext(context);
69         consumerUtilStub.setSelectedInvalidConsumers( consumerUtil.getSelectedInvalidConsumers() );
70         consumerUtilStub.setSelectedKnownConsumers( consumerUtil.getSelectedKnownConsumers() );        
71         consumerUtilStub.setArchivaConfiguration( configuration );
72         
73         assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtilStub );
74         
75         return consumerUtilStub;
76     }
77
78     public void testGetSelectedKnownIds()
79         throws Exception
80     {
81         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
82
83         String expectedKnownIds[] = new String[] {
84             "update-db-artifact",
85             "create-missing-checksums",
86             "update-db-repository-metadata",
87             "validate-checksum",
88             "validate-signature",
89             "index-content",
90             "auto-remove",
91             "auto-rename" };
92
93         List<String> knownConsumers = consumerutil.getSelectedKnownConsumerIds();
94         assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
95         assertEquals( "Known Consumer IDs.size", expectedKnownIds.length, knownConsumers.size() );
96
97         for ( String expectedId : expectedKnownIds )
98         {
99             assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
100         }
101     }
102
103     public void testGetSelectedInvalidIds()
104         throws Exception
105     {
106         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
107
108         String expectedInvalidIds[] = new String[] { "update-db-bad-content" };
109
110         List<String> invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
111         assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
112         assertEquals( "Invalid Consumer IDs.size", expectedInvalidIds.length, invalidConsumers.size() );
113
114         for ( String expectedId : expectedInvalidIds )
115         {
116             assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
117         }
118     }
119
120     public void testGetSelectedKnownConsumerMap()
121         throws Exception
122     {
123         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
124
125         String expectedSelectedKnownIds[] = new String[] {
126             "update-db-artifact",
127             "create-missing-checksums",
128             "update-db-repository-metadata",
129             "validate-checksum",
130             "index-content",
131             "auto-remove",
132             "auto-rename" };
133
134         Map<String, KnownRepositoryContentConsumer> knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
135         assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
136         assertEquals( "Known Consumer Map.size", expectedSelectedKnownIds.length, knownConsumerMap.size() );
137
138         for ( String expectedId : expectedSelectedKnownIds )
139         {
140             KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
141             assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
142             assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
143         }
144     }
145
146     public void testGetSelectedInvalidConsumerMap()
147         throws Exception
148     {
149         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
150
151         String expectedSelectedInvalidIds[] = new String[] { "update-db-bad-content" };
152
153         Map<String, InvalidRepositoryContentConsumer> invalidConsumerMap = consumerutil
154             .getSelectedInvalidConsumersMap();
155         assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
156         assertEquals( "Invalid Consumer Map.size", expectedSelectedInvalidIds.length, invalidConsumerMap.size() );
157
158         for ( String expectedId : expectedSelectedInvalidIds )
159         {
160             InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
161             assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
162             assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
163         }
164     }
165
166     public void testGetAvailableKnownList()
167         throws Exception
168     {
169         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
170
171         String expectedKnownIds[] = new String[] {
172             "update-db-artifact",
173             "create-missing-checksums",
174             "update-db-repository-metadata",
175             "validate-checksum",
176             "index-content",
177             "auto-remove",
178             "auto-rename",
179             "available-but-unselected" };
180
181         List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers();
182         assertNotNull( "known consumers should not be null.", knownConsumers );
183         assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size() );
184
185         List<String> expectedIds = Arrays.asList( expectedKnownIds );
186         for ( KnownRepositoryContentConsumer consumer : knownConsumers )
187         {
188             assertTrue( "Consumer [" + consumer.getId() + "] returned by .getAvailableKnownConsumers() is unexpected.",
189                         expectedIds.contains( consumer.getId() ) );
190         }
191     }
192
193     public void testGetAvailableInvalidList()
194         throws Exception
195     {
196         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
197
198         String expectedInvalidIds[] = new String[] { "update-db-bad-content", "move-to-trash-then-notify" };
199
200         List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers();
201         assertNotNull( "invalid consumers should not be null.", invalidConsumers );
202         assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size() );
203
204         List<String> expectedIds = Arrays.asList( expectedInvalidIds );
205         for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
206         {
207             assertTrue( "Consumer [" + consumer.getId()
208                 + "] returned by .getAvailableInvalidConsumers() is unexpected.", expectedIds.contains( consumer
209                 .getId() ) );
210         }
211     }
212
213     public void testExecution()
214         throws Exception
215     {
216         MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
217         RepositoryContentConsumers consumers = lookupRepositoryConsumers();
218         KnownRepositoryContentConsumer selectedKnownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
219         KnownRepositoryContentConsumer unselectedKnownConsumer =
220             (KnownRepositoryContentConsumer) MockControl.createNiceControl(
221                 KnownRepositoryContentConsumer.class ).getMock();
222
223         consumers.setApplicationContext(new MockApplicationContext(Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ), null));
224
225         consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
226
227         MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
228         InvalidRepositoryContentConsumer selectedInvalidConsumer =
229             (InvalidRepositoryContentConsumer) invalidControl.getMock();
230         InvalidRepositoryContentConsumer unselectedInvalidConsumer =
231             (InvalidRepositoryContentConsumer) MockControl.createControl(
232                 InvalidRepositoryContentConsumer.class ).getMock();
233
234         consumers.setApplicationContext( new MockApplicationContext(null, Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer )));
235
236         consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
237
238         ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
239         File testFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
240
241         Date startTime = new Date( System.currentTimeMillis() );
242         startTime.setTime( 12345678 );
243
244         selectedKnownConsumer.beginScan( repo, startTime );
245         selectedKnownConsumer.getExcludes();
246         knownControl.setReturnValue( Collections.EMPTY_LIST );
247         selectedKnownConsumer.getIncludes();
248         knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
249         selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ) );
250         //        knownConsumer.completeScan();
251         knownControl.replay();
252
253         selectedInvalidConsumer.beginScan( repo, startTime );
254         //        invalidConsumer.completeScan();
255         invalidControl.replay();
256
257         consumers.executeConsumers( repo, testFile );
258
259         knownControl.verify();
260         invalidControl.verify();
261
262         knownControl.reset();
263         invalidControl.reset();
264
265         File notIncludedTestFile = getTestFile( "target/test-repo/path/to/test-file.xml" );
266
267         selectedKnownConsumer.beginScan( repo, startTime );
268         selectedKnownConsumer.getExcludes();
269         knownControl.setReturnValue( Collections.EMPTY_LIST );
270         selectedKnownConsumer.getIncludes();
271         knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
272         //        knownConsumer.completeScan();
273         knownControl.replay();
274
275         selectedInvalidConsumer.beginScan( repo, startTime );
276         selectedInvalidConsumer.processFile( _OS( "path/to/test-file.xml" ) );
277         selectedInvalidConsumer.getId();
278         invalidControl.setReturnValue( "invalid" );
279         //        invalidConsumer.completeScan();
280         invalidControl.replay();
281
282         consumers.executeConsumers( repo, notIncludedTestFile );
283
284         knownControl.verify();
285         invalidControl.verify();
286
287         knownControl.reset();
288         invalidControl.reset();
289
290         File excludedTestFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
291
292         selectedKnownConsumer.beginScan( repo, startTime );
293         selectedKnownConsumer.getExcludes();
294         knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
295         //        knownConsumer.completeScan();
296         knownControl.replay();
297
298         selectedInvalidConsumer.beginScan( repo, startTime );
299         selectedInvalidConsumer.processFile( _OS( "path/to/test-file.txt" ) );
300         selectedInvalidConsumer.getId();
301         invalidControl.setReturnValue( "invalid" );
302         //        invalidConsumer.completeScan();
303         invalidControl.replay();
304
305         consumers.executeConsumers( repo, excludedTestFile );
306
307         knownControl.verify();
308         invalidControl.verify();
309     }
310
311     /**
312      * Create an OS specific version of the filepath.
313      * Provide path in unix "/" format.
314      */
315     private String _OS( String path )
316     {
317         if ( SystemUtils.IS_OS_WINDOWS )
318         {
319             return path.replace( '/', '\\' );
320         }
321         return path;
322     }
323
324     private static Map convertToMap(List objects)
325     {
326         HashMap map = new HashMap();
327         for (Object o : objects)
328         {
329             map.put(o, o);
330         }
331         return map;
332     }
333
334     public class MockApplicationContext implements ApplicationContext
335     {
336         private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
337
338         private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
339
340         public MockApplicationContext(List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer, List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers)
341         {
342             this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
343             this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
344         }
345
346         public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
347             throw new UnsupportedOperationException("Not supported yet.");
348         }
349
350         public String getDisplayName() {
351             throw new UnsupportedOperationException("Not supported yet.");
352         }
353
354         public String getId() {
355             throw new UnsupportedOperationException("Not supported yet.");
356         }
357
358         public ApplicationContext getParent() {
359             throw new UnsupportedOperationException("Not supported yet.");
360         }
361
362         public long getStartupDate() {
363             throw new UnsupportedOperationException("Not supported yet.");
364         }
365
366         public boolean containsBeanDefinition(String beanName) {
367             throw new UnsupportedOperationException("Not supported yet.");
368         }
369
370         public int getBeanDefinitionCount() {
371             throw new UnsupportedOperationException("Not supported yet.");
372         }
373
374         public String[] getBeanDefinitionNames() {
375             throw new UnsupportedOperationException("Not supported yet.");
376         }
377
378         public String[] getBeanNamesForType(Class type) {
379             throw new UnsupportedOperationException("Not supported yet.");
380         }
381
382         public String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit) {
383             throw new UnsupportedOperationException("Not supported yet.");
384         }
385
386         public Map getBeansOfType(Class type) throws BeansException {
387             if (type == KnownRepositoryContentConsumer.class)
388             {
389                 return convertToMap(knownRepositoryContentConsumer);
390             }
391             if (type == InvalidRepositoryContentConsumer.class)
392             {
393                 return convertToMap(invalidRepositoryContentConsumers);
394             }
395             throw new UnsupportedOperationException("Should not have been called");
396         }
397
398         public Map getBeansOfType(Class type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException {
399             throw new UnsupportedOperationException("Not supported yet.");
400         }
401
402         public boolean containsBean(String name) {
403             throw new UnsupportedOperationException("Not supported yet.");
404         }
405
406         public String[] getAliases(String name) {
407             throw new UnsupportedOperationException("Not supported yet.");
408         }
409
410         public Object getBean(String name) throws BeansException {
411             throw new UnsupportedOperationException("Not supported yet.");
412         }
413
414         public Object getBean(String name, Class requiredType) throws BeansException {
415             throw new UnsupportedOperationException("Not supported yet.");
416         }
417
418         public Object getBean(String name, Object[] args) throws BeansException {
419             throw new UnsupportedOperationException("Not supported yet.");
420         }
421
422         public Class getType(String name) throws NoSuchBeanDefinitionException {
423             throw new UnsupportedOperationException("Not supported yet.");
424         }
425
426         public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
427             throw new UnsupportedOperationException("Not supported yet.");
428         }
429
430         public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
431             throw new UnsupportedOperationException("Not supported yet.");
432         }
433
434         public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
435             throw new UnsupportedOperationException("Not supported yet.");
436         }
437
438         public boolean containsLocalBean(String name) {
439             throw new UnsupportedOperationException("Not supported yet.");
440         }
441
442         public BeanFactory getParentBeanFactory() {
443             throw new UnsupportedOperationException("Not supported yet.");
444         }
445
446         public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
447             throw new UnsupportedOperationException("Not supported yet.");
448         }
449
450         public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
451             throw new UnsupportedOperationException("Not supported yet.");
452         }
453
454         public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
455             throw new UnsupportedOperationException("Not supported yet.");
456         }
457
458         public void publishEvent(ApplicationEvent event) {
459             throw new UnsupportedOperationException("Not supported yet.");
460         }
461
462         public Resource[] getResources(String locationPattern) throws IOException {
463             throw new UnsupportedOperationException("Not supported yet.");
464         }
465
466         public ClassLoader getClassLoader() {
467             throw new UnsupportedOperationException("Not supported yet.");
468         }
469
470         public Resource getResource(String location) {
471             throw new UnsupportedOperationException("Not supported yet.");
472         }
473     }
474 }