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