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