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;
47 import java.io.IOException;
48 import java.lang.annotation.Annotation;
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.util.Arrays;
52 import java.util.Collections;
53 import java.util.Date;
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.Locale;
59 import static org.easymock.EasyMock.*;
62 * RepositoryContentConsumersTest
64 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
65 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
66 public class RepositoryContentConsumersTest
71 ApplicationContext applicationContext;
73 protected ManagedRepository createRepository( String id, String name, Path location )
75 ManagedRepository repo = new ManagedRepository();
78 repo.setLocation( location.toAbsolutePath().toString() );
82 protected RemoteRepository createRemoteRepository( String id, String name, String url )
84 RemoteRepository repo = new RemoteRepository();
91 private RepositoryContentConsumers lookupRepositoryConsumers()
95 ArchivaConfiguration configuration =
96 applicationContext.getBean( "archivaConfiguration#test-conf", ArchivaConfiguration.class );
98 ArchivaAdministrationStub administrationStub = new ArchivaAdministrationStub( configuration );
100 RepositoryContentConsumers consumerUtilStub = new RepositoryContentConsumersStub( administrationStub );
102 RepositoryContentConsumers consumerUtil =
103 applicationContext.getBean( "repositoryContentConsumers#test", RepositoryContentConsumers.class );
104 ApplicationContext context = new MockApplicationContext( consumerUtil.getAvailableKnownConsumers(), //
105 consumerUtil.getAvailableInvalidConsumers() );
107 consumerUtilStub.setApplicationContext( context );
108 consumerUtilStub.setSelectedInvalidConsumers( consumerUtil.getSelectedInvalidConsumers() );
109 consumerUtilStub.setSelectedKnownConsumers( consumerUtil.getSelectedKnownConsumers() );
110 consumerUtilStub.setArchivaAdministration( administrationStub );
112 assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtilStub );
114 return consumerUtilStub;
118 public void testGetSelectedKnownIds()
121 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
123 String expectedKnownIds[] =
124 new String[]{ "create-missing-checksums", "validate-checksum", "validate-signature", "index-content",
125 "auto-remove", "auto-rename", "create-archiva-metadata", "duplicate-artifacts" };
126 //update-db-artifact, create-missing-checksums, update-db-repository-metadata,
127 //validate-checksum, validate-signature, index-content, auto-remove, auto-rename,
129 List<String> knownConsumers = consumerutil.getSelectedKnownConsumerIds();
130 assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
131 assertEquals( "Known Consumer IDs.size " + knownConsumers, expectedKnownIds.length, knownConsumers.size() );
133 for ( String expectedId : expectedKnownIds )
135 assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
140 public void testGetSelectedInvalidIds()
143 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
145 String expectedInvalidIds[] = new String[]{ "update-db-bad-content" };
147 List<String> invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
148 assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
149 assertEquals( "Invalid Consumer IDs.size", expectedInvalidIds.length, invalidConsumers.size() );
151 for ( String expectedId : expectedInvalidIds )
153 assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
158 public void testGetSelectedKnownConsumerMap()
161 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
163 String expectedSelectedKnownIds[] =
164 new String[]{ "create-missing-checksums", "validate-checksum", "index-content", "auto-remove",
167 Map<String, KnownRepositoryContentConsumer> knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
168 assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
169 assertEquals( "Known Consumer Map.size but " + knownConsumerMap, expectedSelectedKnownIds.length,
170 knownConsumerMap.size() );
172 for ( String expectedId : expectedSelectedKnownIds )
174 KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
175 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
176 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
181 public void testGetSelectedInvalidConsumerMap()
184 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
186 String expectedSelectedInvalidIds[] = new String[]{ "update-db-bad-content" };
188 Map<String, InvalidRepositoryContentConsumer> invalidConsumerMap =
189 consumerutil.getSelectedInvalidConsumersMap();
190 assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
191 assertEquals( "Invalid Consumer Map.size", expectedSelectedInvalidIds.length, invalidConsumerMap.size() );
193 for ( String expectedId : expectedSelectedInvalidIds )
195 InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
196 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
197 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
202 public void testGetAvailableKnownList()
205 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
207 String expectedKnownIds[] =
208 new String[]{ "update-db-artifact", "create-missing-checksums", "update-db-repository-metadata",
209 "validate-checksum", "index-content", "auto-remove", "auto-rename", "available-but-unselected" };
211 List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers();
212 assertNotNull( "known consumers should not be null.", knownConsumers );
213 assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size() );
215 List<String> expectedIds = Arrays.asList( expectedKnownIds );
216 for ( KnownRepositoryContentConsumer consumer : knownConsumers )
218 assertTrue( "Consumer [" + consumer.getId() + "] returned by .getAvailableKnownConsumers() is unexpected.",
219 expectedIds.contains( consumer.getId() ) );
224 public void testGetAvailableInvalidList()
227 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
229 String expectedInvalidIds[] = new String[]{ "update-db-bad-content", "move-to-trash-then-notify" };
231 List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers();
232 assertNotNull( "invalid consumers should not be null.", invalidConsumers );
233 assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size() );
235 List<String> expectedIds = Arrays.asList( expectedInvalidIds );
236 for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
239 "Consumer [" + consumer.getId() + "] returned by .getAvailableInvalidConsumers() is unexpected.",
240 expectedIds.contains( consumer.getId() ) );
245 public void testExecution()
248 IMocksControl knownControl = createNiceControl();
250 RepositoryContentConsumers consumers = lookupRepositoryConsumers();
251 KnownRepositoryContentConsumer selectedKnownConsumer =
252 knownControl.createMock( KnownRepositoryContentConsumer.class );
254 KnownRepositoryContentConsumer unselectedKnownConsumer =
255 createNiceControl().createMock( KnownRepositoryContentConsumer.class );
257 consumers.setApplicationContext(
258 new MockApplicationContext( Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ), null ) );
260 consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
262 IMocksControl invalidControl = createControl();
264 InvalidRepositoryContentConsumer selectedInvalidConsumer =
265 invalidControl.createMock( InvalidRepositoryContentConsumer.class );
267 InvalidRepositoryContentConsumer unselectedInvalidConsumer =
268 createControl().createMock( InvalidRepositoryContentConsumer.class );
270 consumers.setApplicationContext(
271 new MockApplicationContext( null, Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer ) ) );
273 consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
275 ManagedRepository repo = createRepository( "id", "name", Paths.get( "target/test-repo" ) );
276 Path testFile = Paths.get( "target/test-repo/path/to/test-file.txt" );
278 Date startTime = new Date( System.currentTimeMillis() );
279 startTime.setTime( 12345678 );
281 selectedKnownConsumer.beginScan( repo, startTime, false );
282 expect( selectedKnownConsumer.getIncludes() ).andReturn( Collections.singletonList( "**/*.txt" ) );
283 selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ), false );
285 knownControl.replay();
287 selectedInvalidConsumer.beginScan( repo, startTime, false );
288 invalidControl.replay();
290 consumers.executeConsumers( repo, testFile, true );
292 knownControl.verify();
293 invalidControl.verify();
295 knownControl.reset();
296 invalidControl.reset();
298 Path notIncludedTestFile = Paths.get( "target/test-repo/path/to/test-file.xml" );
300 selectedKnownConsumer.beginScan( repo, startTime, false );
301 expect( selectedKnownConsumer.getExcludes() ).andReturn( Collections.<String>emptyList() );
303 expect( selectedKnownConsumer.getIncludes() ).andReturn( Collections.singletonList( "**/*.txt" ) );
305 knownControl.replay();
307 selectedInvalidConsumer.beginScan( repo, startTime, false );
308 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.xml" ), false );
309 expect( selectedInvalidConsumer.getId() ).andReturn( "invalid" );
310 invalidControl.replay();
312 consumers.executeConsumers( repo, notIncludedTestFile, true );
314 knownControl.verify();
315 invalidControl.verify();
317 knownControl.reset();
318 invalidControl.reset();
320 Path excludedTestFile = Paths.get( "target/test-repo/path/to/test-file.txt" );
322 selectedKnownConsumer.beginScan( repo, startTime, false );
323 expect( selectedKnownConsumer.getExcludes() ).andReturn( Collections.singletonList( "**/test-file.txt" ) );
324 knownControl.replay();
326 selectedInvalidConsumer.beginScan( repo, startTime, false );
327 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.txt" ), false );
328 expect( selectedInvalidConsumer.getId() ).andReturn( "invalid" );
329 invalidControl.replay();
331 consumers.executeConsumers( repo, excludedTestFile, true );
333 knownControl.verify();
334 invalidControl.verify();
338 * Create an OS specific version of the filepath.
339 * Provide path in unix "/" format.
341 private String _OS( String path )
343 if ( SystemUtils.IS_OS_WINDOWS )
345 return path.replace( '/', '\\' );
350 private static Map convertToMap( List objects )
352 HashMap map = new HashMap();
353 for ( Object o : objects )
360 public class MockApplicationContext
361 implements ApplicationContext
363 private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
365 private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
367 public MockApplicationContext( List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer,
368 List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers )
370 this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
371 this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
375 public String getApplicationName()
381 public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
382 throws IllegalStateException
384 throw new UnsupportedOperationException( "Not supported yet." );
388 public String getDisplayName()
390 throw new UnsupportedOperationException( "Not supported yet." );
394 public String getId()
396 throw new UnsupportedOperationException( "Not supported yet." );
400 public ApplicationContext getParent()
402 throw new UnsupportedOperationException( "Not supported yet." );
406 public long getStartupDate()
408 throw new UnsupportedOperationException( "Not supported yet." );
412 public boolean containsBeanDefinition( String beanName )
414 throw new UnsupportedOperationException( "Not supported yet." );
418 public int getBeanDefinitionCount()
420 throw new UnsupportedOperationException( "Not supported yet." );
424 public String[] getBeanDefinitionNames()
426 throw new UnsupportedOperationException( "Not supported yet." );
430 public String[] getBeanNamesForType( Class type )
432 throw new UnsupportedOperationException( "Not supported yet." );
436 public String[] getBeanNamesForType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
438 throw new UnsupportedOperationException( "Not supported yet." );
442 public <T> T getBean( Class<T> aClass, Object... objects )
443 throws BeansException
445 throw new UnsupportedOperationException( "Not supported yet." );
449 public Map getBeansOfType( Class type )
450 throws BeansException
452 if ( type == KnownRepositoryContentConsumer.class )
454 return convertToMap( knownRepositoryContentConsumer );
456 if ( type == InvalidRepositoryContentConsumer.class )
458 return convertToMap( invalidRepositoryContentConsumers );
460 throw new UnsupportedOperationException( "Should not have been called" );
464 public Map getBeansOfType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
465 throws BeansException
467 throw new UnsupportedOperationException( "Not supported yet." );
471 public boolean containsBean( String name )
473 throw new UnsupportedOperationException( "Not supported yet." );
477 public String[] getAliases( String name )
479 throw new UnsupportedOperationException( "Not supported yet." );
483 public Object getBean( String name )
484 throws BeansException
486 throw new UnsupportedOperationException( "Not supported yet." );
490 public Object getBean( String name, Class requiredType )
491 throws BeansException
493 throw new UnsupportedOperationException( "Not supported yet." );
497 public Object getBean( String name, Object[] args )
498 throws BeansException
500 throw new UnsupportedOperationException( "Not supported yet." );
504 public Class getType( String name )
505 throws NoSuchBeanDefinitionException
507 throw new UnsupportedOperationException( "Not supported yet." );
511 public boolean isPrototype( String name )
512 throws NoSuchBeanDefinitionException
514 throw new UnsupportedOperationException( "Not supported yet." );
518 public boolean isSingleton( String name )
519 throws NoSuchBeanDefinitionException
521 throw new UnsupportedOperationException( "Not supported yet." );
525 public boolean isTypeMatch( String name, Class targetType )
526 throws NoSuchBeanDefinitionException
528 throw new UnsupportedOperationException( "Not supported yet." );
532 public boolean containsLocalBean( String name )
534 throw new UnsupportedOperationException( "Not supported yet." );
538 public BeanFactory getParentBeanFactory()
540 throw new UnsupportedOperationException( "Not supported yet." );
544 public String getMessage( String code, Object[] args, String defaultMessage, Locale locale )
546 throw new UnsupportedOperationException( "Not supported yet." );
550 public String getMessage( String code, Object[] args, Locale locale )
551 throws NoSuchMessageException
553 throw new UnsupportedOperationException( "Not supported yet." );
557 public String getMessage( MessageSourceResolvable resolvable, Locale locale )
558 throws NoSuchMessageException
560 throw new UnsupportedOperationException( "Not supported yet." );
564 public void publishEvent( ApplicationEvent event )
566 throw new UnsupportedOperationException( "Not supported yet." );
570 public Resource[] getResources( String locationPattern )
573 throw new UnsupportedOperationException( "Not supported yet." );
577 public ClassLoader getClassLoader()
579 throw new UnsupportedOperationException( "Not supported yet." );
583 public Resource getResource( String location )
585 throw new UnsupportedOperationException( "Not supported yet." );
589 public <T> T getBean( Class<T> tClass )
590 throws BeansException
592 throw new UnsupportedOperationException( "Not supported yet." );
596 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
597 throws BeansException
599 throw new UnsupportedOperationException( "Not supported yet." );
603 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
605 throw new UnsupportedOperationException( "Not supported yet." );
609 public Environment getEnvironment()
615 public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
617 return new String[0];
621 public void publishEvent( Object o )
627 public String[] getBeanNamesForType( ResolvableType resolvableType )
629 return new String[0];
633 public boolean isTypeMatch( String s, ResolvableType resolvableType )
634 throws NoSuchBeanDefinitionException