1 package org.apache.maven.archiva.webdav;
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
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.apache.jackrabbit.webdav.DavException;
27 import org.apache.jackrabbit.webdav.DavResourceLocator;
28 import org.apache.jackrabbit.webdav.DavServletRequest;
29 import org.apache.jackrabbit.webdav.DavServletResponse;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.Configuration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
34 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
35 import org.apache.maven.archiva.repository.RepositoryContentFactory;
36 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
37 import org.apache.maven.archiva.repository.content.RepositoryRequest;
38 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
39 import org.easymock.MockControl;
40 import org.easymock.classextension.MockClassControl;
43 * ArchivaDavResourceFactoryTest
45 public class ArchivaDavResourceFactoryTest
46 extends PlexusInSpringTestCase
48 private static final String RELEASES_REPO = "releases";
50 private static final String INTERNAL_REPO = "internal";
52 private static final String LOCAL_MIRROR_REPO = "local-mirror";
54 private static final String LOCAL_REPO_GROUP = "local";
56 private OverridingArchivaDavResourceFactory resourceFactory;
58 private MockControl requestControl;
60 private DavServletRequest request;
62 private MockControl repoRequestControl;
64 private RepositoryRequest repoRequest;
66 private DavServletResponse response;
68 private MockControl archivaConfigurationControl;
70 private ArchivaConfiguration archivaConfiguration;
72 private Configuration config;
74 private MockControl repoContentFactoryControl;
76 private RepositoryContentFactory repoFactory;
83 requestControl = MockControl.createControl( DavServletRequest.class );
85 request = (DavServletRequest) requestControl.getMock();
87 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
89 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
91 config = new Configuration();
92 config.addManagedRepository( createManagedRepository(
94 new File( getBasedir(), "target/test-classes/releases" ).getPath() ) );
95 config.addManagedRepository( createManagedRepository(
97 new File( getBasedir(), "target/test-classes/internal" ).getPath() ) );
99 RepositoryGroupConfiguration repoGroupConfig = new RepositoryGroupConfiguration();
100 repoGroupConfig.setId( LOCAL_REPO_GROUP );
101 repoGroupConfig.addRepository( RELEASES_REPO );
102 repoGroupConfig.addRepository( INTERNAL_REPO );
104 config.addRepositoryGroup( repoGroupConfig );
106 repoContentFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
107 repoFactory = (RepositoryContentFactory) repoContentFactoryControl.getMock();
109 repoRequestControl = MockClassControl.createControl( RepositoryRequest.class );
110 repoRequest = (RepositoryRequest) repoRequestControl.getMock();
112 resourceFactory = new OverridingArchivaDavResourceFactory();
113 resourceFactory.setArchivaConfiguration( archivaConfiguration );
114 resourceFactory.setRepositoryFactory( repoFactory );
115 resourceFactory.setRepositoryRequest( repoRequest );
118 private ManagedRepositoryConfiguration createManagedRepository( String id, String location )
120 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
121 repoConfig.setId( id );
122 repoConfig.setName( id );
123 repoConfig.setLocation( location );
128 private ManagedRepositoryContent createManagedRepositoryContent( String repoId )
130 ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
131 repoContent.setRepository( config.findManagedRepositoryById( repoId ) );
136 public void tearDown()
142 // MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
143 public void testRepositoryGroupFirstRepositoryRequiresAuthentication()
146 DavResourceLocator locator =
147 new ArchivaDavResourceLocator( "", "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
148 LOCAL_REPO_GROUP, new ArchivaDavLocatorFactory() );
150 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
154 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
155 requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
156 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
157 createManagedRepositoryContent( RELEASES_REPO ) );
158 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
159 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
160 repoRequestControl.expectAndReturn(
161 repoRequest.isSupportFile( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
163 repoRequestControl.expectAndReturn(
164 repoRequest.isDefault( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
166 repoRequestControl.expectAndReturn(
167 repoRequest.toArtifactReference( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
169 repoRequestControl.expectAndReturn(
170 repoRequest.toNativePath(
171 "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
174 config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
175 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
176 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
179 archivaConfigurationControl.replay();
180 requestControl.replay();
181 repoContentFactoryControl.replay();
182 repoRequestControl.replay();
184 resourceFactory.createResource( locator, request, response );
186 archivaConfigurationControl.verify();
187 requestControl.verify();
188 repoContentFactoryControl.verify();
189 repoRequestControl.verify();
191 fail( "A DavException with 401 error code should have been thrown." );
193 catch ( DavException e )
195 assertEquals( 401, e.getErrorCode() );
199 public void testRepositoryGroupLastRepositoryRequiresAuthentication()
202 DavResourceLocator locator =
203 new ArchivaDavResourceLocator( "", "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
204 LOCAL_REPO_GROUP, new ArchivaDavLocatorFactory() );
206 List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
207 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
208 repoGroup.setId( LOCAL_REPO_GROUP );
209 repoGroup.addRepository( INTERNAL_REPO );
210 repoGroup.addRepository( RELEASES_REPO );
212 repoGroups.add( repoGroup );
214 config.setRepositoryGroups( repoGroups );
216 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
220 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
221 requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
222 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
224 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
225 createManagedRepositoryContent( RELEASES_REPO ) );
226 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
227 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
228 repoRequestControl.expectAndReturn(
229 repoRequest.isSupportFile( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
231 repoRequestControl.expectAndReturn(
232 repoRequest.isDefault( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
234 repoRequestControl.expectAndReturn(
235 repoRequest.toArtifactReference( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
237 repoRequestControl.expectAndReturn(
238 repoRequest.toNativePath(
239 "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
242 config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
243 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
245 archivaConfigurationControl.replay();
246 requestControl.replay();
247 repoContentFactoryControl.replay();
248 repoRequestControl.replay();
250 resourceFactory.createResource( locator, request, response );
252 archivaConfigurationControl.verify();
253 requestControl.verify();
254 repoContentFactoryControl.verify();
255 repoRequestControl.verify();
257 fail( "A DavException with 401 error code should have been thrown." );
259 catch ( DavException e )
261 assertEquals( 401, e.getErrorCode() );
265 public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled()
268 DavResourceLocator locator =
269 new ArchivaDavResourceLocator( "", "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
270 LOCAL_REPO_GROUP, new ArchivaDavLocatorFactory() );
272 config.addManagedRepository( createManagedRepository( LOCAL_MIRROR_REPO,
273 new File( getBasedir(),
274 "target/test-classes/local-mirror" ).getPath() ) );
276 List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
277 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
278 repoGroup.setId( LOCAL_REPO_GROUP );
279 repoGroup.addRepository( INTERNAL_REPO );
280 repoGroup.addRepository( LOCAL_MIRROR_REPO );
282 repoGroups.add( repoGroup );
284 config.setRepositoryGroups( repoGroups );
286 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
287 ManagedRepositoryContent localMirrorRepo = createManagedRepositoryContent( LOCAL_MIRROR_REPO );
291 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
292 requestControl.expectAndReturn( request.getMethod(), "GET", 4 );
293 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
295 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ),
297 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 4 );
298 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 4 );
299 repoRequestControl.expectAndReturn(
300 repoRequest.isSupportFile( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
302 repoRequestControl.expectAndReturn(
303 repoRequest.isDefault( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
305 repoRequestControl.expectAndReturn(
306 repoRequest.toArtifactReference( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
308 repoRequestControl.expectAndReturn(
309 repoRequest.toNativePath(
310 "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
313 config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
314 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
316 repoRequestControl.expectAndReturn(
317 repoRequest.toNativePath(
318 "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
321 config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(),
322 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
324 archivaConfigurationControl.replay();
325 requestControl.replay();
326 repoContentFactoryControl.replay();
327 repoRequestControl.replay();
329 resourceFactory.createResource( locator, request, response );
331 archivaConfigurationControl.verify();
332 requestControl.verify();
333 repoContentFactoryControl.verify();
334 repoRequestControl.verify();
336 fail( "A DavException with 404 error code should have been thrown." );
338 catch ( DavException e )
340 assertEquals( 404, e.getErrorCode() );
344 class OverridingArchivaDavResourceFactory
345 extends ArchivaDavResourceFactory
347 protected boolean isAuthorized( DavServletRequest request, String repositoryId )
350 if ( RELEASES_REPO.equals( repositoryId ) )
352 throw new UnauthorizedDavException( repositoryId,
353 "You are not authenticated and authorized to access any repository." );
361 protected String getActivePrincipal( DavServletRequest request )