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