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.ManagedRepositoryConfiguration;
24 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
25 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
26 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
27 import org.easymock.MockControl;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
36 * RepositoryContentConsumersTest
38 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
41 public class RepositoryContentConsumersTest
42 extends AbstractRepositoryLayerTestCase
44 private RepositoryContentConsumers lookupRepositoryConsumers()
47 RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
49 assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtil );
53 public void testGetSelectedKnownIds()
56 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
58 String expectedKnownIds[] = new String[] {
60 "create-missing-checksums",
61 "update-db-repository-metadata",
68 List<String> knownConsumers = consumerutil.getSelectedKnownConsumerIds();
69 assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
70 assertEquals( "Known Consumer IDs.size", expectedKnownIds.length, knownConsumers.size() );
72 for ( String expectedId : expectedKnownIds )
74 assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
78 public void testGetSelectedInvalidIds()
81 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
83 String expectedInvalidIds[] = new String[] { "update-db-bad-content" };
85 List<String> invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
86 assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
87 assertEquals( "Invalid Consumer IDs.size", expectedInvalidIds.length, invalidConsumers.size() );
89 for ( String expectedId : expectedInvalidIds )
91 assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
95 public void testGetSelectedKnownConsumerMap()
98 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
100 String expectedSelectedKnownIds[] = new String[] {
101 "update-db-artifact",
102 "create-missing-checksums",
103 "update-db-repository-metadata",
109 Map<String, KnownRepositoryContentConsumer> knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
110 assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
111 assertEquals( "Known Consumer Map.size", expectedSelectedKnownIds.length, knownConsumerMap.size() );
113 for ( String expectedId : expectedSelectedKnownIds )
115 KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
116 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
117 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
121 public void testGetSelectedInvalidConsumerMap()
124 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
126 String expectedSelectedInvalidIds[] = new String[] { "update-db-bad-content" };
128 Map<String, InvalidRepositoryContentConsumer> invalidConsumerMap = consumerutil
129 .getSelectedInvalidConsumersMap();
130 assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
131 assertEquals( "Invalid Consumer Map.size", expectedSelectedInvalidIds.length, invalidConsumerMap.size() );
133 for ( String expectedId : expectedSelectedInvalidIds )
135 InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
136 assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
137 assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
141 public void testGetAvailableKnownList()
144 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
146 String expectedKnownIds[] = new String[] {
147 "update-db-artifact",
148 "create-missing-checksums",
149 "update-db-repository-metadata",
154 "available-but-unselected" };
156 List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers();
157 assertNotNull( "known consumers should not be null.", knownConsumers );
158 assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size() );
160 List<String> expectedIds = Arrays.asList( expectedKnownIds );
161 for ( KnownRepositoryContentConsumer consumer : knownConsumers )
163 assertTrue( "Consumer [" + consumer.getId() + "] returned by .getAvailableKnownConsumers() is unexpected.",
164 expectedIds.contains( consumer.getId() ) );
168 public void testGetAvailableInvalidList()
171 RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
173 String expectedInvalidIds[] = new String[] { "update-db-bad-content", "move-to-trash-then-notify" };
175 List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers();
176 assertNotNull( "invalid consumers should not be null.", invalidConsumers );
177 assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size() );
179 List<String> expectedIds = Arrays.asList( expectedInvalidIds );
180 for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
182 assertTrue( "Consumer [" + consumer.getId()
183 + "] returned by .getAvailableInvalidConsumers() is unexpected.", expectedIds.contains( consumer
188 public void testExecution()
191 MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
192 RepositoryContentConsumers consumers = lookupRepositoryConsumers();
193 KnownRepositoryContentConsumer knownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
194 consumers.setAvailableKnownConsumers( Collections.singletonList( knownConsumer ) );
196 MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
197 InvalidRepositoryContentConsumer invalidConsumer = (InvalidRepositoryContentConsumer) invalidControl.getMock();
198 consumers.setAvailableInvalidConsumers( Collections.singletonList( invalidConsumer ) );
200 ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
201 File testFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
203 knownConsumer.beginScan( repo );
204 knownConsumer.getExcludes();
205 knownControl.setReturnValue( Collections.EMPTY_LIST );
206 knownConsumer.getIncludes();
207 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
208 knownConsumer.processFile( _OS( "path/to/test-file.txt" ) );
209 // knownConsumer.completeScan();
210 knownControl.replay();
212 invalidConsumer.beginScan( repo );
213 // invalidConsumer.completeScan();
214 invalidControl.replay();
216 consumers.executeConsumers( repo, testFile );
218 knownControl.verify();
219 invalidControl.verify();
221 knownControl.reset();
222 invalidControl.reset();
224 File notIncludedTestFile = getTestFile( "target/test-repo/path/to/test-file.xml" );
226 knownConsumer.beginScan( repo );
227 knownConsumer.getExcludes();
228 knownControl.setReturnValue( Collections.EMPTY_LIST );
229 knownConsumer.getIncludes();
230 knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
231 // knownConsumer.completeScan();
232 knownControl.replay();
234 invalidConsumer.beginScan( repo );
235 invalidConsumer.processFile( _OS( "path/to/test-file.xml" ) );
236 invalidConsumer.getId();
237 invalidControl.setReturnValue( "invalid" );
238 // invalidConsumer.completeScan();
239 invalidControl.replay();
241 consumers.executeConsumers( repo, notIncludedTestFile );
243 knownControl.verify();
244 invalidControl.verify();
246 knownControl.reset();
247 invalidControl.reset();
249 File excludedTestFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
251 knownConsumer.beginScan( repo );
252 knownConsumer.getExcludes();
253 knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
254 // knownConsumer.completeScan();
255 knownControl.replay();
257 invalidConsumer.beginScan( repo );
258 invalidConsumer.processFile( _OS( "path/to/test-file.txt" ) );
259 invalidConsumer.getId();
260 invalidControl.setReturnValue( "invalid" );
261 // invalidConsumer.completeScan();
262 invalidControl.replay();
264 consumers.executeConsumers( repo, excludedTestFile );
266 knownControl.verify();
267 invalidControl.verify();
271 * Create an OS specific version of the filepath.
272 * Provide path in unix "/" format.
274 private String _OS( String path )
276 if ( SystemUtils.IS_OS_WINDOWS )
278 return path.replace( '/', '\\' );