]> source.dussan.org Git - archiva.git/blob
b13e2472207f0c50e46c58c18e36fd59a82a11a0
[archiva.git] /
1 package org.apache.archiva.remotedownload;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
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;
34
35 import javax.ws.rs.RedirectionException;
36 import javax.ws.rs.core.Response;
37 import java.io.IOException;
38 import java.net.URI;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.Locale;
43
44 /**
45  * @author Olivier Lamy
46  */
47 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
48 public class DownloadArtifactFromQueryTest
49     extends AbstractDownloadTest
50 {
51
52     private static Path appServerBase;
53
54     private Path indexDir;
55
56     @BeforeClass
57     public static void setAppServerBase()
58         throws IOException
59     {
60         previousAppServerBase = System.getProperty( "appserver.base" );
61         appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv1_" ).toAbsolutePath();
62         System.setProperty( "appserver.base", appServerBase.toString( ) );
63     }
64
65     @AfterClass
66     public static void resetAppServerBase()
67     {
68         if (Files.exists(appServerBase)) {
69             FileUtils.deleteQuietly( appServerBase.toFile() );
70         }
71         System.setProperty( "appserver.base", previousAppServerBase );
72     }
73
74     @Override
75     protected String getSpringConfigLocation()
76     {
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";
79     }
80
81     @Before
82     public void init() throws IOException
83     {
84         indexDir = Files.createTempDirectory( "archiva-web-common-index" );
85     }
86
87     @After
88     public void cleanup()
89         throws Exception
90     {
91         super.tearDown();
92         if ( Files.exists( indexDir ) )
93         {
94             FileUtils.deleteDirectory( indexDir.toFile() );
95         }
96     }
97
98
99     protected String createAndScanRepo()
100         throws Exception
101     {
102
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 );
108
109
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());
116
117         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
118
119         if ( managedRepositoriesService.getManagedRepository( id ) != null )
120         {
121             managedRepositoriesService.deleteManagedRepository( id, false );
122         }
123
124         getManagedRepositoriesService().addManagedRepository( managedRepository );
125
126         RepositoriesService repositoriesService = getRepositoriesService();
127
128         repositoriesService.scanRepositoryNow( id, true );
129
130         // wait a bit to ensure index is finished
131         int timeout = 20000;
132         while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
133         {
134             Thread.sleep( 500 );
135             timeout -= 500;
136         }
137
138         return id;
139
140     }
141
142     @Test( expected = RedirectionException.class )
143     public void downloadFixedVersion()
144         throws Exception
145     {
146
147         String id = createAndScanRepo();
148
149         try
150         {
151             Response response =
152                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "1.0", null,
153                                                            null );
154
155         }
156         catch ( RedirectionException e )
157         {
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(
160                 0 );
161             throw e;
162         }
163         finally
164         {
165             getManagedRepositoriesService().deleteManagedRepository( id, false );
166         }
167
168     }
169
170
171     @Test( expected = RedirectionException.class )
172     public void downloadLatestVersion()
173         throws Exception
174     {
175         String id = createAndScanRepo();
176
177         try
178         {
179             Response response =
180                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null,
181                                                            null );
182
183         }
184         catch ( RedirectionException e )
185         {
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(
188                 0 );
189             throw e;
190         }
191         finally
192         {
193             getManagedRepositoriesService().deleteManagedRepository( id, false );
194         }
195
196     }
197
198     @Test
199     public void download_no_content()
200         throws Exception
201     {
202         String id = createAndScanRepo();
203
204         try
205         {
206             Response response =
207                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva.beer", "archiva-wine", "LATEST",
208                                                            null, null );
209
210             Assert.assertEquals( Response.Status.NO_CONTENT.getStatusCode(), response.getStatus() );
211
212
213         }
214         finally
215         {
216             getManagedRepositoriesService().deleteManagedRepository( id, false );
217         }
218
219     }
220 }