1 package org.apache.archiva.repository.scanner;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
46 import javax.inject.Inject;
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;
58 import static org.easymock.EasyMock.*;
61 * RepositoryContentConsumersTest
63 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
64 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
65 public class RepositoryContentConsumersTest
70 ApplicationContext applicationContext;
72 protected ManagedRepository createRepository( String id, String name, File location )
74 ManagedRepository repo = new ManagedRepository();
77 repo.setLocation( location.getAbsolutePath() );
81 protected RemoteRepository createRemoteRepository( String id, String name, String url )
83 RemoteRepository repo = new RemoteRepository();
90 private RepositoryContentConsumers lookupRepositoryConsumers()
94 ArchivaConfiguration configuration =
95 applicationContext.getBean( "archivaConfiguration#test-conf", ArchivaConfiguration.class );
97 ArchivaAdministrationStub administrationStub = new ArchivaAdministrationStub( configuration );
99 RepositoryContentConsumers consumerUtilStub = new RepositoryContentConsumersStub( administrationStub );
101 RepositoryContentConsumers consumerUtil =
102 applicationContext.getBean( "repositoryContentConsumers#test", RepositoryContentConsumers.class );
103 ApplicationContext context = new MockApplicationContext( consumerUtil.getAvailableKnownConsumers(), //
104 consumerUtil.getAvailableInvalidConsumers() );
106 consumerUtilStub.setApplicationContext( context );
107 consumerUtilStub.setSelectedInvalidConsumers( consumerUtil.getSelectedInvalidConsumers() );
108 consumerUtilStub.setSelectedKnownConsumers( consumerUtil.getSelectedKnownConsumers() );
109 consumerUtilStub.setArchivaAdministration( administrationStub );
111 assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtilStub );
113 return consumerUtilStub;
117 public void testGetSelectedKnownIds()
120 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
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,
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() );
132 for ( String expectedId : expectedKnownIds )
134 assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
139 public void testGetSelectedInvalidIds()
142 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
144 String expectedInvalidIds[] = new String[]{ "update-db-bad-content" };
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() );
150 for ( String expectedId : expectedInvalidIds )
152 assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
157 public void testGetSelectedKnownConsumerMap()
160 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
162 String expectedSelectedKnownIds[] =
163 new String[]{ "create-missing-checksums", "validate-checksum", "index-content", "auto-remove",
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() );
171 for ( String expectedId : expectedSelectedKnownIds )
173 KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
174 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
175 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
180 public void testGetSelectedInvalidConsumerMap()
183 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
185 String expectedSelectedInvalidIds[] = new String[]{ "update-db-bad-content" };
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() );
192 for ( String expectedId : expectedSelectedInvalidIds )
194 InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
195 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
196 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
201 public void testGetAvailableKnownList()
204 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
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" };
210 List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers();
211 assertNotNull( "known consumers should not be null.", knownConsumers );
212 assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size() );
214 List<String> expectedIds = Arrays.asList( expectedKnownIds );
215 for ( KnownRepositoryContentConsumer consumer : knownConsumers )
217 assertTrue( "Consumer [" + consumer.getId() + "] returned by .getAvailableKnownConsumers() is unexpected.",
218 expectedIds.contains( consumer.getId() ) );
223 public void testGetAvailableInvalidList()
226 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
228 String expectedInvalidIds[] = new String[]{ "update-db-bad-content", "move-to-trash-then-notify" };
230 List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers();
231 assertNotNull( "invalid consumers should not be null.", invalidConsumers );
232 assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size() );
234 List<String> expectedIds = Arrays.asList( expectedInvalidIds );
235 for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
238 "Consumer [" + consumer.getId() + "] returned by .getAvailableInvalidConsumers() is unexpected.",
239 expectedIds.contains( consumer.getId() ) );
244 public void testExecution()
247 IMocksControl knownControl = createNiceControl();
249 RepositoryContentConsumers consumers = lookupRepositoryConsumers();
250 KnownRepositoryContentConsumer selectedKnownConsumer =
251 knownControl.createMock( KnownRepositoryContentConsumer.class );
253 KnownRepositoryContentConsumer unselectedKnownConsumer =
254 createNiceControl().createMock( KnownRepositoryContentConsumer.class );
256 consumers.setApplicationContext(
257 new MockApplicationContext( Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ), null ) );
259 consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
261 IMocksControl invalidControl = createControl();
263 InvalidRepositoryContentConsumer selectedInvalidConsumer =
264 invalidControl.createMock( InvalidRepositoryContentConsumer.class );
266 InvalidRepositoryContentConsumer unselectedInvalidConsumer =
267 createControl().createMock( InvalidRepositoryContentConsumer.class );
269 consumers.setApplicationContext(
270 new MockApplicationContext( null, Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer ) ) );
272 consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
274 ManagedRepository repo = createRepository( "id", "name", new File( "target/test-repo" ) );
275 File testFile = new File( "target/test-repo/path/to/test-file.txt" );
277 Date startTime = new Date( System.currentTimeMillis() );
278 startTime.setTime( 12345678 );
280 selectedKnownConsumer.beginScan( repo, startTime, false );
281 expect( selectedKnownConsumer.getIncludes() ).andReturn( Collections.singletonList( "**/*.txt" ) );
282 selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ), false );
284 knownControl.replay();
286 selectedInvalidConsumer.beginScan( repo, startTime, false );
287 invalidControl.replay();
289 consumers.executeConsumers( repo, testFile, true );
291 knownControl.verify();
292 invalidControl.verify();
294 knownControl.reset();
295 invalidControl.reset();
297 File notIncludedTestFile = new File( "target/test-repo/path/to/test-file.xml" );
299 selectedKnownConsumer.beginScan( repo, startTime, false );
300 expect( selectedKnownConsumer.getExcludes() ).andReturn( Collections.<String>emptyList() );
302 expect( selectedKnownConsumer.getIncludes() ).andReturn( Collections.singletonList( "**/*.txt" ) );
304 knownControl.replay();
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();
311 consumers.executeConsumers( repo, notIncludedTestFile, true );
313 knownControl.verify();
314 invalidControl.verify();
316 knownControl.reset();
317 invalidControl.reset();
319 File excludedTestFile = new File( "target/test-repo/path/to/test-file.txt" );
321 selectedKnownConsumer.beginScan( repo, startTime, false );
322 expect( selectedKnownConsumer.getExcludes() ).andReturn( Collections.singletonList( "**/test-file.txt" ) );
323 knownControl.replay();
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();
330 consumers.executeConsumers( repo, excludedTestFile, true );
332 knownControl.verify();
333 invalidControl.verify();
337 * Create an OS specific version of the filepath.
338 * Provide path in unix "/" format.
340 private String _OS( String path )
342 if ( SystemUtils.IS_OS_WINDOWS )
344 return path.replace( '/', '\\' );
349 private static Map convertToMap( List objects )
351 HashMap map = new HashMap();
352 for ( Object o : objects )
359 public class MockApplicationContext
360 implements ApplicationContext
362 private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
364 private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
366 public MockApplicationContext( List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer,
367 List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers )
369 this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
370 this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
374 public String getApplicationName()
380 public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
381 throws IllegalStateException
383 throw new UnsupportedOperationException( "Not supported yet." );
387 public String getDisplayName()
389 throw new UnsupportedOperationException( "Not supported yet." );
393 public String getId()
395 throw new UnsupportedOperationException( "Not supported yet." );
399 public ApplicationContext getParent()
401 throw new UnsupportedOperationException( "Not supported yet." );
405 public long getStartupDate()
407 throw new UnsupportedOperationException( "Not supported yet." );
411 public boolean containsBeanDefinition( String beanName )
413 throw new UnsupportedOperationException( "Not supported yet." );
417 public int getBeanDefinitionCount()
419 throw new UnsupportedOperationException( "Not supported yet." );
423 public String[] getBeanDefinitionNames()
425 throw new UnsupportedOperationException( "Not supported yet." );
429 public String[] getBeanNamesForType( Class type )
431 throw new UnsupportedOperationException( "Not supported yet." );
435 public String[] getBeanNamesForType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
437 throw new UnsupportedOperationException( "Not supported yet." );
441 public <T> T getBean( Class<T> aClass, Object... objects )
442 throws BeansException
444 throw new UnsupportedOperationException( "Not supported yet." );
448 public Map getBeansOfType( Class type )
449 throws BeansException
451 if ( type == KnownRepositoryContentConsumer.class )
453 return convertToMap( knownRepositoryContentConsumer );
455 if ( type == InvalidRepositoryContentConsumer.class )
457 return convertToMap( invalidRepositoryContentConsumers );
459 throw new UnsupportedOperationException( "Should not have been called" );
463 public Map getBeansOfType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
464 throws BeansException
466 throw new UnsupportedOperationException( "Not supported yet." );
470 public boolean containsBean( String name )
472 throw new UnsupportedOperationException( "Not supported yet." );
476 public String[] getAliases( String name )
478 throw new UnsupportedOperationException( "Not supported yet." );
482 public Object getBean( String name )
483 throws BeansException
485 throw new UnsupportedOperationException( "Not supported yet." );
489 public Object getBean( String name, Class requiredType )
490 throws BeansException
492 throw new UnsupportedOperationException( "Not supported yet." );
496 public Object getBean( String name, Object[] args )
497 throws BeansException
499 throw new UnsupportedOperationException( "Not supported yet." );
503 public Class getType( String name )
504 throws NoSuchBeanDefinitionException
506 throw new UnsupportedOperationException( "Not supported yet." );
510 public boolean isPrototype( String name )
511 throws NoSuchBeanDefinitionException
513 throw new UnsupportedOperationException( "Not supported yet." );
517 public boolean isSingleton( String name )
518 throws NoSuchBeanDefinitionException
520 throw new UnsupportedOperationException( "Not supported yet." );
524 public boolean isTypeMatch( String name, Class targetType )
525 throws NoSuchBeanDefinitionException
527 throw new UnsupportedOperationException( "Not supported yet." );
531 public boolean containsLocalBean( String name )
533 throw new UnsupportedOperationException( "Not supported yet." );
537 public BeanFactory getParentBeanFactory()
539 throw new UnsupportedOperationException( "Not supported yet." );
543 public String getMessage( String code, Object[] args, String defaultMessage, Locale locale )
545 throw new UnsupportedOperationException( "Not supported yet." );
549 public String getMessage( String code, Object[] args, Locale locale )
550 throws NoSuchMessageException
552 throw new UnsupportedOperationException( "Not supported yet." );
556 public String getMessage( MessageSourceResolvable resolvable, Locale locale )
557 throws NoSuchMessageException
559 throw new UnsupportedOperationException( "Not supported yet." );
563 public void publishEvent( ApplicationEvent event )
565 throw new UnsupportedOperationException( "Not supported yet." );
569 public Resource[] getResources( String locationPattern )
572 throw new UnsupportedOperationException( "Not supported yet." );
576 public ClassLoader getClassLoader()
578 throw new UnsupportedOperationException( "Not supported yet." );
582 public Resource getResource( String location )
584 throw new UnsupportedOperationException( "Not supported yet." );
588 public <T> T getBean( Class<T> tClass )
589 throws BeansException
591 throw new UnsupportedOperationException( "Not supported yet." );
595 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
596 throws BeansException
598 throw new UnsupportedOperationException( "Not supported yet." );
602 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
604 throw new UnsupportedOperationException( "Not supported yet." );
608 public Environment getEnvironment()
614 public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
616 return new String[0];
620 public void publishEvent( Object o )
626 public String[] getBeanNamesForType( ResolvableType resolvableType )
628 return new String[0];
632 public boolean isTypeMatch( String s, ResolvableType resolvableType )
633 throws NoSuchBeanDefinitionException