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