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.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;
44 import javax.inject.Inject;
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;
55 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
56 import static org.easymock.EasyMock.*;
59 * 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 = knownControl.createMock( KnownRepositoryContentConsumer.class );
252 KnownRepositoryContentConsumer unselectedKnownConsumer = createNiceControl().createMock( KnownRepositoryContentConsumer.class );
255 consumers.setApplicationContext(
256 new MockApplicationContext( Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ), null ) );
258 consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
260 IMocksControl invalidControl = createControl();
262 InvalidRepositoryContentConsumer selectedInvalidConsumer = invalidControl.createMock( InvalidRepositoryContentConsumer.class );
264 InvalidRepositoryContentConsumer unselectedInvalidConsumer = createControl().createMock( InvalidRepositoryContentConsumer.class );
266 consumers.setApplicationContext(
267 new MockApplicationContext( null, Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer ) ) );
269 consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
271 ManagedRepository repo = createRepository( "id", "name", new File( "target/test-repo" ) );
272 File testFile = new File( "target/test-repo/path/to/test-file.txt" );
274 Date startTime = new Date( System.currentTimeMillis( ) );
275 startTime.setTime( 12345678 );
277 selectedKnownConsumer.beginScan( repo, startTime, false );
278 expect( selectedKnownConsumer.getIncludes( ) ).andReturn( Collections.singletonList( "**/*.txt" ) );
279 selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ), false );
281 knownControl.replay( );
283 selectedInvalidConsumer.beginScan( repo, startTime, false );
284 invalidControl.replay( );
287 consumers.executeConsumers( repo, testFile, true );
289 knownControl.verify( );
290 invalidControl.verify( );
292 knownControl.reset( );
293 invalidControl.reset( );
295 File notIncludedTestFile = new File( "target/test-repo/path/to/test-file.xml" );
297 selectedKnownConsumer.beginScan( repo, startTime, false );
298 expect( selectedKnownConsumer.getExcludes() ).andReturn( Collections.<String>emptyList() );
300 expect( selectedKnownConsumer.getIncludes( ) ).andReturn( Collections.singletonList( "**/*.txt" ) );
302 knownControl.replay( );
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( );
309 consumers.executeConsumers( repo, notIncludedTestFile, true );
311 knownControl.verify( );
312 invalidControl.verify( );
314 knownControl.reset( );
315 invalidControl.reset( );
317 File excludedTestFile = new File( "target/test-repo/path/to/test-file.txt" );
319 selectedKnownConsumer.beginScan( repo, startTime, false );
320 expect( selectedKnownConsumer.getExcludes() ).andReturn( Collections.singletonList( "**/test-file.txt" ) );
321 knownControl.replay( );
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( );
328 consumers.executeConsumers( repo, excludedTestFile, true );
330 knownControl.verify( );
331 invalidControl.verify( );
335 * Create an OS specific version of the filepath.
336 * Provide path in unix "/" format.
338 private String _OS( String path )
340 if ( SystemUtils.IS_OS_WINDOWS )
342 return path.replace( '/', '\\' );
347 private static Map convertToMap( List objects )
349 HashMap map = new HashMap( );
350 for ( Object o : objects )
357 public class MockApplicationContext
358 implements ApplicationContext
360 private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
362 private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
364 public MockApplicationContext( List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer,
365 List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers )
367 this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
368 this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
371 public String getApplicationName()
376 public AutowireCapableBeanFactory getAutowireCapableBeanFactory( )
377 throws IllegalStateException
379 throw new UnsupportedOperationException( "Not supported yet." );
382 public String getDisplayName( )
384 throw new UnsupportedOperationException( "Not supported yet." );
387 public String getId( )
389 throw new UnsupportedOperationException( "Not supported yet." );
392 public ApplicationContext getParent( )
394 throw new UnsupportedOperationException( "Not supported yet." );
397 public long getStartupDate( )
399 throw new UnsupportedOperationException( "Not supported yet." );
402 public boolean containsBeanDefinition( String beanName )
404 throw new UnsupportedOperationException( "Not supported yet." );
407 public int getBeanDefinitionCount( )
409 throw new UnsupportedOperationException( "Not supported yet." );
412 public String[] getBeanDefinitionNames( )
414 throw new UnsupportedOperationException( "Not supported yet." );
417 public String[] getBeanNamesForType( Class type )
419 throw new UnsupportedOperationException( "Not supported yet." );
422 public String[] getBeanNamesForType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
424 throw new UnsupportedOperationException( "Not supported yet." );
427 public Map getBeansOfType( Class type )
428 throws BeansException
430 if ( type == KnownRepositoryContentConsumer.class )
432 return convertToMap( knownRepositoryContentConsumer );
434 if ( type == InvalidRepositoryContentConsumer.class )
436 return convertToMap( invalidRepositoryContentConsumers );
438 throw new UnsupportedOperationException( "Should not have been called" );
441 public Map getBeansOfType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
442 throws BeansException
444 throw new UnsupportedOperationException( "Not supported yet." );
447 public boolean containsBean( String name )
449 throw new UnsupportedOperationException( "Not supported yet." );
452 public String[] getAliases( String name )
454 throw new UnsupportedOperationException( "Not supported yet." );
457 public Object getBean( String name )
458 throws BeansException
460 throw new UnsupportedOperationException( "Not supported yet." );
463 public Object getBean( String name, Class requiredType )
464 throws BeansException
466 throw new UnsupportedOperationException( "Not supported yet." );
469 public Object getBean( String name, Object[] args )
470 throws BeansException
472 throw new UnsupportedOperationException( "Not supported yet." );
475 public Class getType( String name )
476 throws NoSuchBeanDefinitionException
478 throw new UnsupportedOperationException( "Not supported yet." );
481 public boolean isPrototype( String name )
482 throws NoSuchBeanDefinitionException
484 throw new UnsupportedOperationException( "Not supported yet." );
487 public boolean isSingleton( String name )
488 throws NoSuchBeanDefinitionException
490 throw new UnsupportedOperationException( "Not supported yet." );
493 public boolean isTypeMatch( String name, Class targetType )
494 throws NoSuchBeanDefinitionException
496 throw new UnsupportedOperationException( "Not supported yet." );
499 public boolean containsLocalBean( String name )
501 throw new UnsupportedOperationException( "Not supported yet." );
504 public BeanFactory getParentBeanFactory( )
506 throw new UnsupportedOperationException( "Not supported yet." );
509 public String getMessage( String code, Object[] args, String defaultMessage, Locale locale )
511 throw new UnsupportedOperationException( "Not supported yet." );
514 public String getMessage( String code, Object[] args, Locale locale )
515 throws NoSuchMessageException
517 throw new UnsupportedOperationException( "Not supported yet." );
520 public String getMessage( MessageSourceResolvable resolvable, Locale locale )
521 throws NoSuchMessageException
523 throw new UnsupportedOperationException( "Not supported yet." );
526 public void publishEvent( ApplicationEvent event )
528 throw new UnsupportedOperationException( "Not supported yet." );
531 public Resource[] getResources( String locationPattern )
534 throw new UnsupportedOperationException( "Not supported yet." );
537 public ClassLoader getClassLoader( )
539 throw new UnsupportedOperationException( "Not supported yet." );
542 public Resource getResource( String location )
544 throw new UnsupportedOperationException( "Not supported yet." );
547 public <T> T getBean( Class<T> tClass )
548 throws BeansException
550 throw new UnsupportedOperationException( "Not supported yet." );
553 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
554 throws BeansException
556 throw new UnsupportedOperationException( "Not supported yet." );
559 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
561 throw new UnsupportedOperationException( "Not supported yet." );
564 public Environment getEnvironment()