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.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
34 import javax.ws.rs.RedirectionException;
35 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;
44 * @author Olivier Lamy
46 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
47 public class DownloadArtifactFromQueryTest
48 extends AbstractDownloadTest
52 public static void setAppServerBase()
55 previousAppServerBase = System.getProperty( "appserver.base" );
56 System.setProperty( "appserver.base",
57 new File( System.getProperty( "java.io.tmpdir" ) ).getCanonicalPath() + "/target/"
58 + DownloadArtifactFromQueryTest.class.getName() );
62 public static void resetAppServerBase()
64 System.setProperty( "appserver.base", previousAppServerBase );
68 protected String getSpringConfigLocation()
70 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
78 Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" );
79 if ( Files.exists( tmpIndexDir ) )
81 FileUtils.deleteDirectory( tmpIndexDir.toFile() );
86 protected String createAndScanRepo()
90 Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" );
91 if ( Files.exists( tmpIndexDir ) )
93 FileUtils.deleteDirectory( tmpIndexDir.toFile() );
95 String id = Long.toString( System.currentTimeMillis() );
96 ManagedRepository managedRepository = new ManagedRepository();
97 managedRepository.setId( id );
98 managedRepository.setName( "name of " + id );
99 managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
100 managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
102 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
104 if ( managedRepositoriesService.getManagedRepository( id ) != null )
106 managedRepositoriesService.deleteManagedRepository( id, false );
109 getManagedRepositoriesService().addManagedRepository( managedRepository );
111 RepositoriesService repositoriesService = getRepositoriesService();
113 repositoriesService.scanRepositoryNow( id, true );
115 // wait a bit to ensure index is finished
117 while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
127 @Test( expected = RedirectionException.class )
128 public void downloadFixedVersion()
132 String id = createAndScanRepo();
137 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "1.0", null,
141 catch ( RedirectionException e )
143 Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
144 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ) ).isEqualTo(
150 getManagedRepositoriesService().deleteManagedRepository( id, false );
156 @Test( expected = RedirectionException.class )
157 public void downloadLatestVersion()
160 String id = createAndScanRepo();
165 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null,
169 catch ( RedirectionException e )
171 Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
172 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ) ).isEqualTo(
178 getManagedRepositoriesService().deleteManagedRepository( id, false );
184 public void download_no_content()
187 String id = createAndScanRepo();
192 getSearchService().redirectToArtifactFile( null, "org.apache.archiva.beer", "archiva-wine", "LATEST",
195 Assert.assertEquals( Response.Status.NO_CONTENT.getStatusCode(), response.getStatus() );
201 getManagedRepositoriesService().deleteManagedRepository( id, false );