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, true );
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, true );
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, true );
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 @SuppressWarnings("unchecked")
325 public class MockApplicationContext implements ApplicationContext
327 private Map convertToMap(List objects)
329 HashMap map = new HashMap();
330 for (Object o : objects)
337 private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
339 private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
341 public MockApplicationContext(List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer, List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers)
343 this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
344 this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
347 public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
348 throw new UnsupportedOperationException("Not supported yet.");
351 public String getDisplayName() {
352 throw new UnsupportedOperationException("Not supported yet.");
355 public String getId() {
356 throw new UnsupportedOperationException("Not supported yet.");
359 public ApplicationContext getParent() {
360 throw new UnsupportedOperationException("Not supported yet.");
363 public long getStartupDate() {
364 throw new UnsupportedOperationException("Not supported yet.");
367 public boolean containsBeanDefinition(String beanName) {
368 throw new UnsupportedOperationException("Not supported yet.");
371 public int getBeanDefinitionCount() {
372 throw new UnsupportedOperationException("Not supported yet.");
375 public String[] getBeanDefinitionNames() {
376 throw new UnsupportedOperationException("Not supported yet.");
379 public String[] getBeanNamesForType(Class type) {
380 throw new UnsupportedOperationException("Not supported yet.");
383 public String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit) {
384 throw new UnsupportedOperationException("Not supported yet.");
387 public Map getBeansOfType(Class type) throws BeansException {
388 if (type == KnownRepositoryContentConsumer.class)
390 return convertToMap(knownRepositoryContentConsumer);
392 if (type == InvalidRepositoryContentConsumer.class)
394 return convertToMap(invalidRepositoryContentConsumers);
396 throw new UnsupportedOperationException("Should not have been called");
399 public Map getBeansOfType(Class type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException {
400 throw new UnsupportedOperationException("Not supported yet.");
403 public boolean containsBean(String name) {
404 throw new UnsupportedOperationException("Not supported yet.");
407 public String[] getAliases(String name) {
408 throw new UnsupportedOperationException("Not supported yet.");
411 public Object getBean(String name) throws BeansException {
412 throw new UnsupportedOperationException("Not supported yet.");
415 public Object getBean(String name, Class requiredType) throws BeansException {
416 throw new UnsupportedOperationException("Not supported yet.");
419 public Object getBean(String name, Object[] args) throws BeansException {
420 throw new UnsupportedOperationException("Not supported yet.");
423 public Class getType(String name) throws NoSuchBeanDefinitionException {
424 throw new UnsupportedOperationException("Not supported yet.");
427 public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
428 throw new UnsupportedOperationException("Not supported yet.");
431 public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
432 throw new UnsupportedOperationException("Not supported yet.");
435 public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
436 throw new UnsupportedOperationException("Not supported yet.");
439 public boolean containsLocalBean(String name) {
440 throw new UnsupportedOperationException("Not supported yet.");
443 public BeanFactory getParentBeanFactory() {
444 throw new UnsupportedOperationException("Not supported yet.");
447 public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
448 throw new UnsupportedOperationException("Not supported yet.");
451 public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
452 throw new UnsupportedOperationException("Not supported yet.");
455 public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
456 throw new UnsupportedOperationException("Not supported yet.");
459 public void publishEvent(ApplicationEvent event) {
460 throw new UnsupportedOperationException("Not supported yet.");
463 public Resource[] getResources(String locationPattern) throws IOException {
464 throw new UnsupportedOperationException("Not supported yet.");
467 public ClassLoader getClassLoader() {
468 throw new UnsupportedOperationException("Not supported yet.");
471 public Resource getResource(String location) {
472 throw new UnsupportedOperationException("Not supported yet.");