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