]> source.dussan.org Git - archiva.git/blob
01dcbd25350e339fc0ec5e12377a979477d350e3
[archiva.git] /
1 package org.apache.archiva.repository;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.Configuration;
24 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
26 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.springframework.test.context.ContextConfiguration;
34
35 import javax.inject.Inject;
36 import java.io.IOException;
37 import java.net.URISyntaxException;
38 import java.net.URL;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.nio.file.StandardCopyOption;
43 import java.util.Collection;
44
45 import static org.junit.Assert.*;
46
47 /**
48  * Test for RepositoryRegistry
49  */
50 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
51 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" })
52 public class RepositoryRegistryTest
53 {
54
55     @Inject
56     RepositoryRegistry repositoryRegistry;
57
58     @Inject
59     ArchivaConfiguration archivaConfiguration;
60
61     private static final Path userCfg = Paths.get(System.getProperty( "user.home" ), ".m2/archiva.xml");
62
63     private static Path cfgCopy;
64     private static Path archivaCfg;
65
66     @BeforeClass
67     public static void classSetup() throws IOException, URISyntaxException
68     {
69         URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
70         if (archivaCfgUri!=null) {
71             archivaCfg = Paths.get(archivaCfgUri.toURI());
72             cfgCopy = Files.createTempFile( "archiva-backup", ".xml" );
73             Files.copy( archivaCfg, cfgCopy, StandardCopyOption.REPLACE_EXISTING);
74         }
75     }
76
77     @AfterClass
78     public static void classTearDown() throws IOException
79     {
80         if (cfgCopy!=null) {
81             Files.deleteIfExists( cfgCopy );
82         }
83     }
84
85     @Before
86     public void setUp( ) throws Exception
87     {
88         assertNotNull( repositoryRegistry );
89         Files.deleteIfExists( userCfg );
90         URL archivaCfgUri = Thread.currentThread().getContextClassLoader().getResource( "archiva.xml" );
91         if (archivaCfgUri!=null) {
92             archivaCfg = Paths.get(archivaCfgUri.toURI());
93             if (Files.exists(cfgCopy))
94             {
95                 Files.copy( cfgCopy, archivaCfg , StandardCopyOption.REPLACE_EXISTING);
96             }
97         }
98         archivaConfiguration.reload();
99         repositoryRegistry.reload();
100     }
101
102     @After
103     public void tearDown( ) throws Exception
104     {
105         Files.deleteIfExists( userCfg );
106         if (cfgCopy!=null && Files.exists(cfgCopy)) {
107             Files.copy(cfgCopy, archivaCfg, StandardCopyOption.REPLACE_EXISTING);
108         }
109     }
110
111     @Test
112     public void getRepositories( ) throws Exception
113     {
114         Collection<Repository> repos = repositoryRegistry.getRepositories( );
115         assertEquals( 5, repos.size( ) );
116         assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
117         assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
118         assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
119     }
120
121     @Test
122     public void getManagedRepositories( ) throws Exception
123     {
124         Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
125         assertEquals( 4, repos.size( ) );
126         assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals("internal") ));
127         assertTrue( repos.stream( ).anyMatch( rep -> rep.getId( ).equals( "snapshots") ) );
128     }
129
130     @Test
131     public void getRemoteRepositories( ) throws Exception
132     {
133         Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories( );
134         assertEquals( 1, repos.size( ) );
135         assertTrue(repos.stream().anyMatch( rep -> rep.getId().equals( "central") ));
136     }
137
138     @Test
139     public void getRepository( ) throws Exception
140     {
141         Repository repo = repositoryRegistry.getRepository( "internal" );
142         assertNotNull(repo);
143         assertEquals("internal", repo.getId());
144         assertEquals("Archiva Managed Internal Repository", repo.getName());
145         assertEquals("This is internal repository.", repo.getDescription());
146         assertEquals( "default", repo.getLayout( ) );
147         assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
148         assertTrue(repo instanceof ManagedRepository);
149         assertTrue( repo.hasIndex( ) );
150         assertTrue(repo.isScanned());
151         assertEquals(RepositoryType.MAVEN, repo.getType());
152     }
153
154     @Test
155     public void getManagedRepository( ) throws Exception
156     {
157         ManagedRepository repo = repositoryRegistry.getManagedRepository( "internal" );
158         assertNotNull(repo);
159         assertEquals("internal", repo.getId());
160         assertEquals("Archiva Managed Internal Repository", repo.getName());
161         assertEquals("This is internal repository.", repo.getDescription());
162         assertEquals( "default", repo.getLayout( ) );
163         assertEquals("0 0 * * * ?", repo.getSchedulingDefinition());
164         assertTrue( repo.hasIndex( ) );
165         assertTrue(repo.isScanned());
166         assertEquals(RepositoryType.MAVEN, repo.getType());
167         assertTrue(repo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE));
168         assertFalse( repo.getActiveReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
169         assertNotNull(repo.getContent());
170
171         assertNull(repositoryRegistry.getManagedRepository( "xyu" ));
172
173     }
174
175     @Test
176     public void getRemoteRepository( ) throws Exception
177     {
178         RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
179         assertNotNull(repo);
180         assertEquals("central", repo.getId());
181         assertEquals("Central Repository", repo.getName());
182         assertEquals("", repo.getDescription());
183         assertEquals( "default", repo.getLayout( ) );
184         assertEquals("0 0 08 ? * SUN", repo.getSchedulingDefinition());
185         assertTrue( repo.hasIndex( ) );
186         assertTrue(repo.isScanned());
187         assertEquals(RepositoryType.MAVEN, repo.getType());
188
189         assertEquals(35, repo.getTimeout().getSeconds());
190     }
191
192     @Test
193     public void putManagedRepository( ) throws Exception
194     {
195         BasicManagedRepository managedRepository = new BasicManagedRepository( "test001", "Test repo" );
196         managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
197         repositoryRegistry.putRepository(managedRepository);
198
199         assertNotNull(managedRepository.getContent());
200         assertEquals(6, repositoryRegistry.getRepositories().size());
201
202         managedRepository = new BasicManagedRepository( "central", "Test repo" );
203         managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
204         ManagedRepository updatedRepo = null;
205         try {
206             repositoryRegistry.putRepository( managedRepository );
207             throw new RuntimeException("Repository exception should be thrown, if there exists a remote repository already with that id");
208         } catch (RepositoryException e) {
209             // OK
210         }
211         managedRepository = new BasicManagedRepository( "internal", "Test repo" );
212         managedRepository.setDescription( managedRepository.getPrimaryLocale(), "This is just a test" );
213         updatedRepo = repositoryRegistry.putRepository( managedRepository );
214
215         assertTrue(updatedRepo==managedRepository);
216         assertNotNull(managedRepository.getContent());
217         assertEquals(6, repositoryRegistry.getRepositories().size());
218         ManagedRepository managedRepository1 = repositoryRegistry.getManagedRepository( "internal" );
219         assertEquals("Test repo", managedRepository1.getName());
220         assertTrue(managedRepository1==managedRepository);
221
222     }
223
224     @Test
225     public void putManagedRepositoryFromConfig( ) throws Exception
226     {
227         ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
228         cfg.setId("test002");
229         cfg.setName("This is test 002");
230         ManagedRepository repo = repositoryRegistry.putRepository( cfg );
231         assertNotNull(repo);
232         assertEquals("test002", repo.getId());
233         assertEquals("This is test 002", repo.getName());
234         assertNotNull(repo.getContent());
235         archivaConfiguration.reload();
236         Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
237         assertEquals(5, repos.size());
238
239         ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
240         cfg = new ManagedRepositoryConfiguration();
241         cfg.setId("internal");
242         cfg.setName("This is internal test 002");
243         repo = repositoryRegistry.putRepository( cfg );
244         assertTrue(internalRepo==repo);
245         assertEquals("This is internal test 002",repo.getName());
246         assertEquals(5, repositoryRegistry.getManagedRepositories().size());
247
248         repositoryRegistry.reload();
249         assertEquals(5, repositoryRegistry.getManagedRepositories().size());
250
251     }
252
253     @Test
254     public void putManagedRepositoryFromConfigWithoutSave( ) throws Exception
255     {
256         Configuration configuration = archivaConfiguration.getConfiguration();
257         ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration();
258         cfg.setId("test002");
259         cfg.setName("This is test 002");
260         ManagedRepository repo = repositoryRegistry.putRepository( cfg, configuration );
261         assertNotNull(repo);
262         assertEquals("test002", repo.getId());
263         assertEquals("This is test 002", repo.getName());
264         assertNotNull(repo.getContent());
265         archivaConfiguration.reload();
266         assertEquals(3, archivaConfiguration.getConfiguration().getManagedRepositories().size());
267         Collection<ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
268         assertEquals(5, repos.size());
269
270         ManagedRepository internalRepo = repositoryRegistry.getManagedRepository( "internal" );
271         cfg = new ManagedRepositoryConfiguration();
272         cfg.setId("internal");
273         cfg.setName("This is internal test 002");
274         repo = repositoryRegistry.putRepository( cfg, configuration );
275         assertTrue(internalRepo==repo);
276         assertEquals("This is internal test 002",repo.getName());
277         assertEquals(5, repositoryRegistry.getManagedRepositories().size());
278
279         repositoryRegistry.reload();
280         assertEquals(4, repositoryRegistry.getManagedRepositories().size());
281     }
282
283     @Test
284     public void putRemoteRepository( ) throws Exception
285     {
286         BasicRemoteRepository remoteRepository = new BasicRemoteRepository( "test001", "Test repo" );
287         remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
288         RemoteRepository newRepo = repositoryRegistry.putRepository(remoteRepository);
289
290         assertTrue(remoteRepository==newRepo);
291         assertNotNull(remoteRepository.getContent());
292         assertEquals(6, repositoryRegistry.getRepositories().size());
293
294         remoteRepository = new BasicRemoteRepository( "internal", "Test repo" );
295         remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
296         RemoteRepository updatedRepo = null;
297         try
298         {
299             updatedRepo = repositoryRegistry.putRepository( remoteRepository );
300             throw new RuntimeException("Should throw repository exception, if repository exists already and is not the same type.");
301         } catch (RepositoryException e) {
302             // OK
303         }
304
305         remoteRepository = new BasicRemoteRepository( "central", "Test repo" );
306         remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), "This is just a test" );
307         updatedRepo = repositoryRegistry.putRepository( remoteRepository );
308
309         assertTrue(updatedRepo==remoteRepository);
310         assertNotNull(remoteRepository.getContent());
311         assertEquals(6, repositoryRegistry.getRepositories().size());
312         RemoteRepository remoteRepository1 = repositoryRegistry.getRemoteRepository( "central" );
313         assertEquals("Test repo", remoteRepository1.getName());
314         assertTrue(remoteRepository1==remoteRepository);
315     }
316
317     @Test
318     public void putRemoteRepositoryFromConfig( ) throws Exception
319     {
320         RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
321         cfg.setId("test002");
322         cfg.setName("This is test 002");
323         RemoteRepository repo = repositoryRegistry.putRepository( cfg );
324         assertNotNull(repo);
325         assertEquals("test002", repo.getId());
326         assertEquals("This is test 002", repo.getName());
327         assertNotNull(repo.getContent());
328         archivaConfiguration.reload();
329         Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
330         assertEquals(2, repos.size());
331
332         RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
333         cfg = new RemoteRepositoryConfiguration();
334         cfg.setId("central");
335         cfg.setName("This is central test 002");
336         repo = repositoryRegistry.putRepository( cfg );
337         assertTrue(internalRepo==repo);
338         assertEquals("This is central test 002",repo.getName());
339         assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
340
341         repositoryRegistry.reload();
342         assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
343     }
344
345     @Test
346     public void putRemoteRepositoryFromConfigWithoutSave( ) throws Exception
347     {
348         Configuration configuration = archivaConfiguration.getConfiguration();
349         RemoteRepositoryConfiguration cfg = new RemoteRepositoryConfiguration();
350         cfg.setId("test002");
351         cfg.setName("This is test 002");
352         RemoteRepository repo = repositoryRegistry.putRepository( cfg, configuration );
353         assertNotNull(repo);
354         assertEquals("test002", repo.getId());
355         assertEquals("This is test 002", repo.getName());
356         assertNotNull(repo.getContent());
357         archivaConfiguration.reload();
358         assertEquals(1, archivaConfiguration.getConfiguration().getRemoteRepositories().size());
359         Collection<RemoteRepository> repos = repositoryRegistry.getRemoteRepositories();
360         assertEquals(2, repos.size());
361
362         RemoteRepository internalRepo = repositoryRegistry.getRemoteRepository( "central" );
363         cfg = new RemoteRepositoryConfiguration();
364         cfg.setId("central");
365         cfg.setName("This is central test 002");
366         repo = repositoryRegistry.putRepository( cfg, configuration );
367         assertTrue(internalRepo==repo);
368         assertEquals("This is central test 002",repo.getName());
369         assertEquals(2, repositoryRegistry.getRemoteRepositories().size());
370
371         repositoryRegistry.reload();
372         assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
373     }
374
375     @Test
376     public void removeRepository( ) throws Exception
377     {
378         assertEquals(5, repositoryRegistry.getRepositories().size());
379         Repository repo = repositoryRegistry.getRepository( "snapshots" );
380         repositoryRegistry.removeRepository( repo );
381         assertEquals(4, repositoryRegistry.getRepositories().size());
382         assertTrue( repositoryRegistry.getRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
383         archivaConfiguration.reload();
384         repositoryRegistry.reload();
385         assertEquals(4, repositoryRegistry.getRepositories().size());
386     }
387
388     @Test
389     public void removeManagedRepository( ) throws Exception
390     {
391
392         assertEquals(4, repositoryRegistry.getManagedRepositories().size());
393         ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
394         repositoryRegistry.removeRepository( repo );
395         assertEquals(3, repositoryRegistry.getManagedRepositories().size());
396         assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
397         archivaConfiguration.reload();
398         repositoryRegistry.reload();
399         assertEquals(3, repositoryRegistry.getManagedRepositories().size());
400     }
401
402     @Test
403     public void removeManagedRepositoryWithoutSave( ) throws Exception
404     {
405         Configuration configuration = archivaConfiguration.getConfiguration();
406         assertEquals(4, repositoryRegistry.getManagedRepositories().size());
407         ManagedRepository repo = repositoryRegistry.getManagedRepository( "snapshots" );
408         repositoryRegistry.removeRepository( repo, configuration );
409         assertEquals(3, repositoryRegistry.getManagedRepositories().size());
410         assertTrue( repositoryRegistry.getManagedRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "snapshots" ) ) );
411         archivaConfiguration.reload();
412         repositoryRegistry.reload();
413         assertEquals(4, repositoryRegistry.getManagedRepositories().size());
414     }
415
416
417     @Test
418     public void removeRemoteRepository( ) throws Exception
419     {
420         assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
421         RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
422         repositoryRegistry.removeRepository( repo );
423         assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
424         assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
425         archivaConfiguration.reload();
426         repositoryRegistry.reload();
427         assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
428     }
429
430     @Test
431     public void removeRemoteRepositoryWithoutSave( ) throws Exception
432     {
433         Configuration configuration = archivaConfiguration.getConfiguration();
434         assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
435         RemoteRepository repo = repositoryRegistry.getRemoteRepository( "central" );
436         repositoryRegistry.removeRepository( repo, configuration );
437         assertEquals(0, repositoryRegistry.getRemoteRepositories().size());
438         assertTrue( repositoryRegistry.getRemoteRepositories( ).stream( ).noneMatch( rep -> rep.getId( ).equals( "central" ) ) );
439         archivaConfiguration.reload();
440         repositoryRegistry.reload();
441         assertEquals(1, repositoryRegistry.getRemoteRepositories().size());
442     }
443
444
445     @Test
446     public void cloneManagedRepo( ) throws Exception
447     {
448         ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( "internal" );
449
450         try
451         {
452             repositoryRegistry.clone(managedRepository, "snapshots");
453             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
454         }
455         catch ( RepositoryException e )
456         {
457             // OK
458         }
459
460         try
461         {
462             repositoryRegistry.clone(managedRepository, "central");
463             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
464         }
465         catch ( RepositoryException e )
466         {
467             // OK
468         }
469
470         ManagedRepository clone = repositoryRegistry.clone( managedRepository, "newinternal" );
471         assertNotNull(clone);
472         assertNull(clone.getContent());
473         assertEquals("Archiva Managed Internal Repository", clone.getName());
474         assertFalse(managedRepository==clone);
475
476     }
477
478     @Test
479     public void cloneRemoteRepo( ) throws Exception
480     {
481         RemoteRepository remoteRepository = repositoryRegistry.getRemoteRepository( "central" );
482
483         try
484         {
485             repositoryRegistry.clone(remoteRepository, "snapshots");
486             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
487         }
488         catch ( RepositoryException e )
489         {
490             // OK
491         }
492
493         try
494         {
495             repositoryRegistry.clone(remoteRepository, "central");
496             throw new RuntimeException("RepositoryRegistry exception should be thrown if id exists already.");
497         }
498         catch ( RepositoryException e )
499         {
500             // OK
501         }
502
503         RemoteRepository clone = repositoryRegistry.clone( remoteRepository, "newCentral" );
504         assertNotNull(clone);
505         assertNull(clone.getContent());
506         assertEquals("Central Repository", clone.getName());
507         assertFalse(remoteRepository==clone);
508
509     }
510
511 }