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 org.apache.commons.lang.SystemUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
26 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
28 import org.easymock.MockControl;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Date;
34 import java.util.List;
38 * RepositoryContentConsumersTest
40 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
43 public class RepositoryContentConsumersTest
44 extends AbstractRepositoryLayerTestCase
46 private RepositoryContentConsumers lookupRepositoryConsumers()
49 RepositoryContentConsumers consumerUtilStub = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
51 ArchivaConfiguration archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.ROLE );
52 RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
55 consumerUtilStub.setAvailableKnownConsumers( consumerUtil.getAvailableKnownConsumers() );
56 consumerUtilStub.setAvailableInvalidConsumers( consumerUtil.getAvailableInvalidConsumers() );
57 consumerUtilStub.setSelectedInvalidConsumers( consumerUtil.getSelectedInvalidConsumers() );
58 consumerUtilStub.setSelectedKnownConsumers( consumerUtil.getSelectedKnownConsumers() );
59 consumerUtilStub.setArchivaConfiguration( archivaConfiguration );
61 assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtilStub );
63 return consumerUtilStub;
66 public void testGetSelectedKnownIds()
69 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
71 String expectedKnownIds[] = new String[] {
73 "create-missing-checksums",
74 "update-db-repository-metadata",
81 List<String> knownConsumers = consumerutil.getSelectedKnownConsumerIds();
82 assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
83 assertEquals( "Known Consumer IDs.size", expectedKnownIds.length, knownConsumers.size() );
85 for ( String expectedId : expectedKnownIds )
87 assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
91 public void testGetSelectedInvalidIds()
94 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
96 String expectedInvalidIds[] = new String[] { "update-db-bad-content" };
98 List<String> invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
99 assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
100 assertEquals( "Invalid Consumer IDs.size", expectedInvalidIds.length, invalidConsumers.size() );
102 for ( String expectedId : expectedInvalidIds )
104 assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
108 public void testGetSelectedKnownConsumerMap()
111 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
113 String expectedSelectedKnownIds[] = new String[] {
114 "update-db-artifact",
115 "create-missing-checksums",
116 "update-db-repository-metadata",
122 Map<String, KnownRepositoryContentConsumer> knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
123 assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
124 assertEquals( "Known Consumer Map.size", expectedSelectedKnownIds.length, knownConsumerMap.size() );
126 for ( String expectedId : expectedSelectedKnownIds )
128 KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
129 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
130 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
134 public void testGetSelectedInvalidConsumerMap()
137 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
139 String expectedSelectedInvalidIds[] = new String[] { "update-db-bad-content" };
141 Map<String, InvalidRepositoryContentConsumer> invalidConsumerMap = consumerutil
142 .getSelectedInvalidConsumersMap();
143 assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
144 assertEquals( "Invalid Consumer Map.size", expectedSelectedInvalidIds.length, invalidConsumerMap.size() );
146 for ( String expectedId : expectedSelectedInvalidIds )
148 InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
149 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
150 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
154 public void testGetAvailableKnownList()
157 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
159 String expectedKnownIds[] = new String[] {
160 "update-db-artifact",
161 "create-missing-checksums",
162 "update-db-repository-metadata",
167 "available-but-unselected" };
169 List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers();
170 assertNotNull( "known consumers should not be null.", knownConsumers );
171 assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size() );
173 List<String> expectedIds = Arrays.asList( expectedKnownIds );
174 for ( KnownRepositoryContentConsumer consumer : knownConsumers )
176 assertTrue( "Consumer [" + consumer.getId() + "] returned by .getAvailableKnownConsumers() is unexpected.",
177 expectedIds.contains( consumer.getId() ) );
181 public void testGetAvailableInvalidList()
184 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
186 String expectedInvalidIds[] = new String[] { "update-db-bad-content", "move-to-trash-then-notify" };
188 List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers();
189 assertNotNull( "invalid consumers should not be null.", invalidConsumers );
190 assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size() );
192 List<String> expectedIds = Arrays.asList( expectedInvalidIds );
193 for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
195 assertTrue( "Consumer [" + consumer.getId()
196 + "] returned by .getAvailableInvalidConsumers() is unexpected.", expectedIds.contains( consumer
201 public void testExecution()
204 MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
205 RepositoryContentConsumers consumers = lookupRepositoryConsumers();
206 KnownRepositoryContentConsumer selectedKnownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
207 KnownRepositoryContentConsumer unselectedKnownConsumer =
208 (KnownRepositoryContentConsumer) MockControl.createNiceControl(
209 KnownRepositoryContentConsumer.class ).getMock();
210 consumers.setAvailableKnownConsumers( Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ) );
211 consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
213 MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
214 InvalidRepositoryContentConsumer selectedInvalidConsumer =
215 (InvalidRepositoryContentConsumer) invalidControl.getMock();
216 InvalidRepositoryContentConsumer unselectedInvalidConsumer =
217 (InvalidRepositoryContentConsumer) MockControl.createControl(
218 InvalidRepositoryContentConsumer.class ).getMock();
219 consumers.setAvailableInvalidConsumers( Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer ) );
220 consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
222 ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
223 File testFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
225 Date startTime = new Date( System.currentTimeMillis() );
227 selectedKnownConsumer.beginScan( repo, startTime );
228 selectedKnownConsumer.getExcludes();
229 knownControl.setReturnValue( Collections.EMPTY_LIST );
230 selectedKnownConsumer.getIncludes();
231 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
232 selectedKnownConsumer.processFile( _OS( "path/to/test-file.txt" ) );
233 // knownConsumer.completeScan();
234 knownControl.replay();
236 selectedInvalidConsumer.beginScan( repo, startTime );
237 // invalidConsumer.completeScan();
238 invalidControl.replay();
240 consumers.setStartTime( startTime );
241 consumers.executeConsumers( repo, testFile );
243 knownControl.verify();
244 invalidControl.verify();
246 knownControl.reset();
247 invalidControl.reset();
249 File notIncludedTestFile = getTestFile( "target/test-repo/path/to/test-file.xml" );
251 selectedKnownConsumer.beginScan( repo, startTime );
252 selectedKnownConsumer.getExcludes();
253 knownControl.setReturnValue( Collections.EMPTY_LIST );
254 selectedKnownConsumer.getIncludes();
255 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
256 // knownConsumer.completeScan();
257 knownControl.replay();
259 selectedInvalidConsumer.beginScan( repo, startTime );
260 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.xml" ) );
261 selectedInvalidConsumer.getId();
262 invalidControl.setReturnValue( "invalid" );
263 // invalidConsumer.completeScan();
264 invalidControl.replay();
266 consumers.executeConsumers( repo, notIncludedTestFile );
268 knownControl.verify();
269 invalidControl.verify();
271 knownControl.reset();
272 invalidControl.reset();
274 File excludedTestFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
276 selectedKnownConsumer.beginScan( repo, startTime );
277 selectedKnownConsumer.getExcludes();
278 knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
279 // knownConsumer.completeScan();
280 knownControl.replay();
282 selectedInvalidConsumer.beginScan( repo, startTime );
283 selectedInvalidConsumer.processFile( _OS( "path/to/test-file.txt" ) );
284 selectedInvalidConsumer.getId();
285 invalidControl.setReturnValue( "invalid" );
286 // invalidConsumer.completeScan();
287 invalidControl.replay();
289 consumers.executeConsumers( repo, excludedTestFile );
291 knownControl.verify();
292 invalidControl.verify();
296 * Create an OS specific version of the filepath.
297 * Provide path in unix "/" format.
299 private String _OS( String path )
301 if ( SystemUtils.IS_OS_WINDOWS )
303 return path.replace( '/', '\\' );