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
22 import junit.framework.TestCase;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
25 import org.apache.commons.io.FileUtils;
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.proxy.DefaultRepositoryProxyConnectors;
35 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
36 import org.apache.maven.archiva.repository.RepositoryContentFactory;
37 import org.apache.maven.archiva.repository.content.LegacyPathParser;
38 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
39 import org.apache.maven.archiva.repository.content.RepositoryRequest;
40 import org.easymock.MockControl;
41 import org.easymock.classextension.MockClassControl;
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.springframework.context.ApplicationContext;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
50 import javax.inject.Inject;
52 import java.io.IOException;
53 import java.util.ArrayList;
54 import java.util.List;
57 * ArchivaDavResourceFactoryTest
59 @RunWith( SpringJUnit4ClassRunner.class )
60 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
61 public class ArchivaDavResourceFactoryTest
64 private static final String RELEASES_REPO = "releases";
66 private static final String INTERNAL_REPO = "internal";
68 private static final String LOCAL_MIRROR_REPO = "local-mirror";
70 private static final String LEGACY_REPO = "legacy-repo";
72 private static final String LOCAL_REPO_GROUP = "local";
74 private OverridingArchivaDavResourceFactory resourceFactory;
76 private MockControl requestControl;
78 private DavServletRequest request;
80 private MockControl repoRequestControl;
82 private RepositoryRequest repoRequest;
84 private MockControl responseControl;
86 private DavServletResponse response;
88 private MockControl archivaConfigurationControl;
90 private ArchivaConfiguration archivaConfiguration;
92 private Configuration config;
94 private MockControl repoContentFactoryControl;
96 private RepositoryContentFactory repoFactory;
99 ApplicationContext applicationContext;
102 PlexusSisuBridge plexusSisuBridge;
110 requestControl = MockControl.createControl( DavServletRequest.class );
111 request = (DavServletRequest) requestControl.getMock();
113 responseControl = MockControl.createControl( DavServletResponse.class );
114 response = (DavServletResponse) responseControl.getMock();
115 responseControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
117 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
118 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
120 config = new Configuration();
121 config.addManagedRepository(
122 createManagedRepository( RELEASES_REPO, new File( "target/test-classes/" + RELEASES_REPO ).getPath(),
124 config.addManagedRepository(
125 createManagedRepository( INTERNAL_REPO, new File( "target/test-classes/" + INTERNAL_REPO ).getPath(),
128 RepositoryGroupConfiguration repoGroupConfig = new RepositoryGroupConfiguration();
129 repoGroupConfig.setId( LOCAL_REPO_GROUP );
130 repoGroupConfig.addRepository( RELEASES_REPO );
131 repoGroupConfig.addRepository( INTERNAL_REPO );
133 config.addRepositoryGroup( repoGroupConfig );
135 repoContentFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
136 repoFactory = (RepositoryContentFactory) repoContentFactoryControl.getMock();
138 repoRequestControl = MockClassControl.createControl( RepositoryRequest.class );
139 repoRequest = (RepositoryRequest) repoRequestControl.getMock();
142 new OverridingArchivaDavResourceFactory( applicationContext, plexusSisuBridge, archivaConfiguration );
143 resourceFactory.setArchivaConfiguration( archivaConfiguration );
144 resourceFactory.setRepositoryFactory( repoFactory );
145 resourceFactory.setRepositoryRequest( repoRequest );
146 resourceFactory.setConnectors( new OverridingRepositoryProxyConnectors() );
149 private ManagedRepositoryConfiguration createManagedRepository( String id, String location, String layout )
151 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
152 repoConfig.setId( id );
153 repoConfig.setName( id );
154 repoConfig.setLocation( location );
155 repoConfig.setLayout( layout );
160 private ManagedRepositoryContent createManagedRepositoryContent( String repoId )
162 ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
163 repoContent.setRepository( config.findManagedRepositoryById( repoId ) );
169 public void tearDown()
175 // MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
177 public void testRepositoryGroupFirstRepositoryRequiresAuthentication()
180 DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
181 + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
182 new ArchivaDavLocatorFactory() );
184 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
188 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
189 requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
190 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
191 createManagedRepositoryContent( RELEASES_REPO ) );
192 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
193 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
194 repoRequestControl.expectAndReturn(
195 repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
197 repoRequestControl.expectAndReturn(
198 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ), false );
199 repoRequestControl.expectAndReturn(
200 repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
202 repoRequestControl.expectAndReturn(
203 repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
205 new File( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
206 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
207 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
210 archivaConfigurationControl.replay();
211 requestControl.replay();
212 repoContentFactoryControl.replay();
213 repoRequestControl.replay();
215 resourceFactory.createResource( locator, request, response );
217 archivaConfigurationControl.verify();
218 requestControl.verify();
219 repoContentFactoryControl.verify();
220 repoRequestControl.verify();
222 fail( "A DavException with 401 error code should have been thrown." );
224 catch ( DavException e )
226 assertEquals( 401, e.getErrorCode() );
231 public void testRepositoryGroupLastRepositoryRequiresAuthentication()
234 DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
235 + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
236 new ArchivaDavLocatorFactory() );
238 List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
239 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
240 repoGroup.setId( LOCAL_REPO_GROUP );
241 repoGroup.addRepository( INTERNAL_REPO );
242 repoGroup.addRepository( RELEASES_REPO );
244 repoGroups.add( repoGroup );
246 config.setRepositoryGroups( repoGroups );
248 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
252 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
253 requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
254 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
256 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
257 createManagedRepositoryContent( RELEASES_REPO ) );
258 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
259 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
260 repoRequestControl.expectAndReturn(
261 repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
263 repoRequestControl.expectAndReturn(
264 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ), false );
265 repoRequestControl.expectAndReturn(
266 repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
268 repoRequestControl.expectAndReturn(
269 repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
271 new File( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
272 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
274 archivaConfigurationControl.replay();
275 requestControl.replay();
276 repoContentFactoryControl.replay();
277 repoRequestControl.replay();
279 resourceFactory.createResource( locator, request, response );
281 archivaConfigurationControl.verify();
282 requestControl.verify();
283 repoContentFactoryControl.verify();
284 repoRequestControl.verify();
286 fail( "A DavException with 401 error code should have been thrown." );
288 catch ( DavException e )
290 assertEquals( 401, e.getErrorCode() );
295 public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled()
298 DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
299 + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
300 new ArchivaDavLocatorFactory() );
302 config.addManagedRepository(
303 createManagedRepository( LOCAL_MIRROR_REPO, new File( "target/test-classes/local-mirror" ).getPath(),
306 List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
307 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
308 repoGroup.setId( LOCAL_REPO_GROUP );
309 repoGroup.addRepository( INTERNAL_REPO );
310 repoGroup.addRepository( LOCAL_MIRROR_REPO );
312 repoGroups.add( repoGroup );
314 config.setRepositoryGroups( repoGroups );
316 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
317 ManagedRepositoryContent localMirrorRepo = createManagedRepositoryContent( LOCAL_MIRROR_REPO );
321 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
322 requestControl.expectAndReturn( request.getMethod(), "GET", 4 );
323 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
325 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ),
327 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 4 );
328 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 4 );
329 repoRequestControl.expectAndReturn(
330 repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ), false,
332 repoRequestControl.expectAndReturn(
333 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ), false, 2 );
334 repoRequestControl.expectAndReturn(
335 repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
337 repoRequestControl.expectAndReturn(
338 repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
340 new File( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
341 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
343 repoRequestControl.expectAndReturn(
344 repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
346 new File( config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(),
347 "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
349 archivaConfigurationControl.replay();
350 requestControl.replay();
351 repoContentFactoryControl.replay();
352 repoRequestControl.replay();
354 resourceFactory.createResource( locator, request, response );
356 archivaConfigurationControl.verify();
357 requestControl.verify();
358 repoContentFactoryControl.verify();
359 repoRequestControl.verify();
361 fail( "A DavException with 404 error code should have been thrown." );
363 catch ( DavException e )
365 assertEquals( 404, e.getErrorCode() );
371 public void testRequestArtifactMetadataThreePartsRepoHasDefaultLayout()
374 // should fetch metadata
375 DavResourceLocator locator =
376 new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml",
377 INTERNAL_REPO, new ArchivaDavLocatorFactory() );
379 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
381 // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
382 RepositoryRequest repoRequest = new RepositoryRequest( new LegacyPathParser( this.archivaConfiguration ) );
383 resourceFactory.setRepositoryRequest( repoRequest );
387 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
388 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
390 requestControl.expectAndReturn( request.getMethod(), "GET", 3 );
391 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 3 );
392 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
393 requestControl.expectAndReturn( request.getRequestURI(),
394 "http://localhost:8080/archiva/repository/" + INTERNAL_REPO
395 + "/eclipse/jdtcore/maven-metadata.xml" );
396 response.addHeader( "Pragma", "no-cache" );
397 responseControl.setVoidCallable();
399 response.addHeader( "Cache-Control", "no-cache" );
400 responseControl.setVoidCallable();
402 long date = 2039842134;
403 response.addDateHeader( "last-modified", date );
404 responseControl.setVoidCallable();
406 archivaConfigurationControl.replay();
407 repoContentFactoryControl.replay();
408 requestControl.replay();
409 responseControl.replay();
411 resourceFactory.createResource( locator, request, response );
413 archivaConfigurationControl.verify();
414 repoContentFactoryControl.verify();
415 requestControl.verify();
416 responseControl.verify();
418 catch ( DavException e )
420 fail( "A DavException should not have been thrown!" );
425 public void testRequestArtifactMetadataTwoPartsRepoHasDefaultLayout()
428 // should not fetch metadata
429 DavResourceLocator locator =
430 new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/maven-metadata.xml",
431 INTERNAL_REPO, new ArchivaDavLocatorFactory() );
433 ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
435 // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
436 RepositoryRequest repoRequest = new RepositoryRequest( new LegacyPathParser( this.archivaConfiguration ) );
437 resourceFactory.setRepositoryRequest( repoRequest );
441 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
442 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
444 requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
445 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
446 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
448 archivaConfigurationControl.replay();
449 repoContentFactoryControl.replay();
450 requestControl.replay();
452 resourceFactory.createResource( locator, request, response );
454 archivaConfigurationControl.verify();
455 repoContentFactoryControl.verify();
456 requestControl.verify();
458 fail( "A 404 error should have been thrown!" );
460 catch ( DavException e )
462 assertEquals( 404, e.getErrorCode() );
467 public void testRequestMetadataRepoIsLegacy()
470 config.addManagedRepository(
471 createManagedRepository( LEGACY_REPO, new File( "target/test-classes/" + LEGACY_REPO ).getPath(),
473 DavResourceLocator locator =
474 new ArchivaDavResourceLocator( "", "/repository/" + LEGACY_REPO + "/eclipse/maven-metadata.xml",
475 LEGACY_REPO, new ArchivaDavLocatorFactory() );
477 ManagedRepositoryContent legacyRepo = createManagedRepositoryContent( LEGACY_REPO );
479 // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
480 RepositoryRequest repoRequest = new RepositoryRequest( new LegacyPathParser( this.archivaConfiguration ) );
481 resourceFactory.setRepositoryRequest( repoRequest );
485 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
486 repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( LEGACY_REPO ),
488 requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
489 requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
490 requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
492 archivaConfigurationControl.replay();
493 repoContentFactoryControl.replay();
494 requestControl.replay();
496 resourceFactory.createResource( locator, request, response );
498 archivaConfigurationControl.verify();
499 repoContentFactoryControl.verify();
500 requestControl.verify();
502 fail( "A 404 error should have been thrown!" );
504 catch ( DavException e )
506 assertEquals( 404, e.getErrorCode() );
510 class OverridingArchivaDavResourceFactory
511 extends ArchivaDavResourceFactory
514 OverridingArchivaDavResourceFactory( ApplicationContext applicationContext, PlexusSisuBridge plexusSisuBridge,
515 ArchivaConfiguration archivaConfiguration )
516 throws PlexusSisuBridgeException
518 super( applicationContext, plexusSisuBridge, archivaConfiguration );
521 protected boolean isAuthorized( DavServletRequest request, String repositoryId )
524 if ( RELEASES_REPO.equals( repositoryId ) )
526 throw new UnauthorizedDavException( repositoryId,
527 "You are not authenticated and authorized to access any repository." );
535 protected String getActivePrincipal( DavServletRequest request )
541 class OverridingRepositoryProxyConnectors
542 extends DefaultRepositoryProxyConnectors
544 public File fetchMetatadaFromProxies( ManagedRepositoryContent repository, String logicalPath )
546 File target = new File( repository.getRepoRoot(), logicalPath );
549 FileUtils.copyFile( new File( "target/test-classes/maven-metadata.xml" ), target );
551 catch ( IOException e )