]> source.dussan.org Git - archiva.git/blob
4ef1ff4d3ec35f468d2533c6e2de798e4b71305a
[archiva.git] /
1 package org.apache.archiva.webdav;
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 junit.framework.TestCase;
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.beans.RepositoryGroup;
26 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
27 import org.apache.archiva.admin.repository.DefaultRepositoryCommonValidator;
28 import org.apache.archiva.admin.repository.group.DefaultRepositoryGroupAdmin;
29 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
30 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
31 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
32 import org.apache.archiva.configuration.ArchivaConfiguration;
33 import org.apache.archiva.configuration.Configuration;
34 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
35 import org.apache.archiva.proxy.DefaultRepositoryProxyConnectors;
36 import org.apache.archiva.repository.ManagedRepositoryContent;
37 import org.apache.archiva.repository.RepositoryContentFactory;
38 import org.apache.archiva.repository.content.legacy.LegacyPathParser;
39 import org.apache.archiva.repository.content.maven2.ManagedDefaultRepositoryContent;
40 import org.apache.archiva.repository.content.maven2.RepositoryRequest;
41 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
42 import org.apache.commons.io.FileUtils;
43 import org.apache.jackrabbit.webdav.DavException;
44 import org.apache.jackrabbit.webdav.DavResourceLocator;
45 import org.apache.jackrabbit.webdav.DavServletRequest;
46 import org.apache.jackrabbit.webdav.DavServletResponse;
47
48 import org.easymock.IMocksControl;
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.springframework.context.ApplicationContext;
54 import org.springframework.test.context.ContextConfiguration;
55
56 import javax.inject.Inject;
57 import java.io.File;
58 import java.io.IOException;
59 import java.util.ArrayList;
60 import java.util.List;
61
62 import static org.easymock.EasyMock.*;
63
64 /**
65  * ArchivaDavResourceFactoryTest
66  */
67 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
68 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
69 public class ArchivaDavResourceFactoryTest
70     extends TestCase
71 {
72     private static final String RELEASES_REPO = "releases";
73
74     private static final String INTERNAL_REPO = "internal";
75
76     private static final String LOCAL_MIRROR_REPO = "local-mirror";
77
78     private static final String LEGACY_REPO = "legacy-repo";
79
80     private static final String LOCAL_REPO_GROUP = "local";
81
82     private OverridingArchivaDavResourceFactory resourceFactory;
83
84     private IMocksControl requestControl;
85
86     private DavServletRequest request;
87
88     private IMocksControl repoRequestControl;
89
90     private RepositoryRequest repoRequest;
91
92     private IMocksControl responseControl;
93
94     private DavServletResponse response;
95
96     private IMocksControl archivaConfigurationControl;
97
98     private ArchivaConfiguration archivaConfiguration;
99
100     private Configuration config;
101
102     private IMocksControl repoContentFactoryControl;
103
104     private RepositoryContentFactory repoFactory;
105
106     @Inject
107     ApplicationContext applicationContext;
108
109     @Inject
110     PlexusSisuBridge plexusSisuBridge;
111
112     @Inject
113     DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin;
114
115     @Inject
116     RemoteRepositoryAdmin remoteRepositoryAdmin;
117
118
119     @Inject
120     DefaultRepositoryGroupAdmin defaultRepositoryGroupAdmin;
121
122
123     @Before
124     public void setUp()
125         throws Exception
126     {
127         super.setUp();
128
129         requestControl = createControl();
130         request = requestControl.createMock( DavServletRequest.class );
131
132         responseControl = createControl();
133         response = responseControl.createMock( DavServletResponse.class );
134         //responseControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
135
136         archivaConfigurationControl = createControl();
137         archivaConfiguration = archivaConfigurationControl.createMock( ArchivaConfiguration.class );
138
139         config = new Configuration();
140         //archivaConfiguration.getConfiguration();
141         //archivaConfigurationControl.setReturnValue( config, 5, 20 );
142         expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 5, 20 );
143         archivaConfiguration.save( config );
144         //archivaConfigurationControl.setVoidCallable( 1, 4 );
145         expectLastCall().times( 1, 4 );
146         archivaConfigurationControl.replay();
147
148         defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfiguration );
149         ( (DefaultRepositoryCommonValidator) defaultManagedRepositoryAdmin.getRepositoryCommonValidator() ).setArchivaConfiguration(
150             archivaConfiguration );
151         if ( defaultManagedRepositoryAdmin.getManagedRepository( RELEASES_REPO ) == null )
152         {
153             defaultManagedRepositoryAdmin.addManagedRepository(
154                 createManagedRepository( RELEASES_REPO, new File( "target/test-classes/" + RELEASES_REPO ).getPath(),
155                                          "default" ), false, null );
156         }
157         if ( defaultManagedRepositoryAdmin.getManagedRepository( INTERNAL_REPO ) == null )
158         {
159             defaultManagedRepositoryAdmin.addManagedRepository(
160                 createManagedRepository( INTERNAL_REPO, new File( "target/test-classes/" + INTERNAL_REPO ).getPath(),
161                                          "default" ), false, null );
162         }
163         RepositoryGroup repoGroupConfig = new RepositoryGroup();
164         repoGroupConfig.setId( LOCAL_REPO_GROUP );
165         repoGroupConfig.addRepository( RELEASES_REPO );
166         repoGroupConfig.addRepository( INTERNAL_REPO );
167
168         defaultRepositoryGroupAdmin.setArchivaConfiguration( archivaConfiguration );
169         if ( defaultManagedRepositoryAdmin.getManagedRepository( LOCAL_REPO_GROUP ) == null )
170         {
171             defaultRepositoryGroupAdmin.addRepositoryGroup( repoGroupConfig, null );
172         }
173
174         repoContentFactoryControl = createControl();
175         repoFactory = repoContentFactoryControl.createMock( RepositoryContentFactory.class );
176
177         repoRequestControl = createControl();
178         repoRequest = repoRequestControl.createMock( RepositoryRequest.class );
179
180         resourceFactory =
181             new OverridingArchivaDavResourceFactory( applicationContext, plexusSisuBridge, archivaConfiguration );
182         resourceFactory.setArchivaConfiguration( archivaConfiguration );
183         resourceFactory.setRepositoryFactory( repoFactory );
184         resourceFactory.setRepositoryRequest( repoRequest );
185         resourceFactory.setConnectors( new OverridingRepositoryProxyConnectors() );
186         resourceFactory.setRemoteRepositoryAdmin( remoteRepositoryAdmin );
187     }
188
189     private ManagedRepository createManagedRepository( String id, String location, String layout )
190     {
191         ManagedRepository repoConfig = new ManagedRepository();
192         repoConfig.setId( id );
193         repoConfig.setName( id );
194         repoConfig.setLocation( location );
195         repoConfig.setLayout( layout );
196
197         return repoConfig;
198     }
199
200     private ManagedRepositoryContent createManagedRepositoryContent( String repoId )
201         throws RepositoryAdminException
202     {
203         ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
204         repoContent.setRepository( defaultManagedRepositoryAdmin.getManagedRepository( repoId ) );
205
206         return repoContent;
207     }
208
209     @After
210     public void tearDown()
211         throws Exception
212     {
213         super.tearDown();
214     }
215
216     // MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
217     @Test
218     public void testRepositoryGroupFirstRepositoryRequiresAuthentication()
219         throws Exception
220     {
221         DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
222             + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
223                                                                     new ArchivaDavLocatorFactory() );
224
225         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
226         ManagedRepositoryContent releasesRepo = createManagedRepositoryContent( RELEASES_REPO );
227
228         try
229         {
230             archivaConfigurationControl.reset();
231             //archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
232             expect( archivaConfiguration.getConfiguration() ).andReturn( config );
233             //requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
234             expect( request.getMethod() ).andReturn( "GET" ).times( 2 );
235             //requestControl.expectAndReturn( request.getPathInfo(), "org/apache/archiva", 0, 2 );
236             expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 );
237             //repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
238             //                                           releasesRepo );
239             expect( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).andReturn( releasesRepo );
240             //requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
241             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 2 );
242             //requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
243             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
244
245             //repoRequestControl.expectAndReturn(
246             //    repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
247             //    false );
248             expect( repoRequest.isSupportFile(
249                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( true );
250             //repoRequestControl.expectAndReturn(
251             //    repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ), false );
252             expect(
253                 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn(
254                 false );
255             //repoRequestControl.expectAndReturn(
256             //    repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
257             //    null );
258             expect( repoRequest.toArtifactReference(
259                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null );
260             //repoRequestControl.expectAndReturn(
261             //    repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", internalRepo ),
262             //    new File( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
263             //              "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
264             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
265                                               internalRepo ) ).andReturn(
266                 new File( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
267                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
268             //repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
269             //                                           internalRepo );
270             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
271             //repoRequestControl.expectAndReturn(
272             //        repoRequest.isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
273             //        false );
274             expect( repoRequest.isArchetypeCatalog(
275                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false );
276             archivaConfigurationControl.replay();
277             requestControl.replay();
278             repoContentFactoryControl.replay();
279             repoRequestControl.replay();
280
281             resourceFactory.createResource( locator, request, response );
282
283             archivaConfigurationControl.verify();
284             requestControl.verify();
285             repoContentFactoryControl.verify();
286             repoRequestControl.verify();
287
288             fail( "A DavException with 401 error code should have been thrown." );
289         }
290         catch ( DavException e )
291         {
292             assertEquals( 401, e.getErrorCode() );
293         }
294     }
295
296     @Test
297     public void testRepositoryGroupLastRepositoryRequiresAuthentication()
298         throws Exception
299     {
300         DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
301             + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
302                                                                     new ArchivaDavLocatorFactory() );
303
304         List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
305         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
306         repoGroup.setId( LOCAL_REPO_GROUP );
307         repoGroup.addRepository( INTERNAL_REPO );
308         repoGroup.addRepository( RELEASES_REPO );
309
310         repoGroups.add( repoGroup );
311
312         config.setRepositoryGroups( repoGroups );
313
314         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
315
316         ManagedRepositoryContent releasesRepo = createManagedRepositoryContent( RELEASES_REPO );
317
318         try
319         {
320             archivaConfigurationControl.reset();
321             //archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
322             expect( archivaConfiguration.getConfiguration() ).andReturn( config );
323             //requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
324             expect( request.getMethod() ).andReturn( "GET" ).times( 2 );
325             //requestControl.expectAndReturn( request.getPathInfo(), "org/apache/archiva", 0, 2 );
326             expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 );
327             //repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
328             //                                           internalRepo );
329             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
330             //repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
331             //                                           releasesRepo );
332             expect( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).andReturn( releasesRepo );
333             //requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
334             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 2 );
335             //requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
336             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
337             //repoRequestControl.expectAndReturn(
338             //    repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
339             //    false );
340             expect( repoRequest.isSupportFile(
341                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false );
342             //repoRequestControl.expectAndReturn(
343             //    repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ), false );
344             expect(
345                 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn(
346                 false );
347             //repoRequestControl.expectAndReturn(
348             //    repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
349             //    null );
350             expect( repoRequest.toArtifactReference(
351                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null );
352             //repoRequestControl.expectAndReturn(
353             //    repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", internalRepo ),
354             //    new File( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
355             //              "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
356             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
357                                               internalRepo ) ).andReturn(
358                 new File( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
359                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
360
361             //repoRequestControl.expectAndReturn(
362             //        repoRequest.isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
363             //        false );
364             expect( repoRequest.isArchetypeCatalog(
365                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false );
366             archivaConfigurationControl.replay();
367             requestControl.replay();
368             repoContentFactoryControl.replay();
369             repoRequestControl.replay();
370
371             resourceFactory.createResource( locator, request, response );
372
373             archivaConfigurationControl.verify();
374             requestControl.verify();
375             repoContentFactoryControl.verify();
376             repoRequestControl.verify();
377
378             fail( "A DavException with 401 error code should have been thrown." );
379         }
380         catch ( DavException e )
381         {
382             assertEquals( 401, e.getErrorCode() );
383         }
384     }
385
386     @Test
387     public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled()
388         throws Exception
389     {
390         DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
391             + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
392                                                                     new ArchivaDavLocatorFactory() );
393
394         defaultManagedRepositoryAdmin.addManagedRepository(
395             createManagedRepository( LOCAL_MIRROR_REPO, new File( "target/test-classes/local-mirror" ).getPath(),
396                                      "default" ), false, null );
397
398         List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
399         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
400         repoGroup.setId( LOCAL_REPO_GROUP );
401         repoGroup.addRepository( INTERNAL_REPO );
402         repoGroup.addRepository( LOCAL_MIRROR_REPO );
403
404         repoGroups.add( repoGroup );
405
406         config.setRepositoryGroups( repoGroups );
407
408         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
409         ManagedRepositoryContent localMirrorRepo = createManagedRepositoryContent( LOCAL_MIRROR_REPO );
410
411         try
412         {
413             archivaConfigurationControl.reset();
414             //archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
415             expect( archivaConfiguration.getConfiguration() ).andReturn( config );
416             //requestControl.expectAndReturn( request.getMethod(), "GET", 4 );
417             expect( request.getMethod() ).andReturn( "GET" ).times( 4 );
418             //requestControl.expectAndReturn( request.getPathInfo(), "org/apache/archiva", 0, 2 );
419             expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 );
420             //repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
421             //                                           internalRepo );
422             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
423             //repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ),
424             //                                           localMirrorRepo );
425             expect( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ) ).andReturn( localMirrorRepo );
426             //requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 4 );
427             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 4 );
428             //requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 4 );
429             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 4 );
430             //repoRequestControl.expectAndReturn(
431             //    repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ), false,
432             //    2 );
433             expect( repoRequest.isSupportFile(
434                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ).times( 2 );
435             //repoRequestControl.expectAndReturn(
436             //    repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ), false, 2 );
437             expect(
438                 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn(
439                 false ).times( 2 );
440             //repoRequestControl.expectAndReturn(
441             //    repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
442             //    null, 2 );
443             expect( repoRequest.toArtifactReference(
444                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null ).times( 2 );
445             //repoRequestControl.expectAndReturn(
446             //repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
447             //                          internalRepo ),
448             //new File( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
449             //          "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
450             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
451                                               internalRepo ) ).andReturn(
452                 new File( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
453                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
454             //repoRequestControl.expectAndReturn(
455             //    repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", localMirrorRepo ),
456             //    new File( config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(),
457             //              "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
458             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
459                                               localMirrorRepo ) )
460                 .andReturn( new File( config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(),
461                                       "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
462             //repoRequestControl.expectAndReturn(
463             //    repoRequest.isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
464             //    false, 2 );
465             expect( repoRequest.isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ).times( 2 );
466             archivaConfigurationControl.replay();
467             requestControl.replay();
468             repoContentFactoryControl.replay();
469             repoRequestControl.replay();
470
471             resourceFactory.createResource( locator, request, response );
472
473             archivaConfigurationControl.verify();
474             requestControl.verify();
475             repoContentFactoryControl.verify();
476             repoRequestControl.verify();
477
478             fail( "A DavException with 404 error code should have been thrown." );
479         }
480         catch ( DavException e )
481         {
482             assertEquals( 404, e.getErrorCode() );
483         }
484     }
485
486     // MRM-1239
487     @Test
488     public void testRequestArtifactMetadataThreePartsRepoHasDefaultLayout()
489         throws Exception
490     {
491         // should fetch metadata 
492         DavResourceLocator locator =
493             new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml",
494                                            INTERNAL_REPO, new ArchivaDavLocatorFactory() );
495
496         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
497
498         // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
499         RepositoryRequest repoRequest = new RepositoryRequest( new LegacyPathParser( this.archivaConfiguration ) );
500         resourceFactory.setRepositoryRequest( repoRequest );
501
502         try
503         {
504             archivaConfigurationControl.reset();
505             //archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
506             expect( archivaConfiguration.getConfiguration() ).andReturn( config );
507             //repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
508             //                                           internalRepo );
509             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
510             //requestControl.expectAndReturn( request.getMethod(), "GET", 3 );
511             expect( request.getMethod() ).andReturn( "GET" ).times( 3 );
512             //requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 3 );
513             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 3 );
514             //requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
515             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
516             //requestControl.expectAndReturn( request.getRequestURI(),
517             //                                "http://localhost:8080/archiva/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml" );
518             expect( request.getRequestURI() ).andReturn( "http://localhost:8080/archiva/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml" );
519             response.setHeader( "Pragma", "no-cache" );
520             //responseControl.setVoidCallable();
521             expectLastCall();
522
523             response.setHeader( "Cache-Control", "no-cache" );
524             //responseControl.setVoidCallable();
525             expectLastCall();
526
527             //long date = 2039842134;
528             response.setDateHeader( eq("Last-Modified"), anyLong() );
529             //responseControl.setVoidCallable();
530             expectLastCall();
531
532             archivaConfigurationControl.replay();
533             repoContentFactoryControl.replay();
534             requestControl.replay();
535             responseControl.replay();
536
537             resourceFactory.createResource( locator, request, response );
538
539             archivaConfigurationControl.verify();
540             repoContentFactoryControl.verify();
541             requestControl.verify();
542             responseControl.verify();
543         }
544         catch ( DavException e )
545         {
546             fail( "A DavException should not have been thrown!" );
547         }
548     }
549
550     @Test
551     public void testRequestArtifactMetadataTwoPartsRepoHasDefaultLayout()
552         throws Exception
553     {
554         // should not fetch metadata
555         DavResourceLocator locator =
556             new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/maven-metadata.xml",
557                                            INTERNAL_REPO, new ArchivaDavLocatorFactory() );
558
559         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
560
561         // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
562         RepositoryRequest repoRequest = new RepositoryRequest( new LegacyPathParser( this.archivaConfiguration ) );
563         resourceFactory.setRepositoryRequest( repoRequest );
564
565         try
566         {
567             archivaConfigurationControl.reset();
568             //archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
569             expect( archivaConfiguration.getConfiguration() ).andReturn( config );
570             //repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
571             //                                           internalRepo );
572             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
573             //requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
574             expect( request.getMethod() ).andReturn( "GET" ).times( 2 );
575             //requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
576             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 2 );
577             //requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
578             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
579
580             archivaConfigurationControl.replay();
581             repoContentFactoryControl.replay();
582             requestControl.replay();
583
584             resourceFactory.createResource( locator, request, response );
585
586             archivaConfigurationControl.verify();
587             repoContentFactoryControl.verify();
588             requestControl.verify();
589
590             fail( "A 404 error should have been thrown!" );
591         }
592         catch ( DavException e )
593         {
594             assertEquals( 404, e.getErrorCode() );
595         }
596     }
597
598     @Test
599     public void testRequestMetadataRepoIsLegacy()
600         throws Exception
601     {
602         defaultManagedRepositoryAdmin.addManagedRepository(
603             createManagedRepository( LEGACY_REPO, new File( "target/test-classes/" + LEGACY_REPO ).getPath(),
604                                      "legacy" ), false, null );
605         DavResourceLocator locator =
606             new ArchivaDavResourceLocator( "", "/repository/" + LEGACY_REPO + "/eclipse/maven-metadata.xml",
607                                            LEGACY_REPO, new ArchivaDavLocatorFactory() );
608
609         ManagedRepositoryContent legacyRepo = createManagedRepositoryContent( LEGACY_REPO );
610
611         // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
612         RepositoryRequest repoRequest = new RepositoryRequest( new LegacyPathParser( this.archivaConfiguration ) );
613         resourceFactory.setRepositoryRequest( repoRequest );
614
615         try
616         {
617             archivaConfigurationControl.reset();
618             //archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
619             expect( archivaConfiguration.getConfiguration() ).andReturn( config );
620             //repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( LEGACY_REPO ),
621             //                                           legacyRepo );
622             expect( repoFactory.getManagedRepositoryContent( LEGACY_REPO ) ).andReturn( legacyRepo );
623             //requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
624             expect( request.getMethod() ).andReturn( "GET" ).times( 2 );
625             //requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
626             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 2 );
627             //requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
628             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
629
630             archivaConfigurationControl.replay();
631             repoContentFactoryControl.replay();
632             requestControl.replay();
633
634             resourceFactory.createResource( locator, request, response );
635
636             archivaConfigurationControl.verify();
637             repoContentFactoryControl.verify();
638             requestControl.verify();
639
640             fail( "A 404 error should have been thrown!" );
641         }
642         catch ( DavException e )
643         {
644             assertEquals( 404, e.getErrorCode() );
645         }
646     }
647
648     class OverridingArchivaDavResourceFactory
649         extends ArchivaDavResourceFactory
650     {
651
652         OverridingArchivaDavResourceFactory( ApplicationContext applicationContext, PlexusSisuBridge plexusSisuBridge,
653                                              ArchivaConfiguration archivaConfiguration )
654             throws PlexusSisuBridgeException
655         {
656             super( applicationContext, plexusSisuBridge, archivaConfiguration );
657         }
658
659         protected boolean isAuthorized( DavServletRequest request, String repositoryId )
660             throws DavException
661         {
662             if ( RELEASES_REPO.equals( repositoryId ) )
663             {
664                 throw new UnauthorizedDavException( repositoryId,
665                                                     "You are not authenticated and authorized to access any repository." );
666             }
667             else
668             {
669                 return true;
670             }
671         }
672
673         protected String getActivePrincipal( DavServletRequest request )
674         {
675             return "guest";
676         }
677     }
678
679     class OverridingRepositoryProxyConnectors
680         extends DefaultRepositoryProxyConnectors
681     {
682         public File fetchMetatadaFromProxies( ManagedRepositoryContent repository, String logicalPath )
683         {
684             File target = new File( repository.getRepoRoot(), logicalPath );
685             try
686             {
687                 FileUtils.copyFile( new File( "target/test-classes/maven-metadata.xml" ), target );
688             }
689             catch ( IOException e )
690             {
691
692             }
693
694             return target;
695         }
696     }
697 }