]> source.dussan.org Git - archiva.git/blob
e89558057b59ec1c979ffff49802a2cb0161ddb0
[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.consumers.RepositoryContentConsumer;
27 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
28 import org.easymock.MockControl;
29
30 import java.io.File;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map;
34
35 /**
36  * RepositoryContentConsumerUtilTest
37  *
38  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
39  * @version $Id$
40  */
41 public class RepositoryContentConsumerUtilTest
42     extends AbstractRepositoryLayerTestCase
43 {
44     private RepositoryContentConsumers lookupRepositoryConsumerUtil()
45         throws Exception
46     {
47         RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
48             .getName() );
49         assertNotNull( "RepositoryContentConsumerUtil should not be null.", consumerUtil );
50         return consumerUtil;
51     }
52
53     public void testGetSelectedIds()
54         throws Exception
55     {
56         RepositoryContentConsumers consumerutil = lookupRepositoryConsumerUtil();
57
58         List knownConsumers = consumerutil.getSelectedKnownConsumerIds();
59         assertNotNull( "Known Consumer IDs should not be null", knownConsumers );
60         assertEquals( "Known Consumer IDs.size", 9, knownConsumers.size() );
61
62         List invalidConsumers = consumerutil.getSelectedInvalidConsumerIds();
63         assertNotNull( "Invalid Consumer IDs should not be null", invalidConsumers );
64         assertEquals( "Invalid Consumer IDs.size", 1, invalidConsumers.size() );
65     }
66
67     public void testGetSelectedConsumersMaps()
68         throws Exception
69     {
70         RepositoryContentConsumers consumerutil = lookupRepositoryConsumerUtil();
71
72         Map knownConsumerMap = consumerutil.getSelectedKnownConsumersMap();
73         assertNotNull( "Known Consumer Map should not be null", knownConsumerMap );
74         assertEquals( "Known Consumer Map.size", 1, knownConsumerMap.size() );
75
76         Object o = knownConsumerMap.get( "sample-known" );
77         assertNotNull( "Known[sample-known] should not be null.", o );
78         assertInstanceof( "Known[sample-known]", RepositoryContentConsumer.class, o );
79         assertInstanceof( "Known[sample-known]", KnownRepositoryContentConsumer.class, o );
80
81         Map invalidConsumerMap = consumerutil.getSelectedInvalidConsumersMap();
82         assertNotNull( "Invalid Consumer Map should not be null", invalidConsumerMap );
83         assertEquals( "Invalid Consumer Map.size", 0, invalidConsumerMap.size() );
84     }
85
86     private void assertInstanceof( String msg, Class clazz, Object o )
87     {
88         if ( clazz.isInstance( o ) == false )
89         {
90             fail( msg + ": Object [" + o.getClass().getName() + "] should have been an instanceof [" + clazz.getName() +
91                 "]" );
92         }
93     }
94
95     public void testGetAvailableLists()
96         throws Exception
97     {
98         RepositoryContentConsumers consumerutil = lookupRepositoryConsumerUtil();
99
100         List knownConsumers = consumerutil.getAvailableKnownConsumers();
101         assertNotNull( "known consumers should not be null.", knownConsumers );
102         assertEquals( "known consumers", 1, knownConsumers.size() );
103         assertInstanceof( "Available Known Consumers", RepositoryContentConsumer.class, knownConsumers.get( 0 ) );
104
105         List invalidConsumers = consumerutil.getAvailableInvalidConsumers();
106         assertNotNull( "invalid consumers should not be null.", invalidConsumers );
107         assertEquals( "invalid consumers", 0, invalidConsumers.size() );
108     }
109
110     public void testExecution()
111         throws Exception
112     {
113         MockControl knownControl = MockControl.createNiceControl( KnownRepositoryContentConsumer.class );
114         RepositoryContentConsumers consumers = lookupRepositoryConsumerUtil();
115         KnownRepositoryContentConsumer knownConsumer = (KnownRepositoryContentConsumer) knownControl.getMock();
116         consumers.setAvailableKnownConsumers( Collections.singletonList( knownConsumer ) );
117
118         MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
119         InvalidRepositoryContentConsumer invalidConsumer = (InvalidRepositoryContentConsumer) invalidControl.getMock();
120         consumers.setAvailableInvalidConsumers( Collections.singletonList( invalidConsumer ) );
121
122         ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
123         File testFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
124         
125         knownConsumer.beginScan( repo );
126         knownConsumer.getExcludes();
127         knownControl.setReturnValue( Collections.EMPTY_LIST );
128         knownConsumer.getIncludes();
129         knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
130         knownConsumer.processFile( _OS("path/to/test-file.txt") );
131 //        knownConsumer.completeScan();
132         knownControl.replay();
133
134         invalidConsumer.beginScan( repo );
135 //        invalidConsumer.completeScan();
136         invalidControl.replay();
137
138         consumers.executeConsumers( repo, testFile );
139
140         knownControl.verify();
141         invalidControl.verify();
142
143         knownControl.reset();
144         invalidControl.reset();
145
146         File notIncludedTestFile = getTestFile( "target/test-repo/path/to/test-file.xml" );
147
148         knownConsumer.beginScan( repo );
149         knownConsumer.getExcludes();
150         knownControl.setReturnValue( Collections.EMPTY_LIST );
151         knownConsumer.getIncludes();
152         knownControl.setReturnValue( Collections.singletonList( "**/*.txt" ) );
153 //        knownConsumer.completeScan();
154         knownControl.replay();
155
156         invalidConsumer.beginScan( repo );
157         invalidConsumer.processFile( _OS("path/to/test-file.xml") );
158         invalidConsumer.getId();
159         invalidControl.setReturnValue( "invalid" );
160 //        invalidConsumer.completeScan();
161         invalidControl.replay();
162
163         consumers.executeConsumers( repo, notIncludedTestFile );
164
165         knownControl.verify();
166         invalidControl.verify();
167
168         knownControl.reset();
169         invalidControl.reset();
170
171         File excludedTestFile = getTestFile( "target/test-repo/path/to/test-file.txt" );
172
173         knownConsumer.beginScan( repo );
174         knownConsumer.getExcludes();
175         knownControl.setReturnValue( Collections.singletonList( "**/test-file.txt" ) );
176 //        knownConsumer.completeScan();
177         knownControl.replay();
178
179         invalidConsumer.beginScan( repo );
180         invalidConsumer.processFile( _OS("path/to/test-file.txt") );
181         invalidConsumer.getId();
182         invalidControl.setReturnValue( "invalid" );
183 //        invalidConsumer.completeScan();
184         invalidControl.replay();
185
186         consumers.executeConsumers( repo, excludedTestFile );
187
188         knownControl.verify();
189         invalidControl.verify();
190     }
191
192     /**
193      * Create an OS specific version of the filepath.
194      * Provide path in unix "/" format.
195      */
196     private String _OS( String path )
197     {
198         if ( SystemUtils.IS_OS_WINDOWS )
199         {
200             return path.replace( '/', '\\' );
201         }
202         return path;
203     }
204 }