]> source.dussan.org Git - archiva.git/blob
c108af3e952535219057d8fb1e832696bee26b3c
[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.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;
32
33 import javax.ws.rs.core.Response;
34 import java.io.File;
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39
40 /**
41  * @author Olivier Lamy
42  */
43 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
44 public class DownloadArtifactFromQueryTest
45     extends AbstractDownloadTest
46 {
47
48     @BeforeClass
49     public static void setAppServerBase()
50         throws IOException
51     {
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()
56         );
57     }
58
59     @AfterClass
60     public static void resetAppServerBase()
61     {
62         System.setProperty( "appserver.base", previousAppServerBase );
63     }
64
65     @Override
66     protected String getSpringConfigLocation()
67     {
68         return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-merge-index-download.xml";
69     }
70
71     @After
72     public void cleanup()
73         throws Exception
74     {
75         super.tearDown();
76         Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" );
77         if ( Files.exists( tmpIndexDir ) )
78         {
79             FileUtils.deleteDirectory( tmpIndexDir.toFile() );
80         }
81     }
82
83
84     protected String createAndScanRepo()
85         throws Exception
86     {
87
88         Path tmpIndexDir = Paths.get( System.getProperty( "java.io.tmpdir" ), "tmpIndex" );
89         if ( Files.exists( tmpIndexDir ) )
90         {
91             FileUtils.deleteDirectory( tmpIndexDir.toFile() );
92         }
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 );
99
100         ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
101
102         if ( managedRepositoriesService.getManagedRepository( id ) != null )
103         {
104             managedRepositoriesService.deleteManagedRepository( id, false );
105         }
106
107         getManagedRepositoriesService().addManagedRepository( managedRepository );
108
109         RepositoriesService repositoriesService = getRepositoriesService();
110
111         repositoriesService.scanRepositoryNow( id, true );
112
113         // wait a bit to ensure index is finished
114         int timeout = 20000;
115         while ( timeout > 0 && repositoriesService.alreadyScanning( id ) )
116         {
117             Thread.sleep( 500 );
118             timeout -= 500;
119         }
120
121         return id;
122
123     }
124
125     @Test
126     public void downloadFixedVersion()
127         throws Exception
128     {
129
130         String id = createAndScanRepo();
131
132         try
133         {
134             Response response =
135                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "1.0", null,
136                                                            null );
137
138             Assert.assertEquals( Response.Status.TEMPORARY_REDIRECT.getStatusCode(), response.getStatus() );
139
140             String location = String.class.cast( response.getMetadata().get( "Location" ).get( 0 ) );
141
142             /// http://localhost:57168/repository/1400639145722/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar
143
144             Assert.assertEquals( "http://localhost:" + port + "/repository/" + id
145                                      + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar", location
146             );
147         }
148         finally
149         {
150             getManagedRepositoriesService().deleteManagedRepository( id, false );
151         }
152
153     }
154
155
156     @Test
157     public void downloadLatestVersion()
158         throws Exception
159     {
160         String id = createAndScanRepo();
161
162         try
163         {
164             Response response =
165                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null,
166                                                            null );
167
168             Assert.assertEquals( Response.Status.TEMPORARY_REDIRECT.getStatusCode(), response.getStatus() );
169
170             String location = String.class.cast( response.getMetadata().get( "Location" ).get( 0 ) );
171
172             /// http://localhost:57168/repository/1400639145722/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar
173
174             Assert.assertEquals( "http://localhost:" + port + "/repository/" + id
175                                      + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar", location
176             );
177
178
179         }
180         finally
181         {
182             getManagedRepositoriesService().deleteManagedRepository( id, false );
183         }
184
185     }
186
187     @Test
188     public void download_no_content()
189         throws Exception
190     {
191         String id = createAndScanRepo();
192
193         try
194         {
195             Response response =
196                 getSearchService().redirectToArtifactFile( null, "org.apache.archiva.beer", "archiva-wine", "LATEST", null,
197                                                            null );
198
199             Assert.assertEquals( Response.Status.NO_CONTENT.getStatusCode(), response.getStatus() );
200
201
202         }
203         finally
204         {
205             getManagedRepositoriesService().deleteManagedRepository( id, false );
206         }
207
208     }
209 }