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.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.jackrabbit.webdav.DavException;
29 import org.apache.jackrabbit.webdav.DavResourceLocator;
30 import org.apache.jackrabbit.webdav.DavServletRequest;
31 import org.apache.jackrabbit.webdav.DavServletResponse;
32 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
33 import org.apache.maven.archiva.configuration.Configuration;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
36 import org.apache.maven.archiva.proxy.DefaultRepositoryProxyConnectors;
37 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
38 import org.apache.maven.archiva.repository.RepositoryContentFactory;
39 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
40 import org.apache.maven.archiva.repository.content.RepositoryRequest;
41 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
42 import org.easymock.MockControl;
43 import org.easymock.classextension.MockClassControl;
46 * ArchivaDavResourceFactoryTest
48 public class ArchivaDavResourceFactoryTest
49 extends PlexusInSpringTestCase
51 private static final String RELEASES_REPO = "releases";
53 private static final String INTERNAL_REPO = "internal";
55 private static final String LOCAL_MIRROR_REPO = "local-mirror";
57 private static final String LEGACY_REPO = "legacy-repo";
59 private static final String LOCAL_REPO_GROUP = "local";
61 private OverridingArchivaDavResourceFactory resourceFactory;
63 private MockControl requestControl;
65 private DavServletRequest request;
67 private MockControl repoRequestControl;
69 private RepositoryRequest repoRequest;
71 private MockControl responseControl;
73 private DavServletResponse response;
75 private MockControl archivaConfigurationControl;
77 private ArchivaConfiguration archivaConfiguration;
79 private Configuration config;
81 private MockControl repoContentFactoryControl;
83 private RepositoryContentFactory repoFactory;
90 requestControl = MockControl.createControl( DavServletRequest.class );
91 request = (DavServletRequest) requestControl.getMock();
93 responseControl = MockControl.createControl( DavServletResponse.class );
94 response = (DavServletResponse) responseControl.getMock();
95 responseControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
97 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
98 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
100 config = new Configuration();
101 config.addManagedRepository( createManagedRepository( RELEASES_REPO, new File( getBasedir(),
102 "target/test-classes/" +
103 RELEASES_REPO ).getPath(),
105 config.addManagedRepository( createManagedRepository( INTERNAL_REPO, new File( getBasedir(),
106 "target/test-classes/" +
107 INTERNAL_REPO ).getPath(),
110 RepositoryGroupConfiguration repoGroupConfig = new RepositoryGroupConfiguration();
111 repoGroupConfig.setId( LOCAL_REPO_GROUP );
112 repoGroupConfig.addRepository( RELEASES_REPO );
113 repoGroupConfig.addRepository( INTERNAL_REPO );
115 config.addRepositoryGroup( repoGroupConfig );
117 repoContentFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
118 repoFactory = (RepositoryContentFactory) repoContentFactoryControl.getMock();
120 repoRequestControl = MockClassControl.createControl( RepositoryRequest.class );
121 repoRequest = (RepositoryRequest) repoRequestControl.getMock();
123 resourceFactory = new OverridingArchivaDavResourceFactory();
124 resourceFactory.setArchivaConfiguration( archivaConfiguration );
125 resourceFactory.setRepositoryFactory( repoFactory );
126 resourceFactory.setRepositoryRequest( repoRequest );
127 resourceFactory.setConnectors( new OverridingRepositoryProxyConnectors() );
130 private ManagedRepositoryConfiguration createManagedRepository( String id, String location, String layout )
132 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
133 repoConfig.setId( id );
134 repoConfig.setName( id );
135 repoConfig.setLocation( location );
136 repoConfig.setLayout( layout );
141 private ManagedRepositoryContent createManagedRepositoryContent( String repoId )
143 ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
144 repoContent.setRepository( config.findManagedRepositoryById( repoId ) );
149 public void tearDown()
155 // MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
156 public void testRepositoryGroupFirstRepositoryRequiresAuthentication()
159 DavResourceLocator locator =
160 new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP +
161 "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
162 new ArchivaDavLocatorFactory() );
164 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
168 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
169 requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
170 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
171 createManagedRepositoryContent( RELEASES_REPO ) );
172 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
173 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
174 repoRequestControl.expectAndReturn(
175 repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
177 repoRequestControl.expectAndReturn(
178 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
180 repoRequestControl.expectAndReturn(
181 repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
183 repoRequestControl.expectAndReturn(
184 repoRequest.toNativePath(
185 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
188 config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
189 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
190 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
193 archivaConfigurationControl.replay();
194 requestControl.replay();
195 repoContentFactoryControl.replay();
196 repoRequestControl.replay();
198 resourceFactory.createResource( locator, request, response );
200 archivaConfigurationControl.verify();
201 requestControl.verify();
202 repoContentFactoryControl.verify();
203 repoRequestControl.verify();
205 fail( "A DavException with 401 error code should have been thrown." );
207 catch ( DavException e )
209 assertEquals( 401, e.getErrorCode() );
213 public void testRepositoryGroupLastRepositoryRequiresAuthentication()
216 DavResourceLocator locator =
217 new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP +
218 "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
219 new ArchivaDavLocatorFactory() );
221 List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
222 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
223 repoGroup.setId( LOCAL_REPO_GROUP );
224 repoGroup.addRepository( INTERNAL_REPO );
225 repoGroup.addRepository( RELEASES_REPO );
227 repoGroups.add( repoGroup );
229 config.setRepositoryGroups( repoGroups );
231 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
235 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
236 requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
237 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
239 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
240 createManagedRepositoryContent( RELEASES_REPO ) );
241 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
242 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
243 repoRequestControl.expectAndReturn(
244 repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
246 repoRequestControl.expectAndReturn(
247 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
249 repoRequestControl.expectAndReturn(
250 repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
252 repoRequestControl.expectAndReturn(
253 repoRequest.toNativePath(
254 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
257 config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
258 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
260 archivaConfigurationControl.replay();
261 requestControl.replay();
262 repoContentFactoryControl.replay();
263 repoRequestControl.replay();
265 resourceFactory.createResource( locator, request, response );
267 archivaConfigurationControl.verify();
268 requestControl.verify();
269 repoContentFactoryControl.verify();
270 repoRequestControl.verify();
272 fail( "A DavException with 401 error code should have been thrown." );
274 catch ( DavException e )
276 assertEquals( 401, e.getErrorCode() );
280 public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled()
283 DavResourceLocator locator =
284 new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP +
285 "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
286 new ArchivaDavLocatorFactory() );
288 config.addManagedRepository( createManagedRepository( LOCAL_MIRROR_REPO,
289 new File( getBasedir(),
290 "target/test-classes/local-mirror" ).getPath(),
293 List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
294 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
295 repoGroup.setId( LOCAL_REPO_GROUP );
296 repoGroup.addRepository( INTERNAL_REPO );
297 repoGroup.addRepository( LOCAL_MIRROR_REPO );
299 repoGroups.add( repoGroup );
301 config.setRepositoryGroups( repoGroups );
303 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
304 ManagedRepositoryContent localMirrorRepo = createManagedRepositoryContent( LOCAL_MIRROR_REPO );
308 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
309 requestControl.expectAndReturn( request.getMethod(), "GET", 4 );
310 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
312 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ),
314 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 4 );
315 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 4 );
316 repoRequestControl.expectAndReturn(
317 repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
319 repoRequestControl.expectAndReturn(
320 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
322 repoRequestControl.expectAndReturn(
323 repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
325 repoRequestControl.expectAndReturn(
326 repoRequest.toNativePath(
327 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
330 config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
331 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
333 repoRequestControl.expectAndReturn(
334 repoRequest.toNativePath(
335 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
338 config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(),
339 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
341 archivaConfigurationControl.replay();
342 requestControl.replay();
343 repoContentFactoryControl.replay();
344 repoRequestControl.replay();
346 resourceFactory.createResource( locator, request, response );
348 archivaConfigurationControl.verify();
349 requestControl.verify();
350 repoContentFactoryControl.verify();
351 repoRequestControl.verify();
353 fail( "A DavException with 404 error code should have been thrown." );
355 catch ( DavException e )
357 assertEquals( 404, e.getErrorCode() );
362 public void testRequestArtifactMetadataThreePartsRepoHasDefaultLayout()
365 // should fetch metadata
366 DavResourceLocator locator =
367 new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml", INTERNAL_REPO,
368 new ArchivaDavLocatorFactory() );
370 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
372 // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
373 RepositoryRequest repoRequest = (RepositoryRequest) lookup( RepositoryRequest.class );
374 resourceFactory.setRepositoryRequest( repoRequest );
378 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
379 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
381 requestControl.expectAndReturn( request.getMethod(), "GET", 3 );
382 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 3 );
383 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
384 requestControl.expectAndReturn( request.getRequestURI(), "http://localhost:8080/archiva/repository/" +
385 INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml" );
386 response.addHeader( "Pragma", "no-cache" );
387 responseControl.setVoidCallable();
389 response.addHeader( "Cache-Control", "no-cache" );
390 responseControl.setVoidCallable();
392 long date = 2039842134;
393 response.addDateHeader( "last-modified", date );
394 responseControl.setVoidCallable();
396 archivaConfigurationControl.replay();
397 repoContentFactoryControl.replay();
398 requestControl.replay();
399 responseControl.replay();
401 resourceFactory.createResource( locator, request, response );
403 archivaConfigurationControl.verify();
404 repoContentFactoryControl.verify();
405 requestControl.verify();
406 responseControl.verify();
408 catch ( DavException e )
410 fail( "A DavException should not have been thrown!" );
414 public void testRequestArtifactMetadataTwoPartsRepoHasDefaultLayout()
417 // should not fetch metadata
418 DavResourceLocator locator =
419 new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/maven-metadata.xml", INTERNAL_REPO,
420 new ArchivaDavLocatorFactory() );
422 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
424 // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
425 RepositoryRequest repoRequest = (RepositoryRequest) lookup( RepositoryRequest.class );
426 resourceFactory.setRepositoryRequest( repoRequest );
430 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
431 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
433 requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
434 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
435 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
437 archivaConfigurationControl.replay();
438 repoContentFactoryControl.replay();
439 requestControl.replay();
441 resourceFactory.createResource( locator, request, response );
443 archivaConfigurationControl.verify();
444 repoContentFactoryControl.verify();
445 requestControl.verify();
447 fail( "A 404 error should have been thrown!" );
449 catch ( DavException e )
451 assertEquals( 404, e.getErrorCode() );
455 public void testRequestMetadataRepoIsLegacy()
458 config.addManagedRepository( createManagedRepository( LEGACY_REPO, new File( getBasedir(),
459 "target/test-classes/" +
460 LEGACY_REPO ).getPath(),
462 DavResourceLocator locator =
463 new ArchivaDavResourceLocator( "", "/repository/" + LEGACY_REPO + "/eclipse/maven-metadata.xml", LEGACY_REPO,
464 new ArchivaDavLocatorFactory() );
466 ManagedRepositoryContent legacyRepo = createManagedRepositoryContent( LEGACY_REPO );
468 // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
469 RepositoryRequest repoRequest = (RepositoryRequest) lookup( RepositoryRequest.class );
470 resourceFactory.setRepositoryRequest( repoRequest );
474 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
475 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( LEGACY_REPO ),
477 requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
478 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
479 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
481 archivaConfigurationControl.replay();
482 repoContentFactoryControl.replay();
483 requestControl.replay();
485 resourceFactory.createResource( locator, request, response );
487 archivaConfigurationControl.verify();
488 repoContentFactoryControl.verify();
489 requestControl.verify();
491 fail( "A 404 error should have been thrown!" );
493 catch ( DavException e )
495 assertEquals( 404, e.getErrorCode() );
499 class OverridingArchivaDavResourceFactory
500 extends ArchivaDavResourceFactory
502 protected boolean isAuthorized( DavServletRequest request, String repositoryId )
505 if ( RELEASES_REPO.equals( repositoryId ) )
507 throw new UnauthorizedDavException( repositoryId,
508 "You are not authenticated and authorized to access any repository." );
516 protected String getActivePrincipal( DavServletRequest request )
522 class OverridingRepositoryProxyConnectors
523 extends DefaultRepositoryProxyConnectors
525 public File fetchMetatadaFromProxies( ManagedRepositoryContent repository, String logicalPath )
527 File target = new File( repository.getRepoRoot(), logicalPath );
530 FileUtils.copyFile( new File( getBasedir(), "target/test-classes/maven-metadata.xml" ), target );
532 catch ( IOException e )