]> source.dussan.org Git - archiva.git/blob
f5913b7d43cacc8bc38dc932516164254e89fa7a
[archiva.git] /
1 package org.apache.archiva.remotedownload;
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.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.redback.rest.api.services.RoleManagementService;
24 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
25 import org.apache.archiva.security.common.ArchivaRoleConstants;
26 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.maven.wagon.providers.http.HttpWagon;
29 import org.apache.maven.wagon.repository.Repository;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.List;
41 import java.util.zip.ZipEntry;
42 import java.util.zip.ZipFile;
43
44 /**
45  * @author Olivier Lamy
46  */
47 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
48 public class DownloadSnapshotTest
49     extends AbstractDownloadTest
50 {
51     protected Logger log = LoggerFactory.getLogger( getClass() );
52
53     @BeforeClass
54     public static void setAppServerBase()
55     {
56         previousAppServerBase = System.getProperty( "appserver.base" );
57         System.setProperty( "appserver.base", "target/" + DownloadSnapshotTest.class.getName() );
58     }
59
60
61     @AfterClass
62     public static void resetAppServerBase()
63     {
64         System.setProperty( "appserver.base", previousAppServerBase );
65     }
66
67     @Override
68     protected String getSpringConfigLocation()
69     {
70         return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
71     }
72
73
74     @Test
75     public void downloadSNAPSHOT()
76         throws Exception
77     {
78
79         Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
80         if ( Files.exists(tmpIndexDir) )
81         {
82             org.apache.archiva.common.utils.FileUtils.deleteDirectory( tmpIndexDir );
83         }
84         String id = Long.toString( System.currentTimeMillis() );
85         ManagedRepository managedRepository = new ManagedRepository();
86         managedRepository.setId( id );
87         managedRepository.setName( "name of " + id );
88         managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/snapshot-repo" );
89         managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
90
91         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
92
93         if ( managedRepositoriesService.getManagedRepository( id ) != null )
94         {
95             managedRepositoriesService.deleteManagedRepository( id, false );
96         }
97
98         getManagedRepositoriesService().addManagedRepository( managedRepository );
99
100         RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
101
102         if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
103                                                          id ) )
104         {
105             roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id );
106         }
107
108         getUserService( authorizationHeader ).createGuestUser();
109         roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
110
111         roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id,
112                                                    "guest" );
113
114         getUserService( authorizationHeader ).removeFromCache( "guest" );
115
116         Path file = Paths.get( "target/archiva-model-1.4-M4-SNAPSHOT.jar" );
117         Files.deleteIfExists(file);
118
119         HttpWagon httpWagon = new HttpWagon();
120         httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
121
122         httpWagon.get( "/repository/"+ id +"/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-SNAPSHOT.jar", file.toFile() );
123
124         ZipFile zipFile = new ZipFile( file.toFile() );
125         List<String> entries = getZipEntriesNames( zipFile );
126         ZipEntry zipEntry = zipFile.getEntry( "org/apache/archiva/model/ArchivaArtifact.class" );
127         assertNotNull( "cannot find zipEntry org/apache/archiva/model/ArchivaArtifact.class, entries: " + entries + ", content is: "
128                            + FileUtils.readFileToString( file.toFile() ), zipEntry );
129         zipFile.close();
130         file.toFile().deleteOnExit();
131
132
133
134     }
135
136 }