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