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;
28 import org.junit.runner.RunWith;
30 import javax.ws.rs.RedirectionException;
31 import javax.ws.rs.core.Response;
32 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
39 * @author Olivier Lamy
41 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
42 public class DownloadArtifactFromQueryTest
43 extends AbstractDownloadTest
47 public static void setAppServerBase()
50 previousAppServerBase = System.getProperty( "appserver.base" );
51 System.setProperty( "appserver.base",
52 Paths.get( System.getProperty( "java.io.tmpdir" ) ).toAbsolutePath().resolve("target")
53 .resolve(DownloadArtifactFromQueryTest.class.getName() ).toString());
57 public static void resetAppServerBase()
59 System.setProperty( "appserver.base", previousAppServerBase );
63 protected String getSpringConfigLocation()
65 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
73 Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" );
74 if ( Files.exists( tmpIndexDir ) )
76 FileUtils.deleteDirectory( tmpIndexDir.toFile() );
81 protected String createAndScanRepo()
85 Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" );
86 if ( Files.exists( tmpIndexDir ) )
88 FileUtils.deleteDirectory( tmpIndexDir.toFile() );
90 String id = Long.toString( System.currentTimeMillis() );
91 ManagedRepository managedRepository = new ManagedRepository();
92 managedRepository.setId( id );
93 managedRepository.setName( "name of " + id );
94 managedRepository.setLocation( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" );
95 managedRepository.setIndexDirectory( System.getProperty( "java.io.tmpdir" ) + "/tmpIndex/" + id );
97 ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
99 if ( managedRepositoriesService.getManagedRepository( id ) != null )
101 managedRepositoriesService.deleteManagedRepository( id, false );
104 getManagedRepositoriesService().addManagedRepository( managedRepository );
106 RepositoriesService repositoriesService = getRepositoriesService();
108 repositoriesService.scanRepositoryNow( id, true );
110 // wait a bit to ensure index is finished
112 while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
122 @Test( expected = RedirectionException.class )
123 public void downloadFixedVersion()
127 String id = createAndScanRepo();
132 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "1.0", null,
136 catch ( RedirectionException e )
138 Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
139 + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ) ).isEqualTo(
145 getManagedRepositoriesService().deleteManagedRepository( id, false );
151 @Test( expected = RedirectionException.class )
152 public void downloadLatestVersion()
155 String id = createAndScanRepo();
160 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null,
164 catch ( RedirectionException e )
166 Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
167 + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ) ).isEqualTo(
173 getManagedRepositoriesService().deleteManagedRepository( id, false );
179 public void download_no_content()
182 String id = createAndScanRepo();
187 getSearchService().redirectToArtifactFile( null, "org.apache.archiva.beer", "archiva-wine", "LATEST",
190 Assert.assertEquals( Response.Status.NO_CONTENT.getStatusCode(), response.getStatus() );
196 getManagedRepositoriesService().deleteManagedRepository( id, false );