1 package org.apache.maven.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 java.io.IOException;
23 import java.util.Locale;
24 import org.apache.commons.lang.SystemUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
28 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
30 import org.easymock.MockControl;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.Date;
36 import java.util.HashMap;
37 import java.util.List;
39 import org.springframework.beans.BeansException;
40 import org.springframework.beans.factory.BeanFactory;
41 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
42 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.context.ApplicationEvent;
45 import org.springframework.context.MessageSourceResolvable;
46 import org.springframework.context.NoSuchMessageException;
47 import org.springframework.core.io.Resource;
50 * RepositoryContentConsumersTest
54 public class RepositoryContentConsumersTest
55 extends AbstractRepositoryLayerTestCase
57 private RepositoryContentConsumers lookupRepositoryConsumers()
60 ArchivaConfiguration configuration = (ArchivaConfiguration)lookup(ArchivaConfiguration.class);
62 RepositoryContentConsumers consumerUtilStub = new RepositoryContentConsumersStub(configuration);
64 RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
66 ApplicationContext context = new MockApplicationContext(consumerUtil.getAvailableKnownConsumers(), consumerUtil.getAvailableInvalidConsumers());
68 consumerUtilStub.setApplicationContext(context);
69 consumerUtilStub.setSelectedInvalidConsumers( consumerUtil.getSelectedInvalidConsumers() );
70 consumerUtilStub.setSelectedKnownConsumers( consumerUtil.getSelectedKnownConsumers() );
71 consumerUtilStub.setArchivaConfiguration( configuration );
73 assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtilStub );
75 return consumerUtilStub;
78 public void testGetSelectedKnownIds()
81 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
83 String expectedKnownIds[] = new String[] {
85 "create-missing-checksums",
86 "update-db-repository-metadata",
93 List<String> knownConsumers = consumerutil.getSelectedKnownConsumerIds();
94 assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
95 assertEquals( "Known Consumer IDs.size", expectedKnownIds.length, knownConsumers.size() );
97 for ( String expectedId : expectedKnownIds )
99 assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
103 public void testGetSelectedInvalidIds()
106 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
108 String expectedInvalidIds[] = new String[] { "update-db-bad-content" };
110 List<String> invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
111 assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
112 assertEquals( "Invalid Consumer IDs.size", expectedInvalidIds.length, invalidConsumers.size() );
114 for ( String expectedId : expectedInvalidIds )
116 assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
120 public void testGetSelectedKnownConsumerMap()
123 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
125 String expectedSelectedKnownIds[] = new String[] {
126 "update-db-artifact",
127 "create-missing-checksums",
128 "update-db-repository-metadata",
134 Map<String, KnownRepositoryContentConsumer> knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
135 assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
136 assertEquals( "Known Consumer Map.size", expectedSelectedKnownIds.length, knownConsumerMap.size() );
138 for ( String expectedId : expectedSelectedKnownIds )
140 KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
141 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
142 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
146 public void testGetSelectedInvalidConsumerMap()
149 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
151 String expectedSelectedInvalidIds[] = new String[] { "update-db-bad-content" };
153 Map<String, InvalidRepositoryContentConsumer> invalidConsumerMap = consumerutil
154 .getSelectedInvalidConsumersMap();
155 assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
156 assertEquals( "Invalid Consumer Map.size", expectedSelectedInvalidIds.length, invalidConsumerMap.size() );
158 for ( String expectedId : expectedSelectedInvalidIds )
160 InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
161 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
162 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
166 public void testGetAvailableKnownList()
169 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
171 String expectedKnownIds[] = new String[] {
172 "update-db-artifact",
173 "create-missing-checksums",
174 "update-db-repository-metadata",
179 "available-but-unselected" };
181 List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers();
182 assertNotNull( "known consumers should not be null.", knownConsumers );
183 assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size() );
185 List<String> expectedIds = Arrays.asList( expectedKnownIds );
186 for ( KnownRepositoryContentConsumer consumer : knownConsumers )
188 assertTrue( "Consumer [" + consumer.getId() + "] returned by .getAvailableKnownConsumers() is unexpected.",
189 expectedIds.contains( consumer.getId() ) );
193 public void testGetAvailableInvalidList()
196 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
198 String expectedInvalidIds[] = new String[] { "update-db-bad-content", "move-to-trash-then-notify" };
200 List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers();
201 assertNotNull( "invalid consumers should not be null.", invalidConsumers );
202 assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size() );
204 List<String> expectedIds = Arrays.asList( expectedInvalidIds );
205 for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
207 assertTrue( "Consumer [" + consumer.getId()
208 + "] returned by .getAvailableInvalidConsumers() is unexpected.", expectedIds.contains( consumer
213 public void testExecution()
216 MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
217 RepositoryContentConsumers consumers = lookupRepositoryConsumers();
218 KnownRepositoryContentConsumer selectedKnownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
219 KnownRepositoryContentConsumer unselectedKnownConsumer =
220 (KnownRepositoryContentConsumer) MockControl.createNiceControl(
221 KnownRepositoryContentConsumer.class ).getMock();
223 consumers.setApplicationContext(new MockApplicationContext(Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ), null));
225 consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
227 MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
228 InvalidRepositoryContentConsumer selectedInvalidConsumer =
229 (InvalidRepositoryContentConsumer) invalidControl.getMock();
230 InvalidRepositoryContentConsumer unselectedInvalidConsumer =
231 (InvalidRepositoryContentConsumer) MockControl.createControl(
232 InvalidRepositoryContentConsumer.class ).getMock();
234 consumers.setApplicationContext( new MockApplicationContext(null, Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer )));
236 consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
238 ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
239 File testFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
241 Date startTime = new Date( System.currentTimeMillis() );
242 startTime.setTime( 12345678 );
244 selectedKnownConsumer.beginScan( repo, startTime );
245 selectedKnownConsumer.getExcludes();
246 knownControl.setReturnValue( Collections.EMPTY_LIST );
247 selectedKnownConsumer.getIncludes();
248 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
249 selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ) );
250 // knownConsumer.completeScan();
251 knownControl.replay();
253 selectedInvalidConsumer.beginScan( repo, startTime );
254 // invalidConsumer.completeScan();
255 invalidControl.replay();
257 consumers.executeConsumers( repo, testFile );
259 knownControl.verify();
260 invalidControl.verify();
262 knownControl.reset();
263 invalidControl.reset();
265 File notIncludedTestFile = getTestFile( "target/test-repo/path/to/test-file.xml" );
267 selectedKnownConsumer.beginScan( repo, startTime );
268 selectedKnownConsumer.getExcludes();
269 knownControl.setReturnValue( Collections.EMPTY_LIST );
270 selectedKnownConsumer.getIncludes();
271 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
272 // knownConsumer.completeScan();
273 knownControl.replay();
275 selectedInvalidConsumer.beginScan( repo, startTime );
276 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.xml" ) );
277 selectedInvalidConsumer.getId();
278 invalidControl.setReturnValue( "invalid" );
279 // invalidConsumer.completeScan();
280 invalidControl.replay();
282 consumers.executeConsumers( repo, notIncludedTestFile );
284 knownControl.verify();
285 invalidControl.verify();
287 knownControl.reset();
288 invalidControl.reset();
290 File excludedTestFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
292 selectedKnownConsumer.beginScan( repo, startTime );
293 selectedKnownConsumer.getExcludes();
294 knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
295 // knownConsumer.completeScan();
296 knownControl.replay();
298 selectedInvalidConsumer.beginScan( repo, startTime );
299 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.txt" ) );
300 selectedInvalidConsumer.getId();
301 invalidControl.setReturnValue( "invalid" );
302 // invalidConsumer.completeScan();
303 invalidControl.replay();
305 consumers.executeConsumers( repo, excludedTestFile );
307 knownControl.verify();
308 invalidControl.verify();
312 * Create an OS specific version of the filepath.
313 * Provide path in unix "/" format.
315 private String _OS( String path )
317 if ( SystemUtils.IS_OS_WINDOWS )
319 return path.replace( '/', '\\' );
324 private static Map convertToMap(List objects)
326 HashMap map = new HashMap();
327 for (Object o : objects)
334 public class MockApplicationContext implements ApplicationContext
336 private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
338 private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
340 public MockApplicationContext(List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer, List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers)
342 this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
343 this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
346 public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
347 throw new UnsupportedOperationException("Not supported yet.");
350 public String getDisplayName() {
351 throw new UnsupportedOperationException("Not supported yet.");
354 public String getId() {
355 throw new UnsupportedOperationException("Not supported yet.");
358 public ApplicationContext getParent() {
359 throw new UnsupportedOperationException("Not supported yet.");
362 public long getStartupDate() {
363 throw new UnsupportedOperationException("Not supported yet.");
366 public boolean containsBeanDefinition(String beanName) {
367 throw new UnsupportedOperationException("Not supported yet.");
370 public int getBeanDefinitionCount() {
371 throw new UnsupportedOperationException("Not supported yet.");
374 public String[] getBeanDefinitionNames() {
375 throw new UnsupportedOperationException("Not supported yet.");
378 public String[] getBeanNamesForType(Class type) {
379 throw new UnsupportedOperationException("Not supported yet.");
382 public String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit) {
383 throw new UnsupportedOperationException("Not supported yet.");
386 public Map getBeansOfType(Class type) throws BeansException {
387 if (type == KnownRepositoryContentConsumer.class)
389 return convertToMap(knownRepositoryContentConsumer);
391 if (type == InvalidRepositoryContentConsumer.class)
393 return convertToMap(invalidRepositoryContentConsumers);
395 throw new UnsupportedOperationException("Should not have been called");
398 public Map getBeansOfType(Class type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException {
399 throw new UnsupportedOperationException("Not supported yet.");
402 public boolean containsBean(String name) {
403 throw new UnsupportedOperationException("Not supported yet.");
406 public String[] getAliases(String name) {
407 throw new UnsupportedOperationException("Not supported yet.");
410 public Object getBean(String name) throws BeansException {
411 throw new UnsupportedOperationException("Not supported yet.");
414 public Object getBean(String name, Class requiredType) throws BeansException {
415 throw new UnsupportedOperationException("Not supported yet.");
418 public Object getBean(String name, Object[] args) throws BeansException {
419 throw new UnsupportedOperationException("Not supported yet.");
422 public Class getType(String name) throws NoSuchBeanDefinitionException {
423 throw new UnsupportedOperationException("Not supported yet.");
426 public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
427 throw new UnsupportedOperationException("Not supported yet.");
430 public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
431 throw new UnsupportedOperationException("Not supported yet.");
434 public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
435 throw new UnsupportedOperationException("Not supported yet.");
438 public boolean containsLocalBean(String name) {
439 throw new UnsupportedOperationException("Not supported yet.");
442 public BeanFactory getParentBeanFactory() {
443 throw new UnsupportedOperationException("Not supported yet.");
446 public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
447 throw new UnsupportedOperationException("Not supported yet.");
450 public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
451 throw new UnsupportedOperationException("Not supported yet.");
454 public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
455 throw new UnsupportedOperationException("Not supported yet.");
458 public void publishEvent(ApplicationEvent event) {
459 throw new UnsupportedOperationException("Not supported yet.");
462 public Resource[] getResources(String locationPattern) throws IOException {
463 throw new UnsupportedOperationException("Not supported yet.");
466 public ClassLoader getClassLoader() {
467 throw new UnsupportedOperationException("Not supported yet.");
470 public Resource getResource(String location) {
471 throw new UnsupportedOperationException("Not supported yet.");