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