1 package org.apache.archiva.remotedownload;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.beans.ManagedRepository;
22 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
23 import org.apache.archiva.rest.api.services.RepositoriesService;
24 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
25 import org.apache.commons.io.FileUtils;
26 import org.assertj.core.api.Assertions;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
35 import javax.ws.rs.RedirectionException;
36 import javax.ws.rs.core.Response;
37 import java.io.IOException;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.Locale;
45 * @author Olivier Lamy
47 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
48 public class DownloadArtifactFromQueryTest
49 extends AbstractDownloadTest
52 private static Path appServerBase;
54 private Path indexDir;
57 public static void setAppServerBase()
60 previousAppServerBase = System.getProperty( "appserver.base" );
61 appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv1_" ).toAbsolutePath();
62 System.setProperty( "appserver.base", appServerBase.toString( ) );
66 public static void resetAppServerBase()
68 if (Files.exists(appServerBase)) {
69 FileUtils.deleteQuietly( appServerBase.toFile() );
71 System.setProperty( "appserver.base", previousAppServerBase );
75 protected String getSpringConfigLocation()
77 System.out.println( "Appserver base: " + System.getProperty( "appserver.base" ) );
78 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
82 public void init() throws IOException
84 indexDir = Files.createTempDirectory( "archiva-web-common-index" );
92 if ( Files.exists( indexDir ) )
94 FileUtils.deleteDirectory( indexDir.toFile() );
99 protected String createAndScanRepo()
103 String id = Long.toString( System.currentTimeMillis() );
104 Path srcRep = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
105 Path testRep = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
106 FileUtils.copyDirectory( srcRep.toFile( ), testRep.toFile( ) );
107 createdPaths.add( testRep );
110 ManagedRepository managedRepository = new ManagedRepository( Locale.getDefault());
111 managedRepository.setId( id );
112 managedRepository.setName( "name of " + id );
113 managedRepository.setLocation( testRep.toString() );
114 managedRepository.setIndexDirectory( indexDir.resolve( "index-"+id ).toString());
115 managedRepository.setPackedIndexDirectory( indexDir.resolve( "indexpacked-"+id ).toString());
117 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
119 if ( managedRepositoriesService.getManagedRepository( id ) != null )
121 managedRepositoriesService.deleteManagedRepository( id, false );
124 getManagedRepositoriesService().addManagedRepository( managedRepository );
126 RepositoriesService repositoriesService = getRepositoriesService();
128 repositoriesService.scanRepositoryNow( id, true );
130 // wait a bit to ensure index is finished
132 while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
142 @Test( expected = RedirectionException.class )
143 public void downloadFixedVersion()
147 String id = createAndScanRepo();
152 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "1.0", null,
156 catch ( RedirectionException e )
158 Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
159 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ) ).isEqualTo(
165 getManagedRepositoriesService().deleteManagedRepository( id, false );
171 @Test( expected = RedirectionException.class )
172 public void downloadLatestVersion()
175 String id = createAndScanRepo();
180 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null,
184 catch ( RedirectionException e )
186 Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
187 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ) ).isEqualTo(
193 getManagedRepositoriesService().deleteManagedRepository( id, false );
199 public void download_no_content()
202 String id = createAndScanRepo();
207 getSearchService().redirectToArtifactFile( null, "org.apache.archiva.beer", "archiva-wine", "LATEST",
210 Assert.assertEquals( Response.Status.NO_CONTENT.getStatusCode(), response.getStatus() );
216 getManagedRepositoriesService().deleteManagedRepository( id, false );