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.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.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;
58 * RepositoryContentConsumersTest
62 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
63 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
64 public class RepositoryContentConsumersTest
69 ApplicationContext applicationContext;
71 protected ManagedRepository createRepository( String id, String name, File location )
73 ManagedRepository repo = new ManagedRepository( );
76 repo.setLocation( location.getAbsolutePath( ) );
80 protected RemoteRepository createRemoteRepository( String id, String name, String url )
82 RemoteRepository repo = new RemoteRepository( );
89 private RepositoryContentConsumers lookupRepositoryConsumers( )
93 ArchivaConfiguration configuration =
94 applicationContext.getBean( "archivaConfiguration#test-conf", ArchivaConfiguration.class );
96 ArchivaAdministrationStub administrationStub = new ArchivaAdministrationStub( configuration );
98 RepositoryContentConsumers consumerUtilStub = new RepositoryContentConsumersStub( administrationStub );
100 RepositoryContentConsumers consumerUtil =
101 applicationContext.getBean( "repositoryContentConsumers#test", RepositoryContentConsumers.class );
102 ApplicationContext context = new MockApplicationContext( consumerUtil.getAvailableKnownConsumers( ),
103 consumerUtil.getAvailableInvalidConsumers( ) );
105 consumerUtilStub.setApplicationContext( context );
106 consumerUtilStub.setSelectedInvalidConsumers( consumerUtil.getSelectedInvalidConsumers( ) );
107 consumerUtilStub.setSelectedKnownConsumers( consumerUtil.getSelectedKnownConsumers( ) );
108 consumerUtilStub.setArchivaAdministration( administrationStub );
110 assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtilStub );
112 return consumerUtilStub;
116 public void testGetSelectedKnownIds( )
119 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers( );
121 String expectedKnownIds[] =
122 new String[]{ "create-missing-checksums", "validate-checksum", "validate-signature", "index-content",
123 "auto-remove", "auto-rename", "create-archiva-metadata", "duplicate-artifacts" };
124 //update-db-artifact, create-missing-checksums, update-db-repository-metadata,
125 //validate-checksum, validate-signature, index-content, auto-remove, auto-rename,
127 List<String> knownConsumers = consumerutil.getSelectedKnownConsumerIds( );
128 assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
129 assertEquals( "Known Consumer IDs.size " + knownConsumers, expectedKnownIds.length, knownConsumers.size( ) );
131 for ( String expectedId : expectedKnownIds )
133 assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
138 public void testGetSelectedInvalidIds( )
141 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers( );
143 String expectedInvalidIds[] = new String[]{ "update-db-bad-content" };
145 List<String> invalidConsumers = consumerutil.getSelectedInvalidConsumerIds( );
146 assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
147 assertEquals( "Invalid Consumer IDs.size", expectedInvalidIds.length, invalidConsumers.size( ) );
149 for ( String expectedId : expectedInvalidIds )
151 assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
156 public void testGetSelectedKnownConsumerMap( )
159 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers( );
161 String expectedSelectedKnownIds[] =
162 new String[]{ "create-missing-checksums", "validate-checksum", "index-content", "auto-remove",
165 Map<String, KnownRepositoryContentConsumer> knownConsumerMap = consumerutil.getSelectedKnownConsumersMap( );
166 assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
167 assertEquals( "Known Consumer Map.size but " + knownConsumerMap, expectedSelectedKnownIds.length,
168 knownConsumerMap.size( ) );
170 for ( String expectedId : expectedSelectedKnownIds )
172 KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
173 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
174 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId( ) );
179 public void testGetSelectedInvalidConsumerMap( )
182 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers( );
184 String expectedSelectedInvalidIds[] = new String[]{ "update-db-bad-content" };
186 Map<String, InvalidRepositoryContentConsumer> invalidConsumerMap =
187 consumerutil.getSelectedInvalidConsumersMap( );
188 assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
189 assertEquals( "Invalid Consumer Map.size", expectedSelectedInvalidIds.length, invalidConsumerMap.size( ) );
191 for ( String expectedId : expectedSelectedInvalidIds )
193 InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
194 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
195 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId( ) );
200 public void testGetAvailableKnownList( )
203 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers( );
205 String expectedKnownIds[] =
206 new String[]{ "update-db-artifact", "create-missing-checksums", "update-db-repository-metadata",
207 "validate-checksum", "index-content", "auto-remove", "auto-rename", "available-but-unselected" };
209 List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers( );
210 assertNotNull( "known consumers should not be null.", knownConsumers );
211 assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size( ) );
213 List<String> expectedIds = Arrays.asList( expectedKnownIds );
214 for ( KnownRepositoryContentConsumer consumer : knownConsumers )
216 assertTrue( "Consumer [" + consumer.getId( ) + "] returned by .getAvailableKnownConsumers() is unexpected.",
217 expectedIds.contains( consumer.getId( ) ) );
222 public void testGetAvailableInvalidList( )
225 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers( );
227 String expectedInvalidIds[] = new String[]{ "update-db-bad-content", "move-to-trash-then-notify" };
229 List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers( );
230 assertNotNull( "invalid consumers should not be null.", invalidConsumers );
231 assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size( ) );
233 List<String> expectedIds = Arrays.asList( expectedInvalidIds );
234 for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
237 "Consumer [" + consumer.getId( ) + "] returned by .getAvailableInvalidConsumers() is unexpected.",
238 expectedIds.contains( consumer.getId( ) ) );
243 public void testExecution( )
246 MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
247 RepositoryContentConsumers consumers = lookupRepositoryConsumers( );
248 KnownRepositoryContentConsumer selectedKnownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock( );
249 KnownRepositoryContentConsumer unselectedKnownConsumer =
250 (KnownRepositoryContentConsumer) MockControl.createNiceControl(
251 KnownRepositoryContentConsumer.class ).getMock( );
253 consumers.setApplicationContext(
254 new MockApplicationContext( Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ), null ) );
256 consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
258 MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
259 InvalidRepositoryContentConsumer selectedInvalidConsumer =
260 (InvalidRepositoryContentConsumer) invalidControl.getMock( );
261 InvalidRepositoryContentConsumer unselectedInvalidConsumer =
262 (InvalidRepositoryContentConsumer) MockControl.createControl(
263 InvalidRepositoryContentConsumer.class ).getMock( );
265 consumers.setApplicationContext(
266 new MockApplicationContext( null, Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer ) ) );
268 consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
270 ManagedRepository repo = createRepository( "id", "name", new File( "target/test-repo" ) );
271 File testFile = new File( "target/test-repo/path/to/test-file.txt" );
273 Date startTime = new Date( System.currentTimeMillis( ) );
274 startTime.setTime( 12345678 );
276 selectedKnownConsumer.beginScan( repo, startTime, false );
277 selectedKnownConsumer.getExcludes( );
278 knownControl.setReturnValue( Collections.EMPTY_LIST );
279 selectedKnownConsumer.getIncludes( );
280 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
281 selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ), false );
282 // knownConsumer.completeScan();
283 knownControl.replay( );
285 selectedInvalidConsumer.beginScan( repo, startTime, false );
286 // invalidConsumer.completeScan();
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 selectedKnownConsumer.getExcludes( );
301 knownControl.setReturnValue( Collections.EMPTY_LIST );
302 selectedKnownConsumer.getIncludes( );
303 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
304 // knownConsumer.completeScan();
305 knownControl.replay( );
307 selectedInvalidConsumer.beginScan( repo, startTime, false );
308 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.xml" ), false );
309 selectedInvalidConsumer.getId( );
310 invalidControl.setReturnValue( "invalid" );
311 // invalidConsumer.completeScan();
312 invalidControl.replay( );
314 consumers.executeConsumers( repo, notIncludedTestFile, true );
316 knownControl.verify( );
317 invalidControl.verify( );
319 knownControl.reset( );
320 invalidControl.reset( );
322 File excludedTestFile = new File( "target/test-repo/path/to/test-file.txt" );
324 selectedKnownConsumer.beginScan( repo, startTime, false );
325 selectedKnownConsumer.getExcludes( );
326 knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
327 // knownConsumer.completeScan();
328 knownControl.replay( );
330 selectedInvalidConsumer.beginScan( repo, startTime, false );
331 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.txt" ), false );
332 selectedInvalidConsumer.getId( );
333 invalidControl.setReturnValue( "invalid" );
334 // invalidConsumer.completeScan();
335 invalidControl.replay( );
337 consumers.executeConsumers( repo, excludedTestFile, true );
339 knownControl.verify( );
340 invalidControl.verify( );
344 * Create an OS specific version of the filepath.
345 * Provide path in unix "/" format.
347 private String _OS( String path )
349 if ( SystemUtils.IS_OS_WINDOWS )
351 return path.replace( '/', '\\' );
356 private static Map convertToMap( List objects )
358 HashMap map = new HashMap( );
359 for ( Object o : objects )
366 public class MockApplicationContext
367 implements ApplicationContext
369 private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
371 private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
373 public MockApplicationContext( List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer,
374 List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers )
376 this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
377 this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
380 public String getApplicationName()
385 public AutowireCapableBeanFactory getAutowireCapableBeanFactory( )
386 throws IllegalStateException
388 throw new UnsupportedOperationException( "Not supported yet." );
391 public String getDisplayName( )
393 throw new UnsupportedOperationException( "Not supported yet." );
396 public String getId( )
398 throw new UnsupportedOperationException( "Not supported yet." );
401 public ApplicationContext getParent( )
403 throw new UnsupportedOperationException( "Not supported yet." );
406 public long getStartupDate( )
408 throw new UnsupportedOperationException( "Not supported yet." );
411 public boolean containsBeanDefinition( String beanName )
413 throw new UnsupportedOperationException( "Not supported yet." );
416 public int getBeanDefinitionCount( )
418 throw new UnsupportedOperationException( "Not supported yet." );
421 public String[] getBeanDefinitionNames( )
423 throw new UnsupportedOperationException( "Not supported yet." );
426 public String[] getBeanNamesForType( Class type )
428 throw new UnsupportedOperationException( "Not supported yet." );
431 public String[] getBeanNamesForType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
433 throw new UnsupportedOperationException( "Not supported yet." );
436 public Map getBeansOfType( Class type )
437 throws BeansException
439 if ( type == KnownRepositoryContentConsumer.class )
441 return convertToMap( knownRepositoryContentConsumer );
443 if ( type == InvalidRepositoryContentConsumer.class )
445 return convertToMap( invalidRepositoryContentConsumers );
447 throw new UnsupportedOperationException( "Should not have been called" );
450 public Map getBeansOfType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
451 throws BeansException
453 throw new UnsupportedOperationException( "Not supported yet." );
456 public boolean containsBean( String name )
458 throw new UnsupportedOperationException( "Not supported yet." );
461 public String[] getAliases( String name )
463 throw new UnsupportedOperationException( "Not supported yet." );
466 public Object getBean( String name )
467 throws BeansException
469 throw new UnsupportedOperationException( "Not supported yet." );
472 public Object getBean( String name, Class requiredType )
473 throws BeansException
475 throw new UnsupportedOperationException( "Not supported yet." );
478 public Object getBean( String name, Object[] args )
479 throws BeansException
481 throw new UnsupportedOperationException( "Not supported yet." );
484 public Class getType( String name )
485 throws NoSuchBeanDefinitionException
487 throw new UnsupportedOperationException( "Not supported yet." );
490 public boolean isPrototype( String name )
491 throws NoSuchBeanDefinitionException
493 throw new UnsupportedOperationException( "Not supported yet." );
496 public boolean isSingleton( String name )
497 throws NoSuchBeanDefinitionException
499 throw new UnsupportedOperationException( "Not supported yet." );
502 public boolean isTypeMatch( String name, Class targetType )
503 throws NoSuchBeanDefinitionException
505 throw new UnsupportedOperationException( "Not supported yet." );
508 public boolean containsLocalBean( String name )
510 throw new UnsupportedOperationException( "Not supported yet." );
513 public BeanFactory getParentBeanFactory( )
515 throw new UnsupportedOperationException( "Not supported yet." );
518 public String getMessage( String code, Object[] args, String defaultMessage, Locale locale )
520 throw new UnsupportedOperationException( "Not supported yet." );
523 public String getMessage( String code, Object[] args, Locale locale )
524 throws NoSuchMessageException
526 throw new UnsupportedOperationException( "Not supported yet." );
529 public String getMessage( MessageSourceResolvable resolvable, Locale locale )
530 throws NoSuchMessageException
532 throw new UnsupportedOperationException( "Not supported yet." );
535 public void publishEvent( ApplicationEvent event )
537 throw new UnsupportedOperationException( "Not supported yet." );
540 public Resource[] getResources( String locationPattern )
543 throw new UnsupportedOperationException( "Not supported yet." );
546 public ClassLoader getClassLoader( )
548 throw new UnsupportedOperationException( "Not supported yet." );
551 public Resource getResource( String location )
553 throw new UnsupportedOperationException( "Not supported yet." );
556 public <T> T getBean( Class<T> tClass )
557 throws BeansException
559 throw new UnsupportedOperationException( "Not supported yet." );
562 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
563 throws BeansException
565 throw new UnsupportedOperationException( "Not supported yet." );
568 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
570 throw new UnsupportedOperationException( "Not supported yet." );
573 public Environment getEnvironment()