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