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.commons.lang.SystemUtils;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
27 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
28 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
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.io.Resource;
41 import org.springframework.test.context.ContextConfiguration;
42 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
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;
57 * RepositoryContentConsumersTest
61 @RunWith( SpringJUnit4ClassRunner.class )
62 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
63 public class RepositoryContentConsumersTest
68 ApplicationContext applicationContext;
70 protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
72 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
75 repo.setLocation( location.getAbsolutePath() );
79 protected RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String url )
81 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
88 private RepositoryContentConsumers lookupRepositoryConsumers()
92 ArchivaConfiguration configuration =
93 applicationContext.getBean( "archivaConfiguration#test-conf", ArchivaConfiguration.class );
95 RepositoryContentConsumers consumerUtilStub = new RepositoryContentConsumersStub( configuration );
97 RepositoryContentConsumers consumerUtil =
98 (RepositoryContentConsumers) applicationContext.getBean( "repositoryContentConsumers#test",
99 RepositoryContentConsumers.class );
100 ApplicationContext context = new MockApplicationContext( consumerUtil.getAvailableKnownConsumers(),
101 consumerUtil.getAvailableInvalidConsumers() );
103 consumerUtilStub.setApplicationContext( context );
104 consumerUtilStub.setSelectedInvalidConsumers( consumerUtil.getSelectedInvalidConsumers() );
105 consumerUtilStub.setSelectedKnownConsumers( consumerUtil.getSelectedKnownConsumers() );
106 consumerUtilStub.setArchivaConfiguration( configuration );
108 assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtilStub );
110 return consumerUtilStub;
114 public void testGetSelectedKnownIds()
117 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
119 String expectedKnownIds[] =
120 new String[]{ "update-db-artifact", "create-missing-checksums", "update-db-repository-metadata",
121 "validate-checksum", "validate-signature", "index-content", "auto-remove", "auto-rename" };
122 //update-db-artifact, create-missing-checksums, update-db-repository-metadata,
123 //validate-checksum, validate-signature, index-content, auto-remove, auto-rename,
125 List<String> knownConsumers = consumerutil.getSelectedKnownConsumerIds();
126 assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
127 assertEquals( "Known Consumer IDs.size " + knownConsumers, expectedKnownIds.length, knownConsumers.size() );
129 for ( String expectedId : expectedKnownIds )
131 assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
136 public void testGetSelectedInvalidIds()
139 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
141 String expectedInvalidIds[] = new String[]{ "update-db-bad-content" };
143 List<String> invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
144 assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
145 assertEquals( "Invalid Consumer IDs.size", expectedInvalidIds.length, invalidConsumers.size() );
147 for ( String expectedId : expectedInvalidIds )
149 assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
154 public void testGetSelectedKnownConsumerMap()
157 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
159 String expectedSelectedKnownIds[] =
160 new String[]{ "update-db-artifact", "create-missing-checksums", "update-db-repository-metadata",
161 "validate-checksum", "index-content", "auto-remove", "auto-rename" };
163 Map<String, KnownRepositoryContentConsumer> knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
164 assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
165 assertEquals( "Known Consumer Map.size", expectedSelectedKnownIds.length, knownConsumerMap.size() );
167 for ( String expectedId : expectedSelectedKnownIds )
169 KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
170 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
171 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
176 public void testGetSelectedInvalidConsumerMap()
179 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
181 String expectedSelectedInvalidIds[] = new String[]{ "update-db-bad-content" };
183 Map<String, InvalidRepositoryContentConsumer> invalidConsumerMap =
184 consumerutil.getSelectedInvalidConsumersMap();
185 assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
186 assertEquals( "Invalid Consumer Map.size", expectedSelectedInvalidIds.length, invalidConsumerMap.size() );
188 for ( String expectedId : expectedSelectedInvalidIds )
190 InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
191 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
192 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
197 public void testGetAvailableKnownList()
200 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
202 String expectedKnownIds[] =
203 new String[]{ "update-db-artifact", "create-missing-checksums", "update-db-repository-metadata",
204 "validate-checksum", "index-content", "auto-remove", "auto-rename", "available-but-unselected" };
206 List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers();
207 assertNotNull( "known consumers should not be null.", knownConsumers );
208 assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size() );
210 List<String> expectedIds = Arrays.asList( expectedKnownIds );
211 for ( KnownRepositoryContentConsumer consumer : knownConsumers )
213 assertTrue( "Consumer [" + consumer.getId() + "] returned by .getAvailableKnownConsumers() is unexpected.",
214 expectedIds.contains( consumer.getId() ) );
219 public void testGetAvailableInvalidList()
222 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
224 String expectedInvalidIds[] = new String[]{ "update-db-bad-content", "move-to-trash-then-notify" };
226 List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers();
227 assertNotNull( "invalid consumers should not be null.", invalidConsumers );
228 assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size() );
230 List<String> expectedIds = Arrays.asList( expectedInvalidIds );
231 for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
234 "Consumer [" + consumer.getId() + "] returned by .getAvailableInvalidConsumers() is unexpected.",
235 expectedIds.contains( consumer.getId() ) );
240 public void testExecution()
243 MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
244 RepositoryContentConsumers consumers = lookupRepositoryConsumers();
245 KnownRepositoryContentConsumer selectedKnownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
246 KnownRepositoryContentConsumer unselectedKnownConsumer =
247 (KnownRepositoryContentConsumer) MockControl.createNiceControl(
248 KnownRepositoryContentConsumer.class ).getMock();
250 consumers.setApplicationContext(
251 new MockApplicationContext( Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ), null ) );
253 consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
255 MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
256 InvalidRepositoryContentConsumer selectedInvalidConsumer =
257 (InvalidRepositoryContentConsumer) invalidControl.getMock();
258 InvalidRepositoryContentConsumer unselectedInvalidConsumer =
259 (InvalidRepositoryContentConsumer) MockControl.createControl(
260 InvalidRepositoryContentConsumer.class ).getMock();
262 consumers.setApplicationContext(
263 new MockApplicationContext( null, Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer ) ) );
265 consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
267 ManagedRepositoryConfiguration repo = createRepository( "id", "name", new File( "target/test-repo" ) );
268 File testFile = new File( "target/test-repo/path/to/test-file.txt" );
270 Date startTime = new Date( System.currentTimeMillis() );
271 startTime.setTime( 12345678 );
273 selectedKnownConsumer.beginScan( repo, startTime, false );
274 selectedKnownConsumer.getExcludes();
275 knownControl.setReturnValue( Collections.EMPTY_LIST );
276 selectedKnownConsumer.getIncludes();
277 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
278 selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ), false );
279 // knownConsumer.completeScan();
280 knownControl.replay();
282 selectedInvalidConsumer.beginScan( repo, startTime, false );
283 // invalidConsumer.completeScan();
284 invalidControl.replay();
286 consumers.executeConsumers( repo, testFile, true );
288 knownControl.verify();
289 invalidControl.verify();
291 knownControl.reset();
292 invalidControl.reset();
294 File notIncludedTestFile = new File( "target/test-repo/path/to/test-file.xml" );
296 selectedKnownConsumer.beginScan( repo, startTime, false );
297 selectedKnownConsumer.getExcludes();
298 knownControl.setReturnValue( Collections.EMPTY_LIST );
299 selectedKnownConsumer.getIncludes();
300 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
301 // knownConsumer.completeScan();
302 knownControl.replay();
304 selectedInvalidConsumer.beginScan( repo, startTime, false );
305 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.xml" ), false );
306 selectedInvalidConsumer.getId();
307 invalidControl.setReturnValue( "invalid" );
308 // invalidConsumer.completeScan();
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 selectedKnownConsumer.getExcludes();
323 knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
324 // knownConsumer.completeScan();
325 knownControl.replay();
327 selectedInvalidConsumer.beginScan( repo, startTime, false );
328 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.txt" ), false );
329 selectedInvalidConsumer.getId();
330 invalidControl.setReturnValue( "invalid" );
331 // invalidConsumer.completeScan();
332 invalidControl.replay();
334 consumers.executeConsumers( repo, excludedTestFile, true );
336 knownControl.verify();
337 invalidControl.verify();
341 * Create an OS specific version of the filepath.
342 * Provide path in unix "/" format.
344 private String _OS( String path )
346 if ( SystemUtils.IS_OS_WINDOWS )
348 return path.replace( '/', '\\' );
353 private static Map convertToMap( List objects )
355 HashMap map = new HashMap();
356 for ( Object o : objects )
363 public class MockApplicationContext
364 implements ApplicationContext
366 private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
368 private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
370 public MockApplicationContext( List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer,
371 List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers )
373 this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
374 this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
377 public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
378 throws IllegalStateException
380 throw new UnsupportedOperationException( "Not supported yet." );
383 public String getDisplayName()
385 throw new UnsupportedOperationException( "Not supported yet." );
388 public String getId()
390 throw new UnsupportedOperationException( "Not supported yet." );
393 public ApplicationContext getParent()
395 throw new UnsupportedOperationException( "Not supported yet." );
398 public long getStartupDate()
400 throw new UnsupportedOperationException( "Not supported yet." );
403 public boolean containsBeanDefinition( String beanName )
405 throw new UnsupportedOperationException( "Not supported yet." );
408 public int getBeanDefinitionCount()
410 throw new UnsupportedOperationException( "Not supported yet." );
413 public String[] getBeanDefinitionNames()
415 throw new UnsupportedOperationException( "Not supported yet." );
418 public String[] getBeanNamesForType( Class type )
420 throw new UnsupportedOperationException( "Not supported yet." );
423 public String[] getBeanNamesForType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
425 throw new UnsupportedOperationException( "Not supported yet." );
428 public Map getBeansOfType( Class type )
429 throws BeansException
431 if ( type == KnownRepositoryContentConsumer.class )
433 return convertToMap( knownRepositoryContentConsumer );
435 if ( type == InvalidRepositoryContentConsumer.class )
437 return convertToMap( invalidRepositoryContentConsumers );
439 throw new UnsupportedOperationException( "Should not have been called" );
442 public Map getBeansOfType( Class type, boolean includeNonSingletons, boolean allowEagerInit )
443 throws BeansException
445 throw new UnsupportedOperationException( "Not supported yet." );
448 public boolean containsBean( String name )
450 throw new UnsupportedOperationException( "Not supported yet." );
453 public String[] getAliases( String name )
455 throw new UnsupportedOperationException( "Not supported yet." );
458 public Object getBean( String name )
459 throws BeansException
461 throw new UnsupportedOperationException( "Not supported yet." );
464 public Object getBean( String name, Class requiredType )
465 throws BeansException
467 throw new UnsupportedOperationException( "Not supported yet." );
470 public Object getBean( String name, Object[] args )
471 throws BeansException
473 throw new UnsupportedOperationException( "Not supported yet." );
476 public Class getType( String name )
477 throws NoSuchBeanDefinitionException
479 throw new UnsupportedOperationException( "Not supported yet." );
482 public boolean isPrototype( String name )
483 throws NoSuchBeanDefinitionException
485 throw new UnsupportedOperationException( "Not supported yet." );
488 public boolean isSingleton( String name )
489 throws NoSuchBeanDefinitionException
491 throw new UnsupportedOperationException( "Not supported yet." );
494 public boolean isTypeMatch( String name, Class targetType )
495 throws NoSuchBeanDefinitionException
497 throw new UnsupportedOperationException( "Not supported yet." );
500 public boolean containsLocalBean( String name )
502 throw new UnsupportedOperationException( "Not supported yet." );
505 public BeanFactory getParentBeanFactory()
507 throw new UnsupportedOperationException( "Not supported yet." );
510 public String getMessage( String code, Object[] args, String defaultMessage, Locale locale )
512 throw new UnsupportedOperationException( "Not supported yet." );
515 public String getMessage( String code, Object[] args, Locale locale )
516 throws NoSuchMessageException
518 throw new UnsupportedOperationException( "Not supported yet." );
521 public String getMessage( MessageSourceResolvable resolvable, Locale locale )
522 throws NoSuchMessageException
524 throw new UnsupportedOperationException( "Not supported yet." );
527 public void publishEvent( ApplicationEvent event )
529 throw new UnsupportedOperationException( "Not supported yet." );
532 public Resource[] getResources( String locationPattern )
535 throw new UnsupportedOperationException( "Not supported yet." );
538 public ClassLoader getClassLoader()
540 throw new UnsupportedOperationException( "Not supported yet." );
543 public Resource getResource( String location )
545 throw new UnsupportedOperationException( "Not supported yet." );
548 public <T> T getBean( Class<T> tClass )
549 throws BeansException
551 throw new UnsupportedOperationException( "Not supported yet." );
554 public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
555 throws BeansException
557 throw new UnsupportedOperationException( "Not supported yet." );
560 public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
562 throw new UnsupportedOperationException( "Not supported yet." );