1 package org.apache.archiva.repository.scanner;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.File;
48 import java.io.IOException;
49 import java.lang.annotation.Annotation;
50 import java.util.Arrays;
51 import java.util.Collections;
52 import java.util.Date;
53 import java.util.HashMap;
54 import java.util.List;
55 import java.util.Locale;
56 import java.util.Map;
57
58 import static org.easymock.EasyMock.*;
59
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
126
127
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 =
251 knownControl.createMock( KnownRepositoryContentConsumer.class );
252
253 KnownRepositoryContentConsumer unselectedKnownConsumer =
254 createNiceControl().createMock( KnownRepositoryContentConsumer.class );
255
256 consumers.setApplicationContext(
257 new MockApplicationContext( Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ), null ) );
258
259 consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
260
261 IMocksControl invalidControl = createControl();
262
263 InvalidRepositoryContentConsumer selectedInvalidConsumer =
264 invalidControl.createMock( InvalidRepositoryContentConsumer.class );
265
266 InvalidRepositoryContentConsumer unselectedInvalidConsumer =
267 createControl().createMock( InvalidRepositoryContentConsumer.class );
268
269 consumers.setApplicationContext(
270 new MockApplicationContext( null, Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer ) ) );
271
272 consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
273
274 ManagedRepository repo = createRepository( "id", "name", new File( "target/test-repo" ) );
275 File testFile = new File( "target/test-repo/path/to/test-file.txt" );
276
277 Date startTime = new Date( System.currentTimeMillis() );
278 startTime.setTime( 12345678 );
279
280 selectedKnownConsumer.beginScan( repo, startTime, false );
281 expect( selectedKnownConsumer.getIncludes() ).andReturn( Collections.singletonList( "**/*.txt" ) );
282 selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ), false );
283
284 knownControl.replay();
285
286 selectedInvalidConsumer.beginScan( repo, startTime, false );
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 expect( selectedKnownConsumer.getExcludes() ).andReturn( Collections.<String>emptyList() );
301
302 expect( selectedKnownConsumer.getIncludes() ).andReturn( Collections.singletonList( "**/*.txt" ) );
303
304 knownControl.replay();
305
306 selectedInvalidConsumer.beginScan( repo, startTime, false );
307 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.xml" ), false );
308 expect( selectedInvalidConsumer.getId() ).andReturn( "invalid" );
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 expect( selectedKnownConsumer.getExcludes() ).andReturn( Collections.singletonList( "**/test-file.txt" ) );
323 knownControl.replay();
324
325 selectedInvalidConsumer.beginScan( repo, startTime, false );
326 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.txt" ), false );
327 expect( selectedInvalidConsumer.getId() ).andReturn( "invalid" );
328 invalidControl.replay();
329
330 consumers.executeConsumers( repo, excludedTestFile, true );
331
332 knownControl.verify();
333 invalidControl.verify();
334 }
335
336
337
338
339
340 private String _OS( String path )
341 {
342 if ( SystemUtils.IS_OS_WINDOWS )
343 {
344 return path.replace( '/', '\\' );
345 }
346 return path;
347 }
348
349 private static Map convertToMap( List objects )
350 {
351 HashMap map = new HashMap();
352 for ( Object o : objects )
353 {
354 map.put( o, o );
355 }
356 return map;
357 }
358
359 public class MockApplicationContext
360 implements ApplicationContext
361 {
362 private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
363
364 private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
365
366 public MockApplicationContext( List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer,
367 List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers )
368 {
369 this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
370 this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
371 }
372
373 @Override
374 public String getApplicationName()
375 {
376 return "foo";
377 }
378
379 @Override
380 public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
381 throws IllegalStateException
382 {
383 throw new UnsupportedOperationException( "Not supported yet." );
384 }
385
386 @Override
387 public String getDisplayName()
388 {
389 throw new UnsupportedOperationException( "Not supported yet." );
390 }
391
392 @Override
393 public String getId()
394 {
395 throw new UnsupportedOperationException( "Not supported yet." );
396 }
397
398 @Override
399 public ApplicationContext getParent()
400 {
401 throw new UnsupportedOperationException( "Not supported yet." );
402 }
403
404 @Override
405 public long getStartupDate()
406 {
407 throw new UnsupportedOperationException( "Not supported yet." );
408 }
409
410 @Override
411 public boolean containsBeanDefinition( String beanName )
412 {
413 throw new UnsupportedOperationException( "Not supported yet." );
414 }
415
416 @Override
417 public int getBeanDefinitionCount()
418 {
419 throw new UnsupportedOperationException( "Not supported yet." );
420 }
421
422 @Override
423 public String[] getBeanDefinitionNames()
424 {
425 throw new UnsupportedOperationException( "Not supported yet." );
426 }
427
428 @Override
429 public String[] getBeanNamesForType( Class type )
430 {
431 throw new UnsupportedOperationException( "Not supported yet." );
432 }
433
434 @Override
435 public String[] getBeanNamesForType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
436 {
437 throw new UnsupportedOperationException( "Not supported yet." );
438 }
439
440 @Override
441 public <T> T getBean( Class<T> aClass, Object... objects )
442 throws BeansException
443 {
444 throw new UnsupportedOperationException( "Not supported yet." );
445 }
446
447 @Override
448 public Map getBeansOfType( Class type )
449 throws BeansException
450 {
451 if ( type == KnownRepositoryContentConsumer.class )
452 {
453 return convertToMap( knownRepositoryContentConsumer );
454 }
455 if ( type == InvalidRepositoryContentConsumer.class )
456 {
457 return convertToMap( invalidRepositoryContentConsumers );
458 }
459 throw new UnsupportedOperationException( "Should not have been called" );
460 }
461
462 @Override
463 public Map getBeansOfType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
464 throws BeansException
465 {
466 throw new UnsupportedOperationException( "Not supported yet." );
467 }
468
469 @Override
470 public boolean containsBean( String name )
471 {
472 throw new UnsupportedOperationException( "Not supported yet." );
473 }
474
475 @Override
476 public String[] getAliases( String name )
477 {
478 throw new UnsupportedOperationException( "Not supported yet." );
479 }
480
481 @Override
482 public Object getBean( String name )
483 throws BeansException
484 {
485 throw new UnsupportedOperationException( "Not supported yet." );
486 }
487
488 @Override
489 public Object getBean( String name, Class requiredType )
490 throws BeansException
491 {
492 throw new UnsupportedOperationException( "Not supported yet." );
493 }
494
495 @Override
496 public Object getBean( String name, Object[] args )
497 throws BeansException
498 {
499 throw new UnsupportedOperationException( "Not supported yet." );
500 }
501
502 @Override
503 public Class getType( String name )
504 throws NoSuchBeanDefinitionException
505 {
506 throw new UnsupportedOperationException( "Not supported yet." );
507 }
508
509 @Override
510 public boolean isPrototype( String name )
511 throws NoSuchBeanDefinitionException
512 {
513 throw new UnsupportedOperationException( "Not supported yet." );
514 }
515
516 @Override
517 public boolean isSingleton( String name )
518 throws NoSuchBeanDefinitionException
519 {
520 throw new UnsupportedOperationException( "Not supported yet." );
521 }
522
523 @Override
524 public boolean isTypeMatch( String name, Class targetType )
525 throws NoSuchBeanDefinitionException
526 {
527 throw new UnsupportedOperationException( "Not supported yet." );
528 }
529
530 @Override
531 public boolean containsLocalBean( String name )
532 {
533 throw new UnsupportedOperationException( "Not supported yet." );
534 }
535
536 @Override
537 public BeanFactory getParentBeanFactory()
538 {
539 throw new UnsupportedOperationException( "Not supported yet." );
540 }
541
542 @Override
543 public String getMessage( String code, Object[] args, String defaultMessage, Locale locale )
544 {
545 throw new UnsupportedOperationException( "Not supported yet." );
546 }
547
548 @Override
549 public String getMessage( String code, Object[] args, Locale locale )
550 throws NoSuchMessageException
551 {
552 throw new UnsupportedOperationException( "Not supported yet." );
553 }
554
555 @Override
556 public String getMessage( MessageSourceResolvable resolvable, Locale locale )
557 throws NoSuchMessageException
558 {
559 throw new UnsupportedOperationException( "Not supported yet." );
560 }
561
562 @Override
563 public void publishEvent( ApplicationEvent event )
564 {
565 throw new UnsupportedOperationException( "Not supported yet." );
566 }
567
568 @Override
569 public Resource[] getResources( String locationPattern )
570 throws IOException
571 {
572 throw new UnsupportedOperationException( "Not supported yet." );
573 }
574
575 @Override
576 public ClassLoader getClassLoader()
577 {
578 throw new UnsupportedOperationException( "Not supported yet." );
579 }
580
581 @Override
582 public Resource getResource( String location )
583 {
584 throw new UnsupportedOperationException( "Not supported yet." );
585 }
586
587 @Override
588 public <T> T getBean( Class<T> tClass )
589 throws BeansException
590 {
591 throw new UnsupportedOperationException( "Not supported yet." );
592 }
593
594 @Override
595 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
596 throws BeansException
597 {
598 throw new UnsupportedOperationException( "Not supported yet." );
599 }
600
601 @Override
602 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
603 {
604 throw new UnsupportedOperationException( "Not supported yet." );
605 }
606
607 @Override
608 public Environment getEnvironment()
609 {
610 return null;
611 }
612
613 @Override
614 public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
615 {
616 return new String[0];
617 }
618
619 @Override
620 public void publishEvent( Object o )
621 {
622
623 }
624
625 @Override
626 public String[] getBeanNamesForType( ResolvableType resolvableType )
627 {
628 return new String[0];
629 }
630
631 @Override
632 public boolean isTypeMatch( String s, ResolvableType resolvableType )
633 throws NoSuchBeanDefinitionException
634 {
635 return false;
636 }
637 }
638 }