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.junit.After;
27 import org.junit.AfterClass;
28 import org.junit.Assert;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
33 import javax.ws.rs.core.Response;
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
41 * @author Olivier Lamy
43 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
44 public class DownloadArtifactFromQueryTest
45 extends AbstractDownloadTest
49 public static void setAppServerBase()
52 previousAppServerBase = System.getProperty( "appserver.base" );
53 System.setProperty( "appserver.base",
54 new File( System.getProperty( "java.io.tmpdir" ) ).getCanonicalPath() + "/target/"
55 + DownloadArtifactFromQueryTest.class.getName()
60 public static void resetAppServerBase()
62 System.setProperty( "appserver.base", previousAppServerBase );
66 protected String getSpringConfigLocation()
68 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
76 Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" );
77 if ( Files.exists( tmpIndexDir ) )
79 FileUtils.deleteDirectory( tmpIndexDir.toFile() );
84 protected String createAndScanRepo()
88 Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" );
89 if ( Files.exists( tmpIndexDir ) )
91 FileUtils.deleteDirectory( tmpIndexDir.toFile() );
93 String id = Long.toString( System.currentTimeMillis() );
94 ManagedRepository managedRepository = new ManagedRepository();
95 managedRepository.setId( id );
96 managedRepository.setName( "name of " + id );
97 managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
98 managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
100 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
102 if ( managedRepositoriesService.getManagedRepository( id ) != null )
104 managedRepositoriesService.deleteManagedRepository( id, false );
107 getManagedRepositoriesService().addManagedRepository( managedRepository );
109 RepositoriesService repositoriesService = getRepositoriesService();
111 repositoriesService.scanRepositoryNow( id, true );
113 // wait a bit to ensure index is finished
115 while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
126 public void downloadFixedVersion()
130 String id = createAndScanRepo();
135 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "1.0", null,
138 Assert.assertEquals( Response.Status.TEMPORARY_REDIRECT.getStatusCode(), response.getStatus() );
140 String location = String.class.cast( response.getMetadata().get( "Location" ).get( 0 ) );
142 /// http://localhost:57168/repository/1400639145722/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar
144 Assert.assertEquals( "http://localhost:" + port + "/repository/" + id
145 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar", location
150 getManagedRepositoriesService().deleteManagedRepository( id, false );
157 public void downloadLatestVersion()
160 String id = createAndScanRepo();
165 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null,
168 Assert.assertEquals( Response.Status.TEMPORARY_REDIRECT.getStatusCode(), response.getStatus() );
170 String location = String.class.cast( response.getMetadata().get( "Location" ).get( 0 ) );
172 /// http://localhost:57168/repository/1400639145722/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar
174 Assert.assertEquals( "http://localhost:" + port + "/repository/" + id
175 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar", location
182 getManagedRepositoriesService().deleteManagedRepository( id, false );
188 public void download_no_content()
191 String id = createAndScanRepo();
196 getSearchService().redirectToArtifactFile( null, "org.apache.archiva.beer", "archiva-wine", "LATEST", null,
199 Assert.assertEquals( Response.Status.NO_CONTENT.getStatusCode(), response.getStatus() );
205 getManagedRepositoriesService().deleteManagedRepository( id, false );