1 package org.apache.archiva;
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.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;
45 import java.util.List;
46 import java.util.zip.ZipEntry;
47 import java.util.zip.ZipFile;
50 * @author Olivier Lamy
52 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
53 public class DownloadSnapshotTest
54 extends AbstractDownloadTest
56 protected Logger log = LoggerFactory.getLogger( getClass() );
58 public Server redirectServer = null;
60 public int redirectPort;
62 public Server repoServer = null;
64 public int repoServerPort;
67 public static void setAppServerBase()
69 previousAppServerBase = System.getProperty( "appserver.base" );
70 System.setProperty( "appserver.base", "target/" + DownloadSnapshotTest.class.getName() );
75 public static void resetAppServerBase()
77 System.setProperty( "appserver.base", previousAppServerBase );
80 protected String getSpringConfigLocation()
82 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
87 public void downloadSNAPSHOT()
91 File tmpIndexDir = new File( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex" );
92 if ( tmpIndexDir.exists() )
94 FileUtils.deleteDirectory( tmpIndexDir );
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 );
103 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
105 if ( managedRepositoriesService.getManagedRepository( id ) != null )
107 managedRepositoriesService.deleteManagedRepository( id, false );
110 getManagedRepositoriesService().addManagedRepository( managedRepository );
112 RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
114 if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
117 roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id );
120 getUserService( authorizationHeader ).createGuestUser();
121 roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
123 roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, id,
126 getUserService( authorizationHeader ).removeFromCache( "guest" );
128 File file = new File( "target/archiva-model-1.4-M4-SNAPSHOT.jar" );
134 HttpWagon httpWagon = new HttpWagon();
135 httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
137 httpWagon.get( "/repository/"+ id +"/org/apache/archiva/archiva-model/1.4-M4-SNAPSHOT/archiva-model-1.4-M4-SNAPSHOT.jar", file );
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 );