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