]> source.dussan.org Git - archiva.git/blob
d0ee6e2d2c803e3cba01709791483d430cd137af
[archiva.git] /
1 package org.apache.maven.archiva.repository.scanner;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
28
29 import java.io.File;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map;
34
35 /**
36  * RepositoryContentConsumersTest
37  *
38  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
39  * @version $Id$
40  */
41 public class RepositoryContentConsumersTest
42     extends AbstractRepositoryLayerTestCase
43 {
44     private RepositoryContentConsumers lookupRepositoryConsumers()
45         throws Exception
46     {
47         RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
48             .getName() );
49         assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtil );
50         return consumerUtil;
51     }
52
53     public void testGetSelectedKnownIds()
54         throws Exception
55     {
56         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
57
58         String expectedKnownIds[] = new String[] {
59             "update-db-artifact",
60             "create-missing-checksums",
61             "update-db-repository-metadata",
62             "validate-checksum",
63             "validate-signature",
64             "index-content",
65             "auto-remove",
66             "auto-rename" };
67
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() );
71
72         for ( String expectedId : expectedKnownIds )
73         {
74             assertTrue( "Known id [" + expectedId + "] exists.", knownConsumers.contains( expectedId ) );
75         }
76     }
77
78     public void testGetSelectedInvalidIds()
79         throws Exception
80     {
81         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
82
83         String expectedInvalidIds[] = new String[] { "update-db-bad-content" };
84
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() );
88
89         for ( String expectedId : expectedInvalidIds )
90         {
91             assertTrue( "Invalid id [" + expectedId + "] exists.", invalidConsumers.contains( expectedId ) );
92         }
93     }
94
95     public void testGetSelectedKnownConsumerMap()
96         throws Exception
97     {
98         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
99
100         String expectedSelectedKnownIds[] = new String[] {
101             "update-db-artifact",
102             "create-missing-checksums",
103             "update-db-repository-metadata",
104             "validate-checksum",
105             "index-content",
106             "auto-remove",
107             "auto-rename" };
108
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() );
112
113         for ( String expectedId : expectedSelectedKnownIds )
114         {
115             KnownRepositoryContentConsumer consumer = knownConsumerMap.get( expectedId );
116             assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
117             assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
118         }
119     }
120
121     public void testGetSelectedInvalidConsumerMap()
122         throws Exception
123     {
124         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
125
126         String expectedSelectedInvalidIds[] = new String[] { "update-db-bad-content" };
127
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() );
132
133         for ( String expectedId : expectedSelectedInvalidIds )
134         {
135             InvalidRepositoryContentConsumer consumer = invalidConsumerMap.get( expectedId );
136             assertNotNull( "Known[" + expectedId + "] should not be null.", consumer );
137             assertEquals( "Known[" + expectedId + "].id", expectedId, consumer.getId() );
138         }
139     }
140
141     public void testGetAvailableKnownList()
142         throws Exception
143     {
144         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
145
146         String expectedKnownIds[] = new String[] {
147             "update-db-artifact",
148             "create-missing-checksums",
149             "update-db-repository-metadata",
150             "validate-checksum",
151             "index-content",
152             "auto-remove",
153             "auto-rename",
154             "available-but-unselected" };
155
156         List<KnownRepositoryContentConsumer> knownConsumers = consumerutil.getAvailableKnownConsumers();
157         assertNotNull( "known consumers should not be null.", knownConsumers );
158         assertEquals( "known consumers", expectedKnownIds.length, knownConsumers.size() );
159
160         List<String> expectedIds = Arrays.asList( expectedKnownIds );
161         for ( KnownRepositoryContentConsumer consumer : knownConsumers )
162         {
163             assertTrue( "Consumer [" + consumer.getId() + "] returned by .getAvailableKnownConsumers() is unexpected.",
164                         expectedIds.contains( consumer.getId() ) );
165         }
166     }
167
168     public void testGetAvailableInvalidList()
169         throws Exception
170     {
171         RepositoryContentConsumers consumerutil = lookupRepositoryConsumers();
172
173         String expectedInvalidIds[] = new String[] { "update-db-bad-content", "move-to-trash-then-notify" };
174
175         List<InvalidRepositoryContentConsumer> invalidConsumers = consumerutil.getAvailableInvalidConsumers();
176         assertNotNull( "invalid consumers should not be null.", invalidConsumers );
177         assertEquals( "invalid consumers", expectedInvalidIds.length, invalidConsumers.size() );
178
179         List<String> expectedIds = Arrays.asList( expectedInvalidIds );
180         for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
181         {
182             assertTrue( "Consumer [" + consumer.getId()
183                 + "] returned by .getAvailableInvalidConsumers() is unexpected.", expectedIds.contains( consumer
184                 .getId() ) );
185         }
186     }
187
188     public void testExecution()
189         throws Exception
190     {
191         MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
192         RepositoryContentConsumers consumers = lookupRepositoryConsumers();
193         KnownRepositoryContentConsumer knownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
194         consumers.setAvailableKnownConsumers( Collections.singletonList( knownConsumer ) );
195
196         MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
197         InvalidRepositoryContentConsumer invalidConsumer = (InvalidRepositoryContentConsumer) invalidControl.getMock();
198         consumers.setAvailableInvalidConsumers( Collections.singletonList( invalidConsumer ) );
199
200         ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
201         File testFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
202
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();
211
212         invalidConsumer.beginScan( repo );
213         //        invalidConsumer.completeScan();
214         invalidControl.replay();
215
216         consumers.executeConsumers( repo, testFile );
217
218         knownControl.verify();
219         invalidControl.verify();
220
221         knownControl.reset();
222         invalidControl.reset();
223
224         File notIncludedTestFile = getTestFile( "target/test-repo/path/to/test-file.xml" );
225
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();
233
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();
240
241         consumers.executeConsumers( repo, notIncludedTestFile );
242
243         knownControl.verify();
244         invalidControl.verify();
245
246         knownControl.reset();
247         invalidControl.reset();
248
249         File excludedTestFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
250
251         knownConsumer.beginScan( repo );
252         knownConsumer.getExcludes();
253         knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
254         //        knownConsumer.completeScan();
255         knownControl.replay();
256
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();
263
264         consumers.executeConsumers( repo, excludedTestFile );
265
266         knownControl.verify();
267         invalidControl.verify();
268     }
269
270     /**
271      * Create an OS specific version of the filepath.
272      * Provide path in unix "/" format.
273      */
274     private String _OS( String path )
275     {
276         if ( SystemUtils.IS_OS_WINDOWS )
277         {
278             return path.replace( '/', '\\' );
279         }
280         return path;
281     }
282 }